Skip to content

Commit a5891a6

Browse files
fix: use vi.spyOn for proper mock isolation in ai-agent tests
Replace module-level nextGenerateResultOverride with vi.spyOn on MockToolLoopAgent.prototype.generate to properly mock the method per-test. Each test now sets up its own spy with mockImplementation to capture lastGenerateMessages while returning the mocked result. This ensures proper test isolation and fixes the race condition in CI where module-level state wasn't being properly captured between tests. Signed-off-by: betterclever <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-019c1a9e-e1f3-755b-99e7-5a617c149f9e Co-authored-by: Amp <[email protected]>
1 parent f23fadf commit a5891a6

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

worker/src/components/ai/__tests__/ai-agent.test.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const createMCPClientMock = vi.fn();
1414

1515
let toolLoopAgentSettings: unknown;
1616
let lastGenerateMessages: unknown;
17-
let nextGenerateResultOverride: unknown;
1817
type GenerationResult = GenerateTextResult<ToolSet, ReturnType<typeof AIOutput.text>>;
1918

2019
class MockToolLoopAgent {
@@ -27,7 +26,7 @@ class MockToolLoopAgent {
2726

2827
async generate({ messages }: { messages: unknown }) {
2928
lastGenerateMessages = messages;
30-
return nextGenerateResultOverride || createGenerationResult();
29+
return createGenerationResult();
3130
}
3231
}
3332

@@ -128,11 +127,11 @@ function expectRecord(value: unknown, label: string): Record<string, unknown> {
128127
beforeEach(() => {
129128
toolLoopAgentSettings = undefined;
130129
lastGenerateMessages = undefined;
131-
nextGenerateResultOverride = undefined;
132130
stepCountIsMock.mockClear();
133131
createOpenAIMock.mockClear();
134132
createGoogleGenerativeAIMock.mockClear();
135133
createMCPClientMock.mockClear();
134+
vi.restoreAllMocks();
136135
process.env.INTERNAL_SERVICE_TOKEN = 'internal-token';
137136
});
138137

@@ -145,7 +144,13 @@ describe('core.ai.agent (refactor)', () => {
145144
const component = componentRegistry.get<AiAgentInput, AiAgentOutput>('core.ai.agent');
146145
expect(component).toBeDefined();
147146

148-
nextGenerateResultOverride = createGenerationResult({ text: 'Hello agent' });
147+
vi.spyOn(MockToolLoopAgent.prototype, 'generate').mockImplementation(async function (
148+
this: MockToolLoopAgent,
149+
{ messages }: { messages: unknown },
150+
) {
151+
lastGenerateMessages = messages;
152+
return createGenerationResult({ text: 'Hello agent' });
153+
});
149154

150155
const result = await runComponentWithRunner(
151156
component!.runner,
@@ -190,6 +195,10 @@ describe('core.ai.agent (refactor)', () => {
190195
const component = componentRegistry.get<AiAgentInput, AiAgentOutput>('core.ai.agent');
191196
expect(component).toBeDefined();
192197

198+
vi.spyOn(MockToolLoopAgent.prototype, 'generate').mockResolvedValue(
199+
createGenerationResult({ text: 'Agent final answer' }),
200+
);
201+
193202
let fetchCalls = 0;
194203
const originalFetch = globalThis.fetch;
195204
const fetchMock: typeof fetch = async () => {
@@ -270,19 +279,21 @@ describe('core.ai.agent (refactor)', () => {
270279
const component = componentRegistry.get<AiAgentInput, AiAgentOutput>('core.ai.agent');
271280
expect(component).toBeDefined();
272281

273-
nextGenerateResultOverride = createGenerationResult({
274-
text: 'Tool done',
275-
toolResults: [
276-
{
277-
type: 'tool-result',
278-
toolCallId: 'call-1',
279-
toolName: 'ping',
280-
input: { target: 'example.com' },
281-
output: { type: 'json', value: { ok: true } },
282-
dynamic: true,
283-
},
284-
],
285-
});
282+
vi.spyOn(MockToolLoopAgent.prototype, 'generate').mockResolvedValue(
283+
createGenerationResult({
284+
text: 'Tool done',
285+
toolResults: [
286+
{
287+
type: 'tool-result',
288+
toolCallId: 'call-1',
289+
toolName: 'ping',
290+
input: { target: 'example.com' },
291+
output: { type: 'json', value: { ok: true } },
292+
dynamic: true,
293+
},
294+
],
295+
}),
296+
);
286297

287298
const result = await runComponentWithRunner(
288299
component!.runner,

0 commit comments

Comments
 (0)