Files
OmniRoute/tests/unit/chatcore-streaming-response-headers.test.ts
Bob.Hou c75e293a47 fix(api): thread X-OmniRoute-Fallback-Attempts through combo chat (#12339) (#13038)
Right call not to reuse the combo loop's `fallbackCount`: it only increments after a leg fails or is skipped, so the second target would still report 0 at dispatch. Stamping the ordered index (or round-robin offset) at the gate is the only place the number is actually known. This PR also carries the batch's file-size rebaseline, since it merges first and the ceiling has to cover every intermediate state.

---

Validated in one consolidated worktree cut from `release/v3.8.51`, boarded with the other 19 PRs of this batch. Two in-batch conflicts, both additive and resolved by keeping each side: the `ENVIRONMENT.md` table (#13035 + #13011) and the `chatHelpers.ts` import block (#12975 on the tip + #13017).

- `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK; `check:docs-counts` migrations ✓
- complexity 2816 / baseline 3218 and cognitive-complexity 1271 / baseline 1437 — both under baseline
- 531 of 532 focused assertions green across the batch's 46 test files
- `check-file-size` rebaselined for the batch's real growth (annotation `_rebaseline_2026_09_11_mergebatch_v3851_houminxi`, landed on #13038), attributed per PR

The single red is **not this batch**: `tests/unit/combo/quota-weighted-strategy.test.ts` → "A/B isolation: 7 hard-empty + 2 at 0.5% + 1 at 40%, floor=1" asserts an order between two connections of identical weight and flakes on the pure tip too — 2 failures in 4 runs at `origin/release/v3.8.51` with nothing from this batch applied.

⚠️ base-red inherited: #12732 — `Docs Gates`, `Merge integrity`, `No new ESLint warnings`, `Unit Tests fast-path` and `Fast Quality Gates` reproduce on the pure tip (provider count 356 vs the 358 the modules define, SKILL.md drift, and `open-sse/utils/stream.ts` at 3115 > frozen 3098, untouched here).

Thanks @HouMinXi — the live evidence on these (X500 logs, `storage.sqlite` state, real `/v1/models` probes, the 36-minute outage write-up) is what let a 20-PR batch be reviewed as a unit.
2026-09-11 19:27:33 -03:00

81 lines
3.2 KiB
TypeScript

// Characterization of assembleStreamingResponseHeaders — the streaming response header builder
// extracted from handleChatCore's streaming success path (chatCore god-file decomposition, #3501).
// buildStreamingResponseHeaders is injected so the merge of upstream headers + request-id + the
// optional compression header is observable. Locks: zeroed latency/usage/cost at stream start, the
// x-omniroute-request-id, and the compression header only when meta is present.
import { test } from "node:test";
import assert from "node:assert/strict";
const { assembleStreamingResponseHeaders } =
await import("../../open-sse/handlers/chatCore/streamingResponseHeaders.ts");
function makeBuild() {
const calls: Array<{ headers: unknown; meta: Record<string, unknown> }> = [];
const build = (headers: unknown, meta: Record<string, unknown>) => {
calls.push({ headers, meta });
return { "x-upstream": "kept" };
};
return { build: build as Parameters<typeof assembleStreamingResponseHeaders>[1], calls };
}
function baseArgs(overrides: Record<string, unknown> = {}) {
return {
providerHeaders: new Headers({ "content-type": "text/event-stream" }),
provider: "openai",
model: "gpt-x",
pendingRequestId: "preq-1",
compressionResponseMeta: undefined,
...overrides,
} as Parameters<typeof assembleStreamingResponseHeaders>[0];
}
test("merges upstream headers and sets x-omniroute-request-id", () => {
const { build } = makeBuild();
const h = assembleStreamingResponseHeaders(baseArgs(), build);
assert.equal(h["x-upstream"], "kept");
assert.equal(h["x-omniroute-request-id"], "preq-1");
});
test("buildStreamingResponseHeaders receives zeroed latency/usage/cost and cacheHit false", () => {
const { build, calls } = makeBuild();
assembleStreamingResponseHeaders(baseArgs(), build);
assert.equal(calls.length, 1);
assert.equal(calls[0].meta.cacheHit, false);
assert.equal(calls[0].meta.latencyMs, 0);
assert.equal(calls[0].meta.usage, null);
assert.equal(calls[0].meta.costUsd, 0);
assert.equal(calls[0].meta.provider, "openai");
assert.equal(calls[0].meta.model, "gpt-x");
});
test("no compression meta → no compression header", () => {
const { build } = makeBuild();
const h = assembleStreamingResponseHeaders(
baseArgs({ compressionResponseMeta: undefined }),
build
);
assert.ok(!Object.values(h).includes("engine:z"));
});
test("compression meta present → compression header set", () => {
const { build } = makeBuild();
const h = assembleStreamingResponseHeaders(
baseArgs({ compressionResponseMeta: "engine:z; source=routing" }),
build
);
assert.ok(Object.values(h).includes("engine:z; source=routing"));
});
test("forwards fallbackAttempts into the streaming meta payload", () => {
const { build, calls } = makeBuild();
assembleStreamingResponseHeaders(baseArgs({ fallbackAttempts: 2 }), build);
assert.equal(calls.length, 1);
assert.equal(calls[0].meta.fallbackAttempts, 2);
});
test("omitted fallbackAttempts does not invent a count", () => {
const { build, calls } = makeBuild();
assembleStreamingResponseHeaders(baseArgs(), build);
assert.equal("fallbackAttempts" in calls[0].meta, false);
});