Skip to content

Write HTML. Render video. Built for agents.

3.4 KB
Raw Blame
import { spawn, type ChildProcess } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import { waitForTestCondition } from "../utils/processTestUtils.js";

const fixturePath = join(dirname(fileURLToPath(import.meta.url)), "launch.fixture.ts");
const children = new Set<ChildProcess>();
const profiles = new Set<string>();

/** Pids of every process whose command line carries this profile dir (Chromium and its helpers). */
function browserPids(profileDir: string): number[] {
  return readdirSync("/proc")
    .filter((name) => /^\d+$/.test(name))
    .filter((name) => {
      try {
        return readFileSync(`/proc/${name}/cmdline`, "utf8").includes(profileDir);
      } catch {
        return false;
      }
    })
    .map(Number);
}

function startFixture(profileDir: string, readyPath: string, mode = "launch"): ChildProcess {
  const child = spawn(
    process.execPath,
    ["--import", "tsx", fixturePath, profileDir, readyPath, mode],
    { stdio: "ignore" },
  );
  children.add(child);
  profiles.add(profileDir);
  return child;
}

const exited = (child: ChildProcess) =>
  new Promise<{ code: number | null; signal: string | null }>((resolve) =>
    child.once("exit", (code, signal) => resolve({ code, signal })),
  );

// Chrome outlives a SIGKILLed parent, so a failed case also kills its browser directly.
afterEach(() => {
  for (const child of children) child.kill("SIGKILL");
  for (const pid of [...profiles].flatMap(browserPids)) {
    try {
      process.kill(pid, "SIGKILL");
    } catch {}
  }
  for (const profile of profiles) rmSync(dirname(profile), { recursive: true, force: true });
  children.clear();
  profiles.clear();
});

describe.skipIf(process.platform !== "linux")("managed browser shutdown", () => {
  it.each([
    ["SIGTERM", "ready", 143],
    ["SIGINT", "ready", 130],
    ["SIGTERM", "launching", 143],
  ] as const)(
    "%s while %s leaves no browser and asks the CLI to exit",
    async (signal, phase, code) => {
      const dir = mkdtempSync(join(tmpdir(), "hf-launch-"));
      const profileDir = join(dir, "profile");
      const readyPath = join(dir, "ready");
      const child = startFixture(profileDir, readyPath);
      const done = exited(child);
      await waitForTestCondition(
        () => existsSync(readyPath) && readFileSync(readyPath, "utf8") === phase,
        30_000,
      );
      if (phase === "ready")
        await waitForTestCondition(() => browserPids(profileDir).length > 0, 30_000);
      child.kill(signal);
      await done;
      expect(readFileSync(`${readyPath}.exit`, "utf8")).toBe(String(code));
      await waitForTestCondition(() => browserPids(profileDir).length === 0, 5_000);
    },
    60_000,
  );

  it("SIGTERM during the GPU probe asks the CLI to exit instead of being swallowed", async () => {
    const dir = mkdtempSync(join(tmpdir(), "hf-launch-"));
    const readyPath = join(dir, "ready");
    const child = startFixture(join(dir, "profile"), readyPath, "probe");
    const done = exited(child);
    await waitForTestCondition(
      () => existsSync(readyPath) && readFileSync(readyPath, "utf8") === "probing",
      30_000,
    );
    child.kill("SIGTERM");
    await done;
    expect(readFileSync(`${readyPath}.exit`, "utf8")).toBe("143");
  }, 60_000);
});