From 676d9de49da415a50761399e4c510f9c8268eb21 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 26 Oct 2026 22:07:32 -0500 Subject: [PATCH] test(design-code-proxy): regression test for streaming Content-Length (#688) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The streaming half of #685's Content-Length fix had no regression test: the stub never set Content-Length on SSE responses, so ablating writeHead's streaming delete failed nothing (triage task 1067). - Stub gains an 'ok-length-stream' mode: the SSE payload is served with a Content-Length equal to the backend's own byte count, which the proxy's per-chunk model rewrite makes stale. - New test asserts the client receives the full rewritten stream (incl. [DONE]) and that any advertised Content-Length matches the bytes actually sent. Verified: suite 7/7 on current code; with the streaming delete ablated, exactly the new test fails (6 pass / 1 fail). Test-only — no proxy change. --- bin/design-code-proxy.test.mjs | 41 ++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/bin/design-code-proxy.test.mjs b/bin/design-code-proxy.test.mjs index aba4ea9b4..d82d4a3fa 100644 --- a/bin/design-code-proxy.test.mjs +++ b/bin/design-code-proxy.test.mjs @@ -10,6 +10,10 @@ // 'quota-status' -> HTTP 429 status // 'ok-length' -> a successful chat completion with an explicit // Content-Length on the compact JSON body (#684) +// 'ok-length-stream' -> the SSE stream, with a Content-Length set to +// the backend's own (pre-model-rewrite) byte count, +// which is stale the moment the proxy rewrites the +// model in each chunk (#688) // 'ok' (default) -> a successful chat completion naming the backend // // A quota error reported in a 200 JSON body used to hang the client @@ -56,14 +60,20 @@ const startStub = (name) => new Promise((resolve) => { res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(compact) }); res.end(compact); } else if (bodyText.includes('"stream":true')) { - res.writeHead(200, { 'Content-Type': 'text/event-stream' }); const chunk = { id: 'chatcmpl-stub', object: 'chat.completion.chunk', created: 0, model: 'stub-model', choices: [{ index: 0, delta: { content: `hello from ${name}` }, finish_reason: null }] }; - res.write(`data: ${JSON.stringify(chunk)}\n\n`); - res.write('data: [DONE]\n\n'); - res.end(); + const payload = `data: ${JSON.stringify(chunk)}\n\ndata: [DONE]\n\n`; + const headers = { 'Content-Type': 'text/event-stream' }; + if (stub.mode === 'ok-length-stream') { + // #688: the length of the backend's own body. The proxy's + // per-chunk model rewrite makes the body it sends longer, so + // forwarding this length would truncate the stream. + headers['Content-Length'] = Buffer.byteLength(payload); + } + res.writeHead(200, headers); + res.end(payload); } else { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ @@ -193,6 +203,29 @@ test('backend Content-Length does not truncate the re-serialized success body (# assert.equal(fallback.calls, 0); }); +test('backend Content-Length does not truncate the model-rewritten SSE stream (#688)', async (t) => { + const { port, primary, fallback, stop } = await startProxy(); + t.after(stop); + primary.mode = 'ok-length-stream'; + const res = await post(port, { stream: true }); + assert.equal(res.status, 200); + const sse = await res.text(); + // The proxy rewrites the model in every chunk, so the stream it sends + // is longer than the backend body the stub's Content-Length describes. + // Forwarding that stale length truncates the tail — [DONE] is lost. + assert.match(sse, /hello from primary/); + assert.match(sse, /design-code-flash/); + assert.ok(sse.trimEnd().endsWith('data: [DONE]'), `stream truncated: ${JSON.stringify(sse)}`); + // The forwarded head must describe the stream actually sent: either no + // Content-Length (chunked) or exactly the received byte count. + const advertised = res.headers.get('content-length'); + if (advertised !== null) { + assert.equal(Number(advertised), Buffer.byteLength(sse)); + } + assert.equal(primary.calls, 1); + assert.equal(fallback.calls, 0); +}); + test('streaming success is unchanged', async (t) => { const { port, stop } = await startProxy(); t.after(stop);