Upstream emits AI SDK v5 {"type":"error"} events inside an HTTP 200 stream.
The translator turned them into fake success content ([CommandCode error: ...]
+ finish_reason stop), so account/model fallback never fired and logs showed
Status: success.
- translator: error events now emit an OpenAI-shaped error chunk (chunk.error)
instead of content; parseSSEToOpenAIResponse already detects chunk?.error
- executor: peek the first events before committing the response; an early
error event returns 502 so fallback runs before any byte reaches the client
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
/**
|
|
* Unit tests for the CommandCode executor early-error peek.
|
|
*
|
|
* The upstream emits AI SDK v5 NDJSON over an HTTP 200 stream, so a terminal
|
|
* `{"type":"error"}` event is invisible to the normal `response.ok` success
|
|
* check. `peekForUpstreamError` reads the first events before committing the
|
|
* response: an error event → non-ok Response (fallback can kick in); otherwise
|
|
* the buffered bytes are re-emitted and streaming proceeds as before.
|
|
*/
|
|
import { describe, it, expect } from "vitest";
|
|
import { peekForUpstreamError } from "../../open-sse/executors/commandcode.js";
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
function ndjsonResponse(lines) {
|
|
const body = new ReadableStream({
|
|
start(controller) {
|
|
for (const line of lines) controller.enqueue(encoder.encode(line + "\n"));
|
|
controller.close();
|
|
},
|
|
});
|
|
return new Response(body, {
|
|
status: 200,
|
|
headers: { "content-type": "text/event-stream" },
|
|
});
|
|
}
|
|
|
|
describe("commandcode executor — early-error peek", () => {
|
|
it("returns 502 when the first meaningful event is an error", async () => {
|
|
const res = await peekForUpstreamError(
|
|
ndjsonResponse([
|
|
'{"type":"error","error":{"type":"server_error","message":"Network connection lost."}}',
|
|
]),
|
|
"model",
|
|
);
|
|
expect(res.status).toBe(502);
|
|
const body = await res.json();
|
|
expect(body.error.message).toBe("Network connection lost.");
|
|
expect(body.error.type).toBe("server_error");
|
|
});
|
|
|
|
it("detects an error event even when metadata events arrive first", async () => {
|
|
const res = await peekForUpstreamError(
|
|
ndjsonResponse([
|
|
'{"type":"start"}',
|
|
'{"type":"start-step"}',
|
|
'{"type":"error","error":{"type":"server_error","message":"Network connection lost."}}',
|
|
]),
|
|
"model",
|
|
);
|
|
expect(res.status).toBe(502);
|
|
const body = await res.json();
|
|
expect(body.error.message).toContain("Network connection lost");
|
|
});
|
|
|
|
it("commits and streams normally when the first meaningful event is content", async () => {
|
|
const res = await peekForUpstreamError(
|
|
ndjsonResponse([
|
|
'{"type":"start"}',
|
|
'{"type":"text-delta","text":"hi there"}',
|
|
'{"type":"finish"}',
|
|
]),
|
|
"model",
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const text = await res.text();
|
|
expect(text).toContain('"content":"hi there"');
|
|
expect(text).not.toContain("[CommandCode error:");
|
|
});
|
|
|
|
it("commits when the stream ends without any event", async () => {
|
|
const res = await peekForUpstreamError(ndjsonResponse([]), "model");
|
|
expect(res.status).toBe(200);
|
|
await res.body.cancel();
|
|
});
|
|
|
|
it("commits (does not hang) when no event arrives before the peek timeout", async () => {
|
|
const stalled = new Response(new ReadableStream({ start() {} }), {
|
|
status: 200,
|
|
headers: { "content-type": "text/event-stream" },
|
|
});
|
|
const res = await peekForUpstreamError(stalled, "model", { timeoutMs: 50 });
|
|
expect(res.status).toBe(200);
|
|
await res.body.cancel();
|
|
});
|
|
|
|
it("does not hang when the request signal aborts during the peek", async () => {
|
|
const controller = new AbortController();
|
|
const stalled = new Response(new ReadableStream({ start() {} }), {
|
|
status: 200,
|
|
headers: { "content-type": "text/event-stream" },
|
|
});
|
|
setTimeout(() => controller.abort(new Error("client gone")), 10);
|
|
const res = await peekForUpstreamError(stalled, "model", {
|
|
signal: controller.signal,
|
|
timeoutMs: 2000,
|
|
});
|
|
expect(res.status).toBe(200);
|
|
await res.body.cancel();
|
|
});
|
|
});
|