Files
Jyotisha/frontend/tests/home-warm-prefetch.test.ts
T

78 lines
3.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { prefetchHomeWarmSnapshot, resetHomeWarmPrefetchForTests, type HomeWarmPrefetchIo } from "../src/lib/home-warm-prefetch.ts";
import { clearHomeWarmSnapshot, isCompleteHomeWarmSnapshot, readHomeWarmSnapshot, writeHomeWarmSnapshot } from "../src/lib/home-warm-snapshot.ts";
// BUG-1104. Synthetic ids only.
const USER = "11111111-1111-4111-8111-111111111111";
const OTHER = "22222222-2222-4222-8222-222222222222";
const catalog = { defaultModelId: "m", models: [{ id: "m", label: "M", description: "x", creditCost: 1, isDefault: true }] };
function io(overrides: Partial<HomeWarmPrefetchIo> = {}) {
const calls: string[] = [];
const value: HomeWarmPrefetchIo = {
fetchModelCatalog: async () => { calls.push("models"); return catalog; },
fetchRectificationEntrySummary: async () => { calls.push("summary"); return null; },
...overrides,
};
return { value, calls };
}
test.beforeEach(() => {
clearHomeWarmSnapshot();
resetHomeWarmPrefetchForTests();
});
test("writes a complete snapshot through the shared writer; cursor stays with the list boot", async () => {
const { value } = io();
assert.equal(await prefetchHomeWarmSnapshot(USER, () => true, value), true);
const snapshot = readHomeWarmSnapshot(USER);
assert.ok(isCompleteHomeWarmSnapshot(snapshot));
assert.equal(snapshot.sessionsCursor, null);
assert.equal(snapshot.listBoot, null, "the provider's own boot cursor wins on the warm start");
assert.equal(snapshot.entrySummarySettled, true);
});
test("an existing complete snapshot is left alone and nothing is fetched", async () => {
writeHomeWarmSnapshot({ accountId: USER, modelCatalog: catalog, entrySummary: null, entrySummarySettled: true, sessionsCursor: "c", listBoot: null });
const { value, calls } = io();
assert.equal(await prefetchHomeWarmSnapshot(USER, () => true, value), false);
assert.deepEqual(calls, []);
assert.equal(readHomeWarmSnapshot(USER)?.sessionsCursor, "c");
});
test("a late answer after cancel (Home mounted, account switched, sign-out) writes nothing", async () => {
let current = true;
let release: () => void = () => undefined;
const gate = new Promise<void>((resolve) => { release = resolve; });
const { value } = io({ fetchModelCatalog: async () => { await gate; return catalog; } });
const pending = prefetchHomeWarmSnapshot(USER, () => current, value);
current = false;
release();
assert.equal(await pending, false);
assert.equal(readHomeWarmSnapshot(USER), null);
});
test("a failed catalog leaves nothing; a failed summary settles like the cold start", async () => {
assert.equal(await prefetchHomeWarmSnapshot(USER, () => true, io({ fetchModelCatalog: async () => { throw new Error("down"); } }).value), false);
assert.equal(readHomeWarmSnapshot(USER), null);
assert.equal(await prefetchHomeWarmSnapshot(USER, () => true, io({ fetchRectificationEntrySummary: async () => { throw new Error("down"); } }).value), true);
assert.equal(readHomeWarmSnapshot(USER)?.entrySummary, null);
});
test("an empty catalog is not a usable snapshot", async () => {
const { value } = io({ fetchModelCatalog: async () => ({ defaultModelId: "", models: [] }) });
assert.equal(await prefetchHomeWarmSnapshot(USER, () => true, value), false);
assert.equal(readHomeWarmSnapshot(USER), null);
});
test("one flight per account; another account's snapshot is never painted", async () => {
const { value, calls } = io();
const [a, b] = await Promise.all([prefetchHomeWarmSnapshot(USER, () => true, value), prefetchHomeWarmSnapshot(USER, () => true, value)]);
assert.equal(a, true);
assert.equal(b, true, "the second caller shares the first flight");
assert.deepEqual(calls.filter((call) => call === "models"), ["models"]);
assert.equal(readHomeWarmSnapshot(OTHER), null);
});