-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathacp-test-utils.ts
More file actions
156 lines (136 loc) · 5.76 KB
/
Copy pathacp-test-utils.ts
File metadata and controls
156 lines (136 loc) · 5.76 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
import {CodexAcpClient} from '../CodexAcpClient';
import {type CodexConnectionEvent, CodexAppServerClient} from '../CodexAppServerClient';
import {startCodexConnection} from "../CodexJsonRpcConnection";
import {CodexAcpServer} from "../CodexAcpServer";
import type {AgentSideConnection} from "@agentclientprotocol/sdk";
import type {ServerNotification} from "../app-server";
import type {MessageConnection} from "vscode-jsonrpc/node";
import path from "node:path";
import fs from "node:fs";
export type MethodCallEvent = { method: string; args: any[] };
export function createSmartMock<T extends object>(onCall: (event: MethodCallEvent) => void) {
return new Proxy({} as T, {
get(_, prop) {
return (...args: any[]) => {
onCall({ method: String(prop), args });
return { mock: "Mocked return" };
};
}
});
}
export interface TestFixture {
getCodexAppServerClient(): CodexAppServerClient,
getCodexAcpClient(): CodexAcpClient,
getCodexAcpAgent(): CodexAcpServer,
onCodexConnectionEvent(handler: (event: CodexConnectionEvent) => void): void,
getCodexConnectionDump(ignoredFields: string[]): string,
clearCodexConnectionDump(): void,
onAcpConnectionEvent(handler: (event: MethodCallEvent) => void): void,
getAcpConnectionDump(ignoredFields: string[]): string,
clearAcpConnectionDump(): void,
}
export interface ConnectionConfig {
connection: MessageConnection;
getExitCode: () => number | null;
}
export function createBaseTestFixture(config: ConnectionConfig): TestFixture {
const acpConnectionEvents: MethodCallEvent[] = [];
const acpEventHandlers: ((event: MethodCallEvent) => void)[] = [];
const acpConnection = createSmartMock<AgentSideConnection>((event) => {
acpConnectionEvents.push(event);
acpEventHandlers.forEach(handler => handler(event));
});
const codexAppServerClient = new CodexAppServerClient(config.connection);
const codexAcpClient = new CodexAcpClient(codexAppServerClient);
const codexAcpAgent = new CodexAcpServer(acpConnection, codexAcpClient, undefined, config.getExitCode);
const transportEvents: CodexConnectionEvent[] = [];
const codexEventHandlers: ((event: CodexConnectionEvent) => void)[] = [];
codexAppServerClient.onClientTransportEvent((event) => {
transportEvents.push(event);
codexEventHandlers.forEach(handler => handler(event));
});
return {
getCodexAcpAgent(): CodexAcpServer {
return codexAcpAgent;
},
getCodexAcpClient(): CodexAcpClient {
return codexAcpClient;
},
getCodexConnectionDump(ignoredFields: string[]): string {
return createArrayDump(transportEvents, ignoredFields);
},
onCodexConnectionEvent(handler: (event: CodexConnectionEvent) => void): void {
codexEventHandlers.push(handler);
},
getCodexAppServerClient(): CodexAppServerClient {
return codexAppServerClient;
},
clearCodexConnectionDump(): void {
transportEvents.splice(0, transportEvents.length);
},
onAcpConnectionEvent(handler: (event: MethodCallEvent) => void): void {
acpEventHandlers.push(handler);
},
getAcpConnectionDump(ignoredFields: string[]): string {
return createArrayDump(acpConnectionEvents, ignoredFields);
},
clearAcpConnectionDump() {
acpConnectionEvents.splice(0, acpConnectionEvents.length);
}
};
}
/**
* Creates a test fixture with a real Codex connection.
* Use for integration tests that need to interact with the actual Codex binary.
*/
export function createTestFixture(): TestFixture {
const pathToCodex = path.resolve(process.cwd(), "node_modules", ".bin", process.platform === 'win32' ? "codex.cmd" : "codex");
if (!fs.existsSync(pathToCodex)) {
throw new Error(`Codex binary not found at ${pathToCodex}. Did you run 'npm install'?`);
}
const codexConnection = startCodexConnection(pathToCodex);
return createBaseTestFixture({
connection: codexConnection.connection,
getExitCode: () => codexConnection.process.exitCode
});
}
export interface CodexMockTestFixture extends TestFixture {
sendServerNotification(notification: ServerNotification): void,
}
/**
* Creates a test fixture with a mock Codex connection.
* Use for unit tests that don't need a real Codex binary.
* Provides `sendServerNotification()` to simulate server notifications.
*/
export function createCodexMockTestFixture(): CodexMockTestFixture {
let unhandledNotificationHandler: ((notification: any) => void) | null = null;
const mockCodexConnection = {
sendRequest: () => Promise.resolve(undefined),
onUnhandledNotification: (handler: (notification: any) => void) => {
unhandledNotificationHandler = handler;
},
onNotification: () => {},
end: () => {},
} as unknown as MessageConnection;
const baseFixture = createBaseTestFixture({
connection: mockCodexConnection,
getExitCode: () => null
});
return {
...baseFixture,
sendServerNotification(notification: ServerNotification): void {
if (unhandledNotificationHandler) {
unhandledNotificationHandler(notification);
}
}
};
}
export function createObjectDump(obj: any, anonymizedFields: string[] = []) {
function fieldAnonymizer(key: string, value: any): any {
return anonymizedFields.includes(key) ? key : value;
}
return JSON.stringify(obj, fieldAnonymizer, 2);
}
export function createArrayDump(objects: any[], anonymizedFields: string[]): string {
return objects.map(event => createObjectDump(event, anonymizedFields)).join("\n");
}