|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + readStoredFrame, |
| 6 | + readStoredInfo, |
| 7 | + rememberVersion, |
| 8 | + resetStoredThumbnailsForTests, |
| 9 | + STORED_THUMBNAIL_BYTES, |
| 10 | + storedVersion, |
| 11 | + storeFrame, |
| 12 | + storeInfo, |
| 13 | +} from "./thumbnailDiskCache"; |
| 14 | + |
| 15 | +/** Cache Storage as a Map of Maps: what a browser keeps across sessions. */ |
| 16 | +function fakeCacheStorage() { |
| 17 | + const stores = new Map<string, Map<string, Response>>(); |
| 18 | + const storage = { |
| 19 | + open: async (name: string) => { |
| 20 | + const store = stores.get(name) ?? new Map<string, Response>(); |
| 21 | + stores.set(name, store); |
| 22 | + return { |
| 23 | + match: async (url: string) => store.get(String(url))?.clone(), |
| 24 | + put: async (url: string, response: Response) => void store.set(String(url), response), |
| 25 | + }; |
| 26 | + }, |
| 27 | + has: async (name: string) => stores.has(name), |
| 28 | + keys: async () => [...stores.keys()], |
| 29 | + delete: async (name: string) => stores.delete(name), |
| 30 | + }; |
| 31 | + return { storage, stores }; |
| 32 | +} |
| 33 | + |
| 34 | +const v1 = { source: "/media/take.mp4", etag: '"one"' }; |
| 35 | +const v2 = { source: "/media/take.mp4", etag: '"two"' }; |
| 36 | +// a picture that reports `bytes` without holding them, so the budget test needs no real 128 MB |
| 37 | +function jpeg(bytes = 5): Blob { |
| 38 | + const blob = new Blob([new Uint8Array(Math.min(bytes, 5))], { type: "image/jpeg" }); |
| 39 | + return Object.defineProperty(blob, "size", { value: bytes }); |
| 40 | +} |
| 41 | + |
| 42 | +let stores: Map<string, Map<string, Response>>; |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + resetStoredThumbnailsForTests(); |
| 46 | + localStorage.clear(); |
| 47 | + const fake = fakeCacheStorage(); |
| 48 | + stores = fake.stores; |
| 49 | + vi.stubGlobal("caches", fake.storage); |
| 50 | +}); |
| 51 | + |
| 52 | +afterEach(() => { |
| 53 | + vi.unstubAllGlobals(); |
| 54 | + vi.restoreAllMocks(); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("stored thumbnails", () => { |
| 58 | + it("keep a source's facts and each frame with the time it was decoded at", async () => { |
| 59 | + await storeInfo(v1, { aspect: 16 / 9, metadataDuration: 30 }); |
| 60 | + await storeFrame(v1, "cover 320x180 2.5", 2.4, jpeg()); |
| 61 | + |
| 62 | + expect(await readStoredInfo(v1)).toEqual({ aspect: 16 / 9, metadataDuration: 30 }); |
| 63 | + const frame = await readStoredFrame(v1, "cover 320x180 2.5"); |
| 64 | + expect(frame?.decodedAt).toBe(2.4); |
| 65 | + expect(frame?.blob.size).toBe(5); |
| 66 | + expect(await readStoredFrame(v1, "cover 320x180 5")).toBeUndefined(); |
| 67 | + expect(await readStoredFrame(v2, "cover 320x180 2.5")).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("are trusted in a new session only when the server answers the stored ETag with a 304", async () => { |
| 71 | + const fetch = vi.fn(async () => new Response(null, { status: 304 })); |
| 72 | + vi.stubGlobal("fetch", fetch); |
| 73 | + expect(await storedVersion(v1.source)).toBeUndefined(); |
| 74 | + expect(fetch).not.toHaveBeenCalled(); |
| 75 | + |
| 76 | + await rememberVersion(v1); |
| 77 | + expect(await storedVersion(v1.source)).toEqual(v1); |
| 78 | + expect(fetch).toHaveBeenCalledWith(v1.source, { |
| 79 | + headers: { Range: "bytes=0-0", "If-None-Match": '"one"' }, |
| 80 | + }); |
| 81 | + fetch.mockResolvedValueOnce(new Response("x", { status: 206 })); |
| 82 | + expect(await storedVersion(v1.source)).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("drop what was stored for a file's earlier version once a new one is read", async () => { |
| 86 | + await rememberVersion(v1); |
| 87 | + await storeFrame(v1, "cover 320x180 1", 1, jpeg()); |
| 88 | + await rememberVersion(v2); |
| 89 | + |
| 90 | + expect(await readStoredFrame(v1, "cover 320x180 1")).toBeUndefined(); |
| 91 | + expect(stores.size).toBe(0); |
| 92 | + }); |
| 93 | + |
| 94 | + it("drop the least recently used sources once they pass the budget", async () => { |
| 95 | + const big = Math.floor(STORED_THUMBNAIL_BYTES / 2) + 1; |
| 96 | + const others = ["/a.mp4", "/b.mp4", "/c.mp4"].map((source) => ({ source, etag: '"1"' })); |
| 97 | + for (const version of others) { |
| 98 | + await storeFrame(version, "cover 1x1 0", 0, jpeg(big)); |
| 99 | + vi.spyOn(Date, "now").mockReturnValue(Date.now() + 1000); |
| 100 | + } |
| 101 | + |
| 102 | + expect(await readStoredFrame(others[0]!, "cover 1x1 0")).toBeUndefined(); |
| 103 | + expect(await readStoredFrame(others[2]!, "cover 1x1 0")).toBeDefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("never fail a thumbnail: a broken store warns once and reads as empty", async () => { |
| 107 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 108 | + vi.stubGlobal("caches", { |
| 109 | + open: async () => { |
| 110 | + throw new DOMException("quota", "QuotaExceededError"); |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(await readStoredInfo(v1)).toBeUndefined(); |
| 115 | + expect(await storeFrame(v1, "cover 1x1 0", 0, jpeg())).toBeUndefined(); |
| 116 | + expect(warn).toHaveBeenCalledTimes(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it("are skipped where the browser offers no Cache Storage", async () => { |
| 120 | + vi.stubGlobal("caches", undefined); |
| 121 | + expect(await readStoredInfo(v1)).toBeUndefined(); |
| 122 | + expect(await storeInfo(v1, { aspect: 1, metadataDuration: 1 })).toBeUndefined(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("count a frame stored again under its name once, so the budget holds", async () => { |
| 126 | + await storeFrame(v1, "cover 1x1 0", 0, jpeg(100)); |
| 127 | + await storeFrame(v1, "cover 1x1 0", 0, jpeg(100)); |
| 128 | + |
| 129 | + await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), { |
| 130 | + timeout: 3000, |
| 131 | + }); |
| 132 | + const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!); |
| 133 | + expect(Object.values(saved.caches)).toMatchObject([{ bytes: 100 }]); |
| 134 | + }); |
| 135 | + |
| 136 | + it("keep what another tab stored when saving, but not what this tab dropped", async () => { |
| 137 | + await rememberVersion(v1); |
| 138 | + await storeFrame(v1, "cover 1x1 0", 0, jpeg()); |
| 139 | + await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), { |
| 140 | + timeout: 3000, |
| 141 | + }); |
| 142 | + // another tab saves its snapshot: a source of its own, and v1 as it was before this tab moved on |
| 143 | + const other = JSON.parse(localStorage.getItem("hf-thumbnails-index")!); |
| 144 | + other.caches["hf-thumbnails:other:tab"] = { usedAt: Date.now(), bytes: 7 }; |
| 145 | + localStorage.setItem("hf-thumbnails-index", JSON.stringify(other)); |
| 146 | + const v1Cache = Object.keys(other.caches).find((name) => name !== "hf-thumbnails:other:tab")!; |
| 147 | + await rememberVersion(v2); |
| 148 | + await storeFrame(v2, "cover 1x1 0", 0, jpeg()); |
| 149 | + |
| 150 | + await vi.waitFor( |
| 151 | + () => expect(localStorage.getItem("hf-thumbnails-index")).not.toContain(v1Cache), |
| 152 | + { timeout: 3000 }, |
| 153 | + ); |
| 154 | + const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!); |
| 155 | + expect(Object.keys(saved.caches)).toContain("hf-thumbnails:other:tab"); |
| 156 | + expect(Object.keys(saved.caches)).toHaveLength(2); |
| 157 | + }); |
| 158 | +}); |
0 commit comments