import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "vitest"; import { Browser } from "../browser.ts"; import { TMP_PREFIX } from "../lib.ts"; import { testChromiumExecutable } from "./chromium.ts"; test("only tagged dead-owner temporary directories are reclaimed", async () => { const fixture = await mkdtemp(join(tmpdir(), "chrome-cleanup-fixture-")); const stale = await mkdtemp(TMP_PREFIX); const unknown = await mkdtemp(TMP_PREFIX); try { await writeFile( `${stale}/.pi-chrome-cdp-owner.json`, JSON.stringify({ tag: "pi-chrome-cdp-v1", ownerPid: 2147483647, browserPid: 0, }), ); const b = await Browser.launch({ executablePath: await testChromiumExecutable(fixture), }); await b.close(); assert.equal(existsSync(stale), false); assert.equal(existsSync(unknown), true); } finally { await rm(stale, { recursive: true, force: true }); await rm(unknown, { recursive: true, force: true }); await rm(fixture, { recursive: true, force: true }); } });