Files
t3code/apps/server/src/usage/usageScanCache.test.ts
T

283 lines
9.4 KiB
TypeScript

import { describe, expect, it } from "@effect/vitest";
import {
decodeScanCache,
dedupeWithinFile,
encodeScanCache,
makeScanCacheWriter,
pruneScanCache,
type CachedFile,
type ScanCache,
} from "./usageScanCache.ts";
import type { UsageRecord } from "@t3tools/provider-core/server/usage";
import { TEST_FORMAT_MAP } from "./usageTestFormats.ts";
function record(overrides: Partial<UsageRecord> = {}): UsageRecord {
return {
provider: "claude",
timestampMs: 1_786_000_000_000,
model: "claude-fable-5",
sessionId: "session-a",
totals: {
uncachedInputTokens: 2,
cachedInputTokens: 1000,
cacheCreationTokens: 10,
outputTokens: 50,
reasoningTokens: 0,
},
reportedCostUsd: null,
speed: "standard",
dedupeKey: "msg_1:",
...overrides,
};
}
function position(overrides: Partial<CachedFile["position"]> = {}): CachedFile["position"] {
return {
resumeOffset: 120,
guardLength: 64,
guardHash: 0xdeadbeef,
state: null,
...overrides,
};
}
function cacheWith(entries: readonly [string, number, readonly UsageRecord[]][]): ScanCache {
const cache: ScanCache = new Map();
for (const [path, mtimeMs, records] of entries) {
cache.set(path, {
size: records.length * 10,
mtimeMs,
provider: "claude",
records,
tailRecords: [],
position: position(),
});
}
return cache;
}
describe("scan cache round trip", () => {
it("restores records unchanged", () => {
const original = cacheWith([
[
"/a.jsonl",
100,
[record(), record({ dedupeKey: "msg_2:", model: "claude-opus-5-5", speed: "fast" })],
],
["/b.jsonl", 200, [record({ sessionId: "session-b", reportedCostUsd: 1.5 })]],
]);
original.set("/grok.jsonl", {
size: 40,
mtimeMs: 300,
provider: "grok",
records: [
record({ provider: "grok", model: "grok-4.5-build", dedupeKey: "s:p:grok-4.5-build" }),
],
tailRecords: [record({ provider: "grok", model: "grok-4.5-build", dedupeKey: null })],
position: position({ resumeOffset: 30, guardLength: 30, guardHash: 123 }),
});
original.set("/codex.jsonl", {
size: 80,
mtimeMs: 400,
provider: "codex",
records: [
record({ provider: "codex", model: "gpt-6-astra", dedupeKey: null, speed: "ultrafast" }),
],
tailRecords: [],
position: position({
state: {
model: "gpt-6-astra",
speed: "ultrafast",
sessionId: "session-c",
lastUsageSignature: '{"input_tokens":1}',
sawSessionMeta: true,
suppressingForkCopies: false,
forkCopyAnchorMs: 0,
},
}),
});
const restored = decodeScanCache(
JSON.parse(JSON.stringify(encodeScanCache(original))),
TEST_FORMAT_MAP,
);
expect(restored.size).toBe(4);
expect(restored.get("/a.jsonl")).toEqual(original.get("/a.jsonl"));
expect(restored.get("/b.jsonl")).toEqual(original.get("/b.jsonl"));
expect(restored.get("/grok.jsonl")).toEqual(original.get("/grok.jsonl"));
expect(restored.get("/codex.jsonl")).toEqual(original.get("/codex.jsonl"));
});
it("drops an entry whose persisted parse state is corrupt", () => {
// Resuming with a bad reducer state would attach appended usage to the
// wrong model or replay fork-copied history; that entry must cold parse.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = {
...encoded,
files: {
"/a.jsonl": { ...encoded.files["/a.jsonl"]!, cs: { model: 42 } },
},
};
expect(
decodeScanCache(JSON.parse(JSON.stringify(poisoned)), TEST_FORMAT_MAP).has("/a.jsonl"),
).toBe(false);
});
it("drops an entry whose guard length is outside the supported range", () => {
// The guard length sizes a Buffer in the reader; a bogus value would make
// every parse of that file fail and silently drop its usage.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = {
...encoded,
files: { "/a.jsonl": { ...encoded.files["/a.jsonl"]!, gl: 1e20 } },
};
expect(
decodeScanCache(JSON.parse(JSON.stringify(poisoned)), TEST_FORMAT_MAP).has("/a.jsonl"),
).toBe(false);
});
it("drops an entry whose speed is not a known index", () => {
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record({ speed: "fast" })]]]));
const row = encoded.files["/a.jsonl"]!.r[0]!;
const poisoned = {
...encoded,
files: { "/a.jsonl": { ...encoded.files["/a.jsonl"]!, r: [[...row.slice(0, 10), true]] } },
};
expect(
decodeScanCache(JSON.parse(JSON.stringify(poisoned)), TEST_FORMAT_MAP).has("/a.jsonl"),
).toBe(false);
});
it("rejects a document from before records carried a speed", () => {
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const previous = { ...encoded, version: 3 };
expect(decodeScanCache(JSON.parse(JSON.stringify(previous)), TEST_FORMAT_MAP).size).toBe(0);
});
it("rewrites only changed entries and still restores the whole cache", () => {
const write = makeScanCacheWriter();
const cache = cacheWith([
["/a.jsonl", 100, [record()]],
["/b.jsonl", 200, [record({ sessionId: "session-b" })]],
]);
const sources = { "claude\u0000/projects": { dir: "/projects", volumeId: "1:2" } };
expect(decodeScanCache(JSON.parse(write(cache, { sources })), TEST_FORMAT_MAP)).toEqual(cache);
// The replacement adds intern entries; /a's memoised indexes must hold.
cache.set("/b.jsonl", {
...cache.get("/b.jsonl")!,
size: 30,
records: [record({ sessionId: "session-c", model: "claude-opus-5-5", dedupeKey: "msg_3:" })],
});
const document = JSON.parse(write(cache, { sources }));
expect(decodeScanCache(document, TEST_FORMAT_MAP)).toEqual(cache);
expect(document.sources).toEqual(sources);
});
it("interns repeated model and session strings", () => {
const encoded = encodeScanCache(
cacheWith([["/a.jsonl", 100, [record(), record({ dedupeKey: "msg_2:" }), record()]]]),
);
expect(encoded.models).toEqual(["claude-fable-5"]);
expect(encoded.sessions).toEqual(["session-a"]);
});
it("treats a corrupt or foreign document as an empty cache", () => {
// A bad cache should cost one cold scan, never a broken page.
expect(decodeScanCache(null, TEST_FORMAT_MAP).size).toBe(0);
expect(decodeScanCache("nonsense", TEST_FORMAT_MAP).size).toBe(0);
expect(
decodeScanCache({ version: 999, models: [], sessions: [], files: {} }, TEST_FORMAT_MAP).size,
).toBe(0);
});
it("skips malformed file entries but keeps good ones", () => {
const encoded = encodeScanCache(cacheWith([["/good.jsonl", 100, [record()]]]));
const withJunk = {
...encoded,
files: { ...encoded.files, "/bad.jsonl": { s: "nope", m: 1, p: "claude", r: [] } },
};
const restored = decodeScanCache(JSON.parse(JSON.stringify(withJunk)), TEST_FORMAT_MAP);
expect([...restored.keys()]).toEqual(["/good.jsonl"]);
});
it("rejects the whole cache when an intern table holds a non-string", () => {
// models: [1] would pass the undefined guard, put a number in a record's
// model, and crash lookupRate at aggregate time.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = { ...encoded, models: [1] };
expect(decodeScanCache(JSON.parse(JSON.stringify(poisoned)), TEST_FORMAT_MAP).size).toBe(0);
});
it("drops the whole entry when any row is corrupt, forcing a cold re-parse", () => {
// Keeping the surviving rows under the original (size, mtime) would read
// as a valid warm hit and the file would never be re-parsed.
const encoded = encodeScanCache(
cacheWith([["/a.jsonl", 100, [record(), record({ dedupeKey: "msg_2:" })]]]),
);
const rows = encoded.files["/a.jsonl"]!.r;
const poisoned = {
...encoded,
files: {
"/a.jsonl": {
...encoded.files["/a.jsonl"]!,
r: [rows[0]!, [...rows[1]!.slice(0, 3), "not-a-number", ...rows[1]!.slice(4)]],
},
},
};
const restored = decodeScanCache(JSON.parse(JSON.stringify(poisoned)), TEST_FORMAT_MAP);
expect(restored.has("/a.jsonl")).toBe(false);
});
});
describe("pruneScanCache", () => {
const retentionCutoffMs = 1000;
it("drops entries older than retention", () => {
const cache = cacheWith([["/old.jsonl", 500, [record()]]]);
const removed = pruneScanCache(cache, retentionCutoffMs);
expect(removed).toBe(1);
expect(cache.size).toBe(0);
});
it("keeps entries whose file has disappeared", () => {
const cache = cacheWith([["/gone.jsonl", 5000, [record()]]]);
pruneScanCache(cache, retentionCutoffMs);
expect(cache.size).toBe(1);
});
});
describe("dedupeWithinFile", () => {
it("keeps the first record per dedupe key", () => {
const kept = dedupeWithinFile([
record({ totals: { ...record().totals, outputTokens: 1 } }),
record({ totals: { ...record().totals, outputTokens: 999 } }),
record({ dedupeKey: "msg_2:" }),
]);
expect(kept).toHaveLength(2);
expect(kept[0]?.totals.outputTokens).toBe(1);
});
it("keeps every record that has no dedupe key", () => {
expect(
dedupeWithinFile([record({ dedupeKey: null }), record({ dedupeKey: null })]),
).toHaveLength(2);
});
});