droboticflow/tests/frontend/acceptance-metadata-cache.test.mjs

85 lines
3.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
createAcceptanceMetadataCache,
acceptanceMetadataKey,
acceptanceMediaKey,
} from "../../src/lib/acceptance-metadata-cache.js";
const signal = () => new AbortController();
test("shared navigation/prefetch survives one cancellation; stale last reader cannot populate cache", async () => {
const cache = createAcceptanceMetadataCache();
let finish,
calls = 0,
underlying;
const load = (s) => {
calls++;
underlying = s;
return new Promise((r) => (finish = r));
};
const a = signal(),
b = signal();
const first = cache.read("one", load, a.signal),
second = cache.read("one", load, b.signal);
await Promise.resolve();
a.abort();
await assert.rejects(first, { name: "AbortError" });
assert.equal(underlying.aborted, false);
finish({ value: 1 });
assert.deepEqual(await second, { value: 1 });
assert.equal(calls, 1);
const c = signal();
const stale = cache.read("two", load, c.signal);
await Promise.resolve();
c.abort();
await assert.rejects(stale, { name: "AbortError" });
assert.equal(underlying.aborted, true);
finish({ value: "old" });
await new Promise((r) => setImmediate(r));
assert.equal(cache.get("two"), null);
assert.deepEqual(await cache.read("two", async () => ({ value: "new" }), signal().signal), { value: "new" });
cache.dispose();
assert.equal(cache.get("one"), null);
});
test("metadata is bounded by both LRU count and bytes, and failures can retry", async () => {
const c = createAcceptanceMetadataCache(2, 30),
s = signal().signal;
await c.read("a", async () => ({ x: 1 }), s);
await c.read("b", async () => ({ x: 2 }), s);
c.get("a");
await c.read("c", async () => ({ x: 3 }), s);
assert.equal(c.get("b"), null);
await c.read("large", async () => ({ x: "x".repeat(50) }), s);
assert.equal(c.get("large"), null);
await assert.rejects(
c.read(
"error",
async () => {
throw Error("offline");
},
s,
),
);
assert.deepEqual(await c.read("error", async () => ({ x: 4 }), s), { x: 4 });
});
test("media reuse is limited to identical frozen source; metadata keys include task/source/revision/slice", () => {
const g = {
entryIndex: 0,
index: 1,
sourceIndex: true,
entry: { materialization: { artifactId: "a", revision: 1, manifestSha256: "x" } },
};
const next = { ...g, index: 2 };
assert.equal(acceptanceMediaKey("t", g), acceptanceMediaKey("t", next));
assert.notEqual(acceptanceMetadataKey("t", g), acceptanceMetadataKey("t", next));
for (const other of [
{ ...g, entryIndex: 1 },
{ ...g, entry: { materialization: { ...g.entry.materialization, revision: 2 } } },
])
assert.notEqual(acceptanceMediaKey("t", g), acceptanceMediaKey("t", other));
assert.notEqual(acceptanceMediaKey("t", g), acceptanceMediaKey("t2", g));
assert.notEqual(
acceptanceMediaKey("t", { ...g, sourceIndex: false }),
acceptanceMediaKey("t", { ...next, sourceIndex: false }),
);
});