diff --git a/packages/cli/src/server/followReceivedRecords.ts b/packages/cli/src/server/followReceivedRecords.ts
index 97a5322c71..6c06d66bd9 100644
--- a/packages/cli/src/server/followReceivedRecords.ts
+++ b/packages/cli/src/server/followReceivedRecords.ts
@@ -71,8 +71,8 @@ async function startedChange(
/**
* Follows a Studio rename, move or delete in the received-files records. Before the files move, every file listed in
* a record inside a moved or deleted folder is listed in the project's own record, the received content leaving a
- * listed path is kept, and a renamed file is listed under its new path; when that fails, the request is refused.
- * Afterwards old lines go, but a non-media file deleted or renamed to a media name stays listed, still received.
+ * listed path is kept, and a renamed file is listed under the path it moves to; when that fails, the request is refused.
+ * Afterwards an old line goes once its file is listed where it sits; a non-media file deleted or made media stays.
*/
function followMovesAndDeletes(app: Hono, projectDir: string): void {
app.on(["PATCH", "DELETE"], "/api/projects/:id/files/*", async (c, next) => {
diff --git a/packages/cli/src/server/studioServer.receivedRecord.test.ts b/packages/cli/src/server/studioServer.receivedRecord.test.ts
index 192093eab3..4ca5e47e9f 100644
--- a/packages/cli/src/server/studioServer.receivedRecord.test.ts
+++ b/packages/cli/src/server/studioServer.receivedRecord.test.ts
@@ -56,6 +56,17 @@ function caseInsensitiveDisk(): boolean {
}
}
+/** Whether a name in composed Unicode reaches a file named in decomposed Unicode, as on macOS and Windows. */
+function unicodeFormsFolded(): boolean {
+ const probe = mkdtempSync(join(tmpdir(), "hf-unicode-probe-"));
+ try {
+ writeFileSync(join(probe, "caf\u00e9"), "");
+ return existsSync(join(probe, "cafe\u0301"));
+ } finally {
+ rmSync(probe, { recursive: true, force: true });
+ }
+}
+
function write(path: string, text: string | Buffer): void {
mkdirSync(join(path, ".."), { recursive: true });
writeFileSync(path, text);
@@ -379,6 +390,88 @@ describe("a Studio rename, move or delete in a project with a received-files rec
},
);
+ it.runIf(caseInsensitiveDisk())(
+ "refuses a rename that changes only the letter case on a disk that folds case, keeping the line",
+ async () => {
+ const { projectDir, app } = studio();
+ withRecord(projectDir);
+ expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(409);
+ expect(listed(projectDir)).toEqual(["scenes/intro.html", "scenes/logo.png"]);
+ expect(allowed(projectDir)).toBe(false);
+ },
+ );
+
+ it.skipIf(caseInsensitiveDisk())(
+ "lists a received page under its new name after a rename that changes only its letter case",
+ async () => {
+ const { projectDir, app } = studio();
+ withRecord(projectDir);
+ expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(200);
+ expect(listed(projectDir)).toEqual(["scenes/Intro.html", "scenes/logo.png"]);
+ expect(allowed(projectDir)).toBe(false);
+ },
+ );
+
+ it.skipIf(unicodeFormsFolded())(
+ "lists a received page under its new name after a rename from composed to decomposed Unicode",
+ async () => {
+ const { projectDir, app } = studio();
+ withRecord(projectDir);
+ write(join(projectDir, "scenes", "caf\u00e9.html"), `
`);
+ write(record(projectDir), JSON.stringify({ files: ["scenes/caf\u00e9.html"] }));
+ expect((await move(app, "scenes/caf\u00e9.html", "scenes/cafe\u0301.html")).status).toBe(200);
+ expect(listed(projectDir)).toEqual(["scenes/cafe\u0301.html"]);
+ expect(allowed(projectDir)).toBe(false);
+ },
+ );
+
+ it.skipIf(unicodeFormsFolded())(
+ "keeps a received page listed when a file named like it in another Unicode form is renamed",
+ async () => {
+ const { projectDir, app } = studio();
+ withRecord(projectDir);
+ write(join(projectDir, "scenes", "caf\u00e9.html"), `
`);
+ write(join(projectDir, "scenes", "cafe\u0301.html"), "
other
"); + write( + record(projectDir), + JSON.stringify({ files: ["scenes/Opening.html", "scenes/intro.html"] }), + ); + expect((await move(app, "scenes/intro.html", "scenes/opening.html")).status).toBe(200); + expect(listed(projectDir)).toEqual(["scenes/Opening.html", "scenes/opening.html"]); + expect(allowed(projectDir)).toBe(false); + }, + ); + + it.skipIf(caseInsensitiveDisk())( + "keeps refusing outside files after a received page is renamed in another letter case, deleted and made again", + async () => { + const { projectDir, app } = studio(); + withRecord(projectDir); + expect((await move(app, "scenes/intro.html", "scenes/Intro.html")).status).toBe(200); + const page = fileUrl("scenes/Intro.html"); + expect((await app.request(page, { method: "DELETE" })).status).toBe(200); + const made = await app.request(page, { + method: "PUT", + headers: { "If-None-Match": "*" }, + body: `
new
`, + }); + expect(made.ok).toBe(true); + expect(allowed(projectDir)).toBe(false); + }, + ); + it("answers other requests while it waits for the record's lock, then refuses the rename", async () => { const { projectDir, app } = studio(); withRecord(projectDir); @@ -563,13 +656,13 @@ describe("a Studio rename, move or delete in a project with a received-files rec expect(receivedFiles(projectDir).gone).toBe("scenes/intro.html"); }); - it("moves a line its record spells in decomposed Unicode", async () => { + it("moves a line its record spells in decomposed Unicode, spelled as the move spells it", async () => { const { projectDir, app } = studio(); const folder = "cafe\u0301"; write(join(projectDir, folder, "intro.html"), ""); write(record(projectDir), JSON.stringify({ files: [`${folder}/intro.html`] })); expect((await move(app, folder, `parts/${folder}`)).status).toBe(200); - expect(listed(projectDir)).toEqual(["parts/caf\u00e9/intro.html"]); + expect(listed(projectDir)).toEqual([`parts/${folder}/intro.html`]); expect(receivedFiles(projectDir)("parts/caf\u00e9/intro.html")).toBe(true); }); }); diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index 4853cda8c3..cb4ee055df 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -500,7 +500,7 @@ function receivedRecords(projectDir: string): ReceivedRecord[] | null { return records; } -function fileIdentity(path: string): string | null { +export function fileIdentity(path: string): string | null { try { const stat = lstatSync(path, { bigint: true }); return stat.ino === 0n ? null : `${stat.dev}:${stat.ino}`; diff --git a/packages/cli/src/utils/receivedRecordFollow.test.ts b/packages/cli/src/utils/receivedRecordFollow.test.ts new file mode 100644 index 0000000000..1397a8e07a --- /dev/null +++ b/packages/cli/src/utils/receivedRecordFollow.test.ts @@ -0,0 +1,112 @@ +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + renameSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { + dropReceivedArrivals, + dropReceivedLeavers, + onDiskPath, + plannedReceivedChange, + recordReceivedArrivals, +} from "./receivedRecordFollow.js"; + +let projectDir: string; + +afterEach(() => rmSync(projectDir, { recursive: true, force: true })); + +function project(listed: string[]): string { + projectDir = realpathSync.native(mkdtempSync(join(tmpdir(), "hf-record-follow-"))); + mkdirSync(join(projectDir, ".hyperframes")); + writeFileSync( + join(projectDir, ".hyperframes", "received-files.json"), + JSON.stringify({ files: listed }), + ); + for (const line of listed) writeFileSync(join(projectDir, line), "received
"); + return projectDir; +} + +const listed = (dir: string): string[] => + JSON.parse(readFileSync(join(dir, ".hyperframes", "received-files.json"), "utf-8")).files; + +async function followRename(dir: string, planned: string, actual: string): Promisereceived
"); + await dropReceivedLeavers(started); + expect(listed(dir)).toEqual(["intro.html", "opening.html"]); + }); + + it("keeps the new line a refused rename listed when another rename moved the file there", async () => { + const dir = project(["intro.html"]); + const plan = () => + plannedReceivedChange(dir, join(dir, "intro.html"), join(dir, "opening.html")); + const refused = await recordReceivedArrivals(await plan()); + const done = await recordReceivedArrivals(await plan()); + renameSync(join(dir, "intro.html"), join(dir, "opening.html")); + await dropReceivedLeavers(done); + await dropReceivedArrivals(refused); + expect(listed(dir)).toEqual(["opening.html"]); + }); + + it("takes back the new line of a refused rename when the file did not move", async () => { + const dir = project(["intro.html"]); + const change = await plannedReceivedChange( + dir, + join(dir, "intro.html"), + join(dir, "opening.html"), + ); + await dropReceivedArrivals(await recordReceivedArrivals(change)); + expect(listed(dir)).toEqual(["intro.html"]); + }); + + it("keeps the old line when the planned path is the old line itself", async () => { + const dir = project(["intro.html"]); + await followRename(dir, "intro.html", "elsewhere.html"); + expect(listed(dir)).toEqual(["intro.html"]); + }); +}); + +describe("onDiskPath", () => { + it("spells a path as the folder lists it only when the disk opens one file under both spellings", () => { + const dir = project(["intro.html"]); + const foldsCase = existsSync(join(dir, "INTRO.html")); + expect(onDiskPath(join(dir, "INTRO.html"))).toBe( + join(dir, foldsCase ? "intro.html" : "INTRO.html"), + ); + expect(onDiskPath(join(dir, "intro.html"))).toBe(join(dir, "intro.html")); + }); +}); diff --git a/packages/cli/src/utils/receivedRecordFollow.ts b/packages/cli/src/utils/receivedRecordFollow.ts index 9603f13467..e73752e7e7 100644 --- a/packages/cli/src/utils/receivedRecordFollow.ts +++ b/packages/cli/src/utils/receivedRecordFollow.ts @@ -5,19 +5,22 @@ import { realFilePath, resolveWithinProject } from "@hyperframes/studio-server/s import { registryRoot } from "../registry/publication.js"; import { changeReceivedRecordAsync, + fileIdentity, filesListedInFolder, receivedLinesAtOrAbove, } from "./privateProjectFile.js"; -import { contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js"; +import { contentHash, contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js"; import { isMediaFontOrImage, isMediaName } from "./receivedRender.js"; type Lines = Map