Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Installer and service specs: no fresh executables where a fake would do (#959)

Dee2d9840d2574f
diff --git a/cli/src/lib/platformDaemon.spec.ts b/cli/src/lib/platformDaemon.spec.ts
index 859379475..4efd21f27 100644
--- a/cli/src/lib/platformDaemon.spec.ts
+++ b/cli/src/lib/platformDaemon.spec.ts
@@ -19,7 +19,8 @@ function world(platform: PlatformName) {
const fake = fakePlatform(join(dir, 'bin'), { pidFile })
const clock = { now: 0 }
const dead = new Set<number>()
- const service = new PlatformService(platform, defaultPlatformDeps({ env: { PATH: fake.bin }, uid: 501, home }))
+ // In this process (`fake.run`), as serviceCommand.spec.ts runs them: as processes they timed out under load.
+ const service = new PlatformService(platform, { ...defaultPlatformDeps({ env: { PATH: fake.bin }, uid: 501, home }), run: fake.run })
const deps: PlatformDaemonDeps = {
service,
logFile: join(dir, 'data', 'harness.log'),
diff --git a/cli/src/lib/serviceCommand.spec.ts b/cli/src/lib/serviceCommand.spec.ts
index ead2c51c0..e7a7a5cd9 100644
--- a/cli/src/lib/serviceCommand.spec.ts
+++ b/cli/src/lib/serviceCommand.spec.ts
@@ -9,7 +9,8 @@ import { SERVICE_USAGE, serviceCommand, serviceCommandDeps, type ServiceCommandD
/**
* A computer in a temporary folder: a home, a data folder, an installed bundle, a pid file, and fake
- * launchctl or systemctl that run a stand-in master (pid 4242) and write its pid as the real one does.
+ * launchctl or systemctl, run in this process, that run a stand-in master (pid 4242) and write its pid as
+ * the real one does.
*/
function computer(platform: PlatformName) {
const dir = mkdtempSync(join(tmpdir(), 'service-command-'))
@@ -27,7 +28,9 @@ function computer(platform: PlatformName) {
const err: string[] = []
const stops: string[] = []
const relaunches: number[] = []
- const service = new PlatformService(platform, defaultPlatformDeps({ env: { PATH: fake.bin }, uid: 501, home }))
+ // The fakes run in this process (`fake.run`): these tests are about the command, not how launchctl and
+ // systemctl start, and as processes the systemd ones timed out under load (fakePlatform.ts).
+ const service = new PlatformService(platform, { ...defaultPlatformDeps({ env: { PATH: fake.bin }, uid: 501, home }), run: fake.run })
const readPid = (): number | null => { try { return Number(readFileSync(pidFile, 'utf8')) || null } catch { return null } }
const deps: ServiceCommandDeps = {
service,
diff --git a/cli/src/scripts/install.spec.ts b/cli/src/scripts/install.spec.ts
index 10ac14684..954b6effd 100644
--- a/cli/src/scripts/install.spec.ts
+++ b/cli/src/scripts/install.spec.ts
@@ -2,11 +2,18 @@ import { fork, spawn, spawnSync } from "child_process";
import { join } from "path";
import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, rmSync, statSync, symlinkSync, writeFileSync } from "fs";
import { tmpdir } from "os";
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
// vitest runs from cli/, so this is cli/scripts/install.sh — the file published to the CDN.
const installer = join(process.cwd(), "scripts", "install.sh");
+// Every test here runs the real installer under /bin/sh, with fake commands it writes fresh. On macOS the
+// first run of a freshly written executable waits on the system's check of it: 220 ms at a load of 100,
+// 600 ms with 12 busy loops more, against 16 ms for one already run (2026-10-06). A test runs up to a
+// dozen of them, and vitest's 5 s timed out 11 of these tests in one loaded unit run. The tests that
+// already set 20 s did so for the same reason.
+vi.setConfig({ testTimeout: 20_000 });
+
describe('installer interrupted downloads', () => {
it('recovers real truncated HTTP responses without mixing manifest or binary bytes', async () => {
const { createServer } = await import('node:http');
@@ -408,7 +415,9 @@ describe("scripts/install.sh command contract", () => {
execArgv: ["--import", "tsx"],
env: { ...process.env, ...daemonEnv },
silent: true,
- timeout: 5_000,
+ // A node with tsx compiling the backend's modules, then two tmux creates through fresh fakes:
+ // seconds on a loaded machine (see the timeout above), never 5 s of a quiet one.
+ timeout: 15_000,
});
let response: Record<string, unknown> | undefined;
let stderr = "";
diff --git a/cli/src/testing/fakePlatform.ts b/cli/src/testing/fakePlatform.ts
index 266b412a2..734349e0c 100644
--- a/cli/src/testing/fakePlatform.ts
+++ b/cli/src/testing/fakePlatform.ts
@@ -8,8 +8,10 @@
* test cannot register anything with this machine's launchd or systemd, or stop the person's daemon.
*/
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
+import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
-import { join, resolve } from 'node:path'
+import { basename, join, resolve } from 'node:path'
+import { runInNewContext } from 'node:vm'
export interface FakePlatformState {
/** launchd: the user's GUI domain exists (someone is logged in at the screen). */
@@ -43,6 +45,20 @@ export interface FakePlatform {
calls(): string[][]
state(): FakePlatformState
set(change: Partial<FakePlatformState>): void
+ /**
+ * The same fakes, run in this process instead of as one: `PlatformDeps.run` for a test that is about
+ * what harness does with launchd or systemd, not about how it starts their commands. Each call as a
+ * process costs a node start, and on macOS the first run of a freshly written executable waits on the
+ * system's check of it: 220 ms at a load of 100, 600 ms with 12 busy loops more, against 16 ms for one
+ * already run (2026-10-06). A test makes three such files and up to ten calls, and serviceCommand.spec.ts
+ * timed out 12 times in one loaded unit run.
+ */
+ run(command: string, args: string[]): { status: number | null; stdout: string; stderr: string; error?: string }
+}
+
+/** What `process.exit` throws inside an in-process run, so the script stops where it would have. */
+class FakeExit {
+ constructor(readonly status: number) {}
}
const DEFAULTS: FakePlatformState = {
@@ -153,5 +169,27 @@ export function fakePlatform(bin: string, initial: Partial<FakePlatformState> =
calls: () => existsSync(callsFile) ? readFileSync(callsFile, 'utf8').trim().split('\n').filter(Boolean).map((line) => JSON.parse(line) as string[]) : [],
state: () => JSON.parse(readFileSync(stateFile, 'utf8')) as FakePlatformState,
set: (change) => writeFileSync(stateFile, JSON.stringify({ ...(JSON.parse(readFileSync(stateFile, 'utf8')) as FakePlatformState), ...change })),
+ run: (command, args) => {
+ // As a PATH of this folder alone: anything not faked is not found, never the real command.
+ if (!['launchctl', 'systemctl', 'loginctl'].includes(basename(command)) || command !== basename(command)) {
+ return { status: null, stdout: '', stderr: '', error: `spawnSync ${command} ENOENT` }
+ }
+ let stdout = ''
+ let stderr = ''
+ const fakeProcess = {
+ argv: [process.execPath, join(bin, command), ...args],
+ stdout: { write: (text: string) => { stdout += text; return true } },
+ stderr: { write: (text: string) => { stderr += text; return true } },
+ exit: (status: number) => { throw new FakeExit(status) },
+ kill: (pid: number, signal: NodeJS.Signals) => process.kill(pid, signal),
+ }
+ try {
+ runInNewContext(SCRIPT, { require: createRequire(import.meta.url), process: fakeProcess })
+ } catch (error) {
+ if (error instanceof FakeExit) return { status: error.status, stdout, stderr }
+ throw error
+ }
+ return { status: 0, stdout, stderr }
+ },
}
}
diff --git a/cli/src/tui/install.spec.ts b/cli/src/tui/install.spec.ts
index 5530134dc..c2456eff7 100644
--- a/cli/src/tui/install.spec.ts
+++ b/cli/src/tui/install.spec.ts
@@ -1,8 +1,42 @@
import { createHash } from 'node:crypto'
-import { closeSync, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
+import { closeSync, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
+import { promisify } from 'node:util'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+/**
+ * The disposable executables these tests write are run in this process, by what their bytes say: a
+ * `#!/bin/sh` of `printf '<text>\n'` and `exit <n>` lines, as sh would run them. Every update writes a
+ * fresh executable and runs it, and on macOS the first run of a freshly written file waits on the
+ * system's check of it: 220 ms at a load of 100, 600 ms with 12 busy loops more, against 16 ms for one
+ * already run (2026-10-06). Six of these tests timed out at 5 s in one loaded unit run. The real
+ * `--version` of a downloaded hn, over HTTP and on disk, is update.integration.spec.ts's.
+ */
+vi.mock('node:child_process', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('node:child_process')>()
+ const failure = (message: string, fields: Record<string, unknown>) => Object.assign(new Error(message), fields)
+ const run = async (file: string, _args: string[], options: { signal?: AbortSignal } = {}) => {
+ options.signal?.throwIfAborted()
+ let mode: number
+ try { mode = statSync(file).mode } catch (error) { throw failure(`spawn ${file} ENOENT`, { code: (error as NodeJS.ErrnoException).code }) }
+ if (!(mode & 0o111)) throw failure(`spawn ${file} EACCES`, { code: 'EACCES' })
+ const [shebang, ...lines] = readFileSync(file, 'utf8').split('\n')
+ if (shebang !== '#!/bin/sh') throw failure(`spawn ${file} ENOEXEC`, { code: 'ENOEXEC' })
+ let stdout = ''
+ for (const line of lines.filter(Boolean)) {
+ const printed = /^printf (['"])(.*)\\n\1$/.exec(line)
+ const exit = /^exit (\d+)$/.exec(line)
+ if (printed) stdout += `${printed[2]}\n`
+ else if (exit && exit[1] !== '0') throw failure(`Command failed: ${file}`, { code: Number(exit[1]), stdout, stderr: '' })
+ else if (!exit) throw new Error(`the in-process sh does not know: ${line}`)
+ if (exit) break
+ }
+ return { stdout, stderr: '' }
+ }
+ const execFile = Object.assign(vi.fn(), { [promisify.custom]: run })
+ return { ...actual, execFile }
+})
let installedTuiPath: typeof import('./install.js').installedTuiPath
let installTui: typeof import('./install.js').installTui
let platformKey: typeof import('./install.js').platformKey