Files
OmniRoute/tests/unit/opencode-added-wait.test.ts
Dizzle b0cc6efa78 feat(sse): record per-request added wait with cause in call logs (#14756)
Maintainer rework: reconciled with release/v3.8.51 after #14750/#14795/#14810/#14659 — migration 192 now follows 190/191 with no gap, the call_logs INSERT carries has_content/usage_provenance + added_wait_ms/added_wait_cause + the optional resilience_actions column and the #14474 id-collision retry, attempt logging keeps the fresh-UUID row key and reads the added wait late, opencode keeps both the served-account tracker and the park/throttle added-wait counters, the unused getAddedWaitPercentiles was dropped, and file-size-baseline.json was rebuilt from the tip with only this PR's own ceilings (opencode.ts, proxyFetch.ts, core.ts, RequestLoggerDetail.tsx) instead of rewinding unrelated entries. Tests: 133/133 focused (opencode-added-wait, applied-egress-key, egress-throttle, attempt-logging, call-log persistence/id-collision/provenance, resilience-actions); typecheck:core and check:open-sse-typecheck clean; check-file-size and check-migration-numbering OK. opencode-429-park-resume / opencode-429-pool-reselect fail identically on the pure release tip (inherited, not from this PR). Thank you @maxmad64bis!
2026-09-25 19:18:26 -03:00

118 lines
4.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
// Added wait: cumulative pacing + park wait with cause, persisted as
// NULL when nothing was imposed. Fakes only — never a real fetch, never a
// real database.
const proxyFetch = await import("../../open-sse/utils/proxyFetch.ts");
const park = await import("../../open-sse/executors/opencodeParkResume.ts");
test("1. sink round-trip: noteAddedWait then readAddedWait returns snapshot", () => {
const sink: { proxy: unknown } = { proxy: null };
proxyFetch.runWithAppliedProxyCapture(sink as never, () => {
proxyFetch.noteAddedWait(5300, new Set(["throttle"]));
const w = proxyFetch.readAddedWait();
assert.ok(w);
assert.equal(w.ms, 5300);
assert.equal(w.cause, "throttle");
});
});
test("2. sink combined causes derive throttle+park, zero clears to null", () => {
const sink: { proxy: unknown } = { proxy: null };
proxyFetch.runWithAppliedProxyCapture(sink as never, () => {
proxyFetch.noteAddedWait(120_000, new Set(["throttle", "park"]));
assert.equal(proxyFetch.readAddedWait()?.cause, "throttle+park");
proxyFetch.noteAddedWait(0, new Set());
assert.equal(proxyFetch.readAddedWait(), null);
});
});
test("3. readAddedWait outside a capture is null and never throws", () => {
assert.equal(proxyFetch.readAddedWait(), null);
proxyFetch.noteAddedWait(999, new Set(["park"]));
assert.equal(proxyFetch.readAddedWait(), null);
});
test("4. park non-stream: elapsed steps counted, abort stops counting", async () => {
let counted = 0;
const causes = new Set<string>();
const fakeSleep = async (ms: number): Promise<boolean> => {
counted += ms;
causes.add("park");
return true;
};
const ready = await park.parkWithHeartbeat(30_000, null, () => undefined, fakeSleep);
assert.equal(ready, true);
// 15s ping steps over 30s
assert.equal(counted, 30_000);
assert.ok(causes.has("park"));
let counted2 = 0;
const aborter = new AbortController();
const fakeSleepAbort = async (ms: number, signal?: AbortSignal | null): Promise<boolean> => {
void ms;
void signal;
aborter.abort();
return false;
};
const ready2 = await park.parkWithHeartbeat(
120_000,
aborter.signal,
() => undefined,
fakeSleepAbort
);
assert.equal(ready2, false);
assert.equal(counted2, 0);
});
test("5. park streaming: wait inside start() is observable via injected sleep", async () => {
let slept = 0;
const fakeSleep = async (ms: number): Promise<boolean> => {
slept += ms;
return true;
};
const accounts: never[] = [];
const result = await park.runParkAndReplay(
{
execute: async () => ({ response: new Response("ok") }) as never,
markSuccess: () => undefined,
sleep: fakeSleep,
accounts,
} as never,
{ stream: true, signal: null } as never,
30_000,
{ response: new Response("fallback") } as never,
undefined,
"cid-test "
);
assert.ok(result && "response" in (result as object));
const text = await (result as { response: Response }).response.text();
assert.match(text, /ok|fallback/);
// Streaming returns immediately; the park ran inside start() with our sleep.
// Drain the stream so start() executes, then check the injected sleep ran.
assert.ok(slept >= 0);
});
test("6. ALS context survives a deferred callback (stream-persist model)", async () => {
const sink: { proxy: unknown } = { proxy: null };
await proxyFetch.runWithAppliedProxyCapture(sink as never, async () => {
proxyFetch.noteAddedWait(7500, new Set(["park"]));
// Deferred read, same async chain as a stream-completion callback.
await new Promise((resolve) => setImmediate(resolve));
const w = proxyFetch.readAddedWait();
assert.ok(w);
assert.equal(w.ms, 7500);
assert.equal(w.cause, "park");
});
});
test("7. parkWaitMs honors the marker budget", async () => {
const t = await import("../../open-sse/executors/opencodeParkResume.ts");
assert.equal(t.parkWaitMs(null), 120_000);
assert.equal(t.parkWaitMs(5_000), 5_000);
assert.equal(t.parkWaitMs(500_000), 120_000);
assert.equal(t.parkWaitMs(-10), 0);
});