mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
test(studio): the edit-accuracy bench reads what a person ends with after sealed saves and undos (#8475)
* 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. * test(studio): a sequence's undo step waits until the undo is shown and records how long it took * test(studio): an undo step records when the box first moves apart from when Studio is idle * test(studio): the bench's rest wait takes its clock, so its times are exact in tests
This commit is contained in:
@@ -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<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);
|
||||
|
||||
@@ -87,7 +87,11 @@ 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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
@@ -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 () =>
|
||||
|
||||
@@ -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,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/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user