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
28 changes: 28 additions & 0 deletions packages/studio/src/utils/studioPendingEdits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
addStudioPendingEditFlushListener,
adoptingStudioPendingEdit,
beginStudioPendingEdit,
beginStudioShownRestore,
flushStudioPendingEdits,
afterStudioPendingEdits,
hasStudioPendingEdits,
Expand Down Expand Up @@ -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<void>((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);
Expand Down
15 changes: 14 additions & 1 deletion packages/studio/src/utils/studioPendingEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,25 @@ export function beginStudioShownRestore(): () => void {
let end = () => {};
const landed = new Promise<void>((resolve) => (end = resolve));
shownRestores.add(landed);
void landed.then(() => shownRestores.delete(landed));
markEditsSavingOnPage();
void landed.then(() => {
shownRestores.delete(landed);
markEditsSavingOnPage();
});
return end;
}

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 = () => {
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 47 additions & 3 deletions packages/studio/tests/e2e/edit-accuracy/sequences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +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");
// A key's DOM change reaches the CDP quads only after a frame; the next step reads from them.
await nextFrame(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" };
return { do: "undo", shownMs, restMs };
}
if (step.do === "nudge") {
await recording(page, true);
Expand Down Expand Up @@ -439,6 +449,35 @@ 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, with the ms until the first of
* those reads; throws at `timeout` ms.
*/
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; now() < deadline; await frame()) {
const readAt = now();
clear = (await busy()) ? 0 : clear + 1;
if (clear === 1) clearSince = readAt;
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, 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-preview-changing"),
);

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;
Expand Down Expand Up @@ -517,6 +556,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(
() => studioBusy(page),
() => nextFrame(page),
);
await page.waitForFunction(
// Asked over each preview's editing port: a preview that cannot answer yet is not clear.
async () =>
Expand Down
35 changes: 34 additions & 1 deletion packages/studio/tests/e2e/edit-accuracy/settle.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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") => ({
Expand Down Expand Up @@ -289,3 +289,36 @@ 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", () => {
// Each frame advances a fake clock by 10 ms, so the times are exact.
const reads = (states) => {
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, 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, 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, frame, now } = reads([true]);
await expect(restedFor(busy, frame, { now, timeout: 30 })).rejects.toThrow(
/still waiting or saving/,
);
});
});
Loading