// Real Chrome: undo and redo of a style, timing or tween show in place; one that changes which elements exist reloads. import { readFileSync } from "node:fs"; import { createRequire } from "node:module"; import type { Browser, Frame, Page } from "puppeteer-core"; import { afterAll, beforeAll, expect, it, vi } from "vitest"; import { closeTestChrome, launchTestChrome } from "../../tests/chromeTestUtils"; import { buildStudioBundle, openPreview, type Studio } from "../../tests/sealedPreviewHarness"; import { PARITY_MODES as MODES } from "../../tests/parityModes"; vi.setConfig({ testTimeout: 60_000, hookTimeout: 60_000 }); const require = createRequire(import.meta.url); // Studio is served from a site, so the transport can read the preview frame's URL against it. const ORIGIN = "http://studio.test"; const GSAP_URL = `${ORIGIN}/gsap.min.js`; let browser: Browser; let studioBundle: string; beforeAll(async () => { browser = await launchTestChrome(); studioBundle = await buildStudioBundle(); }); afterAll(() => closeTestChrome(browser)); type Film = { left: number; bStart: number; x: number }; const SAVED: Film = { left: 40, bStart: 2, x: 100 }; /** The composition file as saved, ids persisted: card a's left, clip b's start and a's tween distance. */ const file = ({ left, bStart, x }: Film) => `
`; /** The saved file as Studio's server answers its preview: marked, with the runtime. */ function previewHtml(): string { const runtime = readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8"); return file(SAVED) .replace("", '') .replace("", () => ``); } const scripts = () => ({ [GSAP_URL]: readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8"), }); /** Studio's history applying the file `restored` over `previous` at 2 s, as undo and redo both do. */ const restoreHtml = (page: Page, previous: string, restored: string, version: number) => page.evaluate( (files, at) => { const w = window as unknown as { hfStudio: Studio; __reloads?: number }; return w.hfStudio.applyUndoRestoreToPreview( document.querySelector("iframe"), null, files, 2, () => void (w.__reloads = (w.__reloads ?? 0) + 1), undefined, at, ); }, { "index.html": { previous, restored } }, version, ); const restore = (page: Page, previous: Film, restored: Film, version: number) => restoreHtml(page, file(previous), file(restored), version); const shown = (frame: Frame) => frame.evaluate(() => { const a = document.getElementById("a")!; const gsap = (window as unknown as { gsap: { getProperty(t: Element, p: string): unknown } }) .gsap; return { left: a.style.left, bStart: document.getElementById("b")!.getAttribute("data-start"), y: Math.round(Number(gsap.getProperty(a, "y"))), sameDocument: (window as unknown as { __sameDocument?: boolean }).__sameDocument === true, }; }); async function open(mode: string) { const preview = await openPreview(browser, studioBundle, previewHtml(), mode, scripts(), ORIGIN); await preview.seek(2); await preview.frame.evaluate( () => void ((window as unknown as { __sameDocument: boolean }).__sameDocument = true), ); return preview; } const reloads = (page: Page) => page.evaluate(() => (window as unknown as { __reloads?: number }).__reloads ?? 0); it.each(MODES)("a %s preview shows a style undone and then redone in place", async (mode) => { const { page, frame } = await open(mode); const undone = { ...SAVED, left: 5 }; expect(await restore(page, SAVED, undone, 1)).toBe("soft"); await expect.poll(async () => (await shown(frame)).left).toBe("5px"); expect(await restore(page, undone, SAVED, 2)).toBe("soft"); await expect.poll(async () => (await shown(frame)).left).toBe("40px"); expect(await shown(frame)).toMatchObject({ sameDocument: true }); expect(await reloads(page)).toBe(0); await page.close(); }); it.each(MODES)("a %s preview shows a clip's timing undone in place", async (mode) => { const { page, frame } = await open(mode); expect(await restore(page, SAVED, { ...SAVED, bStart: 1 }, 1)).toBe("soft"); await expect.poll(async () => (await shown(frame)).bStart).toBe("1"); expect(await shown(frame)).toMatchObject({ sameDocument: true }); expect(await reloads(page)).toBe(0); await page.close(); }); it.each(MODES)("a %s preview shows a tween edit undone in place", async (mode) => { const { page, frame } = await open(mode); // Half of 100 at 2 s; the undone tween goes 40, so half of that. expect((await shown(frame)).y).toBe(50); expect(await restore(page, SAVED, { ...SAVED, x: 40 }, 1)).toBe("soft"); await expect.poll(async () => (await shown(frame)).y).toBe(20); expect(await shown(frame)).toMatchObject({ sameDocument: true }); expect(await reloads(page)).toBe(0); await page.close(); }); const C_CARD = `
`; const STRUCTURAL: Array<[string, (saved: string) => string]> = [ ["adds", (saved) => saved.replace("