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

codexAppServerManager.test.ts

200 lines5.5 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import {
4 CodexAppServerManager,
5 classifyCodexStderrLine,
6 isRecoverableThreadResumeError,
7 normalizeCodexModelSlug,
8} from "./codexAppServerManager";
9​
10function createSendTurnHarness() {
11 const manager = new CodexAppServerManager();
12 const context = {
13 session: {
14 sessionId: "sess_1",
15 provider: "codex",
16 status: "ready",
17 threadId: "thread_1",
18 createdAt: "2026-02-10T00:00:00.000Z",
19 updatedAt: "2026-02-10T00:00:00.000Z",
20 },
21 };
22​
23 const requireSession = vi
24 .spyOn(
25 manager as unknown as { requireSession: (sessionId: string) => unknown },
26 "requireSession",
27 )
28 .mockReturnValue(context);
29 const sendRequest = vi
30 .spyOn(
31 manager as unknown as { sendRequest: (...args: unknown[]) => Promise<unknown> },
32 "sendRequest",
33 )
34 .mockResolvedValue({
35 turn: {
36 id: "turn_1",
37 },
38 });
39 const updateSession = vi
40 .spyOn(manager as unknown as { updateSession: (...args: unknown[]) => void }, "updateSession")
41 .mockImplementation(() => {});
42​
43 return { manager, context, requireSession, sendRequest, updateSession };
45​
46describe("classifyCodexStderrLine", () => {
47 it("ignores empty lines", () => {
48 expect(classifyCodexStderrLine(" ")).toBeNull();
49 });
50​
51 it("ignores non-error structured codex logs", () => {
52 const line =
53 "2026-02-08T04:24:19.241256Z WARN codex_core::features: unknown feature key in config: skills";
54 expect(classifyCodexStderrLine(line)).toBeNull();
55 });
56​
57 it("ignores known benign rollout path errors", () => {
58 const line =
59 "\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";
60 expect(classifyCodexStderrLine(line)).toBeNull();
61 });
62​
63 it("keeps unknown structured errors", () => {
64 const line = "2026-02-08T04:24:20.085687Z ERROR codex_core::runtime: unrecoverable failure";
65 expect(classifyCodexStderrLine(line)).toEqual({
66 message: line,
67 });
68 });
69​
70 it("keeps plain stderr messages", () => {
71 const line = "fatal: permission denied";
72 expect(classifyCodexStderrLine(line)).toEqual({
73 message: line,
74 });
75 });
76});
77​
78describe("normalizeCodexModelSlug", () => {
79 it("maps 5.3 aliases to gpt-5.3-codex", () => {
80 expect(normalizeCodexModelSlug("5.3")).toBe("gpt-5.3-codex");
81 expect(normalizeCodexModelSlug("gpt-5.3")).toBe("gpt-5.3-codex");
82 });
83​
84 it("prefers codex id when model differs", () => {
85 expect(normalizeCodexModelSlug("gpt-5.3", "gpt-5.3-codex")).toBe("gpt-5.3-codex");
86 });
87​
88 it("keeps non-aliased models as-is", () => {
89 expect(normalizeCodexModelSlug("gpt-5.2-codex")).toBe("gpt-5.2-codex");
90 expect(normalizeCodexModelSlug("gpt-5.2")).toBe("gpt-5.2");
91 });
92});
93​
94describe("isRecoverableThreadResumeError", () => {
95 it("matches not-found resume errors", () => {
96 expect(
97 isRecoverableThreadResumeError(new Error("thread/resume failed: thread not found")),
98 ).toBe(true);
99 });
100​
101 it("ignores non-resume errors", () => {
102 expect(
103 isRecoverableThreadResumeError(new Error("thread/start failed: permission denied")),
104 ).toBe(false);
105 });
106​
107 it("ignores non-recoverable resume errors", () => {
108 expect(
109 isRecoverableThreadResumeError(
110 new Error("thread/resume failed: timed out waiting for server"),
111 ),
112 ).toBe(false);
113 });
114});
115​
116describe("sendTurn", () => {
117 it("sends text and image user input items to turn/start", async () => {
118 const { manager, context, requireSession, sendRequest, updateSession } =
119 createSendTurnHarness();
120​
121 const result = await manager.sendTurn({
122 sessionId: "sess_1",
123 input: "Inspect this image",
124 attachments: [
125 {
126 type: "image",
127 name: "error.png",
128 mimeType: "image/png",
129 sizeBytes: 1_024,
130 dataUrl: "data:image/png;base64,AAAA",
131 },
132 ],
133 model: "gpt-5.3",
134 effort: "high",
135 });
136​
137 expect(result).toEqual({
138 threadId: "thread_1",
139 turnId: "turn_1",
140 });
141 expect(requireSession).toHaveBeenCalledWith("sess_1");
142 expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
143 threadId: "thread_1",
144 input: [
145 {
146 type: "text",
147 text: "Inspect this image",
148 text_elements: [],
149 },
150 {
151 type: "image",
152 url: "data:image/png;base64,AAAA",
153 },
154 ],
155 model: "gpt-5.3-codex",
156 effort: "high",
157 });
158 expect(updateSession).toHaveBeenCalledWith(context, {
159 status: "running",
160 activeTurnId: "turn_1",
161 });
162 });
163​
164 it("supports image-only turns", async () => {
165 const { manager, context, sendRequest } = createSendTurnHarness();
166​
167 await manager.sendTurn({
168 sessionId: "sess_1",
169 attachments: [
170 {
171 type: "image",
172 name: "diagram.png",
173 mimeType: "image/png",
174 sizeBytes: 256,
175 dataUrl: "data:image/png;base64,BBBB",
176 },
177 ],
178 });
179​
180 expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
181 threadId: "thread_1",
182 input: [
183 {
184 type: "image",
185 url: "data:image/png;base64,BBBB",
186 },
187 ],
188 });
189 });
190​
191 it("rejects empty turn input", async () => {
192 const { manager } = createSendTurnHarness();
193​
194 await expect(
195 manager.sendTurn({
196 sessionId: "sess_1",
197 }),
198 ).rejects.toThrow("Turn input must include text or attachments.");
199 });
200});