From 02afdd6d09fbd3950409ccc767883409ab2d7b0e Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 2 Oct 2026 01:16:55 +0000 Subject: [PATCH] Test that a reopened library precaches a live photo with no JSON file Reopening the library over a live photo's image and video in the cache with no JSON file beside them records no original for the photo; the precache then fetches both and writes the JSON file. Model: opus-5-5 --- test/library/content-library.test.ts | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/library/content-library.test.ts b/test/library/content-library.test.ts index a57722b..d5976a4 100644 --- a/test/library/content-library.test.ts +++ b/test/library/content-library.test.ts @@ -176,6 +176,66 @@ describe("Library content wiring", () => { await lib.close(); }); + it("does not take a live photo's image or video with no JSON file as its original when it opens, and precaches both", async () => { + const { file: live, body } = await asLivePhoto(file(1, 1)); + class LiveClient extends MockClient { + override async filesSince(): Promise { + return { files: [live], deleted: [], cursor: 1 }; + } + } + const originals = join(root, "cache", "originals"); + const open = (opts: Partial): Promise => + Library.open({ + client: new LiveClient(), + cacheDirectory: join(root, "cache"), + contentSource: cdnSource(new Map([[1, body]])), + refreshIntervalSeconds: 3600, + precacheThumbnails: false, + precacheOriginals: false, + ...opts, + }); + // A first run records the library, so the next one knows that file 1 + // is a live photo when it opens the cache. + await (await open({})).close(); + writeFileSync(join(originals, "1.heic"), "an image"); + writeFileSync(join(originals, "1.mov"), "a video"); + + let precached!: () => void; + const done = new Promise((r) => (precached = r)); + const lib = await open({ + precacheOriginals: true, + onProgress: (e) => { + if (e.operation === "precacheOriginals" && e.status === "done") + precached(); + }, + }); + expect( + lib.photos.byID({ fileID: 1 })!.record().originalPath, + ).toBeUndefined(); + await done; + + expect(readdirSync(originals).sort()).toEqual([ + "1.heic", + "1.livephoto.json", + "1.mov", + ]); + expect(readFileSync(join(originals, "1.heic"))).toEqual( + Buffer.from(IMAGE), + ); + expect(readFileSync(join(originals, "1.mov"))).toEqual( + Buffer.from(VIDEO), + ); + expect( + JSON.parse( + readFileSync(join(originals, "1.livephoto.json"), "utf-8"), + ), + ).toEqual({ image: "1.heic", video: "1.mov" }); + expect(lib.photos.byID({ fileID: 1 })!.record().originalPath).toBe( + join(originals, "1.heic"), + ); + await lib.close(); + }); + it("throws from content methods when opened without a content source", async () => { const lib = await Library.open({ client: new MockClient(),