Files
hyperframes/packages/cli/src/browser/launch.startupFailure.test.ts
T
Miguel Ángel 9e5da67409 fix(cli): a Chrome that fails to start says how it ended, and a slow start says so (#6516)
* fix(cli): a Chrome that fails to start says how it ended, and a slow start says so

A failed launch only reported "Protocol error (Target.setDiscoverTargets): Target closed". It now
runs the browser once more on its own with the same flags and reports its exit code (Windows
codes named: missing runtime, out of memory, security software), its last stderr lines, or that
it stayed up. The CLI and the engine's render launch both use it. A start slower than 5 s prints
"Chrome is still starting...", and a --timeout that looks like seconds says it is milliseconds.

* chore(cli): cut the Chrome launch diagnosis comments to the comment limit

* fix(cli): the Chrome startup check runs Chrome as puppeteer does and can be cancelled

The check started the browser with only the caller's flags, so a full Chrome ran headful and
was blamed for a missing X server. It now uses puppeteer's default arguments (headless
included). The check is stopped when the CLI is stopped, and the GPU probe, whose failure
falls back to software anyway, skips it.

* fix(cli): a Chrome launch error is never replaced by a failure to explain it

The startup diagnosis asks puppeteer for its default arguments before re-running
Chrome. If that call fails, its error used to replace the launch error the caller
needed. The launch error is now reported as it was. The core package size budget
rises to cover the new chromeStartup module (about 4.7 KB unpacked).

* refactor(engine): the Chrome startup diagnosis lives in the engine, not core

Both callers launch Chrome through the engine (the CLI already imports it), so the
engine owns the diagnosis. Core's package size budget is back to its old value.
2026-10-27 07:49:04 +00:00

87 lines
3.0 KiB
TypeScript

import { chmodSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { launchManagedBrowser } from "./launch.js";
function fakeBrowser(body: string): string {
const path = join(mkdtempSync(join(tmpdir(), "hf-fake-chrome-")), "chrome");
writeFileSync(path, `#!/usr/bin/env node\n${body}\n`);
chmodSync(path, 0o755);
return path;
}
describe.runIf(process.platform !== "win32")("a browser that dies at startup", () => {
it("fails the launch with its exit code and last stderr lines, not just 'Target closed'", async () => {
const executablePath = fakeBrowser(
'process.stderr.write("[1:1:FATAL:headless_shell] out of memory\\n"); process.exit(7);',
);
const puppeteer = (await import("puppeteer-core")).default;
const failure = await launchManagedBrowser(puppeteer, {
executablePath,
headless: true,
args: [],
}).then(
() => undefined,
(error: unknown) => error as Error,
);
expect(failure).toBeInstanceOf(Error);
expect(failure?.message).toContain("exited with code 7");
expect(failure?.message).toContain("out of memory");
expect(failure?.cause).toBeInstanceOf(Error);
}, 60_000);
it("starts the probe headless, as puppeteer starts it, so a full Chrome is not run headful", async () => {
const executablePath = fakeBrowser(
"console.error(process.argv.slice(2).join(' ')); process.exit(3);",
);
const puppeteer = (await import("puppeteer-core")).default;
const failure = await launchManagedBrowser(puppeteer, {
executablePath,
headless: true,
args: [],
}).then(
() => undefined,
(error: unknown) => error as Error,
);
expect(failure?.message).toMatch(/--headless/);
}, 60_000);
it("skips the diagnosis for a launch whose failure the caller handles, such as the GPU probe", async () => {
const executablePath = fakeBrowser("process.exit(3);");
const puppeteer = (await import("puppeteer-core")).default;
const failure = await launchManagedBrowser(
puppeteer,
{ executablePath, headless: true, args: [] },
{ diagnose: false },
).then(
() => undefined,
(error: unknown) => error as Error,
);
expect(failure?.message).not.toContain("exited with code");
}, 60_000);
});
describe("a launch failure whose diagnosis cannot run", () => {
it("still reports the launch error when puppeteer cannot give the default arguments", async () => {
const launchError = new Error("Failed to launch the browser process");
const puppeteer = {
launch: async () => {
throw launchError;
},
defaultArgs: () => {
throw new TypeError("no defaults");
},
};
const failure = await launchManagedBrowser(puppeteer as never, {
executablePath: "/bin/true",
headless: true,
args: [],
}).then(
() => undefined,
(error: unknown) => error as Error,
);
expect(failure).toBe(launchError);
});
});