Skip to content

Commit 7df94bc

Browse files
Aleksandr Slapoguzovslapoguzov
authored andcommitted
LLM-25226 refactor test
- extract common code - use direct comparison for fuzzy-file-search-events
1 parent 29cae1f commit 7df94bc

7 files changed

Lines changed: 121 additions & 238 deletions

src/__tests__/CodexACPAgent/command-action-events.test.ts

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import type { SessionState } from '../../CodexAcpServer';
33
import type { ServerNotification } from '../../app-server';
4-
import { createCodexMockTestFixture, createTestSessionState, type CodexMockTestFixture } from '../acp-test-utils';
4+
import { createCodexMockTestFixture, createTestSessionState, setupPromptAndSendNotifications, type CodexMockTestFixture } from '../acp-test-utils';
55
import {AgentMode} from "../../AgentMode";
66

77
describe('CodexEventHandler - command action events', () => {
@@ -19,36 +19,6 @@ describe('CodexEventHandler - command action events', () => {
1919
agentMode: AgentMode.DEFAULT_AGENT_MODE
2020
});
2121

22-
async function setupAndSendNotifications(notifications: ServerNotification[]) {
23-
const codexAcpAgent = mockFixture.getCodexAcpAgent();
24-
25-
mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue({
26-
turn: { id: "turn-id", items: [], status: "inProgress", error: null }
27-
});
28-
mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue({
29-
threadId: sessionId,
30-
turn: { id: "turn-id", items: [], status: "completed", error: null }
31-
});
32-
33-
vi.spyOn(codexAcpAgent, 'getSessionState').mockReturnValue(sessionState);
34-
35-
await codexAcpAgent.prompt({
36-
sessionId,
37-
prompt: [{ type: 'text', text: 'test prompt' }],
38-
});
39-
40-
mockFixture.clearAcpConnectionDump();
41-
42-
for (const notification of notifications) {
43-
mockFixture.sendServerNotification(notification);
44-
}
45-
46-
await vi.waitFor(() => {
47-
const dump = mockFixture.getAcpConnectionDump([]);
48-
expect(dump.length).toBeGreaterThan(0);
49-
});
50-
}
51-
5222
it('should handle list files command with explicit path', async () => {
5323
const listFilesNotification: ServerNotification = {
5424
method: 'item/started',
@@ -76,7 +46,7 @@ describe('CodexEventHandler - command action events', () => {
7646
},
7747
};
7848

79-
await setupAndSendNotifications([listFilesNotification]);
49+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [listFilesNotification]);
8050

8151
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
8252
'data/command-list-files-with-path.json'
@@ -110,7 +80,7 @@ describe('CodexEventHandler - command action events', () => {
11080
},
11181
};
11282

113-
await setupAndSendNotifications([listFilesNotification]);
83+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [listFilesNotification]);
11484

11585
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
11686
'data/command-list-files-without-path.json'
@@ -145,7 +115,7 @@ describe('CodexEventHandler - command action events', () => {
145115
},
146116
};
147117

148-
await setupAndSendNotifications([searchNotification]);
118+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [searchNotification]);
149119

150120
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
151121
'data/command-search-with-query-and-path.json'
@@ -180,7 +150,7 @@ describe('CodexEventHandler - command action events', () => {
180150
},
181151
};
182152

183-
await setupAndSendNotifications([searchNotification]);
153+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [searchNotification]);
184154

185155
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
186156
'data/command-search-with-query-only.json'
@@ -215,7 +185,7 @@ describe('CodexEventHandler - command action events', () => {
215185
},
216186
};
217187

218-
await setupAndSendNotifications([searchNotification]);
188+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [searchNotification]);
219189

220190
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
221191
'data/command-search-with-path-only.json'
@@ -250,7 +220,7 @@ describe('CodexEventHandler - command action events', () => {
250220
},
251221
};
252222

253-
await setupAndSendNotifications([searchNotification]);
223+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [searchNotification]);
254224

255225
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
256226
'data/command-search-no-query-no-path.json'
@@ -277,7 +247,7 @@ describe('CodexEventHandler - command action events', () => {
277247
},
278248
};
279249

280-
await setupAndSendNotifications([searchNotification]);
250+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [searchNotification]);
281251

282252
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
283253
'data/mcp-tool-in-progress.json'
@@ -303,7 +273,7 @@ describe('CodexEventHandler - command action events', () => {
303273
},
304274
};
305275

306-
await setupAndSendNotifications([dynamicToolNotification]);
276+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [dynamicToolNotification]);
307277

308278
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
309279
'data/dynamic-tool-in-progress.json'

src/__tests__/CodexACPAgent/data/fuzzy-file-search-flow.json

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/__tests__/CodexACPAgent/file-change-events.test.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import type { SessionState } from '../../CodexAcpServer';
33
import type { ServerNotification } from '../../app-server';
4-
import { createCodexMockTestFixture, createTestSessionState, type CodexMockTestFixture } from '../acp-test-utils';
4+
import { createCodexMockTestFixture, createTestSessionState, setupPromptAndSendNotifications, type CodexMockTestFixture } from '../acp-test-utils';
55
import {AgentMode} from "../../AgentMode";
66

77
const { mockFiles, mockFileContent, clearMockFiles } = vi.hoisted(() => {
@@ -39,36 +39,6 @@ describe('CodexEventHandler - file change events', () => {
3939
agentMode: AgentMode.DEFAULT_AGENT_MODE
4040
});
4141

42-
async function setupAndSendNotifications(notifications: ServerNotification[]) {
43-
const codexAcpAgent = mockFixture.getCodexAcpAgent();
44-
45-
mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue({
46-
turn: { id: "turn-id", items: [], status: "inProgress", error: null }
47-
});
48-
mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue({
49-
threadId: sessionId,
50-
turn: { id: "turn-id", items: [], status: "completed", error: null }
51-
});
52-
53-
vi.spyOn(codexAcpAgent, 'getSessionState').mockReturnValue(sessionState);
54-
55-
await codexAcpAgent.prompt({
56-
sessionId,
57-
prompt: [{ type: 'text', text: 'test prompt' }],
58-
});
59-
60-
mockFixture.clearAcpConnectionDump();
61-
62-
for (const notification of notifications) {
63-
mockFixture.sendServerNotification(notification);
64-
}
65-
66-
await vi.waitFor(() => {
67-
const dump = mockFixture.getAcpConnectionDump([]);
68-
expect(dump.length).toBeGreaterThan(0);
69-
});
70-
}
71-
7242
it('should handle new file creation', async () => {
7343
const newFileNotification: ServerNotification = {
7444
method: 'item/started',
@@ -97,7 +67,7 @@ describe('CodexEventHandler - file change events', () => {
9767
},
9868
};
9969

100-
await setupAndSendNotifications([newFileNotification]);
70+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [newFileNotification]);
10171

10272
await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot(
10373
'data/file-change-add-new-file.json'
@@ -136,7 +106,7 @@ describe('CodexEventHandler - file change events', () => {
136106
},
137107
};
138108

139-
await setupAndSendNotifications([multiFileNotification]);
109+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [multiFileNotification]);
140110

141111
await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot(
142112
'data/file-change-add-multiple-files.json'
@@ -165,7 +135,7 @@ describe('CodexEventHandler - file change events', () => {
165135
},
166136
};
167137

168-
await setupAndSendNotifications([newFileNotification]);
138+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [newFileNotification]);
169139

170140
await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot(
171141
'data/file-change-add-raw-content.json'
@@ -198,7 +168,7 @@ describe('CodexEventHandler - file change events', () => {
198168
},
199169
};
200170

201-
await setupAndSendNotifications([deleteFileNotification]);
171+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [deleteFileNotification]);
202172

203173
await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot(
204174
'data/file-change-delete-file.json'
@@ -229,7 +199,7 @@ describe('CodexEventHandler - file change events', () => {
229199
},
230200
};
231201

232-
await setupAndSendNotifications([deletedFileNotification]);
202+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [deletedFileNotification]);
233203

234204
await expect(mockFixture.getAcpConnectionDump(['id'])).toMatchFileSnapshot(
235205
'data/file-change-delete-raw-content.json'
Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import type { SessionState } from "../../CodexAcpServer";
33
import type { ServerNotification } from "../../app-server";
4-
import { createCodexMockTestFixture, createTestSessionState, type CodexMockTestFixture } from "../acp-test-utils";
4+
import { createCodexMockTestFixture, createTestSessionState, setupPromptAndSendNotifications, type CodexMockTestFixture } from "../acp-test-utils";
55
import { AgentMode } from "../../AgentMode";
66

77
describe("CodexEventHandler - fuzzy file search events", () => {
@@ -19,37 +19,12 @@ describe("CodexEventHandler - fuzzy file search events", () => {
1919
agentMode: AgentMode.DEFAULT_AGENT_MODE,
2020
});
2121

22-
async function setupAndSendNotifications(notifications: ServerNotification[]) {
23-
const codexAcpAgent = mockFixture.getCodexAcpAgent();
24-
25-
mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue({
26-
turn: { id: "turn-id", items: [], status: "inProgress", error: null },
27-
});
28-
mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue({
29-
threadId: sessionId,
30-
turn: { id: "turn-id", items: [], status: "completed", error: null },
31-
});
32-
33-
vi.spyOn(codexAcpAgent, "getSessionState").mockReturnValue(sessionState);
34-
35-
await codexAcpAgent.prompt({
36-
sessionId,
37-
prompt: [{ type: "text", text: "test prompt" }],
38-
});
39-
40-
mockFixture.clearAcpConnectionDump();
41-
42-
for (const notification of notifications) {
43-
mockFixture.sendServerNotification(notification);
44-
}
45-
46-
await vi.waitFor(() => {
47-
const dump = mockFixture.getAcpConnectionDump([]);
48-
expect(dump.length).toBeGreaterThan(0);
22+
it("maps fuzzy file search as search tool call flow", async () => {
23+
const events: { method: string; args: any[] }[] = [];
24+
mockFixture.onAcpConnectionEvent((event) => {
25+
events.push(event);
4926
});
50-
}
5127

52-
it("maps fuzzy file search as search tool call flow", async () => {
5328
const updated1: ServerNotification = {
5429
method: "fuzzyFileSearch/sessionUpdated",
5530
params: {
@@ -78,10 +53,58 @@ describe("CodexEventHandler - fuzzy file search events", () => {
7853
},
7954
};
8055

81-
await setupAndSendNotifications([updated1, updated2, completed]);
56+
await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, [updated1, updated2, completed]);
8257

83-
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot(
84-
"data/fuzzy-file-search-flow.json"
85-
);
58+
expect(events).toHaveLength(3);
59+
expect(events[0]).toEqual({
60+
method: "sessionUpdate",
61+
args: [
62+
{
63+
sessionId: "test-session-id",
64+
update: {
65+
sessionUpdate: "tool_call",
66+
toolCallId: "fuzzyFileSearch.search-1",
67+
kind: "search",
68+
title: "Search for 'event handler'",
69+
status: "in_progress",
70+
locations: [
71+
{ path: "/repo/src/CodexEventHandler.ts" },
72+
{ path: "/repo/src/CodexToolCallMapper.ts" },
73+
],
74+
rawInput: {
75+
query: "event handler",
76+
},
77+
},
78+
},
79+
],
80+
});
81+
expect(events[1]).toEqual({
82+
method: "sessionUpdate",
83+
args: [
84+
{
85+
sessionId: "test-session-id",
86+
update: {
87+
sessionUpdate: "tool_call_update",
88+
toolCallId: "fuzzyFileSearch.search-1",
89+
title: "Search for 'event handler'",
90+
status: "in_progress",
91+
locations: [{ path: "/repo/src/CodexEventHandler.ts" }],
92+
},
93+
},
94+
],
95+
});
96+
expect(events[2]).toEqual({
97+
method: "sessionUpdate",
98+
args: [
99+
{
100+
sessionId: "test-session-id",
101+
update: {
102+
sessionUpdate: "tool_call_update",
103+
toolCallId: "fuzzyFileSearch.search-1",
104+
status: "completed",
105+
},
106+
},
107+
],
108+
});
86109
});
87110
});

0 commit comments

Comments
 (0)