From 11fcad12b93e8ed456ba3ec144c03316ef7cda9c Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 23 Sep 2026 02:39:50 +0000 Subject: [PATCH] Test that close() waits for the originals precache (closes #93) The close() test that holds a precache fetch open now runs twice: once with only the thumbnail fill and once with only the originals fill, so removing either wait from Precache.close() fails a test. Model: opus-5-5 --- TODO.md | 4 ++ test/library/precache.test.ts | 80 +++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/TODO.md b/TODO.md index 5b17221..bfc840b 100644 --- a/TODO.md +++ b/TODO.md @@ -18,6 +18,10 @@ Tag v1.0.0. # Completed Steps +- 2026-09-23: Tested that `Library.close()` waits for the originals precache + (issue 93). The test that holds a precache fetch open while `close()` runs now + runs once with only the thumbnail fill and once with only the originals fill, + so dropping either wait from `Precache.close()` fails a test. - 2026-09-23: Fixed two intermittently failing library tests (issue 90). `Library.close()` now returns a promise that resolves once an in-flight refresh (including its cache write), the ML data fetch and running precache diff --git a/test/library/precache.test.ts b/test/library/precache.test.ts index 0c0e777..977867f 100644 --- a/test/library/precache.test.ts +++ b/test/library/precache.test.ts @@ -446,47 +446,53 @@ describe("Precache through Library.open", () => { await lib.close(); }); - it("close() resolves only after a running precache fetch has written its file", async () => { - // Every thumbnail fetch waits until the test releases it. - let release!: () => void; - const held = new Promise((r) => (release = r)); - let fetchStarted!: (destination: string) => void; - const started = new Promise((r) => (fetchStarted = r)); - const source: ContentSource = { - original: async ({ destination }) => { - await writeFile(destination, Buffer.alloc(10, 1)); - return { bytesWritten: 10 }; - }, - thumbnail: async ({ destination }) => { + it.each(["thumbnail", "original"] as const)( + "close() resolves only after a running %s precache fetch has written its file", + async (kind) => { + // Only the fill under test runs, and each of its fetches waits + // until the test releases it. + let release!: () => void; + const held = new Promise((r) => (release = r)); + let fetchStarted!: (destination: string) => void; + const started = new Promise((r) => (fetchStarted = r)); + const fetch = async ({ destination }: { destination: string }) => { fetchStarted(destination); await held; await writeFile(destination, Buffer.alloc(10, 1)); return { bytesWritten: 10 }; - }, - }; - const lib = await Library.open({ - client: new MockClient(), - cacheDirectory: join(root, "cache"), - contentSource: source, - refreshIntervalSeconds: 3600, - precacheOriginals: false, - }); - try { - const destination = await started; - - let closed = false; - const closing = lib.close().then(() => { - closed = true; + }; + const unused = async () => { + throw new Error("this fill is turned off"); + }; + const source: ContentSource = + kind === "thumbnail" + ? { original: unused, thumbnail: fetch } + : { original: fetch, thumbnail: unused }; + const lib = await Library.open({ + client: new MockClient(), + cacheDirectory: join(root, "cache"), + contentSource: source, + refreshIntervalSeconds: 3600, + precacheThumbnails: kind === "thumbnail", + precacheOriginals: kind === "original", }); - await new Promise((r) => setTimeout(r, 20)); - expect(closed).toBe(false); + try { + const destination = await started; - release(); - await closing; - expect(existsSync(destination)).toBe(true); - } finally { - release(); - await lib.close(); - } - }); + let closed = false; + const closing = lib.close().then(() => { + closed = true; + }); + await new Promise((r) => setTimeout(r, 20)); + expect(closed).toBe(false); + + release(); + await closing; + expect(existsSync(destination)).toBe(true); + } finally { + release(); + await lib.close(); + } + }, + ); }); -- 2.54.0