Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

codexAppServerManager.test.ts

355 lines9.9 KB

1import { describe, expect, it, vi } from "vitest";
2import { ProviderSessionId } from "@t3tools/contracts";
3​
4import {
5 CodexAppServerManager,
6 classifyCodexStderrLine,
7 isRecoverableThreadResumeError,
8 normalizeCodexModelSlug,
9} from "./codexAppServerManager";
10​
11const asSessionId = (value: string): ProviderSessionId => ProviderSessionId.makeUnsafe(value);
12​
13function createSendTurnHarness() {
14 const manager = new CodexAppServerManager();
15 const context = {
16 session: {
17 sessionId: "sess_1",
18 provider: "codex",
19 status: "ready",
20 threadId: "thread_1",
21 createdAt: "2026-02-10T00:00:00.000Z",
22 updatedAt: "2026-02-10T00:00:00.000Z",
23 },
24 };
25​
26 const requireSession = vi
27 .spyOn(
28 manager as unknown as { requireSession: (sessionId: string) => unknown },
29 "requireSession",
30 )
31 .mockReturnValue(context);
32 const sendRequest = vi
33 .spyOn(
34 manager as unknown as { sendRequest: (...args: unknown[]) => Promise<unknown> },
35 "sendRequest",
36 )
37 .mockResolvedValue({
38 turn: {
39 id: "turn_1",
40 },
41 });
42 const updateSession = vi
43 .spyOn(manager as unknown as { updateSession: (...args: unknown[]) => void }, "updateSession")
44 .mockImplementation(() => {});
45​
46 return { manager, context, requireSession, sendRequest, updateSession };
48​
49function createThreadControlHarness() {
50 const manager = new CodexAppServerManager();
51 const context = {
52 session: {
53 sessionId: "sess_1",
54 provider: "codex",
55 status: "ready",
56 threadId: "thread_1",
57 createdAt: "2026-02-10T00:00:00.000Z",
58 updatedAt: "2026-02-10T00:00:00.000Z",
59 },
60 };
61​
62 const requireSession = vi
63 .spyOn(
64 manager as unknown as { requireSession: (sessionId: string) => unknown },
65 "requireSession",
66 )
67 .mockReturnValue(context);
68 const sendRequest = vi.spyOn(
69 manager as unknown as { sendRequest: (...args: unknown[]) => Promise<unknown> },
70 "sendRequest",
71 );
72 const updateSession = vi
73 .spyOn(manager as unknown as { updateSession: (...args: unknown[]) => void }, "updateSession")
74 .mockImplementation(() => {});
75​
76 return { manager, context, requireSession, sendRequest, updateSession };
78​
79describe("classifyCodexStderrLine", () => {
80 it("ignores empty lines", () => {
81 expect(classifyCodexStderrLine(" ")).toBeNull();
82 });
83​
84 it("ignores non-error structured codex logs", () => {
85 const line =
86 "2026-02-08T04:24:19.241256Z WARN codex_core::features: unknown feature key in config: skills";
87 expect(classifyCodexStderrLine(line)).toBeNull();
88 });
89​
90 it("ignores known benign rollout path errors", () => {
91 const line =
92 "\u001b[2m2026-02-08T04:24:20.085687Z\u001b[0m \u001b[31mERROR\u001b[0m \u001b[2mcodex_core::rollout::list\u001b[0m: state db missing rollout path for thread 019c3b6c-46b8-7b70-ad23-82f824d161fb";
93 expect(classifyCodexStderrLine(line)).toBeNull();
94 });
95​
96 it("keeps unknown structured errors", () => {
97 const line = "2026-02-08T04:24:20.085687Z ERROR codex_core::runtime: unrecoverable failure";
98 expect(classifyCodexStderrLine(line)).toEqual({
99 message: line,
100 });
101 });
102​
103 it("keeps plain stderr messages", () => {
104 const line = "fatal: permission denied";
105 expect(classifyCodexStderrLine(line)).toEqual({
106 message: line,
107 });
108 });
109});
110​
111describe("normalizeCodexModelSlug", () => {
112 it("maps 5.3 aliases to gpt-5.3-codex", () => {
113 expect(normalizeCodexModelSlug("5.3")).toBe("gpt-5.3-codex");
114 expect(normalizeCodexModelSlug("gpt-5.3")).toBe("gpt-5.3-codex");
115 });
116​
117 it("prefers codex id when model differs", () => {
118 expect(normalizeCodexModelSlug("gpt-5.3", "gpt-5.3-codex")).toBe("gpt-5.3-codex");
119 });
120​
121 it("keeps non-aliased models as-is", () => {
122 expect(normalizeCodexModelSlug("gpt-5.2-codex")).toBe("gpt-5.2-codex");
123 expect(normalizeCodexModelSlug("gpt-5.2")).toBe("gpt-5.2");
124 });
125});
126​
127describe("isRecoverableThreadResumeError", () => {
128 it("matches not-found resume errors", () => {
129 expect(
130 isRecoverableThreadResumeError(new Error("thread/resume failed: thread not found")),
131 ).toBe(true);
132 });
133​
134 it("ignores non-resume errors", () => {
135 expect(
136 isRecoverableThreadResumeError(new Error("thread/start failed: permission denied")),
137 ).toBe(false);
138 });
139​
140 it("ignores non-recoverable resume errors", () => {
141 expect(
142 isRecoverableThreadResumeError(
143 new Error("thread/resume failed: timed out waiting for server"),
144 ),
145 ).toBe(false);
146 });
147});
148​
149describe("startSession", () => {
150 it("emits session/startFailed when resolving cwd throws before process launch", async () => {
151 const manager = new CodexAppServerManager();
152 const events: Array<{ method: string; kind: string; message?: string }> = [];
153 manager.on("event", (event) => {
154 events.push({
155 method: event.method,
156 kind: event.kind,
157 ...(event.message ? { message: event.message } : {}),
158 });
159 });
160​
161 const processCwd = vi.spyOn(process, "cwd").mockImplementation(() => {
162 throw new Error("cwd missing");
163 });
164 try {
165 await expect(
166 manager.startSession({
167 provider: "codex",
168 }),
169 ).rejects.toThrow("cwd missing");
170 expect(events).toHaveLength(1);
171 expect(events[0]).toEqual({
172 method: "session/startFailed",
173 kind: "error",
174 message: "cwd missing",
175 });
176 } finally {
177 processCwd.mockRestore();
178 manager.stopAll();
179 }
180 });
181});
182​
183describe("sendTurn", () => {
184 it("sends text and image user input items to turn/start", async () => {
185 const { manager, context, requireSession, sendRequest, updateSession } =
186 createSendTurnHarness();
187​
188 const result = await manager.sendTurn({
189 sessionId: asSessionId("sess_1"),
190 input: "Inspect this image",
191 attachments: [
192 {
193 type: "image",
194 url: "data:image/png;base64,AAAA",
195 },
196 ],
197 model: "gpt-5.3",
198 effort: "high",
199 });
200​
201 expect(result).toEqual({
202 threadId: "thread_1",
203 turnId: "turn_1",
204 resumeCursor: {
205 threadId: "thread_1",
206 },
207 });
208 expect(requireSession).toHaveBeenCalledWith("sess_1");
209 expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
210 threadId: "thread_1",
211 input: [
212 {
213 type: "text",
214 text: "Inspect this image",
215 text_elements: [],
216 },
217 {
218 type: "image",
219 url: "data:image/png;base64,AAAA",
220 },
221 ],
222 model: "gpt-5.3-codex",
223 effort: "high",
224 });
225 expect(updateSession).toHaveBeenCalledWith(context, {
226 status: "running",
227 activeTurnId: "turn_1",
228 resumeCursor: {
229 threadId: "thread_1",
230 },
231 });
232 });
233​
234 it("supports image-only turns", async () => {
235 const { manager, context, sendRequest } = createSendTurnHarness();
236​
237 await manager.sendTurn({
238 sessionId: asSessionId("sess_1"),
239 attachments: [
240 {
241 type: "image",
242 url: "data:image/png;base64,BBBB",
243 },
244 ],
245 });
246​
247 expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
248 threadId: "thread_1",
249 input: [
250 {
251 type: "image",
252 url: "data:image/png;base64,BBBB",
253 },
254 ],
255 });
256 });
257​
258 it("rejects empty turn input", async () => {
259 const { manager } = createSendTurnHarness();
260​
261 await expect(
262 manager.sendTurn({
263 sessionId: asSessionId("sess_1"),
264 }),
265 ).rejects.toThrow("Turn input must include text or attachments.");
266 });
267});
268​
269describe("thread checkpoint control", () => {
270 it("reads thread turns from thread/read", async () => {
271 const { manager, context, requireSession, sendRequest } = createThreadControlHarness();
272 sendRequest.mockResolvedValue({
273 thread: {
274 id: "thread_1",
275 turns: [
276 {
277 id: "turn_1",
278 items: [{ type: "userMessage", content: [{ type: "text", text: "hello" }] }],
279 },
280 ],
281 },
282 });
283​
284 const result = await manager.readThread(asSessionId("sess_1"));
285​
286 expect(requireSession).toHaveBeenCalledWith("sess_1");
287 expect(sendRequest).toHaveBeenCalledWith(context, "thread/read", {
288 threadId: "thread_1",
289 includeTurns: true,
290 });
291 expect(result).toEqual({
292 threadId: "thread_1",
293 turns: [
294 {
295 id: "turn_1",
296 items: [{ type: "userMessage", content: [{ type: "text", text: "hello" }] }],
297 },
298 ],
299 });
300 });
301​
302 it("reads thread turns from flat thread/read responses", async () => {
303 const { manager, context, sendRequest } = createThreadControlHarness();
304 sendRequest.mockResolvedValue({
305 threadId: "thread_1",
306 turns: [
307 {
308 id: "turn_1",
309 items: [{ type: "userMessage", content: [{ type: "text", text: "hello" }] }],
310 },
311 ],
312 });
313​
314 const result = await manager.readThread(asSessionId("sess_1"));
315​
316 expect(sendRequest).toHaveBeenCalledWith(context, "thread/read", {
317 threadId: "thread_1",
318 includeTurns: true,
319 });
320 expect(result).toEqual({
321 threadId: "thread_1",
322 turns: [
323 {
324 id: "turn_1",
325 items: [{ type: "userMessage", content: [{ type: "text", text: "hello" }] }],
326 },
327 ],
328 });
329 });
330​
331 it("rolls back turns via thread/rollback and resets session running state", async () => {
332 const { manager, context, sendRequest, updateSession } = createThreadControlHarness();
333 sendRequest.mockResolvedValue({
334 thread: {
335 id: "thread_1",
336 turns: [],
337 },
338 });
339​
340 const result = await manager.rollbackThread(asSessionId("sess_1"), 2);
341​
342 expect(sendRequest).toHaveBeenCalledWith(context, "thread/rollback", {
343 threadId: "thread_1",
344 numTurns: 2,
345 });
346 expect(updateSession).toHaveBeenCalledWith(context, {
347 status: "ready",
348 activeTurnId: undefined,
349 });
350 expect(result).toEqual({
351 threadId: "thread_1",
352 turns: [],
353 });
354 });
355});