Files
hyperframes/packages/cli/src/server/studioServer.folderReplacement.test.ts
T
Miguel Ángel dbb8222c8d fix(studio): catch up the preview after missed file changes (#5126)
* fix(studio): recover previews after missed file changes

* fix(studio): keep queued reloads with the current owner

* fix(studio): keep a queued outside change when switching compositions

* test(cli): the project watcher closes its parent folder's watch too

* fix(studio): keep the project-folder change out of the shared signature check

* fix(studio): keep the edited file's draft and reload every session after a reconnect

A reconnect or replaced folder is a project-wide change: every open SDK session reloads, a failed save is recorded against the file the draft belongs to, and two queued changes to different files merge into one project-wide reload.

* fix(studio): refresh the preview cache when the .hyperframes folder is replaced

* fix(cli): report the files of a folder moved into the project

Replacing a populated folder such as .hyperframes/ now reaches the signature cache through its files, so the shared signature rule stays as on main and Studio's own writes inside .hyperframes/ still invalidate nothing.

* refactor(studio): name the merged file-change payload and share the test sender

* fix(studio): keep a failed draft's recovery copy until that file itself saves

* style(studio): rewrap the merged payload line after the rename

* fix(studio): deliver a held change's full reload when its block clears

A clean save, Use external and Keep Studio now all deliver the merged scope a blocked change owes, and a moved-in .hyperframes folder reports its manifests so a dropped one still clears the cached signature.

* fix(studio): keep restored drafts out of the reload scope and clear the lint gates

* fix(studio): retain held reload scope before React renders
2026-10-07 07:32:16 +00:00

133 lines
5.6 KiB
TypeScript

import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import { compositionsAffectedBy } from "@hyperframes/studio-server";
import { createStudioServer, type StudioServer } from "./studioServer.js";
import { cleanupStudioServerRoot, makeStudioServerRoot } from "./studioServerTestFixture.js";
let root: string;
let server: StudioServer | undefined;
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
afterEach(async () => {
await reader?.cancel();
if (server) cleanupStudioServerRoot(server, root);
});
it.runIf(process.platform === "linux")(
"refreshes every composition and cached preview when its project directory is replaced",
async () => {
const fixture = makeStudioServerRoot("hf-preview-replace-");
root = fixture.root;
const { projectDir } = fixture;
const rootHtml = (label: string) => `<html><head></head><body>
<div data-composition-id="main" data-width="320" data-height="180">
<p>${label}</p><div data-composition-id="intro" data-composition-src="scenes/intro.html" data-start="0" data-duration="1"></div>
</div></body></html>`;
const scene = (
text: string,
) => `<template><div data-composition-id="intro" data-width="320" data-height="180">
<h1>${text}</h1></div></template>`;
mkdirSync(join(projectDir, "scenes"));
writeFileSync(join(projectDir, "index.html"), rootHtml("Initial"));
writeFileSync(join(projectDir, "scenes/intro.html"), scene("Original"));
expect(compositionsAffectedBy(projectDir, "index.html")).toBeNull();
writeFileSync(join(projectDir, "index.html"), rootHtml("Unchanged root"));
server = createStudioServer({ projectDir, projectName: "film" });
expect(compositionsAffectedBy(projectDir, "index.html")).toEqual(["index.html"]);
const app = server.app;
const endpoint = "/api/projects/film/preview/comp/scenes/intro.html";
const before = await app.request(endpoint);
expect(before.status).toBe(200);
expect(await before.text()).toContain("Original");
const beforeTag = before.headers.get("etag");
expect(beforeTag).toBeTruthy();
const response = await app.request("/api/events");
reader = response.body!.getReader();
const deliveries: string[] = [];
const consume = async () => {
while (true) {
const next = await reader!.read();
if (next.done) return;
deliveries.push(new TextDecoder().decode(next.value));
}
};
const consuming = consume();
try {
const replacement = join(root, "replacement");
mkdirSync(join(replacement, "scenes"), { recursive: true });
writeFileSync(join(replacement, "index.html"), readFileSync(join(projectDir, "index.html")));
writeFileSync(join(replacement, "scenes/intro.html"), scene("Replacement"));
renameSync(projectDir, join(root, "previous"));
renameSync(replacement, projectDir);
await vi.waitFor(() => {
const event = deliveries
.join("")
.split("\n")
.find((line) => line.startsWith("data:") && line.includes('"path":"."'));
expect(event).toBeDefined();
expect(JSON.parse(event!.slice(5))).toMatchObject({
path: ".",
projectId: "film",
affectsPreview: true,
affectedCompositions: null,
});
});
const after = await app.request(endpoint, { headers: { "If-None-Match": beforeTag! } });
expect(after.status).toBe(200);
expect(after.headers.get("etag")).not.toBe(beforeTag);
expect(await after.text()).toContain("Replacement");
writeFileSync(join(projectDir, "scenes/intro.html"), scene("Later edit"));
await vi.waitFor(async () => {
const later = await app.request(endpoint);
expect(later.status).toBe(200);
expect(await later.text()).toContain("Later edit");
});
} finally {
await reader.cancel();
await consuming;
}
},
);
it.runIf(process.platform === "linux").each([
{ kept: "a changed manifest", file: "studio-motion.json", content: '{"intro":{"opacity":0.5}}' },
{ kept: "no manifest", file: "arbitrary.txt", content: "not a manifest" },
])(
"refreshes the cached preview when a populated .hyperframes folder is replaced by one with $kept",
async ({ file, content }) => {
const fixture = makeStudioServerRoot("hf-preview-replace-manifests-");
root = fixture.root;
const { projectDir } = fixture;
writeFileSync(
join(projectDir, "index.html"),
`<html><body><div data-composition-id="main" data-width="320" data-height="180"></div></body></html>`,
);
mkdirSync(join(projectDir, ".hyperframes"));
writeFileSync(join(projectDir, ".hyperframes/studio-motion.json"), "{}");
server = createStudioServer({ projectDir, projectName: "film" });
const endpoint = "/api/projects/film/preview/comp/index.html";
const before = await server.app.request(endpoint);
expect(before.status).toBe(200);
const beforeTag = before.headers.get("etag");
expect(beforeTag).toBeTruthy();
const replacement = join(root, "next-manifests");
mkdirSync(replacement);
writeFileSync(join(replacement, file), content);
renameSync(join(projectDir, ".hyperframes"), join(root, "previous-manifests"));
renameSync(replacement, join(projectDir, ".hyperframes"));
await vi.waitFor(async () => {
const after = await server!.app.request(endpoint, {
headers: { "If-None-Match": beforeTag! },
});
expect(after.status).toBe(200);
expect(after.headers.get("etag")).not.toBe(beforeTag);
});
},
);