mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-03 04:38:33 +00:00
* fix(studio): invalidate the preview signature off the watcher that sees project writes The preview ETag is a hash of the project's files, memoised per project directory. That cache was cleared from Vite's own watcher, which `server.watch.ignored` deliberately excludes `data/projects/**` from, so nothing ever cleared it: the ETag stayed frozen for the life of the dev server, the preview answered every revalidation with 304, and the browser went on serving the composition as it was when it first loaded. The visible cost is thumbnails. Their disk cache key already content-hashes the composition, so an edit correctly asks for a fresh capture, but the capture is taken against the stale page, and a clip's filmstrip keeps showing frames of a layout that no longer exists until the dev server is restarted. Studio already runs its own chokidar watcher over exactly these directories, because Vite's would answer a composition edit with a full page reload. That watcher now owns the invalidation, and the cache asks it to follow any project directory it has not seen. All five event types count: an added or deleted asset changes the signature as surely as an edited one. The cache moves behind `createProjectSignatureCache` so the invalidation rule is a unit under test rather than a subscription buried in the adapter. * fix(studio): filter signature invalidation, and stop the CLI server missing motion saves Review follow-up on the unfiltered invalidation. The watcher fired on everything under a project dir, but the signature walk skips 14 directories and `.thumbnails` is one of them. That directory is where the thumbnail route keeps its disk cache, and every capture also reads the preview, so populating a timeline row discarded the memo on roughly every request of the one workload it exists for. The filter is a single exported predicate beside the exclusion set it reads, and it is applied inside `invalidate` rather than at the watcher, so no caller can subscribe and forget it. It is deliberately not `WATCHER_EXCLUDED_DIRS`: that set is character-identical but drops all of `.hyperframes/`, and the signature reads two manifest files back out of there. Which is the same bug, still live, in the CLI server: its watcher filters through `shouldWatchProjectFile`, so `.hyperframes/studio-motion.json` never reached the listener that clears the cached signature. Studio writes that file at runtime, so saving motion state left the preview ETag stale until restart. The watcher now admits signature-relevant paths and the reload listener re-applies its own filter, so what triggers a browser reload is unchanged. Also from review: drop the `createViteAdapter` signature-cache default, which produced exactly the memo-nothing-clears bug this PR fixes, and correct the docstring — the content hash is already gated behind a stat fingerprint, so what the memo saves is the walk.
123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { resolve } from "node:path";
|
|
import { createProjectSignatureCache } from "./vite.adapter";
|
|
|
|
const PROJECT = resolve("/projects/demo");
|
|
|
|
/** A compute that changes every call, so a stale read is visible as a repeat. */
|
|
function countingCompute() {
|
|
let calls = 0;
|
|
return {
|
|
compute: () => `sig-${++calls}`,
|
|
get calls() {
|
|
return calls;
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("createProjectSignatureCache", () => {
|
|
it("memoises a project's signature", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
expect(cache.get(PROJECT)).toBe("sig-1");
|
|
expect(cache.get(PROJECT)).toBe("sig-1");
|
|
expect(source.calls).toBe(1);
|
|
});
|
|
|
|
it("recomputes after a file inside the project changes", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
expect(cache.get(PROJECT)).toBe("sig-1");
|
|
cache.invalidate(resolve(PROJECT, "index.html"));
|
|
|
|
// The preview ETag is this string. Serving "sig-1" again answers the
|
|
// browser's revalidation with a 304 and the pre-edit composition is what
|
|
// renders — which is how a thumbnail taken after an edit showed the old frame.
|
|
expect(cache.get(PROJECT)).toBe("sig-2");
|
|
});
|
|
|
|
it("recomputes for an asset added or removed, not only one edited", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
cache.get(PROJECT);
|
|
cache.invalidate(resolve(PROJECT, "assets/new-clip.mp4"));
|
|
expect(cache.get(PROJECT)).toBe("sig-2");
|
|
|
|
cache.invalidate(resolve(PROJECT, "compositions/scene.html"));
|
|
expect(cache.get(PROJECT)).toBe("sig-3");
|
|
});
|
|
|
|
it("leaves other projects alone", () => {
|
|
const signatures = new Map([
|
|
[PROJECT, "demo"],
|
|
[resolve("/projects/other"), "other"],
|
|
]);
|
|
let bumped = 0;
|
|
const cache = createProjectSignatureCache({
|
|
compute: (dir) => `${signatures.get(dir)}-${bumped}`,
|
|
});
|
|
|
|
expect(cache.get(PROJECT)).toBe("demo-0");
|
|
expect(cache.get(resolve("/projects/other"))).toBe("other-0");
|
|
|
|
bumped = 1;
|
|
cache.invalidate(resolve(PROJECT, "index.html"));
|
|
|
|
expect(cache.get(PROJECT)).toBe("demo-1");
|
|
expect(cache.get(resolve("/projects/other"))).toBe("other-0");
|
|
});
|
|
|
|
it("asks for a project directory to be watched once, on first use", () => {
|
|
const watched: string[] = [];
|
|
const cache = createProjectSignatureCache({
|
|
compute: () => "sig",
|
|
watch: (dir) => watched.push(dir),
|
|
});
|
|
|
|
cache.get(PROJECT);
|
|
cache.get(PROJECT);
|
|
cache.invalidate(resolve(PROJECT, "index.html"));
|
|
cache.get(PROJECT);
|
|
|
|
// Re-registering on every recompute would stack duplicate chokidar entries.
|
|
expect(watched).toEqual([PROJECT]);
|
|
});
|
|
|
|
it("normalises the directory it is asked about", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
expect(cache.get(PROJECT)).toBe("sig-1");
|
|
expect(cache.get(`${PROJECT}/`)).toBe("sig-1");
|
|
expect(cache.get(resolve(PROJECT, "nested/.."))).toBe("sig-1");
|
|
expect(source.calls).toBe(1);
|
|
});
|
|
|
|
it("does not invalidate on a write the signature cannot see", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
cache.get(PROJECT);
|
|
// Each thumbnail capture writes here and then reads the preview, so an
|
|
// unfiltered watcher discards the memo on roughly every request of the one
|
|
// workload the memo exists for.
|
|
cache.invalidate(resolve(PROJECT, ".thumbnails/frame-0.jpg"));
|
|
|
|
expect(cache.get(PROJECT)).toBe("sig-1");
|
|
expect(source.calls).toBe(1);
|
|
});
|
|
|
|
it("invalidates on a motion-state save, which lives under an otherwise-skipped dir", () => {
|
|
const source = countingCompute();
|
|
const cache = createProjectSignatureCache({ compute: source.compute });
|
|
|
|
cache.get(PROJECT);
|
|
cache.invalidate(resolve(PROJECT, ".hyperframes/studio-motion.json"));
|
|
|
|
expect(cache.get(PROJECT)).toBe("sig-2");
|
|
});
|
|
});
|