-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathCodexAcpClient.test.ts
More file actions
205 lines (165 loc) · 9.23 KB
/
Copy pathCodexAcpClient.test.ts
File metadata and controls
205 lines (165 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {describe, expect, it, vi, beforeEach} from 'vitest';
import type {CodexAuthRequest} from "../../CodexAuthMethod";
import {createTestFixture, createCodexMockTestFixture, type TestFixture} from "../acp-test-utils";
import type {ServerNotification} from "../../app-server";
import type {SessionState} from "../../CodexAcpServer";
describe('ACP server test', { timeout: 40_000 }, () => {
let fixture: TestFixture;
beforeEach(() => {
fixture = createTestFixture();
vi.clearAllMocks();
});
const ignoredFields = ["thread", "cwd", "id", "createdAt", "path", "threadId", "userAgent", "sandbox", "reasoningEffort", "conversationId"];
it('should start conversation', async () => {
const codexAcpAgent = fixture.getCodexAcpAgent();
await codexAcpAgent.initialize({protocolVersion: 1});
fixture.getCodexAcpClient().authRequired = vi.fn().mockResolvedValue(false);
const newSessionResponse = await codexAcpAgent.newSession({cwd: "", mcpServers: []});
codexAcpAgent.prompt({sessionId: newSessionResponse.sessionId, prompt: [{type: "text", text: "Hi!"}]});
const transportDump = fixture.getCodexConnectionDump(ignoredFields);
await expect(transportDump).toMatchFileSnapshot("data/start-conversation.json");
});
it('should throw error without authentication', async () => {
const codexAcpAgent = fixture.getCodexAcpAgent();
await codexAcpAgent.initialize({protocolVersion: 1});
await fixture.getCodexAcpClient().logout();
fixture.clearCodexConnectionDump();
await expect(
codexAcpAgent.newSession({cwd: "", mcpServers: []})
).rejects.toThrow("Authentication required");
const transportDump = fixture.getCodexConnectionDump(ignoredFields);
await expect(transportDump).toMatchFileSnapshot("data/auth-failed.json");
});
it('should authenticate with key', async () => {
const codexAcpAgent = fixture.getCodexAcpAgent();
await codexAcpAgent.initialize({protocolVersion: 1});
await fixture.getCodexAcpClient().logout();
fixture.clearCodexConnectionDump();
const authRequest: CodexAuthRequest = { methodId: "api-key", _meta: {apiKey: "TOKEN"} }
await codexAcpAgent.authenticate(authRequest);
const newSessionResponse = await codexAcpAgent.newSession({cwd: "", mcpServers: []});
expect(newSessionResponse.sessionId).toBeDefined()
const transportDump = fixture.getCodexConnectionDump(ignoredFields);
await expect(transportDump).toMatchFileSnapshot("data/auth-with-key.json");
});
function loadNotifications(){
//TODO collect logs form dev run and then load them from file to speedup
const serverNotifications: ServerNotification[] = [
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "He", }},
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "ll", }},
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "o!", }},
];
function onServerNotification(_sessionId: string, callback: (event: ServerNotification) => void){
for (const notification of serverNotifications) {
callback(notification);
}
}
return onServerNotification;
}
it('should map events from dump', async () => {
fixture.getCodexAppServerClient().onServerNotification = loadNotifications();
const codexAcpAgent = fixture.getCodexAcpAgent();
fixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined);
fixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined);
const sessionState: SessionState = {
pendingPrompt: null,
sessionMetadata: {
sessionId: "id",
currentModelId: "model-id",
models: [],
}
};
vi.spyOn(codexAcpAgent, "getSessionState").mockReturnValue(sessionState);
await codexAcpAgent.prompt({ sessionId: "id", prompt: [{type: "text", text: ""}] });
expect(fixture.getAcpConnectionDump([])).toMatchFileSnapshot("data/output-acp-events.json");
});
it('should not duplicate messages on follow-up prompts', async () => {
const mockFixture = createCodexMockTestFixture();
const codexAcpAgent = mockFixture.getCodexAcpAgent();
mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined);
mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined);
const sessionState: SessionState = {
pendingPrompt: null,
sessionMetadata: {
sessionId: "id",
currentModelId: "model-id",
models: [],
}
};
vi.spyOn(codexAcpAgent, "getSessionState").mockReturnValue(sessionState);
// First prompt - registers first notification handler
await codexAcpAgent.prompt({ sessionId: "id", prompt: [{type: "text", text: "First message"}] });
// Follow-up prompt - should NOT accumulate handlers
await codexAcpAgent.prompt({ sessionId: "id", prompt: [{type: "text", text: "Follow-up message"}] });
mockFixture.clearAcpConnectionDump();
// Trigger notifications after both prompts - should produce only 3 events, not 6
const serverNotifications: ServerNotification[] = [
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "He", }},
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "ll", }},
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "o!", }},
];
for (const notification of serverNotifications) {
mockFixture.sendServerNotification(notification);
}
// Wait for async handlers to complete
await vi.waitFor(() => {
const dump = mockFixture.getAcpConnectionDump([]);
expect(dump.length).toBeGreaterThan(0);
});
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot("data/follow-up-no-duplicates.json");
});
it('should handle multiple sessions independently', async () => {
const mockFixture = createCodexMockTestFixture();
const codexAcpAgent = mockFixture.getCodexAcpAgent();
mockFixture.getCodexAppServerClient().turnStart = vi.fn().mockResolvedValue(undefined);
mockFixture.getCodexAppServerClient().awaitTurnCompleted = vi.fn().mockResolvedValue(undefined);
const sessionState1: SessionState = {
pendingPrompt: null,
sessionMetadata: {
sessionId: "session-1",
currentModelId: "model-id",
models: [],
}
};
const sessionState2: SessionState = {
pendingPrompt: null,
sessionMetadata: {
sessionId: "session-2",
currentModelId: "model-id",
models: [],
}
};
vi.spyOn(codexAcpAgent, "getSessionState").mockImplementation((sessionId: string) => {
return sessionId === "session-1" ? sessionState1 : sessionState2;
});
// Start prompts for two different sessions
await codexAcpAgent.prompt({ sessionId: "session-1", prompt: [{type: "text", text: "Message to session 1"}] });
await codexAcpAgent.prompt({ sessionId: "session-2", prompt: [{type: "text", text: "Message to session 2"}] });
mockFixture.clearAcpConnectionDump();
// Trigger notifications - both session handlers should receive them
const serverNotifications: ServerNotification[] = [
{ method: "item/agentMessage/delta", params: { threadId: "string", turnId: "string", itemId: "string", delta: "Hello", }},
];
for (const notification of serverNotifications) {
mockFixture.sendServerNotification(notification);
}
// Wait for async handlers to complete
await vi.waitFor(() => {
const dump = mockFixture.getAcpConnectionDump([]);
expect(dump.length).toBeGreaterThan(0);
});
// Should have 2 events - one for each session's handler
await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot("data/multiple-sessions.json");
});
//dev-time test
it.skip('should convert session notification to acp events', async () => {
fixture.onCodexConnectionEvent((event) => {
console.log(JSON.stringify(event, null, 2));
});
const codexAcpAgent = fixture.getCodexAcpAgent();
await codexAcpAgent.initialize({protocolVersion: 1});
const newSessionResponse = await codexAcpAgent.newSession({cwd: "/home/alex/work/spring-petclinic/", mcpServers: []});
fixture.clearCodexConnectionDump();
await codexAcpAgent.prompt({ sessionId: newSessionResponse.sessionId, prompt: [{type: "text", text: "Add method `minus` to Math Utils."}] });
});
});