import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { launchLocalChrome, planLocalLaunch, type LaunchDeps, type LaunchPlan } from "../../src/cdp/local-launch"; const plan: LaunchPlan = { exe: "/x/chrome", args: ["--a"], profileDir: "/p", port: 9222 }; const deps = (over: Partial): LaunchDeps => { let t = 0; return { start: async () => undefined, probe: async () => null, sleep: async (ms) => { t += ms; }, now: () => t, ...over }; }; describe("planLocalLaunch", () => { test("plans a launch on macOS with the dedicated profile and the debugging port", () => { const r = planLocalLaunch({ HOME: "/h" }, "darwin", () => true); assert.equal(r.kind, "plan"); if (r.kind !== "plan") return; assert.ok(r.plan.args.includes("--remote-debugging-port=9222")); assert.ok(r.plan.args.some((a) => a.startsWith("--user-data-dir=") && a.endsWith(".chrome-cdp-profile"))); }); test("BU_LOCAL_CHROME_PROFILE and BU_LOCAL_CHROME_EXE override the defaults", () => { const r = planLocalLaunch({ BU_LOCAL_CHROME_PROFILE: "/tmp/p", BU_LOCAL_CHROME_EXE: "/tmp/c" }, "darwin", () => true); assert.equal(r.kind === "plan" && r.plan.exe, "/tmp/c"); assert.ok(r.kind === "plan" && r.plan.args.includes("--user-data-dir=/tmp/p")); }); test("is skipped when switched off, off macOS, or without a Chrome", () => { assert.equal(planLocalLaunch({ BU_LOCAL_CHROME_LAUNCH: "off" }, "darwin", () => true).kind, "skip"); assert.equal(planLocalLaunch({}, "linux", () => true).kind, "skip"); assert.equal(planLocalLaunch({}, "darwin", () => false).kind, "skip"); }); }); describe("launchLocalChrome", () => { test("resolves with the websocket url once the port answers", async () => { let calls = 0; const r = await launchLocalChrome(plan, deps({ probe: async () => (++calls >= 3 ? "ws://127.0.0.1:9222/x" : null) })); assert.deepEqual(r.success && r.data, "ws://127.0.0.1:9222/x"); }); test("reports a spawn failure without probing", async () => { let probed = false; const r = await launchLocalChrome(plan, deps({ start: async () => "ENOENT", probe: async () => { probed = true; return null; } })); assert.equal(r.success, false); assert.equal(probed, false); }); test("gives up after the deadline and says what to check", async () => { const r = await launchLocalChrome(plan, deps({})); assert.equal(r.success, false); if (!r.success) assert.match(r.error, /nothing answered on 127\.0\.0\.1:9222/); }); });