MCPcopy
hub / github.com/codeaashu/claude-code / withFixture

Function withFixture

src/services/vcr.ts:39–86  ·  view source on GitHub ↗

* Generic fixture management helper * Handles caching, reading, writing fixtures for any data type

(
  input: unknown,
  fixtureName: string,
  f: () => Promise<T>,
)

Source from the content-addressed store, hash-verified

37 * Handles caching, reading, writing fixtures for any data type
38 */
39async function withFixture<T>(
40 input: unknown,
41 fixtureName: string,
42 f: () => Promise<T>,
43): Promise<T> {
44 if (!shouldUseVCR()) {
45 return await f()
46 }
47
48 // Create hash of input for fixture filename
49 const hash = createHash('sha1')
50 .update(jsonStringify(input))
51 .digest('hex')
52 .slice(0, 12)
53 const filename = join(
54 process.env.CLAUDE_CODE_TEST_FIXTURES_ROOT ?? getCwd(),
55 `fixtures/${fixtureName}-${hash}.json`,
56 )
57
58 // Fetch cached fixture
59 try {
60 const cached = jsonParse(
61 await readFile(filename, { encoding: 'utf8' }),
62 ) as T
63 return cached
64 } catch (e: unknown) {
65 const code = getErrnoCode(e)
66 if (code !== 'ENOENT') {
67 throw e
68 }
69 }
70
71 if ((env.isCI || process.env.CI) && !isEnvTruthy(process.env.VCR_RECORD)) {
72 throw new Error(
73 `Fixture missing: ${filename}. Re-run tests with VCR_RECORD=1, then commit the result.`,
74 )
75 }
76
77 // Create & write new fixture
78 const result = await f()
79
80 await mkdir(dirname(filename), { recursive: true })
81 await writeFile(filename, jsonStringify(result, null, 2), {
82 encoding: 'utf8',
83 })
84
85 return result
86}
87
88export async function withVCR(
89 messages: Message[],

Callers 1

withTokenCountVCRFunction · 0.85

Calls 10

shouldUseVCRFunction · 0.85
jsonStringifyFunction · 0.85
getCwdFunction · 0.85
jsonParseFunction · 0.85
readFileFunction · 0.85
getErrnoCodeFunction · 0.85
isEnvTruthyFunction · 0.85
mkdirFunction · 0.85
updateMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected