Skip to content

Commit d4ef2b3

Browse files
lidge-junjun
andauthored
test(responses): pin unknown usage-field passthrough across forward, non-streaming, and WS paths (#3365)
Locks the contract upstream openai/codex#41980 relies on: a future whitelist rebuild must not silently drop unknown response.usage keys (upstream issue #37138 documents the silent-accounting failure mode). Also pins that a usage-less response.completed stays accepted. Co-authored-by: jun <[email protected]>
1 parent 4a4f95f commit d4ef2b3

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3837,3 +3837,96 @@ describe("reasoning input content channel", () => {
38373837
expect(out.encrypted_content).toBe("upstream-issued-blob");
38383838
});
38393839
});
3840+
3841+
3842+
describe("raw usage passthrough on the forward path (#41980 parity, #37138 adjacency)", () => {
3843+
const usageExtras = {
3844+
input_tokens: 7,
3845+
output_tokens: 4,
3846+
total_tokens: 11,
3847+
subscription: { window: { used_percent: 12 } },
3848+
future_counter_v2: "wire-value",
3849+
};
3850+
const config = {
3851+
port: 0,
3852+
defaultProvider: "fixture",
3853+
providers: {
3854+
fixture: {
3855+
adapter: "openai-responses",
3856+
baseUrl: "https://fixture.test/v1",
3857+
authMode: "key" as const,
3858+
apiKey: "fixture-key",
3859+
},
3860+
},
3861+
} as OcxConfig;
3862+
const requestBody = (stream: boolean) => JSON.stringify({
3863+
model: "fixture/model",
3864+
stream,
3865+
input: [{ role: "user", content: [{ type: "input_text", text: "hi" }] }],
3866+
});
3867+
const call = (stream: boolean) => handleResponses(new Request("http://localhost/v1/responses", {
3868+
method: "POST",
3869+
headers: { "content-type": "application/json" },
3870+
body: requestBody(stream),
3871+
}), config, { model: "", provider: "" });
3872+
3873+
test("streamed response.completed with usage extras reaches the client byte-identical", async () => {
3874+
const savedFetch = globalThis.fetch;
3875+
globalThis.fetch = (async () => new Response([
3876+
'event: response.completed',
3877+
`data: ${JSON.stringify({ type: "response.completed", response: {
3878+
id: "resp_x", status: "completed", output: [
3879+
{ type: "message", status: "completed", content: [{ type: "output_text", text: "hi" }] },
3880+
], usage: usageExtras,
3881+
} })}`,
3882+
"",
3883+
"data: [DONE]",
3884+
"",
3885+
].join("\n"), { headers: { "content-type": "text/event-stream" } })) as typeof fetch;
3886+
try {
3887+
const response = await call(true);
3888+
const text = await response.text();
3889+
const completedLine = text.split("\n").find(line => line.startsWith("data:") && line.includes("response.completed"));
3890+
expect(completedLine).toBeDefined();
3891+
const payload = JSON.parse(completedLine!.slice(5).trim()) as { response: { usage: unknown } };
3892+
expect(payload.response.usage).toEqual(usageExtras);
3893+
} finally {
3894+
globalThis.fetch = savedFetch;
3895+
}
3896+
});
3897+
3898+
test("non-streaming forward JSON keeps usage extras", async () => {
3899+
const savedFetch = globalThis.fetch;
3900+
globalThis.fetch = (async () => new Response(JSON.stringify({
3901+
id: "resp_x",
3902+
status: "completed",
3903+
output: [{ type: "message", status: "completed", content: [{ type: "output_text", text: "hi" }] }],
3904+
usage: usageExtras,
3905+
}), { headers: { "content-type": "application/json" } })) as typeof fetch;
3906+
try {
3907+
const response = await call(false);
3908+
const json = await response.json() as { usage: unknown };
3909+
expect(json.usage).toEqual(usageExtras);
3910+
} finally {
3911+
globalThis.fetch = savedFetch;
3912+
}
3913+
});
3914+
3915+
test("response.completed without usage is accepted (Codex tolerates usage: null, #37138)", async () => {
3916+
const savedFetch = globalThis.fetch;
3917+
globalThis.fetch = (async () => new Response([
3918+
'data: {"type":"response.completed","response":{"id":"resp_x","status":"completed","output":[{"type":"message","status":"completed","content":[{"type":"output_text","text":"hi"}]}]}}',
3919+
"",
3920+
"data: [DONE]",
3921+
"",
3922+
].join("\n"), { headers: { "content-type": "text/event-stream" } })) as typeof fetch;
3923+
try {
3924+
const response = await call(true);
3925+
const text = await response.text();
3926+
expect(text).toContain("response.completed");
3927+
expect(text).not.toContain('"usage"');
3928+
} finally {
3929+
globalThis.fetch = savedFetch;
3930+
}
3931+
});
3932+
});

‎tests/ws-upstream.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,4 +952,33 @@ describe("oversized Codex create frames", () => {
952952
expect(response.headers.get("content-type")).toContain("text/event-stream");
953953
expect(await response.text()).toContain("response.completed");
954954
});
955+
956+
test("response.done normalization keeps unknown usage fields (#41980 parity)", async () => {
957+
const usage = {
958+
input_tokens: 5,
959+
output_tokens: 2,
960+
total_tokens: 7,
961+
subscription: { window: { used_percent: 3 } },
962+
future_counter_v2: true,
963+
};
964+
installFake(ws => {
965+
ws.emit("open", {});
966+
ws.emit("message", {
967+
data: JSON.stringify({
968+
type: "response.done",
969+
response: { id: "r-done", status: "completed", output: [], usage },
970+
}),
971+
});
972+
ws.emit("close", { code: 1000, reason: "normal" });
973+
});
974+
const response = await codexWsUpstreamFetch(CODEX_URL, streamingInit(), (() => {
975+
throw new Error("fallback must not run after open");
976+
}) as unknown as typeof fetch);
977+
978+
const text = await response.text();
979+
const line = text.split("\n").find(l => l.startsWith("data:") && l.includes("response.completed"));
980+
expect(line).toBeDefined();
981+
const payload = JSON.parse(line!.slice(5).trim()) as { response: { usage: unknown } };
982+
expect(payload.response.usage).toEqual(usage);
983+
});
955984
});

0 commit comments

Comments
 (0)