|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { createProjectSignatureCache } from "./vite.adapter"; |
| 4 | + |
| 5 | +const PROJECT = resolve("/projects/demo"); |
| 6 | + |
| 7 | +/** A compute that changes every call, so a stale read is visible as a repeat. */ |
| 8 | +function countingCompute() { |
| 9 | + let calls = 0; |
| 10 | + return { |
| 11 | + compute: () => `sig-${++calls}`, |
| 12 | + get calls() { |
| 13 | + return calls; |
| 14 | + }, |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +describe("createProjectSignatureCache", () => { |
| 19 | + it("memoises a project's signature", () => { |
| 20 | + const source = countingCompute(); |
| 21 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 22 | + |
| 23 | + expect(cache.get(PROJECT)).toBe("sig-1"); |
| 24 | + expect(cache.get(PROJECT)).toBe("sig-1"); |
| 25 | + expect(source.calls).toBe(1); |
| 26 | + }); |
| 27 | + |
| 28 | + it("recomputes after a file inside the project changes", () => { |
| 29 | + const source = countingCompute(); |
| 30 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 31 | + |
| 32 | + expect(cache.get(PROJECT)).toBe("sig-1"); |
| 33 | + cache.invalidate(resolve(PROJECT, "index.html")); |
| 34 | + |
| 35 | + // The preview ETag is this string. Serving "sig-1" again answers the |
| 36 | + // browser's revalidation with a 304 and the pre-edit composition is what |
| 37 | + // renders — which is how a thumbnail taken after an edit showed the old frame. |
| 38 | + expect(cache.get(PROJECT)).toBe("sig-2"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("recomputes for an asset added or removed, not only one edited", () => { |
| 42 | + const source = countingCompute(); |
| 43 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 44 | + |
| 45 | + cache.get(PROJECT); |
| 46 | + cache.invalidate(resolve(PROJECT, "assets/new-clip.mp4")); |
| 47 | + expect(cache.get(PROJECT)).toBe("sig-2"); |
| 48 | + |
| 49 | + cache.invalidate(resolve(PROJECT, "compositions/scene.html")); |
| 50 | + expect(cache.get(PROJECT)).toBe("sig-3"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("leaves other projects alone", () => { |
| 54 | + const signatures = new Map([ |
| 55 | + [PROJECT, "demo"], |
| 56 | + [resolve("/projects/other"), "other"], |
| 57 | + ]); |
| 58 | + let bumped = 0; |
| 59 | + const cache = createProjectSignatureCache({ |
| 60 | + compute: (dir) => `${signatures.get(dir)}-${bumped}`, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(cache.get(PROJECT)).toBe("demo-0"); |
| 64 | + expect(cache.get(resolve("/projects/other"))).toBe("other-0"); |
| 65 | + |
| 66 | + bumped = 1; |
| 67 | + cache.invalidate(resolve(PROJECT, "index.html")); |
| 68 | + |
| 69 | + expect(cache.get(PROJECT)).toBe("demo-1"); |
| 70 | + expect(cache.get(resolve("/projects/other"))).toBe("other-0"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("asks for a project directory to be watched once, on first use", () => { |
| 74 | + const watched: string[] = []; |
| 75 | + const cache = createProjectSignatureCache({ |
| 76 | + compute: () => "sig", |
| 77 | + watch: (dir) => watched.push(dir), |
| 78 | + }); |
| 79 | + |
| 80 | + cache.get(PROJECT); |
| 81 | + cache.get(PROJECT); |
| 82 | + cache.invalidate(resolve(PROJECT, "index.html")); |
| 83 | + cache.get(PROJECT); |
| 84 | + |
| 85 | + // Re-registering on every recompute would stack duplicate chokidar entries. |
| 86 | + expect(watched).toEqual([PROJECT]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("normalises the directory it is asked about", () => { |
| 90 | + const source = countingCompute(); |
| 91 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 92 | + |
| 93 | + expect(cache.get(PROJECT)).toBe("sig-1"); |
| 94 | + expect(cache.get(`${PROJECT}/`)).toBe("sig-1"); |
| 95 | + expect(cache.get(resolve(PROJECT, "nested/.."))).toBe("sig-1"); |
| 96 | + expect(source.calls).toBe(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not invalidate on a write the signature cannot see", () => { |
| 100 | + const source = countingCompute(); |
| 101 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 102 | + |
| 103 | + cache.get(PROJECT); |
| 104 | + // Each thumbnail capture writes here and then reads the preview, so an |
| 105 | + // unfiltered watcher discards the memo on roughly every request of the one |
| 106 | + // workload the memo exists for. |
| 107 | + cache.invalidate(resolve(PROJECT, ".thumbnails/frame-0.jpg")); |
| 108 | + |
| 109 | + expect(cache.get(PROJECT)).toBe("sig-1"); |
| 110 | + expect(source.calls).toBe(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it("invalidates on a motion-state save, which lives under an otherwise-skipped dir", () => { |
| 114 | + const source = countingCompute(); |
| 115 | + const cache = createProjectSignatureCache({ compute: source.compute }); |
| 116 | + |
| 117 | + cache.get(PROJECT); |
| 118 | + cache.invalidate(resolve(PROJECT, ".hyperframes/studio-motion.json")); |
| 119 | + |
| 120 | + expect(cache.get(PROJECT)).toBe("sig-2"); |
| 121 | + }); |
| 122 | +}); |
0 commit comments