Skip to content

Commit bf29126

Browse files
lidge-junmaoxin1234
andcommitted
test(server): cover live-outcome booking for invalid answers and alias failure
Follow-up to lidge-jun#4512 (maoxin1234), which recorded the real upstream status before body handling on the audio/live routes but landed without the two live regressions CodeRabbit asked for — pushing to the contributor branch would have reset its completed readiness gate. Two cases in the /v1/live fixture: an invalid live answer (upstream 200, no location, empty body) gives the client 502 while recordCodexUpstreamOutcome books 200 for the creating account, and an alias registration failure (valid 200 answer, binding create returns null) gives the client 503 while the same 200 is booked. The pre-fetch hasCapacity 503 is not the path under test; the alias case asserts the response message so it cannot pass through the capacity branch. Co-authored-by: maoxin1234 <[email protected]>
1 parent 866367a commit bf29126

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

‎tests/server/audio-dictation.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { saveConfig } from "../../src/config";
1010
import { startServer } from "../../src/server";
1111
import { createDictationFrameValidator } from "../../src/server/audio-dictation";
1212
import { abortAndReleaseAllTurns, resetLifecycleDrainStateForTests } from "../../src/server/lifecycle";
13+
import { LiveCallBindings } from "../../src/server/live-call-bindings";
1314
import type { OcxConfig } from "../../src/types";
1415
import { fakeChatGptJwt } from "../helpers/fake-chatgpt-jwt";
1516
import { installIsolatedCodexHome, type IsolatedCodexHome } from "../helpers/isolated-codex-home";
@@ -32,7 +33,7 @@ const startEvent = { type: "session.start", config: {
3233
vad: { type: "server_vad", threshold: 0.5, prefix_padding_ms: 300, silence_duration_ms: 500 },
3334
} };
3435

35-
function createFixture(options: { failDictation?: boolean } = {}) {
36+
function createFixture(options: { failDictation?: boolean; answer?: "invalid" | "ok200" } = {}) {
3637
const creates: Headers[] = [];
3738
const handshakes: Array<{ url: string; headers: Headers; protocols?: string[] }> = [];
3839
const frames: string[] = [];
@@ -71,6 +72,8 @@ function createFixture(options: { failDictation?: boolean } = {}) {
7172
if (["chatgpt.com", "api.openai.com"].includes(new URL(req.url).hostname)) {
7273
if (new URL(req.url).pathname.endsWith("/realtime/calls") || new URL(req.url).pathname === "/v1/live") {
7374
creates.push(new Headers(req.headers));
75+
if (options.answer === "invalid") return new Response("", { status: 200 });
76+
if (options.answer === "ok200") return new Response("v=0\r\n", { status: 200, headers: { "content-type": "application/sdp", location: `https://api.openai.com/v1/live/rtc_upstream_${creates.length}` } });
7477
return new Response("v=0\r\n", { status: 201, headers: { "content-type": "application/sdp", location: `https://api.openai.com/v1/live/rtc_upstream_${creates.length}` } });
7578
}
7679
return Response.json({});
@@ -270,6 +273,36 @@ describe("external audio sockets", () => {
270273
}
271274
expect(fixture.handshakes).toHaveLength(0);
272275
});
276+
test("invalid live answer books the upstream 200 while the client gets 502", async () => {
277+
fixture = createFixture({ answer: "invalid" });
278+
const outcomes = spyOn(routing, "recordCodexUpstreamOutcome");
279+
try {
280+
const response = await fetchOriginal(new URL("/v1/live", fixture.server.url), {
281+
method: "POST", headers: { authorization: `Bearer ${KEY}`, "content-type": "application/json" }, body: JSON.stringify({ sdp: "v=0\r\n" }),
282+
});
283+
expect(response.status).toBe(502);
284+
const body = await response.text();
285+
expect(body).toContain("invalid call answer");
286+
const accountId = fixture.creates[0]!.get("chatgpt-account-id") === "acct-b" ? "pool-b" : "pool-a";
287+
expect(outcomes.mock.calls.filter(call => call[1] === accountId).map(call => call[2])).toEqual([200]);
288+
} finally { outcomes.mockRestore(); }
289+
});
290+
test("alias registration failure books the upstream 200 while the client gets 503", async () => {
291+
fixture = createFixture({ answer: "ok200" });
292+
const outcomes = spyOn(routing, "recordCodexUpstreamOutcome");
293+
const create = spyOn(LiveCallBindings.prototype, "create").mockReturnValue(null);
294+
try {
295+
const response = await fetchOriginal(new URL("/v1/live", fixture.server.url), {
296+
method: "POST", headers: { authorization: `Bearer ${KEY}`, "content-type": "application/json" }, body: JSON.stringify({ sdp: "v=0\r\n" }),
297+
});
298+
expect(response.status).toBe(503);
299+
const body = await response.text();
300+
expect(body).toContain("Live call could not be registered");
301+
expect(body).not.toContain("Live call capacity reached");
302+
const accountId = fixture.creates[0]!.get("chatgpt-account-id") === "acct-b" ? "pool-b" : "pool-a";
303+
expect(outcomes.mock.calls.filter(call => call[1] === accountId).map(call => call[2])).toEqual([200]);
304+
} finally { outcomes.mockRestore(); create.mockRestore(); }
305+
});
273306
test("missing reserved aliases never become legacy native joins", async () => {
274307
fixture = createFixture();
275308
const response = await fetchOriginal(new URL("/v1/live/rtc_ocx_expired", fixture.server.url), { headers: { upgrade: "websocket" } });

0 commit comments

Comments
 (0)