Skip to content

Commit 755a18e

Browse files
authored
test(trace): wait for the last trace write before a test ends (#1568)
Two trace-capture tests stopped waiting once the call record appeared, but the lifecycle event is appended after it. That write could run after afterEach removed the temp dir and fail with ENOENT between tests, which turned the nightly run red.
1 parent 5c89d50 commit 755a18e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎src/server/__tests__/trace-capture.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,11 @@ describe('trace capture service', () => {
855855
method: 'POST', headers: { 'X-Claude-Code-Session-Id': 'fixture-root' },
856856
body: JSON.stringify({ model: 'gpt-6-astra', max_tokens: 16, messages: [{ role: 'user', content: 'Audit 中文 '.repeat(1000) }] }),
857857
})).text()
858-
const trace = await waitForTrace('session-zstd-audit', snapshot => Boolean(snapshot.calls[0]?.response))
858+
const trace = await waitForTrace(
859+
'session-zstd-audit',
860+
snapshot => Boolean(snapshot.calls[0]?.response)
861+
&& snapshot.events.some((event) => event.phase === 'api_call_completed'),
862+
)
859863
expect(trace.calls).toHaveLength(1)
860864
const call = trace.calls[0]
861865
expect(call.request.headers['content-encoding']).toBe('zstd')
@@ -1088,13 +1092,17 @@ describe('trace capture service', () => {
10881092
await new Promise((resolve) => setTimeout(resolve, 20))
10891093
abortController.abort()
10901094

1095+
// The aborted event is written after the call record; wait for it so the
1096+
// write does not outlive the test and land in a removed temp dir.
10911097
const trace = await waitForTrace(
10921098
'session-direct-abort-bare',
1093-
(snapshot) => snapshot.calls[0]?.status === 'error',
1099+
(snapshot) => snapshot.calls[0]?.status === 'error'
1100+
&& snapshot.events.some((event) => event.phase === 'api_call_aborted'),
10941101
)
10951102
expect(trace.calls[0].status).toBe('error')
10961103
expect(trace.calls[0].error?.name).toBe('AbortError')
10971104
expect(trace.calls[0].metadata).toMatchObject({ phase: 'api_call_aborted', aborted: true })
1105+
expect(trace.events.map((event) => event.phase)).toEqual(['api_call_started', 'api_call_aborted'])
10981106
} finally {
10991107
globalThis.fetch = originalFetch
11001108
if (originalTraceEnv === undefined) delete process.env.CC_HAHA_TRACE_API_CALLS

0 commit comments

Comments
 (0)