fix(studio): the edit bench reads a sealed preview through its editing port (#8010)

* test(studio): the edit bench measures an element inside a sealed preview

* fix(studio): the edit bench reads a sealed preview through its editing port

* fix(studio): the edit bench drops a preview reply that lands after a release or the next drag

* fix(studio): the edit bench pairs each frame's pushed geometry with the pointer at that moment

* fix(studio): the edit bench pairs Studio's outlines and the watched element by time, not arrival

* fix(studio): the edit bench starts a drag only once the preview has measured the element
This commit is contained in:
Miguel Ángel
2026-11-17 14:36:04 +00:00
committed by GitHub
parent 0be1a0719c
commit 5f8b7a6eee
3 changed files with 617 additions and 57 deletions
@@ -510,16 +510,11 @@ async function measureSequence({ spec, dir, files, evidence }, session, control,
if (backSince !== null && Date.now() - backSince >= UNDONE_QUIET_MS) break;
}
await page.waitForFunction(
() => {
if (document.querySelector("[data-dom-edit-press-waiting]")) return false;
const frames = Array.from(document.querySelectorAll("iframe, hyperframes-player")).flatMap(
(host) =>
host.shadowRoot ? Array.from(host.shadowRoot.querySelectorAll("iframe")) : [host],
);
return frames.every(
(frame) => !frame.contentDocument?.querySelector("[data-hf-studio-manual-edit-gesture]"),
);
},
// Asked over each preview's editing port: a preview that cannot answer yet is not clear.
async () =>
!document.querySelector("[data-dom-edit-press-waiting]") &&
(await window.__editBenchFrames.matchesInPreviews("[data-hf-studio-manual-edit-gesture]")) ===
false,
{ timeout: 20_000 },
);
await waitForFiles(ctx.A, { timeout: 5000 });
@@ -54,9 +54,12 @@ function linear(node) {
}
/** Border-box size in CSS px: offsetWidth rounds to whole px, and a bounding rect grows with rotation. */
// fallow-ignore-next-line complexity
function boxSize(el) {
const s = el.ownerDocument.defaultView.getComputedStyle(el);
return styleBoxSize(el.ownerDocument.defaultView.getComputedStyle(el));
}
// fallow-ignore-next-line complexity
function styleBoxSize(s) {
const px = (v) => Number.parseFloat(v) || 0;
const edges = (a, b) =>
s.boxSizing === "border-box"
@@ -68,6 +71,18 @@ function boxSize(el) {
return [px(s.width) + edges("Left", "Right"), px(s.height) + edges("Top", "Bottom")];
}
/** A point in the viewport px of iframe `f`'s page, in the px of the page holding `f`. */
function throughFrame(f, [x, y]) {
const fr = f.getBoundingClientRect();
const fs = f.ownerDocument.defaultView.getComputedStyle(f);
const [fw, fh] = boxSize(f);
const d = apply(linear(f), [
x + f.clientLeft + parseFloat(fs.paddingLeft) - fw / 2,
y + f.clientTop + parseFloat(fs.paddingTop) - fh / 2,
]);
return [fr.left + fr.width / 2 + d[0], fr.top + fr.height / 2 + d[1]];
}
/** The element's quad in top-frame px, crossing each iframe through its element's own transform. */
export function quadOf(el, top = el.ownerDocument.defaultView.top) {
const r = el.getBoundingClientRect();
@@ -75,17 +90,8 @@ export function quadOf(el, top = el.ownerDocument.defaultView.top) {
let m = linear(el);
for (let win = el.ownerDocument.defaultView; win !== top; win = win.parent) {
const f = win.frameElement;
const fr = f.getBoundingClientRect();
const fm = linear(f);
const fs = f.ownerDocument.defaultView.getComputedStyle(f);
const [fw, fh] = boxSize(f);
const inset = [
f.clientLeft + parseFloat(fs.paddingLeft),
f.clientTop + parseFloat(fs.paddingTop),
];
const d = apply(fm, [c[0] + inset[0] - fw / 2, c[1] + inset[1] - fh / 2]);
c = [fr.left + fr.width / 2 + d[0], fr.top + fr.height / 2 + d[1]];
m = mul(fm, m);
c = throughFrame(f, c);
m = mul(linear(f), m);
}
const [w, h] = boxSize(el);
const corner = (u, v) => {
@@ -101,8 +107,20 @@ export function quadOf(el, top = el.ownerDocument.defaultView.top) {
*/
function frameSampler() {
if (window.top !== window) return;
const rec = { on: false, selector: null, pointer: null, down: false, samples: [] };
const rec = {
on: false,
selector: null,
pointer: null,
down: false,
samples: [],
marks: [],
since: -Infinity,
history: [],
overlayHistory: [],
};
window.__editBenchFrames = rec;
// The clock each preview stamps its geometry pushes on: the same instant reads the same in every document.
const clock = () => performance.timeOrigin + performance.now();
for (const type of ["pointerdown", "pointermove", "pointerup"])
window.addEventListener(
type,
@@ -112,6 +130,7 @@ function frameSampler() {
rec.pointer = [e.clientX, e.clientY];
// From the buttons, not the event type: a move after release is not a drag.
rec.down = (e.buttons & 1) === 1;
rec.history.push({ at: clock(), pointer: rec.pointer, down: rec.down });
// Kept as it happens, so a release that no frame painted still ends the drag at its own point.
if (type === "pointerup" && rec.on)
rec.samples.push({ t: performance.now(), up: rec.pointer });
@@ -119,37 +138,176 @@ function frameSampler() {
true,
);
// Studio's previews sit in <hyperframes-player> shadow roots, which window.frames does not list.
const previewWindows = () =>
const previewFrames = () =>
Array.from(document.querySelectorAll("iframe, hyperframes-player"))
.flatMap((e) => (e.shadowRoot ? Array.from(e.shadowRoot.querySelectorAll("iframe")) : [e]))
.map((e) => e.contentWindow)
.filter((f) => {
try {
return f.location.pathname.includes("/preview");
} catch {
return false; // cross-origin
}
.filter((f) => /\/preview/.test(new URL(f.src || "about:blank").pathname));
// A sealed preview's page is out of reach, so each is read over an editing port of the bench's own. When Studio
// takes the same handoff both listen on that port; ids from 1e9 up never meet Studio's.
const ROOT = '[data-composition-id="main"]';
const SIZE = ["width", "height", "box-sizing", "clip-path"].concat(
...["left", "right", "top", "bottom"].map((s) => [`padding-${s}`, `border-${s}-width`]),
);
const links = new Map();
let lastId = 1e9;
const linkOf = (f) => {
if (!links.has(f))
links.set(f, {
win: f.contentWindow,
port: null,
asked: -1e9,
pending: new Map(),
handles: {},
watched: null,
for: null,
confirmed: false,
pushes: [],
styles: null,
styling: false,
});
// The largest visible preview holding the element; Studio loads edits in a hidden shadow frame.
// fallow-ignore-next-line complexity
const findElement = (selector) => {
let best = null;
for (const f of previewWindows()) {
const el = f.document.querySelector(selector);
const fe = f.frameElement;
if (!el || !fe.checkVisibility({ visibilityProperty: true, opacityProperty: true })) continue;
const r = fe.getBoundingClientRect();
if (!best || r.width * r.height > best.area) best = { el, area: r.width * r.height };
}
// A page with no preview (the blank-page control) holds its element itself.
return best?.el ?? document.querySelector(selector);
return links.get(f);
};
// fallow-ignore-next-line complexity
const read = () => {
const el = findElement(rec.selector);
const root = el?.ownerDocument.querySelector('[data-composition-id="main"]');
const takePort = (l, port) => {
l.port = port;
port.addEventListener("message", ({ data }) =>
data?.event === "geometry"
? l.confirmed && l.pushes.push(data)
: l.pending.get(data?.id)?.(data),
);
port.start();
};
const isHandoff = (e) => e.data.type === "editing-port" && e.data.v === 1 && e.ports.length === 1;
const heard = (l, e) => {
// A new page in the frame: the old port and its handles went with the old one.
if (e.data.type === "ready")
Object.assign(l, { port: null, handles: {}, watched: null, pushes: [] });
else if (isHandoff(e) && !l.port) takePort(l, e.ports[0]);
};
window.addEventListener("message", (e) => {
if (e.data?.source !== "hf-preview") return;
for (const l of links.values()) if (l.win === e.source) heard(l, e);
});
const ask = (l, kind, params) =>
new Promise((resolve) => {
if (!l.port) {
// A connect while Studio waits for its own handoff could take Studio's: asked at most twice a second.
if (performance.now() - l.asked > 500) {
l.asked = performance.now();
l.win?.postMessage({ source: "hf-parent", type: "editing-connect", v: 1 }, "*");
}
return resolve(null);
}
const id = ++lastId;
const timer = setTimeout(() => (l.pending.delete(id), (l.port = null), resolve(null)), 1000);
l.pending.set(id, (reply) => {
clearTimeout(timer);
l.pending.delete(id);
resolve(reply.ok ? reply.result : null);
});
l.port.postMessage({ v: 1, id, kind, params });
});
const handleOf = async (l, selector) =>
(l.handles[selector] ??= (await ask(l, "query", { selector }))?.matches?.[0]?.handle ?? null);
const forget = (l) => Object.assign(l, { handles: {}, watched: null, styles: null });
// Each preview pushes the element's and the root's geometry as it paints; the size is asked beside it.
const watchIn = async (f, selector) => {
const l = linkOf(f);
// Before any await: a push for the previous element must not land in this one's frames.
if (l.for !== selector) Object.assign(l, { for: selector, confirmed: false, pushes: [] });
const [el, root] = [await handleOf(l, selector), await handleOf(l, ROOT)];
if (!l.port || !el || !root) return;
if (l.watched !== `${el} ${root}`) {
// Port messages keep their order: every push before this request's reply is the previous watch's.
Object.assign(l, { watched: `${el} ${root}`, pushes: [], styles: null, confirmed: false });
void ask(l, "watch-geometry", { handles: [el, root] }).then(
(reply) => (l.confirmed = !!reply),
);
}
if (l.styling) return;
l.styling = true;
const reply = await ask(l, "computed-styles", { handle: el, properties: SIZE });
l.styling = false;
if (reply?.styles) l.styles = reply.styles;
};
// Resolves once a frame measured the element, so a drag's first sample is before the press, as a direct read's is.
rec.measured = async (from, ms = 3000) => {
const end = performance.now() + ms;
while (!rec.samples.slice(from).some((s) => s.quad) && performance.now() < end)
await new Promise(requestAnimationFrame);
};
rec.unwatch = () => {
for (const l of links.values())
if (l.watched) void ask(forget(l), "watch-geometry", { handles: [] });
};
const cornersOf = ({ rect: r, corners: c }) =>
c
? [c.nw, c.ne, c.se, c.sw].map((p) => [p.x, p.y])
: [
[r.x, r.y],
[r.x + r.width, r.y],
[r.x + r.width, r.y + r.height],
[r.x, r.y + r.height],
];
const camel = (styles) =>
Object.fromEntries(
Object.entries(styles).map(([k, v]) => [k.replace(/-(.)/g, (_, c) => c.toUpperCase()), v]),
);
const shownPreview = (frames) => {
const shown = frames.filter((f) =>
f.checkVisibility({ visibilityProperty: true, opacityProperty: true }),
);
const area = (f) => f.getBoundingClientRect().width * f.getBoundingClientRect().height;
// The largest shown preview holding the element; Studio loads edits in a hidden shadow frame. One shown and
// larger with no port yet holds it somewhere no push says, so nothing is sampled until it answers.
const [first] = shown
.filter((f) => !linkOf(f).port || linkOf(f).watched)
.sort((a, b) => area(b) - area(a));
return first && linkOf(first).port ? first : null;
};
const historyAt = (at) =>
rec.history.findLast((h) => h.at <= at) ?? { pointer: null, down: false };
// Studio's outlines as this page painted them at a push's stamp, read like the pointer: by time.
const overlaysAt = (at) => rec.overlayHistory.findLast((o) => o.at <= at)?.shown ?? {};
const overlays = () => {
const outline = document.querySelector("[data-dom-edit-crop-frame] > div.border-dashed");
const waiting = document.querySelector("[data-dom-edit-press-waiting]");
return {
...(outline && { outline: quadOf(outline) }),
...(waiting && { waitingQuad: quadOf(waiting) }),
};
};
// Each push becomes a sample paired with the pointer at the push's own stamp, so a late push is never scored
// against a later pointer, and one from before this drag started is not this drag's.
const sampleFrom = (f) => {
const l = linkOf(f);
for (const { at, elements } of l.pushes.splice(0)) {
const [box, rootBox] = elements;
if (!box || !rootBox) return void forget(l);
if (
![box, rootBox].every(({ rect: r }) => [r.x, r.y, r.width, r.height].every(Number.isFinite))
)
continue;
if (at < rec.since || at < shown.since) continue;
const lift = (b) => cornersOf(b).map((p) => throughFrame(f, p));
const { pointer, down } = historyAt(at);
rec.samples.push({
t: at - performance.timeOrigin,
pointer,
down,
root: lift(rootBox),
...(l.styles && {
quad: lift(box),
size: styleBoxSize(camel(l.styles)),
clip: box.clipPath || "none",
}),
...overlaysAt(at),
});
}
};
// A page with no preview (the blank-page control) holds its element itself, read as it paints.
const sampleHere = (selector) => {
const el = document.querySelector(selector);
const root = el?.ownerDocument.querySelector(ROOT);
rec.samples.push({
t: performance.now(),
pointer: rec.pointer,
@@ -160,18 +318,37 @@ function frameSampler() {
size: boxSize(el),
clip: el.ownerDocument.defaultView.getComputedStyle(el).clipPath,
}),
...(outline && { outline: quadOf(outline) }),
...(waiting && { waitingQuad: quadOf(waiting) }),
...overlays(),
});
};
// ResizeObserver runs after every document's rAF callbacks and layout, just before paint, so it reads
// what the frame shows. A task after paint can see a state no frame painted.
const shown = { frame: null, since: -Infinity };
const sample = () => {
const frames = previewFrames();
if (!frames.length) return sampleHere(rec.selector);
for (const f of frames) void watchIn(f, rec.selector);
rec.overlayHistory.push({ at: clock(), shown: overlays() });
const f = shownPreview(frames);
for (const other of frames) if (other !== f) linkOf(other).pushes.length = 0;
// A preview only just shown painted its queued pushes while another one was the one on screen.
if (f !== shown.frame) Object.assign(shown, { frame: f, since: clock() });
if (f) sampleFrom(f);
};
// True when a preview holds a match, false when every preview answered without one, null while one cannot answer.
rec.matchesInPreviews = async (selector) => {
const replies = await Promise.all(
previewFrames().map((f) => ask(linkOf(f), "query", { selector })),
);
if (replies.some((r) => r?.matches?.length)) return true;
return replies.every(Boolean) ? false : null;
};
// ResizeObserver runs after every document's rAF callbacks and layout, just before paint; each one asks for the
// frame it fires on, and one ask at a time keeps the replies in order.
const tick = document.createElement("div");
tick.style.cssText = "position: fixed; height: 1px; opacity: 0; pointer-events: none";
new ResizeObserver(() => {
if (!rec.on) return;
try {
read();
sample();
} catch (error) {
rec.samples.push({ t: performance.now(), error: String(error) });
}
@@ -188,7 +365,7 @@ function frameSampler() {
/** The page script with the geometry it calls, for evaluateOnNewDocument; scoped so no page global clashes. */
export const frameSamplerScript = `(() => {
${[mul, apply, rotation, ownLinear, parentOf, linear, boxSize, quadOf].map(String).join("\n")}
${[mul, apply, rotation, ownLinear, parentOf, linear, boxSize, styleBoxSize, throughFrame, quadOf].map(String).join("\n")}
(${frameSampler})();
})();`;
@@ -196,9 +373,12 @@ ${[mul, apply, rotation, ownLinear, parentOf, linear, boxSize, quadOf].map(Strin
export const startFrames = (page, selector) =>
page.evaluate((sel) => {
const rec = window.__editBenchFrames;
if (!rec.on) Object.assign(rec, { on: true, samples: [], marks: [] });
if (!rec.on)
Object.assign(rec, { on: true, samples: [], marks: [], history: [], overlayHistory: [] });
rec.selector = sel;
rec.marks.push(rec.samples.length);
rec.since = performance.timeOrigin + performance.now();
return rec.measured(rec.marks.at(-1));
}, selector);
/** Stops recording: one window of frames per drag, each running until the next drag's mark. */
@@ -206,6 +386,7 @@ export async function stopFrames(page) {
const { samples, marks } = await page.evaluate(() => {
const rec = window.__editBenchFrames;
rec.on = false;
rec.unwatch();
return { samples: rec.samples, marks: rec.marks ?? [] };
});
return marks.map((m, i) => samples.slice(m, marks[i + 1] ?? samples.length));
@@ -1,6 +1,6 @@
import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vitest";
import { frameSamplerScript, quadOf, scoreTeleport } from "./teleport.mjs";
import { frameSamplerScript, quadOf, scoreTeleport, startFrames } from "./teleport.mjs";
import { entry, score } from "./report.mjs";
// The composition root drawn 1:1 at the origin, and a 100 px box whose centre the pointer holds.
@@ -256,3 +256,387 @@ describe("frameSamplerScript", () => {
expect(runInNewContext("[linear, quadOf]", page)).toEqual([1, 2]);
});
});
describe("frameSamplerScript in a sealed preview", () => {
const style = {
transform: "none",
rotate: "none",
scale: "none",
width: "1920px",
height: "1080px",
boxSizing: "content-box",
paddingLeft: "0px",
paddingRight: "0px",
paddingTop: "0px",
paddingBottom: "0px",
borderLeftWidth: "0px",
borderRightWidth: "0px",
borderTopWidth: "0px",
borderBottomWidth: "0px",
};
const topDocument = { querySelector: () => null };
const topWindow = { getComputedStyle: () => style };
topWindow.top = topWindow;
topDocument.defaultView = topWindow;
const BOXES = {
"#target": { rect: { x: 100, y: 200, width: 100, height: 50 }, angle: 0, corners: null },
'[data-composition-id="main"]': {
rect: { x: 0, y: 0, width: 1920, height: 1080 },
angle: 0,
corners: null,
},
};
const geometry = (handle, x = 0) => {
const { rect, ...rest } = BOXES[handle];
return {
...rest,
rect: { ...rect, x: rect.x + x },
source: null,
clipPath: null,
visible: true,
members: null,
};
};
// The frame's document and location throw, as a cross-origin frame's do; only postMessage reaches it.
function sealedFrame(studioReplies, watchHeld = null) {
const channel = new MessageChannel();
const asked = [];
channel.port1.onmessage = async ({ data: { id, kind, params } }) => {
asked.push({ kind, params });
if (kind === "watch-geometry" && watchHeld) await watchHeld;
const result =
kind === "query"
? {
matches: BOXES[params.selector] ? [{ handle: params.selector }] : [],
truncated: false,
}
: kind === "watch-geometry"
? { watching: params.handles.length }
: kind === "computed-styles"
? { styles: { width: "100px", height: "50px", "box-sizing": "border-box" } }
: null;
channel.port1.postMessage({ v: 1, id, ok: true, result });
};
const win = {
get document() {
throw new Error("cross-origin");
},
get location() {
throw new Error("cross-origin");
},
postMessage(message) {
if (message.type !== "editing-connect") return;
// Studio already holds the port when the bench's handoff arrives: both listen on it.
channel.port2.onmessage = ({ data }) => studioReplies.push(data);
queueMicrotask(() =>
on.message({
source: win,
data: { source: "hf-preview", type: "editing-port", v: 1 },
ports: [channel.port2],
}),
);
},
};
const on = {};
const iframe = {
src: "http://studio.test/preview/index.html",
contentWindow: win,
ownerDocument: topDocument,
clientLeft: 0,
clientTop: 0,
parentElement: null,
getRootNode: () => ({}),
checkVisibility: () => true,
getBoundingClientRect: () => ({ left: 0, top: 0, width: 1920, height: 1080 }),
};
// A push as the frame sends one each time it paints, stamped `at` on the shared clock.
const push = (at, x = 0) =>
channel.port1.postMessage({
v: 1,
event: "geometry",
at,
elements: [geometry("#target", x), geometry('[data-composition-id="main"]')],
});
const close = () => (channel.port1.close(), channel.port2.close());
return { iframe, on, asked, push, close, channel };
}
// Runs the page script in a top page whose previews are `iframe`; ticks stand in for painted frames.
function samplerFor(iframe, on) {
let tick = () => {};
const clock = { now: 0 };
const studio = { waiting: null };
const frames = [];
const window = { addEventListener: (type, f) => (on[type] = f) };
window.top = window;
const page = {
window,
document: {
querySelectorAll: () => [].concat(iframe),
querySelector: (selector) => (selector.includes("press-waiting") ? studio.waiting : null),
createElement: () => ({ style: {}, isConnected: true }),
documentElement: { append() {} },
},
ResizeObserver: class {
constructor(f) {
tick = f;
}
observe() {}
},
requestAnimationFrame: (f) => frames.push(f),
performance: { timeOrigin: 1_000, now: () => clock.now },
setTimeout,
clearTimeout,
URL,
DOMMatrix: class {},
};
runInNewContext(frameSamplerScript, page);
const rec = window.__editBenchFrames;
Object.assign(rec, { on: true, selector: "#target", samples: [], marks: [0], history: [] });
const settle = () => new Promise((resolve) => setTimeout(resolve, 5));
// Ticks until the frame is asked to watch, so its pushes have somewhere to land.
const watching = async (asked) => {
for (let i = 0; i < 50 && !asked.some((a) => a.kind === "watch-geometry"); i++) {
tick();
await settle();
}
for (let i = 0; i < 5; i++) {
tick();
await settle();
}
};
const move = (x, buttons = 1) =>
on.pointermove({ isTrusted: true, clientX: x, clientY: 225, buttons });
const frame = () => frames.splice(0).forEach((f) => f());
const pageApi = {
evaluate: (f, arg) => runInNewContext(`(${f})(${JSON.stringify(arg)})`, page),
};
return { rec, tick, clock, settle, watching, move, studio, frame, pageApi };
}
it("measures the element from the preview's pushes, in top-frame px, and Studio keeps the shared port", async () => {
const studioReplies = [];
const { iframe, on, asked, push, close, channel } = sealedFrame(studioReplies);
const { rec, tick, settle, watching, move } = samplerFor(iframe, on);
try {
move(150);
await watching(asked);
expect(asked.find((a) => a.kind === "watch-geometry").params.handles).toEqual([
"#target",
'[data-composition-id="main"]',
]);
push(1_000);
await settle();
tick();
const sample = rec.samples.find((s) => s.quad);
expect(sample, JSON.stringify(rec.samples.slice(-2))).toBeDefined();
expect(sample.quad).toEqual([
[100, 200],
[200, 200],
[200, 250],
[100, 250],
]);
expect(sample.size).toEqual([100, 50]);
expect(sample.root[2]).toEqual([1920, 1080]);
expect(sample.pointer).toEqual([150, 225]);
channel.port2.postMessage({ v: 1, id: 1, kind: "query", params: { selector: "#target" } });
await new Promise((resolve) => setTimeout(resolve, 20));
expect(studioReplies.map((r) => r.id)).toContain(1);
} finally {
close();
}
});
it("pairs each push with the pointer at its own stamp, not where the pointer is when it lands", async () => {
const { iframe, on, asked, push, close } = sealedFrame([]);
const { rec, tick, clock, settle, watching, move } = samplerFor(iframe, on);
try {
await watching(asked);
clock.now = 10;
move(150);
clock.now = 20;
move(160);
clock.now = 30;
move(170);
// Painted at 15 and 25, both landing after the pointer reached 170.
push(1_015);
push(1_025, 10);
await settle();
tick();
const measured = rec.samples.filter((s) => s.quad);
expect(measured.map((s) => [s.t, s.pointer[0], s.quad[0][0]])).toEqual([
[15, 150, 100],
[25, 160, 110],
]);
} finally {
close();
}
});
it("leaves out a push painted before this drag started", async () => {
const { iframe, on, asked, push, close } = sealedFrame([]);
const { rec, tick, settle, watching, move } = samplerFor(iframe, on);
try {
move(150);
await watching(asked);
rec.since = 1_050;
push(1_040);
push(1_060);
await settle();
tick();
expect(rec.samples.filter((s) => s.quad).map((s) => s.t)).toEqual([60]);
} finally {
close();
}
});
it("starts recording only once a frame has measured the element, so a drag's first sample is before the press", async () => {
const { iframe, on, asked, push, close } = sealedFrame([]);
const { rec, tick, settle, watching, move, frame, pageApi } = samplerFor(iframe, on);
try {
move(150);
let ready = false;
void startFrames(pageApi, "#target").then(() => (ready = true));
await watching(asked);
frame();
await settle();
expect(ready).toBe(false);
push(1_010);
await settle();
tick();
frame();
await settle();
expect([ready, rec.samples.filter((s) => s.quad).length]).toEqual([true, 1]);
} finally {
close();
}
});
it("drops pushes that land before the frame confirms the new watch, and pushes with a box that is not finite", async () => {
let confirm = () => {};
const held = new Promise((resolve) => (confirm = resolve));
const { iframe, on, asked, push, close } = sealedFrame([], held);
const { rec, tick, settle, move } = samplerFor(iframe, on);
try {
move(150);
for (let i = 0; i < 50 && !asked.some((a) => a.kind === "watch-geometry"); i++) {
tick();
await settle();
}
push(1_010);
await settle();
confirm();
await settle();
push(1_020, Number.NaN);
push(1_030);
await settle();
tick();
expect(rec.samples.filter((s) => s.quad).map((s) => s.t)).toEqual([30]);
} finally {
close();
}
});
it("takes a newly shown preview's pushes only from the moment it is the one on screen", async () => {
const first = sealedFrame([]);
const second = sealedFrame([]);
let shownFrame = first.iframe;
for (const x of [first, second]) x.iframe.checkVisibility = () => x.iframe === shownFrame;
const { rec, tick, clock, settle, move } = samplerFor([first.iframe, second.iframe], first.on);
Object.assign(second.on, first.on);
try {
move(150);
for (
let i = 0;
i < 50 && ![first, second].every((x) => x.asked.some((a) => a.kind === "watch-geometry"));
i++
) {
tick();
await settle();
}
clock.now = 100;
// Studio swaps previews: the second, hidden until now, painted at 90 while the first was on screen.
second.push(1_090, 50);
shownFrame = second.iframe;
await settle();
tick();
second.push(1_110);
await settle();
tick();
expect(rec.samples.filter((s) => s.quad).map((s) => s.t)).toEqual([110]);
} finally {
first.close();
second.close();
}
});
it("pairs each push with the outline Studio showed at its stamp, not when the push lands", async () => {
const { iframe, on, asked, push, close } = sealedFrame([]);
const { rec, tick, clock, settle, watching, move, studio } = samplerFor(iframe, on);
const outline = {
ownerDocument: topDocument,
parentElement: null,
getRootNode: () => ({}),
getBoundingClientRect: () => ({ left: 0, top: 0, width: 1920, height: 1080 }),
};
try {
move(150);
await watching(asked);
clock.now = 10;
tick();
// Painted at 15, before Studio showed its waiting outline at 20; it lands after.
push(1_015);
clock.now = 20;
studio.waiting = outline;
await settle();
tick();
push(1_025);
clock.now = 30;
await settle();
tick();
const measured = rec.samples.filter((s) => s.quad);
expect(measured.map((s) => [s.t, Boolean(s.waitingQuad)])).toEqual([
[15, false],
[25, true],
]);
} finally {
close();
}
});
it("stops every watch when recording stops", async () => {
const { iframe, on, asked, close } = sealedFrame([]);
const { rec, settle, watching } = samplerFor(iframe, on);
try {
await watching(asked);
rec.unwatch();
await settle();
expect(asked.at(-1)).toEqual({ kind: "watch-geometry", params: { handles: [] } });
} finally {
close();
}
});
it("leaves a frame unmeasured while a larger shown preview cannot answer yet", async () => {
const { iframe, on, asked, push, close } = sealedFrame([]);
// Studio's next preview, shown and larger, before its page has handed out a port.
const next = {
...iframe,
contentWindow: { postMessage() {} },
getBoundingClientRect: () => ({ left: 0, top: 0, width: 2400, height: 1350 }),
};
const { rec, tick, settle, watching, move } = samplerFor([iframe, next], on);
try {
move(150);
await watching(asked);
push(1_000);
await settle();
tick();
expect(rec.samples.filter((s) => s.quad)).toEqual([]);
} finally {
close();
}
});
});