/** * releaseOrphanedBrowser: a WhatsApp browser left over from a sidecar that died * without its shutdown is stopped; a browser another running server owns, or * any other process, is left alone. */ import { execFileSync, spawn, spawnSync } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { releaseOrphanedBrowser } from '../src/local/channels/whatsapp-profile-lock.js'; const IDLE = 'setInterval(() => {}, 1000)'; function isAlive(pid: number): boolean { try { process.kill(pid, 0); return true; } catch { return false; } } async function waitUntilGone(pid: number): Promise { for (let i = 0; i < 50 && isAlive(pid); i++) await new Promise(r => setTimeout(r, 100)); return !isAlive(pid); } describe.skipIf(process.platform === 'win32')('releaseOrphanedBrowser', () => { let root: string; let profileDir: string; const started: number[] = []; function lockTo(pid: number, host = os.hostname()): void { fs.symlinkSync(`${host}-${pid}`, path.join(profileDir, 'SingletonLock')); } /** A process for this profile whose parent has exited, like the browser of a killed sidecar. */ function startOrphan(): number { const out = execFileSync('sh', [ '-c', `"${process.execPath}" -e "${IDLE}" -- --user-data-dir=${profileDir} >/dev/null 2>&1 & echo $!`, ], { encoding: 'utf8' }); const pid = Number(out.trim()); started.push(pid); return pid; } beforeEach(() => { root = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-wa-lock-')); profileDir = path.join(root, 'session'); fs.mkdirSync(profileDir); }); afterEach(() => { for (const pid of started.splice(0)) { try { process.kill(pid, 'SIGKILL'); } catch { /* already gone */ } } fs.rmSync(root, { recursive: true, force: true }); }); it('stops a browser for this profile whose parent is gone', async () => { const pid = startOrphan(); lockTo(pid); expect(await releaseOrphanedBrowser(profileDir, { graceMs: 3000 })).toEqual({ kind: 'released', pid }); expect(await waitUntilGone(pid)).toBe(true); }); it('leaves a browser that a running server still owns', async () => { const child = spawn(process.execPath, ['-e', IDLE, '--', `--user-data-dir=${profileDir}`], { stdio: 'ignore' }); started.push(child.pid!); lockTo(child.pid!); expect(await releaseOrphanedBrowser(profileDir)).toEqual({ kind: 'in-use', pid: child.pid }); expect(isAlive(child.pid!)).toBe(true); }); it('leaves a process that does not use this profile', async () => { lockTo(process.pid); expect(await releaseOrphanedBrowser(profileDir)).toEqual({ kind: 'free' }); }); it('treats a lock whose process is gone as free', async () => { const gone = spawnSync('true').pid; lockTo(gone); expect(await releaseOrphanedBrowser(profileDir)).toEqual({ kind: 'free' }); }); it('ignores a lock from another computer', async () => { const pid = startOrphan(); lockTo(pid, 'another-host.local'); expect(await releaseOrphanedBrowser(profileDir)).toEqual({ kind: 'free' }); expect(isAlive(pid)).toBe(true); }); });