Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/studio/src/hooks/gsapValueAtPlayhead.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { gsap } from "gsap";
import { parseGsapScriptAcorn } from "@hyperframes/parsers/gsap-parser-acorn";
import { replaceTweenWithKeyframesInScript } from "@hyperframes/parsers/gsap-writer-acorn";
import { expect, it, vi } from "vitest";
import { launchTestChrome, showWithRuntime } from "../../tests/chromeTestUtils";
import { closeTestChrome, launchTestChrome, showWithRuntime } from "../../tests/chromeTestUtils";
import type { DomEditSelection } from "../components/editor/domEditingTypes";
import { usePlayerStore } from "../player/store/playerStore";
import { planValueEdit } from "./gsapValueAtPlayhead";
Expand Down Expand Up @@ -59,6 +59,6 @@ it("a drag on a fromTo's start shows the dropped x there once the runtime seeks"
});
expect(shown).toBeCloseTo(90, 1);
} finally {
await browser.close();
await closeTestChrome(browser);
}
});
4 changes: 2 additions & 2 deletions packages/studio/src/utils/gsapUndoRestore.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath } from "node:url";
import type { Browser } from "puppeteer-core";
import { build, type Plugin } from "esbuild";
import { afterAll, beforeAll, expect, it, vi } from "vitest";
import { launchTestChrome, showWithRuntime } from "../../tests/chromeTestUtils";
import { closeTestChrome, launchTestChrome, showWithRuntime } from "../../tests/chromeTestUtils";
import { writeFixture } from "../../tests/e2e/edit-accuracy/grid.mjs";

const CROP = "inset(0px 40px 0px 0px)";
Expand Down Expand Up @@ -47,7 +47,7 @@ beforeAll(async () => {
softReloadBundle = await bundle("./gsapSoftReload.ts", "hfSoftReload");
});

afterAll(() => browser?.close());
afterAll(() => closeTestChrome(browser));

type Spec = { gsap: string; placement: string; rotation: number; time: number };

Expand Down
64 changes: 64 additions & 0 deletions packages/studio/tests/chromeTestUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { TimeoutError, type Browser } from "puppeteer-core";
import { describe, expect, it, vi } from "vitest";
import { closeTestChrome, launchTestChrome } from "./chromeTestUtils";

vi.mock("../vite.browser", () => ({ findSystemChrome: () => "/test/chrome" }));

const fakeBrowser = (close: () => Promise<void>) => {
const kill = vi.fn();
const disconnect = vi.fn(async () => undefined);
const browser = { close, disconnect, process: () => ({ kill }) } as unknown as Browser;
return { browser, kill, disconnect };
};

describe("closeTestChrome", () => {
it("kills a Chrome whose close never finishes", async () => {
const { browser, kill, disconnect } = fakeBrowser(() => new Promise(() => undefined));
await closeTestChrome(browser, 0);
expect(kill).toHaveBeenCalledWith("SIGKILL");
expect(disconnect).toHaveBeenCalledOnce();
});

it("leaves a Chrome that closed alone", async () => {
const { browser, kill } = fakeBrowser(async () => undefined);
await closeTestChrome(browser, 60_000);
expect(kill).not.toHaveBeenCalled();
});
});

describe("launchTestChrome", () => {
it("bounds each launch and tries once more after a stuck one", async () => {
const { browser } = fakeBrowser(async () => undefined);
const launch = vi
.fn()
.mockRejectedValueOnce(new TimeoutError("stuck"))
.mockResolvedValueOnce(browser);
await expect(launchTestChrome(launch)).resolves.toBe(browser);
expect(launch).toHaveBeenCalledTimes(2);
expect(launch.mock.calls[0]![0]).toMatchObject({ timeout: 20_000 });
});

it("tries once more after a stuck launch that Windows reports as already running", async () => {
vi.useFakeTimers();
try {
const { browser } = fakeBrowser(async () => undefined);
const launch = vi
.fn()
.mockImplementationOnce(async () => {
vi.advanceTimersByTime(20_000);
throw new Error("The browser is already running for C:\\tmp\\x");
})
.mockResolvedValueOnce(browser);
await expect(launchTestChrome(launch)).resolves.toBe(browser);
expect(launch).toHaveBeenCalledTimes(2);
} finally {
vi.useRealTimers();
}
});

it("does not retry a launch that failed for another reason", async () => {
const launch = vi.fn().mockRejectedValue(new Error("no such file"));
await expect(launchTestChrome(launch)).rejects.toThrow("no such file");
expect(launch).toHaveBeenCalledOnce();
});
});
45 changes: 39 additions & 6 deletions packages/studio/tests/chromeTestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import puppeteer, { type Browser, type Page } from "puppeteer-core";
import puppeteer, { TimeoutError, type Browser, type Page } from "puppeteer-core";
import { findSystemChrome } from "../vite.browser";

const require = createRequire(import.meta.url);

/** Headless Chrome with the e2e launcher's flags; the test's own timeout bounds the launch. */
export async function launchTestChrome(): Promise<Browser> {
const LAUNCH_TIMEOUT_MS = 20_000;

/** 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");
return puppeteer.launch({
const options = {
executablePath,
headless: true,
args: ["--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu"],
timeout: 0,
});
timeout: LAUNCH_TIMEOUT_MS,
};
const startedAt = Date.now();
try {
return await launch(options);
} catch (error) {
const stuck = error instanceof TimeoutError || Date.now() - startedAt >= LAUNCH_TIMEOUT_MS;
if (!stuck) throw error;
return launch(options);
}
}

/** 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);
}

/** Shows `html` with the HyperFrames runtime; only its GSAP CDN script is served, nothing is fetched. */
Expand Down
Loading