From 81b73ee4b0920e949f435faf42f88d67b4185272 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 22 Nov 2026 03:09:34 -0500 Subject: [PATCH 1/4] test(studio): the edit-accuracy bench reads a sequence's file once its last edit has saved A sealed press made while the previous save is out waits, then replays and saves. The sequence bench read the file once its write count was met, which one gesture writing twice can reach before the last gesture saves, so that edit looked lost. Studio now says on the page while an edit saves, and the bench also waits for that and for no waiting press, two frames in a row. --- .../src/utils/studioPendingEdits.test.ts | 28 +++++++++++++++++++ .../studio/src/utils/studioPendingEdits.ts | 15 +++++++++- .../tests/e2e/edit-accuracy/sequences.mjs | 23 +++++++++++++++ .../tests/e2e/edit-accuracy/settle.test.mjs | 25 ++++++++++++++++- 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/packages/studio/src/utils/studioPendingEdits.test.ts b/packages/studio/src/utils/studioPendingEdits.test.ts index 834de7962ed..40c97929413 100644 --- a/packages/studio/src/utils/studioPendingEdits.test.ts +++ b/packages/studio/src/utils/studioPendingEdits.test.ts @@ -5,6 +5,7 @@ import { addStudioPendingEditFlushListener, adoptingStudioPendingEdit, beginStudioPendingEdit, + beginStudioShownRestore, flushStudioPendingEdits, afterStudioPendingEdits, hasStudioPendingEdits, @@ -42,6 +43,33 @@ describe("a canvas press still waiting to run", () => { }); }); +// fails without the fix: nothing on the page said an edit was still saving, so the edit-accuracy bench read the file +// after a sealed press replayed and before that press's own save landed. +describe("the page's word that an edit is still saving", () => { + const saving = () => document.documentElement.hasAttribute("data-studio-edits-saving"); + + // Each ends its edit before asserting, so a failure leaves no edit pending for the tests after it. + it("holds from a pending edit's start until its save lands", async () => { + let save!: () => void; + const edit = beginStudioPendingEdit(null); + const atStart = saving(); + edit.settle(new Promise((resolve) => (save = resolve))); + await Promise.resolve(); + const whileSaving = saving(); + save(); + await vi.waitFor(() => expect(saving()).toBe(false)); + expect([atStart, whileSaving]).toEqual([true, true]); + }); + + it("holds while a shown undo or redo waits for its save", async () => { + const landed = beginStudioShownRestore(); + const whileWaiting = saving(); + landed(); + await vi.waitFor(() => expect(saving()).toBe(false)); + expect(whileWaiting).toBe(true); + }); +}); + describe("studio pending edit flush", () => { it("waits for mounted panels to persist pending local edits", async () => { const persist = vi.fn(async () => undefined); diff --git a/packages/studio/src/utils/studioPendingEdits.ts b/packages/studio/src/utils/studioPendingEdits.ts index 1e9ad6eeeca..fe8cf62753e 100644 --- a/packages/studio/src/utils/studioPendingEdits.ts +++ b/packages/studio/src/utils/studioPendingEdits.ts @@ -87,7 +87,11 @@ export function beginStudioShownRestore(): () => void { let end = () => {}; const landed = new Promise((resolve) => (end = resolve)); shownRestores.add(landed); - void landed.then(() => shownRestores.delete(landed)); + markEditsSavingOnPage(); + void landed.then(() => { + shownRestores.delete(landed); + markEditsSavingOnPage(); + }); return end; } @@ -95,6 +99,13 @@ export function isStudioEditSaving(): boolean { return pendingEdits.size > 0 || shownRestores.size > 0; } +function markEditsSavingOnPage(): void { + globalThis.document?.documentElement.toggleAttribute( + "data-studio-edits-saving", + isStudioEditSaving(), + ); +} + export function afterStudioPendingEdits(run: () => void): () => void { let waiting = true; const check = () => { @@ -125,8 +136,10 @@ export function trackStudioPendingEdit( showAgain: null, claimsAtBegin: historyClaims(), }); + markEditsSavingOnPage(); const done = () => { pendingEdits.delete(promise); + markEditsSavingOnPage(); if (!pendingEdits.size) allSettled?.(); }; promise.then(done, done); diff --git a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs index b84fb83c60f..d5b0c49a79e 100644 --- a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs @@ -439,6 +439,24 @@ export function heldSize(sizeKeys, time) { const sizeError = (a, b) => a && b ? Math.max(Math.abs(a.width - b.width), Math.abs(a.height - b.height)) : 0; +/** Resolves once `busy()` has read false `frames` reads in a row, a `frame()` apart; throws at `timeout` ms. */ +export async function restedFor(busy, frame, { frames = 2, timeout = 20_000 } = {}) { + let clear = 0; + for (const deadline = Date.now() + timeout; Date.now() < deadline; await frame()) { + clear = (await busy()) ? 0 : clear + 1; + if (clear >= frames) return; + } + throw new Error("an edit was still waiting or saving"); +} + +// A sealed press waits for the previous save, then replays: the file is final once none waits and no edit saves. +const editsBusy = (page) => + page.evaluate( + () => + Boolean(document.querySelector("[data-dom-edit-press-waiting]")) || + document.documentElement.hasAttribute("data-studio-edits-saving"), + ); + async function untilSaved(watcher, owed) { for (const deadline = Date.now() + 10_000; Date.now() < deadline; await sleep(50)) if (watcher.versions.length - 1 >= owed) return; @@ -517,6 +535,11 @@ async function measureSequence({ spec, dir, files, evidence }, session, control, backSince = back ? (backSince ?? Date.now()) : null; if (backSince !== null && Date.now() - backSince >= UNDONE_QUIET_MS) break; } + // The write count is a floor: one gesture may write twice, so it can be met before the last gesture saves. + await restedFor( + () => editsBusy(page), + () => nextFrame(page), + ); await page.waitForFunction( // Asked over each preview's editing port: a preview that cannot answer yet is not clear. async () => diff --git a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs index d357b7d1d5a..b962222b69d 100644 --- a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs @@ -13,7 +13,7 @@ import { swapPending, unsettledBy, } from "./case.mjs"; -import { heldSize, keyedBy } from "./sequences.mjs"; +import { heldSize, keyedBy, restedFor } from "./sequences.mjs"; describe("settling on the shown preview", () => { const read = (pending, x = 0, frames = "a") => ({ @@ -289,3 +289,26 @@ describe("the box a press is measured against", () => { expect(shown.m.visible[0][0]).toBe(0); }); }); + +// fails without the fix: the sequence read the file once its write count was met, while a sealed press that waited for +// the previous save was still saving, so the last gesture's edit looked lost. +describe("the end of a sequence", () => { + const reads = (states) => { + let at = 0; + const busy = async () => states[Math.min(at++, states.length - 1)]; + return { busy, read: () => at }; + }; + + it("waits until no press waits and no edit saves, two reads in a row", async () => { + const { busy, read } = reads([true, true, false, true, false, false, true]); + await restedFor(busy, async () => {}); + expect(read()).toBe(6); + }); + + it("gives up with a reason when an edit never stops saving", async () => { + const { busy } = reads([true]); + await expect(restedFor(busy, async () => {}, { timeout: 30 })).rejects.toThrow( + /still waiting or saving/, + ); + }); +}); From cd5018aff72b4c8b4f7b5fa76f58a4501670e0bf Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 22 Nov 2026 03:32:48 -0500 Subject: [PATCH 2/4] test(studio): a sequence's undo step waits until the undo is shown and records how long it took --- .../tests/e2e/edit-accuracy/sequences.mjs | 32 ++++++++++++------- .../tests/e2e/edit-accuracy/settle.test.mjs | 6 ++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs index d5b0c49a79e..c1108b5b7c1 100644 --- a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs @@ -244,12 +244,15 @@ async function driveStep(ctx, step, state) { if (step.do === "undo") { await blurPreview(page); await chord(page, "Control+z"); - // A key's DOM change reaches the CDP quads only after a frame; the next step reads from them. - await nextFrame(page); + // The next step presses the box where it is shown; a person sees the old box until then. + const shownMs = await restedFor( + () => studioBusy(page), + () => nextFrame(page), + ); state.depth -= 1; // Undone back to the start, the box belongs where it began; any other undo leaves it unknown here. state.intended = state.depth === 0 ? state.start : null; - return { do: "undo" }; + return { do: "undo", shownMs }; } if (step.do === "nudge") { await recording(page, true); @@ -439,22 +442,29 @@ export function heldSize(sizeKeys, time) { const sizeError = (a, b) => a && b ? Math.max(Math.abs(a.width - b.width), Math.abs(a.height - b.height)) : 0; -/** Resolves once `busy()` has read false `frames` reads in a row, a `frame()` apart; throws at `timeout` ms. */ +/** + * Resolves once `busy()` has read false `frames` reads in a row, a `frame()` apart, with the ms until the first of + * those reads; throws at `timeout` ms. + */ export async function restedFor(busy, frame, { frames = 2, timeout = 20_000 } = {}) { - let clear = 0; - for (const deadline = Date.now() + timeout; Date.now() < deadline; await frame()) { + const start = Date.now(); + let [clear, clearSince] = [0, 0]; + for (const deadline = start + timeout; Date.now() < deadline; await frame()) { + const now = Date.now(); clear = (await busy()) ? 0 : clear + 1; - if (clear >= frames) return; + if (clear === 1) clearSince = now; + if (clear >= frames) return clearSince - start; } throw new Error("an edit was still waiting or saving"); } -// A sealed press waits for the previous save, then replays: the file is final once none waits and no edit saves. -const editsBusy = (page) => +// A sealed press waits for the previous save, then replays, and a sealed undo shows only once the preview applies it. +const studioBusy = (page) => page.evaluate( () => Boolean(document.querySelector("[data-dom-edit-press-waiting]")) || - document.documentElement.hasAttribute("data-studio-edits-saving"), + document.documentElement.hasAttribute("data-studio-edits-saving") || + document.documentElement.hasAttribute("data-preview-changing"), ); async function untilSaved(watcher, owed) { @@ -537,7 +547,7 @@ async function measureSequence({ spec, dir, files, evidence }, session, control, } // The write count is a floor: one gesture may write twice, so it can be met before the last gesture saves. await restedFor( - () => editsBusy(page), + () => studioBusy(page), () => nextFrame(page), ); await page.waitForFunction( diff --git a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs index b962222b69d..4d0c610e0b0 100644 --- a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs @@ -305,6 +305,12 @@ describe("the end of a sequence", () => { expect(read()).toBe(6); }); + it("says how long until the first of the clear reads, the undo's time to show", async () => { + const { busy } = reads([true, true, false, false]); + const ms = await restedFor(busy, () => new Promise((r) => setTimeout(r, 10))); + expect(ms).toBeGreaterThanOrEqual(20); + }); + it("gives up with a reason when an edit never stops saving", async () => { const { busy } = reads([true]); await expect(restedFor(busy, async () => {}, { timeout: 30 })).rejects.toThrow( From d020e443af25311346d5a6e8278843532890884b Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 22 Nov 2026 03:52:05 -0500 Subject: [PATCH 3/4] test(studio): an undo step records when the box first moves apart from when Studio is idle --- .../studio/tests/e2e/edit-accuracy/sequences.mjs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs index c1108b5b7c1..1cd2e20833a 100644 --- a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs @@ -243,16 +243,23 @@ async function driveStep(ctx, step, state) { const { page } = ctx.A; if (step.do === "undo") { await blurPreview(page); + const before = (await measure(ctx.A)).visible; + const pressedAt = Date.now(); await chord(page, "Control+z"); - // The next step presses the box where it is shown; a person sees the old box until then. - const shownMs = await restedFor( - () => studioBusy(page), + // shownMs: the box first moved, what a person sees. restMs: Studio idle, so the next press lands on the shown box. + let shownMs = null; + const restMs = await restedFor( + async () => { + const moved = quadDistance((await measure(ctx.A)).visible, before) >= 0.5; + if (moved && shownMs === null) shownMs = Date.now() - pressedAt; + return studioBusy(page); + }, () => nextFrame(page), ); state.depth -= 1; // Undone back to the start, the box belongs where it began; any other undo leaves it unknown here. state.intended = state.depth === 0 ? state.start : null; - return { do: "undo", shownMs }; + return { do: "undo", shownMs, restMs }; } if (step.do === "nudge") { await recording(page, true); From aca81b37f23c42b037e951da47f31c0d048f4830 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 22 Nov 2026 04:27:56 -0500 Subject: [PATCH 4/4] test(studio): the bench's rest wait takes its clock, so its times are exact in tests --- .../tests/e2e/edit-accuracy/sequences.mjs | 14 +++++++---- .../tests/e2e/edit-accuracy/settle.test.mjs | 24 +++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs index 1cd2e20833a..bb58cc735bd 100644 --- a/packages/studio/tests/e2e/edit-accuracy/sequences.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/sequences.mjs @@ -453,13 +453,17 @@ const sizeError = (a, b) => * Resolves once `busy()` has read false `frames` reads in a row, a `frame()` apart, with the ms until the first of * those reads; throws at `timeout` ms. */ -export async function restedFor(busy, frame, { frames = 2, timeout = 20_000 } = {}) { - const start = Date.now(); +export async function restedFor( + busy, + frame, + { frames = 2, timeout = 20_000, now = Date.now } = {}, +) { + const start = now(); let [clear, clearSince] = [0, 0]; - for (const deadline = start + timeout; Date.now() < deadline; await frame()) { - const now = Date.now(); + for (const deadline = start + timeout; now() < deadline; await frame()) { + const readAt = now(); clear = (await busy()) ? 0 : clear + 1; - if (clear === 1) clearSince = now; + if (clear === 1) clearSince = readAt; if (clear >= frames) return clearSince - start; } throw new Error("an edit was still waiting or saving"); diff --git a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs index 4d0c610e0b0..ee8d652f3a6 100644 --- a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs @@ -293,27 +293,31 @@ describe("the box a press is measured against", () => { // fails without the fix: the sequence read the file once its write count was met, while a sealed press that waited for // the previous save was still saving, so the last gesture's edit looked lost. describe("the end of a sequence", () => { + // Each frame advances a fake clock by 10 ms, so the times are exact. const reads = (states) => { - let at = 0; - const busy = async () => states[Math.min(at++, states.length - 1)]; - return { busy, read: () => at }; + let [at, ms] = [0, 0]; + return { + busy: async () => states[Math.min(at++, states.length - 1)], + frame: async () => void (ms += 10), + now: () => ms, + read: () => at, + }; }; it("waits until no press waits and no edit saves, two reads in a row", async () => { - const { busy, read } = reads([true, true, false, true, false, false, true]); - await restedFor(busy, async () => {}); + const { busy, frame, now, read } = reads([true, true, false, true, false, false, true]); + await restedFor(busy, frame, { now }); expect(read()).toBe(6); }); it("says how long until the first of the clear reads, the undo's time to show", async () => { - const { busy } = reads([true, true, false, false]); - const ms = await restedFor(busy, () => new Promise((r) => setTimeout(r, 10))); - expect(ms).toBeGreaterThanOrEqual(20); + const { busy, frame, now } = reads([true, true, false, false]); + expect(await restedFor(busy, frame, { now })).toBe(20); }); it("gives up with a reason when an edit never stops saving", async () => { - const { busy } = reads([true]); - await expect(restedFor(busy, async () => {}, { timeout: 30 })).rejects.toThrow( + const { busy, frame, now } = reads([true]); + await expect(restedFor(busy, frame, { now, timeout: 30 })).rejects.toThrow( /still waiting or saving/, ); });