Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/studio-server/src/helpers/applyFileMutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ describe("applyFileMutations", () => {
expectStaleMutation("after", false);
});

it("leaves a receipt saying which bytes the write started from", () => {
resetFileWriteReceipts();
const projectDir = mkdtempSync(join(tmpdir(), "hf-mutation-from-"));
const path = join(projectDir, "index.html");
try {
// Someone else's change lands before the patch reads the file: the receipt names it as the start.
writeFileSync(path, "external", "utf8");
applyFileMutations(
projectDir,
[{ sourceFile: "index.html", absPath: path, after: "patched" }],
"hand",
);
expect(identifyFileWrite(path, fileContentVersion("patched"))).toMatchObject({
writeToken: "hand",
from: fileContentVersion("external"),
});
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
});

it("clears receipts for writes rolled back after a partial batch", () => {
resetFileWriteReceipts();
const projectDir = mkdtempSync(join(tmpdir(), "hf-mutation-rollback-"));
Expand Down
23 changes: 23 additions & 0 deletions packages/studio-server/src/helpers/fileVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ describe("file versions and write receipts", () => {
expect(identifyFileWrite("/project/index.html", first.version)).toEqual(first);
});

it("says which version a write replaced when the writer read it, and nothing when it did not", () => {
const version = fileContentVersion("<h1>Title!</h1>");
recordFileWriteReceipt("/project/index.html", {
path: "index.html",
version,
writeToken: "hand",
overwrote: "<h1>Title</h1>",
});
expect(identifyFileWrite("/project/index.html", version)).toEqual({
path: "index.html",
version,
writeToken: "hand",
from: fileContentVersion("<h1>Title</h1>"),
});
const restored = fileContentVersion("<h1>Old</h1>");
recordFileWriteReceipt("/project/index.html", {
path: "index.html",
version: restored,
writeToken: "restore",
});
expect(identifyFileWrite("/project/index.html", restored)).not.toHaveProperty("from");
});

it("labels a repeat of earlier bytes with the newest token, not the spent one", () => {
const version = fileContentVersion("same bytes");
const older = { path: "index.html", version, writeToken: "write-older" };
Expand Down
11 changes: 8 additions & 3 deletions packages/studio-server/src/helpers/fileVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface FileWriteReceipt {
path: string;
version: string;
writeToken: string;
from?: string;
}

interface StoredReceipt extends FileWriteReceipt {
Expand Down Expand Up @@ -70,7 +71,11 @@ export function recordFileWriteReceipt(
if (live.length > 0) receipts.set(path, live);
else receipts.delete(path);
}
receipts.set(absPath, [...(receipts.get(absPath) ?? []), { ...receipt, recordedAt: now }]);
const from = overwrote === undefined ? undefined : fileContentVersion(overwrote);
receipts.set(absPath, [
...(receipts.get(absPath) ?? []),
{ ...receipt, ...(from && { from }), recordedAt: now },
]);
}

export function clearFileWriteReceipt(filePath: string, version: string, writeToken: string): void {
Expand All @@ -96,8 +101,8 @@ export function identifyFileWrite(
): FileWriteReceipt | null {
const receipt = newestReceipt(realFilePath(filePath), expectedVersion);
if (!receipt) return null;
const { path, version, writeToken } = receipt;
return { path, version, writeToken };
const { path, version, writeToken, from } = receipt;
return { path, version, writeToken, ...(from && { from }) };
}

function newestReceipt(absPath: string, expectedVersion: string): StoredReceipt | undefined {
Expand Down
7 changes: 7 additions & 0 deletions packages/studio-server/src/routes/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe("registerFileRoutes", () => {
path: "index.html",
version: result.version,
writeToken: "studio-insert-1",
from: fileContentVersion(before),
});

const committed = result.after;
Expand Down Expand Up @@ -428,6 +429,7 @@ describe("registerFileRoutes", () => {
path: "image.png",
version: payload.version,
writeToken: "binary-write",
from: fileContentVersion(before),
});
});

Expand Down Expand Up @@ -541,6 +543,7 @@ describe("registerFileRoutes", () => {
path: "index.html",
version: payload.version,
writeToken: "studio-write-1",
from: fileContentVersion("before"),
});
expect(payload.backupPath).toMatch(/^\.hyperframes\/backup\//);
expect(readFileSync(join(projectDir, payload.backupPath!), "utf-8")).toBe("before");
Expand Down Expand Up @@ -734,6 +737,7 @@ describe("registerFileRoutes", () => {
path: "index.html",
version,
writeToken: "studio-patch-1",
from: fileContentVersion('<div id="title">Before</div>'),
});
});

Expand Down Expand Up @@ -780,6 +784,7 @@ describe("registerFileRoutes", () => {
path: "index.html",
version,
writeToken: "studio-layer-order-1",
from: fileContentVersion(original),
});
expect(readdirSync(join(projectDir, ".hyperframes", "backup"))).toHaveLength(1);
});
Expand Down Expand Up @@ -884,6 +889,7 @@ describe("registerFileRoutes", () => {
path: file.sourceFile,
version,
writeToken: "studio-group-drag-1",
from: fileContentVersion(`<div id="${file.sourceFile.replace(".html", "")}">Before</div>`),
});
}
});
Expand Down Expand Up @@ -1105,6 +1111,7 @@ describe("registerFileRoutes", () => {
path: "index.html",
version: payload.files[0].version,
writeToken: "cut-test",
from: fileContentVersion(before),
});
});

Expand Down
Loading