// The shared walk's memo is timed from when the walk FINISHED: a walk slower than the memo still answers the // caller that asks right after it lands (timed from its start, every stage of a slow page walked again in turn). import "../report/noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { forgetSharedWalks, sharedWalk, walkStats } from "./walk.mjs"; test("a walk slower than the memo answers the next caller; `since` still asks for a fresh one", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "shared-walk-memo-")); try { for (let i = 0; i < 600; i += 1) { const dir = path.join(root, `group-${i % 20}`, `piece-${i}`); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), "{}"); } forgetSharedWalks(); // A memo shorter than the walk takes: find one by measuring. const t0 = Date.now(); await sharedWalk(root, { memoMs: 0 }); const took = Date.now() - t0; const memoMs = Math.max(5, Math.floor(took / 2)); forgetSharedWalks(); const before = walkStats.calls; const a = await sharedWalk(root, { memoMs }); const b = await sharedWalk(root, { memoMs }); assert.equal(walkStats.calls - before, 1, `one walk for two callers in turn (walk ${took} ms, memo ${memoMs} ms)`); assert.equal(a, b); const asked = Date.now() + 1; await new Promise((r) => setTimeout(r, 2)); await sharedWalk(root, { memoMs: 60_000, since: asked }); assert.equal(walkStats.calls - before, 2, "a caller that needs a walk begun after it asked gets a fresh one"); } finally { forgetSharedWalks(); await rm(root, { recursive: true, force: true }); } });