// Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/profileBrowserPid.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { spawn, type ChildProcess } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync, symlinkSync } from "node:fs"; import { hostname, tmpdir } from "node:os"; import path from "node:path"; import { killProfileBrowser, profileBrowserPid } from "./profileBrowserPid"; const TMP = mkdtempSync(path.join(tmpdir(), "profile-pid-")); const children: ChildProcess[] = []; after(() => { for (const c of children) if (c.pid !== undefined && c.exitCode === null) c.kill("SIGKILL"); rmSync(TMP, { recursive: true, force: true }); }); function profile(name: string, lock?: string): string { const dir = path.join(TMP, name); mkdirSync(dir, { recursive: true }); if (lock) symlinkSync(lock, path.join(dir, "SingletonLock")); return dir; } // A process that stands in for a browser: it sleeps, with `arg` on its command line. // `argv0` is what /proc shows as its name: "chrome" for the stand-in browser. function sleeper(arg: string, argv0 = "chrome"): Promise { const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000); console.log('up')", "--", arg], { argv0, stdio: ["ignore", "pipe", "ignore"] }); children.push(child); return new Promise((resolve, reject) => { child.once("error", reject); child.stdout!.once("data", () => resolve(child)); }); } const exited = (c: ChildProcess) => new Promise((resolve) => c.once("exit", (_code, signal) => resolve(signal))); const linux = process.platform === "linux"; test("the pid comes from the profile's own lock, and only when that process names this exact profile", async () => { const none = profile("none"); assert.deepEqual(await profileBrowserPid(none, { platform: "linux" }), { pid: null, why: "the profile has no browser lock (no browser holds it)" }); const dir = profile("exact", "host-4242"); const deps = (cmdline: string) => ({ platform: "linux", hostname: "host", cmdline: async () => cmdline }); assert.deepEqual(await profileBrowserPid(dir, deps(`chrome\0--headless\0--user-data-dir=${dir}\0`)), { pid: 4242 }); // Another profile whose path merely starts the same: left alone. const other = await profileBrowserPid(dir, deps(`chrome\0--user-data-dir=${dir}-other\0`)); assert.equal(other.pid, null); assert.match(other.pid === null ? other.why : "", /process 4242 does not name this profile on its command line \(a stale lock\): it is left alone/); // The directory mentioned, but not as the profile argument. assert.equal((await profileBrowserPid(dir, deps(`vim\0${dir}\0`))).pid, null); // Review 1: another program that carries the very argument is not a browser... const notChrome = await profileBrowserPid(dir, deps(`/usr/bin/python3\0--user-data-dir=${dir}\0`)); assert.match(notChrome.pid === null ? notChrome.why : "", /process 4242 is not a Chromium browser \(python3\): it is left alone/); // ...a browser's child process (a renderer) is not the browser... const child = await profileBrowserPid(dir, deps(`/opt/chromium/chrome\0--type=renderer\0--user-data-dir=${dir}\0`)); assert.match(child.pid === null ? child.why : "", /one of a browser's child processes/); // ...and a lock written on another machine names a pid of that machine. const elsewhere = await profileBrowserPid(dir, { platform: "linux", hostname: "another-host", cmdline: async () => `chrome\0--user-data-dir=${dir}\0` }); assert.match(elsewhere.pid === null ? elsewhere.why : "", /written on another machine/); // The headless shell, by its full path, is a browser. assert.deepEqual(await profileBrowserPid(dir, deps(`/opt/pw/chromium_headless_shell-1/chrome-linux/headless_shell\0--user-data-dir=${dir}\0`)), { pid: 4242 }); // A lock whose process is gone. const gone = await profileBrowserPid(dir, { platform: "linux", hostname: "host", cmdline: async () => { throw new Error("ENOENT"); } }); assert.match(gone.pid === null ? gone.why : "", /\(4242\) is gone/); // A lock that names no usable pid: 0, 1, this process, no number. for (const lock of ["host-0", "host-1", `host-${process.pid}`, "nonsense"]) { const d = profile(`bad-${lock}`, lock); assert.equal((await profileBrowserPid(d, deps(`x\0--user-data-dir=${d}\0`))).pid, null, lock); } // Off Linux nothing is looked for. assert.deepEqual(await profileBrowserPid(dir, { platform: "darwin" }), { pid: null, why: "finding the browser's process is Linux-only" }); }); test("a kill signals the checked pid and nothing else", async () => { const dir = profile("kill", "host-777"); const sent: Array<[number, string]> = []; const kill = (pid: number, signal: NodeJS.Signals) => void sent.push([pid, signal]); assert.deepEqual(await killProfileBrowser(dir, { platform: "linux", hostname: "host", cmdline: async () => `chrome\0--user-data-dir=${dir}\0`, kill }), { killed: true, pid: 777 }); assert.deepEqual(sent, [[777, "SIGKILL"]]); // A stale lock: no signal at all. const stale = await killProfileBrowser(dir, { platform: "linux", hostname: "host", cmdline: async () => "bash\0", kill }); assert.equal(stale.killed, false); assert.equal(sent.length, 1); // Off Linux: no signal, and why. assert.deepEqual(await killProfileBrowser(dir, { platform: "win32", kill }), { killed: false, why: "finding the browser's process is Linux-only" }); assert.equal(sent.length, 1); }); test("for real: a process holding the profile is killed; one that does not name it is not", { skip: !linux }, async () => { const dir = path.join(TMP, "real"); mkdirSync(dir, { recursive: true }); const holder = await sleeper(`--user-data-dir=${dir}`); const bystander = await sleeper(`--user-data-dir=${dir}-other`); // The lock names the bystander: nothing is killed. symlinkSync(`${hostname()}-${bystander.pid}`, path.join(dir, "SingletonLock")); const no = await killProfileBrowser(dir); assert.equal(no.killed, false); assert.equal(bystander.exitCode, null); // The lock names the holder: it is killed, the bystander is not. rmSync(path.join(dir, "SingletonLock")); // A process that names the profile and is not a browser: left alone. const impostor = await sleeper(`--user-data-dir=${dir}`, "node"); symlinkSync(`${hostname()}-${impostor.pid}`, path.join(dir, "SingletonLock")); assert.equal((await killProfileBrowser(dir)).killed, false); assert.equal(impostor.exitCode, null); rmSync(path.join(dir, "SingletonLock")); symlinkSync(`${hostname()}-${holder.pid}`, path.join(dir, "SingletonLock")); const gone = exited(holder); assert.deepEqual(await killProfileBrowser(dir), { killed: true, pid: holder.pid }); assert.equal(await gone, "SIGKILL"); assert.equal(bystander.exitCode, null); assert.equal(bystander.signalCode, null); });