feat: add SSE parser and in-flight request tracker
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { InflightTracker, type FeedEvent } from "../src/lib/inflight-tracker";
|
||||
|
||||
test("snapshot rebuilds counts for all in-flight requests", () => {
|
||||
const tracker = new InflightTracker();
|
||||
tracker.apply({
|
||||
type: "inflight",
|
||||
operation: "snapshot",
|
||||
requests: [
|
||||
{ model: "A", id: "1" },
|
||||
{ model: "A", id: "2" },
|
||||
{ model: "B", id: "3" },
|
||||
],
|
||||
});
|
||||
assert.equal(tracker.count("A"), 2);
|
||||
assert.equal(tracker.count("B"), 1);
|
||||
assert.equal(tracker.count("C"), 0);
|
||||
});
|
||||
|
||||
test("add and remove adjust per-model counts", () => {
|
||||
const tracker = new InflightTracker();
|
||||
tracker.apply({ type: "inflight", operation: "add", requests: [{ model: "A", id: "1" }] });
|
||||
tracker.apply({ type: "inflight", operation: "add", requests: [{ model: "A", id: "2" }] });
|
||||
assert.equal(tracker.count("A"), 2);
|
||||
tracker.apply({ type: "inflight", operation: "remove", requests: [{ model: "A", id: "1" }] });
|
||||
assert.equal(tracker.count("A"), 1);
|
||||
tracker.apply({ type: "inflight", operation: "remove", requests: [{ model: "A", id: "2" }] });
|
||||
assert.equal(tracker.count("A"), 0);
|
||||
tracker.apply({ type: "inflight", operation: "remove", requests: [{ model: "A", id: "9" }] });
|
||||
assert.equal(tracker.count("A"), 0);
|
||||
});
|
||||
|
||||
test("modelStatus normalizes states", () => {
|
||||
const tracker = new InflightTracker();
|
||||
tracker.apply({
|
||||
type: "modelStatus",
|
||||
models: [
|
||||
{ id: "A", state: "ready" },
|
||||
{ id: "B", state: "loading" },
|
||||
{ id: "C", state: "stopped" },
|
||||
{ id: "D", state: "weird" },
|
||||
],
|
||||
});
|
||||
assert.equal(tracker.state("A"), "ready");
|
||||
assert.equal(tracker.state("B"), "loading");
|
||||
assert.equal(tracker.state("C"), "stopped");
|
||||
assert.equal(tracker.state("D"), "stopped");
|
||||
assert.equal(tracker.state("missing"), undefined);
|
||||
});
|
||||
Reference in New Issue
Block a user