|
| 1 | +import { spawn } from 'child_process' |
| 2 | +import { mkdtempSync, readFileSync, rmSync } from 'fs' |
| 3 | +import { tmpdir } from 'os' |
| 4 | +import { join } from 'path' |
| 5 | + |
| 6 | +import { afterAll, describe, expect, test } from 'bun:test' |
| 7 | + |
| 8 | +import { TERMINAL_RESET_SEQUENCES } from '../utils/terminal-reset-sequences' |
| 9 | + |
| 10 | +import type { ChildProcess } from 'child_process' |
| 11 | + |
| 12 | +const FIXTURE = join(import.meta.dir, 'helpers', 'terminal-watchdog-fixture.ts') |
| 13 | + |
| 14 | +const tempDir = mkdtempSync(join(tmpdir(), 'terminal-watchdog-')) |
| 15 | + |
| 16 | +afterAll(() => { |
| 17 | + rmSync(tempDir, { recursive: true, force: true }) |
| 18 | +}) |
| 19 | + |
| 20 | +function spawnFixture(mode: 'hang' | 'clean', ttyPath: string): ChildProcess { |
| 21 | + return spawn(process.execPath, [FIXTURE, mode, ttyPath], { |
| 22 | + stdio: ['ignore', 'pipe', 'inherit'], |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +/** Resolve once the fixture prints "ready" (watchdog armed). */ |
| 27 | +function waitForReady(child: ChildProcess): Promise<void> { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + let out = '' |
| 30 | + child.stdout!.on('data', (chunk: Buffer) => { |
| 31 | + out += chunk.toString() |
| 32 | + if (out.includes('ready')) resolve() |
| 33 | + }) |
| 34 | + child.on('exit', () => resolve()) // "clean" mode exits immediately |
| 35 | + child.on('error', reject) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +function waitForExit(child: ChildProcess): Promise<void> { |
| 40 | + return new Promise((resolve) => { |
| 41 | + if (child.exitCode !== null || child.signalCode !== null) return resolve() |
| 42 | + child.on('exit', () => resolve()) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +function readTty(ttyPath: string): string { |
| 47 | + try { |
| 48 | + return readFileSync(ttyPath, 'utf8') |
| 49 | + } catch { |
| 50 | + return '' |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function pollForContent(ttyPath: string, timeoutMs: number): Promise<string> { |
| 55 | + const deadline = Date.now() + timeoutMs |
| 56 | + while (Date.now() < deadline) { |
| 57 | + const content = readTty(ttyPath) |
| 58 | + if (content) return content |
| 59 | + await new Promise((r) => setTimeout(r, 50)) |
| 60 | + } |
| 61 | + return readTty(ttyPath) |
| 62 | +} |
| 63 | + |
| 64 | +// The watchdog is POSIX-only (sh + /dev/tty); on Windows the npm wrapper is |
| 65 | +// the safety net instead. |
| 66 | +describe.skipIf(process.platform === 'win32')('terminal watchdog', () => { |
| 67 | + test('writes reset sequences to the tty when the process dies uncleanly', async () => { |
| 68 | + const ttyPath = join(tempDir, 'unclean.out') |
| 69 | + const child = spawnFixture('hang', ttyPath) |
| 70 | + await waitForReady(child) |
| 71 | + |
| 72 | + child.kill('SIGKILL') |
| 73 | + await waitForExit(child) |
| 74 | + |
| 75 | + const written = await pollForContent(ttyPath, 5_000) |
| 76 | + expect(written).toBe(TERMINAL_RESET_SEQUENCES) |
| 77 | + }, 15_000) |
| 78 | + |
| 79 | + test('stays silent when the process shuts down cleanly', async () => { |
| 80 | + const ttyPath = join(tempDir, 'clean.out') |
| 81 | + const child = spawnFixture('clean', ttyPath) |
| 82 | + await waitForExit(child) |
| 83 | + |
| 84 | + // Give a killed-too-late watchdog time to (incorrectly) fire. |
| 85 | + await new Promise((r) => setTimeout(r, 500)) |
| 86 | + expect(readTty(ttyPath)).toBe('') |
| 87 | + }, 15_000) |
| 88 | +}) |
0 commit comments