diff --git a/packages/studio/tests/e2e/edit-accuracy/case.mjs b/packages/studio/tests/e2e/edit-accuracy/case.mjs index 727a7e5fe6..7f1321d89d 100644 --- a/packages/studio/tests/e2e/edit-accuracy/case.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/case.mjs @@ -313,16 +313,18 @@ const previewFrames = (page) => /** A hidden preview holding the target is a shadow reload not yet promoted: the visible frame is about to go stale. */ async function hiddenTarget(page, selector = "#target") { - for (const f of page.frames().filter((f) => f.url().includes("/preview"))) { - const host = await f.frameElement().catch(() => null); - const shown = await host - ?.evaluate((e) => e.checkVisibility({ visibilityProperty: true })) - .catch(() => null); - if (shown === false && (await f.$(selector).catch(() => null))) return true; - } + for (const f of page.frames().filter((f) => f.url().includes("/preview"))) + if (await hiddenIn(f, selector).catch(() => false)) return true; return false; } +// Async so a frame detached mid-scan rejects: puppeteer throws that synchronously, before any `.catch`. +async function hiddenIn(frame, selector) { + const host = await frame.frameElement(); + const shown = await host?.evaluate((e) => e.checkVisibility({ visibilityProperty: true })); + return shown === false && Boolean(await frame.$(selector)); +} + // Studio's own word that the preview is about to change: a reload asked for, loading, or an undo or redo in flight. const previewChanging = (page) => Promise.resolve() @@ -389,26 +391,44 @@ const callTool = (page, name, args) => */ // fallow-ignore-next-line complexity export async function seekBound(ctx) { + let unmet; for (const deadline = Date.now() + 30_000; Date.now() < deadline; await sleep(50)) { const look = await callTool(ctx.page, "studio_look", {}); - if (!(look?.ok && look.duration > 0)) continue; + if (!(look?.ok && look.duration > 0)) { + unmet = `studio_look duration > 0, last ${JSON.stringify(lookSummary(look))}`; + continue; + } const seek = await callTool(ctx.page, "studio_seek", { time: ctx.playhead }); - if ( - seek?.ok && - seek.duration > 0 && - seek.playhead === ctx.playhead && - (await findTarget(ctx.page)) - ) - return seek; + if (!(seek?.ok && seek.duration > 0 && seek.playhead === ctx.playhead)) { + unmet = `studio_seek to ${ctx.playhead}, last ${JSON.stringify(seek)}`; + continue; + } + if (await findTarget(ctx.page)) return seek; + unmet = `#target in a shown preview, last frames ${previewFrames(ctx.page) || "none"}`; } - return null; + throw new Error(`studio never reported a seekable composition: ${unmet}`); } -/** Ready once Studio's own seek tool reports the composition and the playhead landed. */ +const lookSummary = (look) => + look?.ok ? { duration: look.duration, playhead: look.playhead, scene: look.sceneStatus } : look; + +/** Runs in the page: Studio's own word that the composition loaded, a duration from its timeline bind. */ +const compositionLoaded = async () => + Boolean(window.__editBench?.has("studio_look")) && + (await window.__editBench.call("studio_look", {})).duration > 0; + +/** Ready once Studio reports the composition loaded and its own seek tool lands the playhead. */ export async function openStudio(ctx) { ctx.handles = null; - await ctx.page.waitForFunction(() => window.__editBench?.has("studio_seek"), { timeout: 90_000 }); - if (!(await seekBound(ctx))) throw new Error("studio never reported a seekable composition"); + // The whole boot shares one budget: under load the composition alone has taken over 20 s after a reload. + await ctx.page + .waitForFunction(compositionLoaded, { timeout: 90_000, polling: 100 }) + .catch(async (error) => { + const look = JSON.stringify(lookSummary(await callTool(ctx.page, "studio_look", {}))); + const message = `studio never reported the composition loaded: last ${look}`; + throw new Error(message, { cause: error }); + }); + await seekBound(ctx); return settled(ctx); } diff --git a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs index f284d4779c..d357b7d1d5 100644 --- a/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs +++ b/packages/studio/tests/e2e/edit-accuracy/settle.test.mjs @@ -8,6 +8,7 @@ import { heldStill, measureShown, networkBusy, + openStudio, seekBound, swapPending, unsettledBy, @@ -66,6 +67,16 @@ describe("settling on the shown preview", () => { expect(await swapPending({ page: page(gone), keys })).toBe(false); }); + it("reads a frame detached before the read as holding nothing: puppeteer throws that synchronously", async () => { + const detached = () => { + throw new Error("Attempted to use detached Frame"); + }; + const gone = { url: () => "/preview/a?_t=1", frameElement: detached, $: detached }; + const hostGone = { ...frame("/preview/a?_t=1", false, true), $: detached }; + expect(await swapPending({ page: page(gone), keys })).toBe(false); + expect(await swapPending({ page: page(hostGone), keys })).toBe(false); + }); + it("calls a swap pending while Studio says the preview is changing, with no hidden preview yet", async () => { const changing = (on) => ({ frames: () => [], evaluate: async () => on }); expect(await swapPending({ page: changing(true), keys })).toBe(true); @@ -202,6 +213,31 @@ describe("seeking to the playhead after a reload", () => { delete globalThis.window; } }); + + it("waits in the page for Studio to report the composition loaded, and names its last report if it never does", async () => { + let loaded; + const page = { + waitForFunction: async (fn) => { + loaded = fn; + throw new Error("Waiting failed: 90000ms exceeded"); + }, + evaluate: async () => ({ ok: true, duration: 0, playhead: 0, sceneStatus: "loading" }), + }; + await expect(openStudio({ page })).rejects.toThrow( + 'studio never reported the composition loaded: last {"duration":0,"playhead":0,"scene":"loading"}', + ); + let duration = 0; + globalThis.window = {}; + try { + expect(await loaded(), "before Studio registers its tools").toBe(false); + window.__editBench = { has: () => true, call: async () => ({ ok: true, duration }) }; + expect(await loaded(), "before the timeline bind").toBe(false); + duration = 4; + expect(await loaded()).toBe(true); + } finally { + delete globalThis.window; + } + }); }); describe("the size auto-record keys hold", () => {