Skip to content

Commit 099e029

Browse files
fix: improve test isolation in ai-agent tests
Replace module-level nextGenerateResult variable with per-test nextGenerateResultOverride to avoid global state issues in CI. The MockToolLoopAgent now uses the override value if set, otherwise falls back to a default. This fixes the flaky test failures in CI where the global state wasn't being properly isolated between test runs. 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 3b4a24c commit 099e029

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

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

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

1515
let toolLoopAgentSettings: unknown;
1616
let lastGenerateMessages: unknown;
17+
let nextGenerateResultOverride: unknown;
1718
type GenerationResult = GenerateTextResult<ToolSet, ReturnType<typeof AIOutput.text>>;
18-
let nextGenerateResult = createGenerationResult();
1919

2020
class MockToolLoopAgent {
2121
settings: unknown;
@@ -27,7 +27,7 @@ class MockToolLoopAgent {
2727

2828
async generate({ messages }: { messages: unknown }) {
2929
lastGenerateMessages = messages;
30-
return nextGenerateResult;
30+
return nextGenerateResultOverride || createGenerationResult();
3131
}
3232
}
3333

@@ -125,7 +125,7 @@ function expectRecord(value: unknown, label: string): Record<string, unknown> {
125125
beforeEach(() => {
126126
toolLoopAgentSettings = undefined;
127127
lastGenerateMessages = undefined;
128-
nextGenerateResult = createGenerationResult();
128+
nextGenerateResultOverride = undefined;
129129
stepCountIsMock.mockClear();
130130
createOpenAIMock.mockClear();
131131
createGoogleGenerativeAIMock.mockClear();
@@ -142,7 +142,7 @@ describe('core.ai.agent (refactor)', () => {
142142
const component = componentRegistry.get<AiAgentInput, AiAgentOutput>('core.ai.agent');
143143
expect(component).toBeDefined();
144144

145-
nextGenerateResult = createGenerationResult({ text: 'Hello agent' });
145+
nextGenerateResultOverride = createGenerationResult({ text: 'Hello agent' });
146146

147147
const result = await runComponentWithRunner(
148148
component!.runner,
@@ -268,7 +268,7 @@ describe('core.ai.agent (refactor)', () => {
268268
const component = componentRegistry.get<AiAgentInput, AiAgentOutput>('core.ai.agent');
269269
expect(component).toBeDefined();
270270

271-
nextGenerateResult = createGenerationResult({
271+
nextGenerateResultOverride = createGenerationResult({
272272
text: 'Tool done',
273273
toolResults: [
274274
{

0 commit comments

Comments
 (0)