Skip to content

Commit 155fbd1

Browse files
committed
fix(telemetry): mock session-tracing.js directly instead of telemetry/index.js
Mocking the barrel re-export (telemetry/index.js) with importActual was unreliable — vitest's module resolution could bind production code to the real endToolSpan before the mock override took effect. Mock the source module (session-tracing.js) directly to guarantee interception.
1 parent d296fbe commit 155fbd1

1 file changed

Lines changed: 36 additions & 41 deletions

File tree

packages/core/src/core/coreToolScheduler.test.ts

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -123,47 +123,42 @@ function createMockToolSpan(
123123
});
124124
}
125125

126-
vi.mock('../telemetry/index.js', async (importOriginal) => {
127-
const actual = await importOriginal<Record<string, unknown>>();
128-
return {
129-
...actual,
130-
startToolSpan: vi.fn(
131-
(name: string, attrs?: Record<string, string | number | boolean>) =>
132-
createMockToolSpan(`tool.${name}`, { tool_name: name, ...attrs }),
133-
),
134-
endToolSpan: vi.fn(
135-
(
136-
span: ToolSpanRecord & ReturnType<typeof createMockToolSpan>,
137-
metadata?: { success?: boolean; error?: string },
138-
) => {
139-
if (metadata) {
140-
const status =
141-
metadata.success !== false
142-
? { code: 1 }
143-
: { code: 2, message: metadata.error ?? 'tool error' };
144-
span.statusCalls.push(status);
145-
}
146-
span.ended = true;
147-
},
148-
),
149-
runInToolSpanContext: vi.fn(<T>(_span: unknown, fn: () => T): T => fn()),
150-
startToolExecutionSpan: vi.fn(() =>
151-
createMockToolSpan('tool.execution', {}),
152-
),
153-
endToolExecutionSpan: vi.fn(
154-
(
155-
span: ReturnType<typeof createMockToolSpan>,
156-
_metadata?: { success?: boolean; error?: string },
157-
) => {
158-
try {
159-
span.end();
160-
} catch {
161-
// best-effort
162-
}
163-
},
164-
),
165-
};
166-
});
126+
vi.mock('../telemetry/session-tracing.js', () => ({
127+
startToolSpan: vi.fn(
128+
(name: string, attrs?: Record<string, string | number | boolean>) =>
129+
createMockToolSpan(`tool.${name}`, { tool_name: name, ...attrs }),
130+
),
131+
endToolSpan: vi.fn(
132+
(
133+
span: ToolSpanRecord & ReturnType<typeof createMockToolSpan>,
134+
metadata?: { success?: boolean; error?: string },
135+
) => {
136+
if (metadata) {
137+
const status =
138+
metadata.success !== false
139+
? { code: 1 }
140+
: { code: 2, message: metadata.error ?? 'tool error' };
141+
span.statusCalls.push(status);
142+
}
143+
span.ended = true;
144+
},
145+
),
146+
runInToolSpanContext: vi.fn(<T>(_span: unknown, fn: () => T): T => fn()),
147+
startToolExecutionSpan: vi.fn(() => createMockToolSpan('tool.execution', {})),
148+
endToolExecutionSpan: vi.fn(
149+
(
150+
span: ReturnType<typeof createMockToolSpan>,
151+
_metadata?: { success?: boolean; error?: string },
152+
) => {
153+
span.ended = true;
154+
},
155+
),
156+
startInteractionSpan: vi.fn(),
157+
endInteractionSpan: vi.fn(),
158+
startLLMRequestSpan: vi.fn(),
159+
endLLMRequestSpan: vi.fn(),
160+
clearSessionTracingForTesting: vi.fn(),
161+
}));
167162

168163
vi.mock('fs/promises', () => ({
169164
writeFile: vi.fn(),

0 commit comments

Comments
 (0)