|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import "fake-indexeddb/auto"; |
| 3 | + |
| 4 | +import type { FileId } from "@app/types/file"; |
| 5 | + |
| 6 | +/** |
| 7 | + * A TTL bump is `put(record)`, and `record.data` is the file itself - there is |
| 8 | + * no partial update in IndexedDB. So "note that this thumbnail was used" used |
| 9 | + * to rewrite every byte of every file in the library, on every listing, and the |
| 10 | + * sidebar lists on mount and on every workbench change. |
| 11 | + * |
| 12 | + * These tests pin the debounce by counting writes, because nothing else fails |
| 13 | + * if it is removed: the behaviour is identical, just far more expensive. Each |
| 14 | + * asserts on a NON-EMPTY set of writes - "nothing was written" would pass |
| 15 | + * before the fire-and-forget bump had a chance to run. |
| 16 | + */ |
| 17 | + |
| 18 | +const nativePut = IDBObjectStore.prototype.put; |
| 19 | + |
| 20 | +/** Ids written back during a listing, in order. */ |
| 21 | +let writes: FileId[] = []; |
| 22 | + |
| 23 | +function countWrites() { |
| 24 | + IDBObjectStore.prototype.put = function ( |
| 25 | + this: IDBObjectStore, |
| 26 | + value: unknown, |
| 27 | + key?: IDBValidKey, |
| 28 | + ) { |
| 29 | + writes.push((value as { id: FileId }).id); |
| 30 | + return key === undefined |
| 31 | + ? nativePut.call(this, value) |
| 32 | + : nativePut.call(this, value, key); |
| 33 | + } as typeof IDBObjectStore.prototype.put; |
| 34 | +} |
| 35 | + |
| 36 | +const HOUR = 60 * 60 * 1000; |
| 37 | +const DAY = 24 * HOUR; |
| 38 | + |
| 39 | +/** |
| 40 | + * A fresh service over an empty store. fake-indexeddb keeps its data for the |
| 41 | + * whole file, so without the clear each test would see the previous test's |
| 42 | + * records and its write count would depend on execution order. |
| 43 | + */ |
| 44 | +async function freshFileStorage() { |
| 45 | + vi.resetModules(); |
| 46 | + const [{ fileStorage }, { createStirlingFile, createNewStirlingFileStub }] = |
| 47 | + await Promise.all([ |
| 48 | + import("@app/services/fileStorage"), |
| 49 | + import("@app/types/fileContext"), |
| 50 | + ]); |
| 51 | + await fileStorage.clearAll(); |
| 52 | + |
| 53 | + /** Store one file carrying a thumbnail recorded `ageMs` ago. */ |
| 54 | + const store = async (name: string, ageMs: number) => { |
| 55 | + const file = new File(["%PDF-1.7 stirling"], name, { |
| 56 | + type: "application/pdf", |
| 57 | + }); |
| 58 | + const stub = createNewStirlingFileStub(file); |
| 59 | + await fileStorage.storeStirlingFile( |
| 60 | + createStirlingFile(file, stub.id), |
| 61 | + stub, |
| 62 | + ); |
| 63 | + // storeStirlingFile stamps `Date.now()`; age it directly so the test does |
| 64 | + // not depend on how the thumbnail got there. |
| 65 | + await fileStorage.updateFileMetadata(stub.id, { |
| 66 | + thumbnail: "data:image/webp;base64,AAAA", |
| 67 | + thumbnailStoredAt: Date.now() - ageMs, |
| 68 | + }); |
| 69 | + return stub.id; |
| 70 | + }; |
| 71 | + |
| 72 | + return { fileStorage, store }; |
| 73 | +} |
| 74 | + |
| 75 | +beforeEach(() => { |
| 76 | + writes = []; |
| 77 | +}); |
| 78 | + |
| 79 | +afterEach(() => { |
| 80 | + IDBObjectStore.prototype.put = nativePut; |
| 81 | +}); |
| 82 | + |
| 83 | +describe("thumbnail TTL bump — the whole record is rewritten, so debounce it", () => { |
| 84 | + test("rewrites the record recorded a day ago and leaves the recent one alone", async () => { |
| 85 | + const { fileStorage, store } = await freshFileStorage(); |
| 86 | + const recent = await store("recent.pdf", 1 * HOUR); |
| 87 | + const stale = await store("stale.pdf", 2 * DAY); |
| 88 | + |
| 89 | + countWrites(); |
| 90 | + await fileStorage.getLeafStirlingFileStubs(); |
| 91 | + |
| 92 | + // Exactly one write, and it is the stale one. Before the debounce both were |
| 93 | + // rewritten, which on real files is the whole library. |
| 94 | + await vi.waitFor(() => expect(writes).toEqual([stale])); |
| 95 | + expect(writes).not.toContain(recent); |
| 96 | + }); |
| 97 | + |
| 98 | + test("repeated listings rewrite at most once, not once per listing", async () => { |
| 99 | + const { fileStorage, store } = await freshFileStorage(); |
| 100 | + const stale = await store("stale.pdf", 2 * DAY); |
| 101 | + |
| 102 | + countWrites(); |
| 103 | + await fileStorage.getLeafStirlingFileStubs(); |
| 104 | + await vi.waitFor(() => expect(writes).toEqual([stale])); |
| 105 | + |
| 106 | + // The first bump reset the stamp to now, so the next three are free. This |
| 107 | + // is the case that used to cost a full-library rewrite every time. |
| 108 | + for (let i = 0; i < 3; i++) await fileStorage.getLeafStirlingFileStubs(); |
| 109 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 110 | + expect(writes).toEqual([stale]); |
| 111 | + }); |
| 112 | + |
| 113 | + test("an expired thumbnail is still cleared on the first listing that sees it", async () => { |
| 114 | + const { fileStorage, store } = await freshFileStorage(); |
| 115 | + // Past the 30-day TTL: expiry must not be debounced away. |
| 116 | + const expired = await store("expired.pdf", 31 * DAY); |
| 117 | + |
| 118 | + countWrites(); |
| 119 | + const stubs = await fileStorage.getLeafStirlingFileStubs(); |
| 120 | + await vi.waitFor(() => expect(writes).toEqual([expired])); |
| 121 | + |
| 122 | + expect(stubs.find((s) => s.id === expired)?.thumbnailUrl).toBeUndefined(); |
| 123 | + }); |
| 124 | + |
| 125 | + test("the same debounce applies to the all-files listing", async () => { |
| 126 | + const { fileStorage, store } = await freshFileStorage(); |
| 127 | + await store("recent.pdf", 1 * HOUR); |
| 128 | + const stale = await store("stale.pdf", 2 * DAY); |
| 129 | + |
| 130 | + countWrites(); |
| 131 | + await fileStorage.getAllStirlingFileStubs(); |
| 132 | + await vi.waitFor(() => expect(writes).toEqual([stale])); |
| 133 | + }); |
| 134 | +}); |
0 commit comments