mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(studio-server): a save made while an import is taken in keeps its own undo step (#8561)
This commit is contained in:
@@ -1971,6 +1971,53 @@ describe("openProjectHistory", () => {
|
||||
});
|
||||
|
||||
describe("claim: a writer that records after writing", () => {
|
||||
/** A save the history has seen but not yet logged: the change is pending outside when the next claim runs. */
|
||||
async function pendingOutside(history: Awaited<ReturnType<typeof open>>, path: string) {
|
||||
history.noteChange(path);
|
||||
await vi.waitFor(() =>
|
||||
expect(history.next("back", you)?.files.map((file) => file.path)).toContain(path),
|
||||
);
|
||||
}
|
||||
const backdate = (file: string) =>
|
||||
utimesSync(file, new Date(Date.now() - 60_000), new Date(Date.now() - 60_000));
|
||||
|
||||
// fails without the fix: the import's claim logged the save as a change from outside, the save's own claim then
|
||||
// found nothing, and Cmd+Z took back the import instead of the edit.
|
||||
it("a save made while an import is still taken in keeps its own step: Cmd+Z undoes the edit, the media stays", async () => {
|
||||
const { history, write, read, has, projectDir } = await project(
|
||||
{ "index.html": "<h1>Hello</h1>" },
|
||||
{ quietMs: 60_000 },
|
||||
);
|
||||
write("assets/logo.png", "png");
|
||||
backdate(join(projectDir, "assets/logo.png"));
|
||||
write("index.html", "<h1>Two</h1>");
|
||||
await pendingOutside(history, "index.html");
|
||||
await history.claim(you, "Imported media", ["assets/logo.png"]);
|
||||
await history.claim(you, "Edited", ["index.html"]);
|
||||
expect(history.list().map((entry) => entry.label)).toEqual(["Imported media", "Edited"]);
|
||||
expect(await history.step("back", you)).toMatchObject({ ok: true });
|
||||
expect(read("index.html")).toBe("<h1>Hello</h1>");
|
||||
expect(has("assets/logo.png")).toBe(true);
|
||||
});
|
||||
|
||||
it("a change made before a claim's own files is still logged ahead of the claim", async () => {
|
||||
const { history, write, projectDir } = await project(
|
||||
{ "index.html": "v1" },
|
||||
{ quietMs: 60_000 },
|
||||
);
|
||||
write("style.css", "h1 {}");
|
||||
backdate(join(projectDir, "style.css"));
|
||||
await pendingOutside(history, "style.css");
|
||||
write("index.html", "v2");
|
||||
await history.claim(you, "Edited", ["index.html"]);
|
||||
expect(
|
||||
history.list().map((entry) => [entry.who.kind, entry.files.map((file) => file.path)]),
|
||||
).toEqual([
|
||||
["outside", ["style.css"]],
|
||||
["person", ["index.html"]],
|
||||
]);
|
||||
});
|
||||
|
||||
it("files the claimed paths' writes as the claimer's entry; other outside writes stay outside, logged first", async () => {
|
||||
const { history, write, read } = await project({ "index.html": "A", "notes.md": "n" });
|
||||
write("index.html", "B");
|
||||
|
||||
@@ -1256,7 +1256,7 @@ class Engine {
|
||||
if (!taken.length) return this.claimNothing(coalesceKey);
|
||||
const held = this.heldFor(coalesceKey, taken);
|
||||
if (!held) await this.commitClaim();
|
||||
await this.commitPending();
|
||||
await this.commitPendingWrittenBefore(taken);
|
||||
const group = held ?? this.newGroup(who, label);
|
||||
for (const change of taken) addChange(group, change.path, change.before, change.after);
|
||||
this.markWentAhead(pending, group);
|
||||
@@ -1291,6 +1291,25 @@ class Engine {
|
||||
return null;
|
||||
}
|
||||
|
||||
async commitPendingWrittenBefore(taken: readonly HistoryFileChange[]): Promise<void> {
|
||||
const writtenAt = Math.max(
|
||||
...(await Promise.all(taken.map((change) => this.mtimeOf(change.path)))),
|
||||
);
|
||||
const later: HistoryFileChange[] = [];
|
||||
if (Number.isFinite(writtenAt))
|
||||
for (const change of [...(this.outside?.changes.values() ?? [])])
|
||||
if ((await this.mtimeOf(change.path)) > writtenAt) later.push(change);
|
||||
for (const change of later) this.outside!.changes.delete(change.path);
|
||||
await this.commitPending();
|
||||
if (!later.length) return;
|
||||
const outside = this.outsideGroup();
|
||||
for (const change of later) outside.changes.set(change.path, change);
|
||||
}
|
||||
|
||||
async mtimeOf(path: string): Promise<number> {
|
||||
return (await stat(join(this.dir, path)).catch(() => null))?.mtimeMs ?? -Infinity;
|
||||
}
|
||||
|
||||
/** Commits every other writer's pending changes, oldest first; open windows stay open. */
|
||||
async commitPending(): Promise<void> {
|
||||
await this.commitOutside();
|
||||
|
||||
Reference in New Issue
Block a user