Files
OmniRoute/tests/unit/chatcore-nonstreaming-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

103 lines
3.7 KiB
TypeScript

// Characterization of buildNonStreamingResponseHeaders — the cache-MISS response header builder
// extracted from handleChatCore's non-streaming success path (chatCore god-file decomposition,
// #3501). attachOmniRouteMetaHeaders + now are injected so the static headers, the meta payload,
// and the optional compression header are observable. Locks: Content-Type + cache MISS, latencyMs =
// now - startTime, and the compression header only when meta is present.
import { test } from "node:test";
import assert from "node:assert/strict";
const { buildNonStreamingResponseHeaders } =
await import("../../open-sse/handlers/chatCore/nonStreamingResponseHeaders.ts");
function makeDeps(now = 1000) {
const metaCalls: Array<{ headers: Record<string, string>; meta: Record<string, unknown> }> = [];
const deps = {
attachOmniRouteMetaHeaders: (
headers: Record<string, string>,
meta: Record<string, unknown>
) => {
metaCalls.push({ headers, meta });
headers["x-omniroute-meta"] = "attached";
},
now: () => now,
} as Parameters<typeof buildNonStreamingResponseHeaders>[1];
return { deps, metaCalls };
}
function baseArgs(overrides: Record<string, unknown> = {}) {
return {
provider: "openai",
model: "gpt-x",
startTime: 600,
responseUsage: { prompt_tokens: 5 },
estimatedCost: 0.0012,
requestId: "req-1",
compressionResponseMeta: undefined,
...overrides,
} as Parameters<typeof buildNonStreamingResponseHeaders>[0];
}
function acceptsAttachMetaInputContract(args: {
responseUsage: Record<string, unknown> | null | undefined;
requestId: string | null | undefined;
}) {
return args;
}
test("builder input types match the metadata attachment contract", () => {
acceptsAttachMetaInputContract(baseArgs());
});
test("static headers: Content-Type json + cache MISS", () => {
const { deps } = makeDeps();
const h = buildNonStreamingResponseHeaders(baseArgs(), deps);
assert.equal(h["Content-Type"], "application/json");
// cache marker key/value
assert.ok(Object.values(h).includes("MISS"));
});
test("meta receives provider/model/cacheHit false/latency/usage/cost/requestId", () => {
const { deps, metaCalls } = makeDeps(1000);
buildNonStreamingResponseHeaders(baseArgs({ startTime: 600 }), deps);
assert.equal(metaCalls.length, 1);
const meta = metaCalls[0].meta;
assert.equal(meta.provider, "openai");
assert.equal(meta.model, "gpt-x");
assert.equal(meta.cacheHit, false);
assert.equal(meta.latencyMs, 400); // now 1000 - startTime 600
assert.deepEqual(meta.usage, { prompt_tokens: 5 });
assert.equal(meta.costUsd, 0.0012);
assert.equal(meta.requestId, "req-1");
});
test("no compression meta → no compression header", () => {
const { deps } = makeDeps();
const h = buildNonStreamingResponseHeaders(
baseArgs({ compressionResponseMeta: undefined }),
deps
);
assert.ok(!Object.values(h).includes("engine:x"));
});
test("compression meta present → compression header set to that value", () => {
const { deps } = makeDeps();
const h = buildNonStreamingResponseHeaders(
baseArgs({ compressionResponseMeta: "engine:x; source=header" }),
deps
);
assert.ok(Object.values(h).includes("engine:x; source=header"));
});
test("forwards fallbackAttempts into the non-streaming meta payload", () => {
const { deps, metaCalls } = makeDeps();
buildNonStreamingResponseHeaders(baseArgs({ fallbackAttempts: 3 }), deps);
assert.equal(metaCalls.length, 1);
assert.equal(metaCalls[0].meta.fallbackAttempts, 3);
});
test("omitted fallbackAttempts does not invent a count", () => {
const { deps, metaCalls } = makeDeps();
buildNonStreamingResponseHeaders(baseArgs(), deps);
assert.equal("fallbackAttempts" in metaCalls[0].meta, false);
});