Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/host-kit/src/internal/host-process-liveness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
readProcessIdentityFacts,
} from './host-process.ts';

const readFile = fs.readFileSync;

function psReturns(stdout: string, exitCode = 0): void {
mockRunCmdSync.mockReturnValue({ stdout, stderr: '', exitCode });
}
Expand All @@ -44,6 +46,11 @@ function psInstalledAt(installed: readonly string[]): void {
beforeEach(() => {
mockRunCmdSync.mockReset();
mockRunCmd.mockReset();
vi.spyOn(fs, 'readFileSync').mockImplementation((...args) => {
// The ps fixture must not borrow the real host's identity for a fabricated pid.
if (String(args[0]) === '/proc/sys/kernel/random/boot_id') throw new Error('ENOENT');
return readFile(...args);
});
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { afterEach, test, vi } from 'vitest';

// Every claim the stopper makes is about what the host answers, so one double answers both syscalls it
Expand All @@ -16,6 +17,7 @@ import {
type ProcessStopTarget,
} from './host-process.ts';

const readFile = fs.readFileSync;
const PID = 4242;
const BUDGET = { termTimeoutMs: 1_000, killTimeoutMs: 500 };
type Signal = 'SIGTERM' | 'SIGINT' | 'SIGKILL';
Expand Down Expand Up @@ -256,6 +258,11 @@ const STOP_CASES: HostCase[] = [
];

function driveHost(model: HostCase): { signals: Signal[] } {
vi.spyOn(fs, 'readFileSync').mockImplementation((...args) => {
// This host model supplies process identity through its ps answers.
if (String(args[0]) === '/proc/sys/kernel/random/boot_id') throw new Error('ENOENT');
return readFile(...args);
});
const signals: Signal[] = [];
let psRefused = false;
let ended = false;
Expand Down
Loading