Skip to content

Commit 7bfcec4

Browse files
committed
test(ws-upstream): prove the rewrite chain through handleResponses, not the factory
The `eager-relay marker preconditions` test asserted only that `createResponsesFieldBackfillBlockRewrite()` returns a function. That would stay green if `handleResponses` stopped adding it to `blockRewrites`, so it did not actually protect the contract the two marker assertions depend on. Replace it with an integration case in the existing `handleResponses` describe: drive a Responses stream whose `output_text` part omits the required `annotations` field, then read the client bytes back. Seeing `annotations: []` there is only possible if the rewrite is registered and ran, which is exactly what makes `clientBlockRewrite !== undefined` and `needsClientRewrite === true`. The platform half stays a pure unit test on the real exported helper. Verified on win32, exact head: - bun run typecheck exit 0 - bun test tests/ws-upstream.test.ts 25 pass / 0 fail Mutation-checked: - dropping `createResponsesFieldBackfillBlockRewrite()` from `blockRewrites` fails the new case on `toHaveProperty("annotations")` (3 fail) - widening `isWin32EagerRewrite` past win32 fails the truth table (1 fail) Still test-only; no runtime change.
1 parent 9a9c090 commit 7bfcec4

1 file changed

Lines changed: 51 additions & 13 deletions

File tree

‎tests/ws-upstream.test.ts‎

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { providerFetch } from "../src/server/responses/fetch-helpers";
33
import { handleResponses } from "../src/server/responses";
44
import { isEagerRelaySseResponse } from "../src/server/relay";
55
import { isWin32EagerRewrite } from "../src/lib/bun-stream-caps";
6-
import { createResponsesFieldBackfillBlockRewrite } from "../src/server/responses/responses-field-backfill";
76
import {
87
bunSupportsBoundedCodexWsRelay,
98
codexWsUpstreamFetch as rawCodexWsUpstreamFetch,
@@ -347,20 +346,59 @@ describe("handleResponses Codex WS relay selection", () => {
347346
expect(await response.text()).toContain("response.completed");
348347
},
349348
);
350-
});
351349

352-
describe("eager-relay marker preconditions", () => {
353-
test("the client rewrite chain is never empty, so win32 always takes the eager path", () => {
354-
// This is the coupling that silently changed the two cases above.
355-
// `createResponsesFieldBackfillBlockRewrite()` is added to `blockRewrites`
356-
// unconditionally and returns a rewrite rather than `undefined`, so
357-
// `needsClientRewrite` in `handleResponses` is a constant `true`. Combined with
358-
// the win32 rule that makes the eager relay the only safe path for rewrite
359-
// traffic, every Responses stream on Windows is marked. If either half changes,
360-
// this fails here rather than in an assertion that looks like it is about
361-
// WebSocket selection.
362-
expect(typeof createResponsesFieldBackfillBlockRewrite()).toBe("function");
350+
// The two cases above assert the marker against `EAGER_RELAY_FORCED_BY_PLATFORM`,
351+
// which is only the right expectation while `needsClientRewrite` is genuinely
352+
// `true`. Rather than assert that through the rewrite factory — which would stay
353+
// green if `handleResponses` stopped registering it — this drives a real Responses
354+
// stream through the handler and reads the rewrite's own effect off the client
355+
// bytes. `createResponsesFieldBackfillBlockRewrite()` is the unconditional entry in
356+
// `blockRewrites`, so observing its transformation is what proves
357+
// `clientBlockRewrite !== undefined`, hence `needsClientRewrite === true`.
358+
test("the registered rewrite chain transforms the client stream, so needsClientRewrite is true", async () => {
359+
const upstreamEvent = {
360+
type: "response.completed",
361+
response: {
362+
id: "r-backfill",
363+
status: "completed",
364+
// Deliberately spec-non-compliant: `annotations` is required on
365+
// `output_text` and this upstream omits it. Only the backfill rewrite
366+
// puts it back.
367+
output: [{
368+
type: "message",
369+
role: "assistant",
370+
content: [{ type: "output_text", text: "hi" }],
371+
}],
372+
},
373+
};
374+
globalThis.fetch = (async () => new Response(
375+
`event: response.completed\ndata: ${JSON.stringify(upstreamEvent)}\n\n`,
376+
{ status: 200, headers: { "content-type": "text/event-stream" } },
377+
)) as typeof fetch;
378+
379+
const response = await handleResponses(request(), forwardConfig(), { model: "", provider: "" });
380+
const text = await response.text();
381+
382+
const payload = text
383+
.split("\n")
384+
.filter(line => line.startsWith("data: ") && line !== "data: [DONE]")
385+
.map(line => JSON.parse(line.slice("data: ".length)))
386+
.find(event => event.type === "response.completed");
387+
388+
expect(payload).toBeDefined();
389+
// Absent on the wire, present to the client: the chain ran.
390+
expect(payload.response.output[0].content[0]).toHaveProperty("annotations");
391+
expect(payload.response.output[0].content[0].annotations).toEqual([]);
392+
393+
// And with the chain proven non-empty, the marker is exactly the win32 rule.
394+
expect(isEagerRelaySseResponse(response)).toBe(EAGER_RELAY_FORCED_BY_PLATFORM);
395+
});
396+
});
363397

398+
describe("isWin32EagerRewrite", () => {
399+
test("marks rewrite traffic on win32 only", () => {
400+
// #864 is a win32-only Bun sink defect, so the rule must not widen to other
401+
// platforms, and must not fire when there is nothing to rewrite.
364402
expect(isWin32EagerRewrite("win32", true)).toBe(true);
365403
expect(isWin32EagerRewrite("win32", false)).toBe(false);
366404
expect(isWin32EagerRewrite("darwin", true)).toBe(false);

0 commit comments

Comments
 (0)