Files
hyperframes/packages/studio/tests/chromeTestUtils.ts
T
Miguel Ángel c3a6739903 fix(studio): a sealed undo of a GSAP-animated layer's style shows in place (#8320)
* fix(studio): a sealed undo of a GSAP-animated layer's style shows in place, without a reload

* fix(studio): a sealed undo refused after a later one reloads instead of replaying the older state

* fix(studio): a sealed undo's script re-run travels in its restore request, so no write races it

* fix(studio): import the sealed re-run builder where the undo sends it

* refactor(studio): one shape guard for the restore parser and a named re-run choice

* refactor(studio): the sealed undo attaches its re-run in one helper

* test(studio): count only the restore's own requests to the sealed frame

* fix(studio): a sealed re-run keeps a newer scrub and an unusable re-run keeps the plain restore

* fix(studio): a deferred re-run keeps a newer scrub and a newer edit; sealed preloads MotionPath

* refactor(studio): name a soft reload's mark and its script run

* test(studio): share the Chrome bundle's node shim and read the composition's own script

* fix(studio): a waiting re-run leaves the composition's script until it runs

* fix(studio): the soft-reload wait only swaps timelines a newer run left alone

* test(studio): the soft-reload wait tables drive each step from one map

* fix(studio): one MotionPath loader settles every waiting re-run when the plugin loads

* test(core): a failed MotionPath load is retried by the next re-run

* test(studio): a wait-table step after the swap waits for the swap's render

* fix(studio): only the newest soft-reload run may swap, reload or move the playhead

* fix(studio): a refused sealed tween patch re-runs the script instead of reloading

* test(studio): the playing-through-the-wait row allows the player's frame snap

* fix(studio): a late preview answer acts only when no later re-run carries its change

* test(studio): held-answer rows release a still-waiting run when the page closes

* test(core): a restore says whether it re-ran the script

* test(studio): sealed preview equals a fresh load after late answers in any order

* fix(studio): a late refusal recovers with the whole saved snapshot, attributes and timing

* fix(studio): only an explicit refusal keeps an undo's attributes from being covered

* test(studio): ordering table holds a refusal until its request posts, restores a safe attribute

* test(studio): ordering fixture carries the hf ids Studio's server serves

* fix(studio): a refused retime is never covered by a script-only re-run

* test(studio): ordering table keeps the title undo and restores safe and unsafe attributes

* refactor(studio): a restore's snapshot comes from one helper

* test(studio): a refused request is answered at its own step, as in the retime probe

* test(studio): the ordering runner lives in a shared sequence harness

* test(studio): a patch recovery after a live retime carries the retime's timing

* test(studio): the ordering fixture sends its own script instead of parsing the file

* test(studio): the sequence harness keeps its refusal table private

* test(studio): ordering table generates live retimes, scrubs and port rejections

* test(studio): a harness reload shows the new load at Studio's playhead

* test(studio): the ordering table carries the reviewer's round-8 sequences

* test(studio): the first round-8 sequence holds the live retime's refusal

* test(studio): the ordering table pins the seeds that found each round-8 class

* fix(studio): sealed recoveries keep saved timing, the live playhead and port rejections

* test(studio): sealed mocks answer through previewAnswer; channel row for error vs no reply

* test(studio): the timing sync and script commit mocks answer through previewAnswer

* refactor(studio): a late live retime's recovery is its own step

* test(studio): ordering table generates two-script files, missed deadlines and unanswered requests

* test(studio): the data-start undo restores a clip no script animates

* test(studio): a save that reloads at once counts as one nothing can apply in place

* fix(studio): sealed writes count as covered only by a newer applied run that carried their state

* test(studio): the ordering table pins the seeds that found each round-9 class

* fix(studio): a sealed re-run covers from its send only what it carried

* test(studio): the ordering fixture marks each save as Studio's write does

* test(studio): a two-script sequence may recover only by reloading

* test(studio): the harness reload loads again after a save, as Studio's shadow reload does

* test(studio): the ordering table splits its fixed rows from fresh seeds to fit the flake gate

* test(studio): the ordering fixture keeps its generator private

* fix(studio): a sealed write's file version is its save's, and timing coverage survives a later run

* fix(studio): the ledger keeps each timeline's newest snapshot by its save's version

* test(studio): soft-reload expectations name the save revision where a real save made one

* test(studio): the ordering table pins the seeds that found reversed save responses

* refactor(studio): a sealed restore's send moves to its own module under the file size cap

* test(studio): the wait table settles two waiting runs and a pause before it reads them

* test(studio): the ordering table reverses older timing writes behind a newer data-start undo

* fix(studio): a sealed write older than a file already sent to the preview is not sent

* test(studio): mocked preview channels run the send gate as the real one does

* test(studio): the patch request names its default deadline

* test(studio): the ledger pins an older live retime sent after a newer file

* fix(studio): a sealed tween patch carries its save's version without a snapshot too

* test(studio): the undo row expects the in-place re-run of the undone file

* test(studio): the ordering table loads the newer file between reversed timing syncs

* fix(studio): a new sealed preview load refuses writes older than the file it shows

* refactor(studio): the sequence runner plays a reversed step in its own function
2026-11-21 23:38:57 +00:00

100 lines
4.1 KiB
TypeScript

import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import { inspect } from "node:util";
import puppeteer, { TimeoutError, type Browser, type Page } from "puppeteer-core";
import type { Plugin } from "esbuild";
import { browserVersion, findSystemChrome } from "../vite.browser";
const require = createRequire(import.meta.url);
const LAUNCH_TIMEOUT_MS = 20_000;
const LAUNCH_ATTEMPTS = 2;
/** Headless Chrome with the e2e launcher's flags. A launch stuck past 20 s (seen on Windows runners)
* is killed and tried once more. On Windows Puppeteer reports it as "already running", not a timeout. */
export async function launchTestChrome(
launch: typeof puppeteer.launch = (options) => puppeteer.launch(options),
): Promise<Browser> {
const executablePath = findSystemChrome();
if (!executablePath) throw new Error("no Chrome found: set HYPERFRAMES_BROWSER_PATH");
const options = {
executablePath,
headless: true,
dumpio: true,
args: ["--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu"],
timeout: LAUNCH_TIMEOUT_MS,
};
const version = browserVersion(executablePath) ?? "version unavailable";
for (let attempt = 1; attempt <= LAUNCH_ATTEMPTS; attempt += 1) {
const startedAt = Date.now();
console.info(
`[studio-test-chrome] launch ${attempt}/${LAUNCH_ATTEMPTS}: ${executablePath} (${version})`,
);
const killStuckLaunch = new AbortController();
const deadline = setTimeout(() => killStuckLaunch.abort(), LAUNCH_TIMEOUT_MS);
try {
const browser = await launch({ ...options, signal: killStuckLaunch.signal });
console.info(
`[studio-test-chrome] launch ${attempt}/${LAUNCH_ATTEMPTS} ready after ${Date.now() - startedAt} ms`,
);
return browser;
} catch (error) {
const elapsed = Date.now() - startedAt;
const reason =
error instanceof Error ? error.message : inspect(error, { breakLength: Infinity });
console.error(
`[studio-test-chrome] launch ${attempt}/${LAUNCH_ATTEMPTS} failed after ${elapsed} ms: ${reason}`,
);
const stuck = error instanceof TimeoutError || elapsed >= LAUNCH_TIMEOUT_MS;
if (!stuck || attempt === LAUNCH_ATTEMPTS) throw error;
} finally {
clearTimeout(deadline);
}
}
throw new Error("Chrome launch attempts exhausted");
}
/** Closes a test Chrome, killing it when close hangs (seen on Windows runners) so the test never times out. */
export async function closeTestChrome(
browser: Browser | undefined,
timeoutMs = 5_000,
): Promise<void> {
if (!browser) return;
let timer: ReturnType<typeof setTimeout> | undefined;
const closed = await Promise.race([
browser.close().then(
() => true,
() => false,
),
new Promise<boolean>((resolve) => (timer = setTimeout(resolve, timeoutMs, false))),
]);
clearTimeout(timer);
if (closed) return;
browser.process()?.kill("SIGKILL");
await browser.disconnect().catch(() => undefined);
}
const GSAP_CDN_TAG = /<script src="[^"]*\/gsap\.min\.js"><\/script>/;
/** Shows `html` with the HyperFrames runtime. Its GSAP CDN script tag is replaced by the script itself, so the
* page makes no request at all and `load` waits on nothing outside the page. */
export async function showWithRuntime(page: Page, html: string): Promise<void> {
const gsap = readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8");
await page.setContent(
html.replace(GSAP_CDN_TAG, () => `<script>${gsap}</script>`),
{ waitUntil: "load" },
);
await page.evaluate(readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8"));
await page.waitForFunction(() => "__player" in window);
}
// recast imports these, and its code that uses them is dropped from the bundle. A new
// built-in fails the build instead of turning into an empty module the way Vite makes it.
export const emptyNodeBuiltins: Plugin = {
name: "empty-node-builtins",
setup(build) {
build.onResolve({ filter: /^(node:)?(fs|os)$/ }, ({ path }) => ({ path, namespace: "empty" }));
build.onLoad({ filter: /.*/, namespace: "empty" }, () => ({ contents: "module.exports = {}" }));
},
};