A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
import { spawnSync } from "node:child_process";
import {
copyFileSync,
existsSync,
mkdirSync,
readdirSync,
readFileSync,
realpathSync,
statSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { beforeAll, describe, expect, it } from "vitest";
import { lockedEnginePackages, piWrapper } from "../src/runtime-build.ts";
import { tempDir } from "./helpers.ts";
import { WINDOWS_STOP_ADDON } from "./support/windows.ts";
const PACKAGE = fileURLToPath(new URL("..", import.meta.url));
const BUILD = join(PACKAGE, "scripts", "build-runtime.ts");
const ENGINE_CLI = join("@earendil-works", "pi-coding-agent", "dist", "bundle", "cli.js");
let out: string;
// The real build stages the Windows stop addon, built with MSVC (build:windows-stop), and rightly
// refuses without it. A local Windows checkout that never ran build:windows-stop has none, so the
// build tests skip there; CI builds the addon first, so a missing one fails the build instead.
const skipBuild =
process.platform === "win32" &&
!process.env.CI &&
!existsSync(join(PACKAGE, "native", "windows-stop.node"));
if (skipBuild) console.info(`Windows-only test skip: ${WINDOWS_STOP_ADDON}`);
beforeAll(() => {
if (skipBuild) return;
out = tempDir();
const build = spawnSync(process.execPath, [BUILD, "--out", out], { encoding: "utf8" });
expect(build.stderr).toBe("");
expect(build.status).toBe(0);
}, 60_000);
function listFiles(dir: string, prefix = ""): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) =>
entry.isDirectory()
? listFiles(join(dir, entry.name), `${prefix}${entry.name}/`)
: [`${prefix}${entry.name}`],
);
}
describe("piWrapper", () => {
it("emits a relocatable Windows cmd wrapper without relying on sh or ambient Node", () => {
expect(piWrapper("@earendil-works/pi-coding-agent/dist/bundle/cli.js", "win32")).toBe(
'@echo off\r\nsetlocal DisableDelayedExpansion\r\n"%~dp0..\\node\\bin\\node.exe" "%~dp0..\\daemon\\node_modules\\@earendil-works\\pi-coding-agent\\dist\\bundle\\cli.js" %*\r\nexit /b %errorlevel%\r\n',
);
});
it("preserves the Unix shell wrapper for Linux and macOS", () => {
for (const platform of ["linux", "darwin"] as const) {
const wrapper = piWrapper("@earendil-works/pi-coding-agent/dist/bundle/cli.js", platform);
expect(wrapper).toContain("#!/bin/sh\n");
expect(wrapper).toContain('exec "$runtime/node/bin/node"');
expect(wrapper).toContain('"$@"');
}
});
});
describe.skipIf(skipBuild)("build:runtime", () => {
it("emits the daemon part of the runtime layout, without node_modules", () => {
const windows = process.platform === "win32";
expect(listFiles(out).sort()).toEqual(
[
windows ? "bin/pi.cmd" : "bin/pi",
"daemon/cli.mjs",
"daemon/extensions/approval-guard.ts",
"daemon/extensions/screenshot-context.ts",
"daemon/identity.md",
...(windows ? ["daemon/native/windows-stop.node"] : []),
"daemon/package.json",
].sort(),
);
expect(readFileSync(join(out, "daemon", "identity.md"), "utf8")).toBe(
readFileSync(join(PACKAGE, "src", "identity.md"), "utf8"),
);
expect(readFileSync(join(out, "daemon", "extensions", "approval-guard.ts"), "utf8")).toBe(
readFileSync(join(PACKAGE, "src", "extensions", "approval-guard.ts"), "utf8"),
);
});
it("bundles the protocol and leaves the engine and ws external", () => {
const bundle = readFileSync(join(out, "daemon", "cli.mjs"), "utf8");
expect(bundle.startsWith("#!/usr/bin/env node\n")).toBe(true);
expect(bundle).not.toContain("@gentle-dot/protocol");
expect(bundle).toMatch(/from "ws"/);
expect(bundle).toContain('import("@earendil-works/pi-coding-agent")');
// The production addon stays external and resolves beside the staged entry,
// not relative to the source checkout; Unix's file layout remains unchanged.
expect(bundle).toContain("./native/windows-stop.node");
expect(bundle).not.toContain("NAPI_MODULE");
});
it("lists the external dependencies with the lockfile's exact versions", () => {
const manifest = JSON.parse(readFileSync(join(out, "daemon", "package.json"), "utf8"));
expect(manifest).toMatchObject({ private: true, type: "module" });
expect(manifest.dependencies).toEqual({
"@earendil-works/pi-ai": "1.5.0",
"@earendil-works/pi-coding-agent": "1.5.0",
"@earendil-works/pi-mcp": "1.5.0",
"@earendil-works/pi-tui": "1.5.0",
"gentle-pi": "4.7.0",
jiti: "2.7.0",
ws: "8.22.0",
});
});
// The engine asks for its siblings with `^` ranges; a fresh install would take newer ones
// that no longer match it (pi-codemode 1.4.0 broke pi-coding-agent 1.0.4).
// The manifest is this suite's boundary: proving npm honours `overrides` needs a real
// install from the registry, which the packaging step does (scripts/package/stage-runtime.mjs
// installs from this manifest, then scripts/package/smoke-runtime.mjs starts the engine).
it("pins the engine's own packages to the lockfile's versions", () => {
const manifest = JSON.parse(readFileSync(join(out, "daemon", "package.json"), "utf8"));
expect(manifest.overrides).toEqual({
"@earendil-works/pi-agent-core": "1.5.0",
"@earendil-works/pi-codemode": "1.5.0",
"@earendil-works/pi-schema": "1.5.0",
"@earendil-works/pi-telemetry": "1.5.0",
});
});
it("reads the engine packages from a lockfile, without the direct dependencies", () => {
const lockfile = [
"importers:",
" '@earendil-works/[email protected]':",
"packages:",
" '@earendil-works/[email protected]':",
" '@earendil-works/[email protected]':",
" [email protected]:",
"snapshots:",
" '@earendil-works/[email protected]':",
"packages:",
" '@earendil-works/[email protected]':",
" '@earendil-works/[email protected]([email protected])':",
" '@earendil-works/[email protected]([email protected])([email protected])':",
].join("\n");
expect(lockedEnginePackages({ "@earendil-works/pi-coding-agent": "1.4.0" }, lockfile)).toEqual({
"@earendil-works/pi-ai": "1.4.0",
"@earendil-works/pi-agent-core": "1.4.0",
});
});
it("refuses a lockfile holding two versions of one engine package", () => {
const lockfile = [
"packages:",
" '@earendil-works/[email protected]':",
" '@earendil-works/[email protected]([email protected])':",
].join("\n");
expect(() => lockedEnginePackages({}, lockfile)).toThrow(
"The lockfile holds two versions of @earendil-works/pi-codemode.",
);
// Two versions of a direct dependency are pnpm's business, not an override's.
expect(lockedEnginePackages({ "@earendil-works/pi-codemode": "1.4.0" }, lockfile)).toEqual({});
});
it("runs its self-check from the output with the engine resolved inside it", () => {
symlinkSync(join(PACKAGE, "node_modules"), join(out, "daemon", "node_modules"));
const check = spawnSync(process.execPath, [join(out, "daemon", "cli.mjs"), "--self-check"], {
encoding: "utf8",
env: {
PATH: "/usr/bin:/bin",
HOME: tempDir(),
GENTLE_DOT_DATA_DIR: tempDir(),
GENTLE_DOT_RUNTIME: out,
ENGRAM_BIN: "/x/engram",
},
});
expect(check.stderr).toBe("");
expect(check.status).toBe(0);
const report = JSON.parse(check.stdout);
const real = realpathSync(out);
expect(report).toMatchObject({
ok: true,
runtime: out,
uiDir: join(out, "ui"),
gentleShell: join(out, "daemon", "node_modules", "gentle-pi", "bin", "gentle-shell.mjs"),
engineCli: join(out, "daemon", "node_modules", ENGINE_CLI),
engramBin: "/x/engram",
});
expect(realpathSync(report.approvalGuard)).toBe(join(real, "daemon", "extensions", "approval-guard.ts"));
expect(realpathSync(report.identity)).toBe(join(real, "daemon", "identity.md"));
});
it.runIf(process.platform === "win32")(
"emits a `pi.cmd` that runs the runtime's node.exe and engine wherever the runtime is, arguments intact",
() => {
const moved = join(tempDir(), "moved runtime");
mkdirSync(join(moved, "bin"), { recursive: true });
mkdirSync(join(moved, "node", "bin"), { recursive: true });
copyFileSync(join(out, "bin", "pi.cmd"), join(moved, "bin", "pi.cmd"));
copyFileSync(process.execPath, join(moved, "node", "bin", "node.exe"));
const engine = join(moved, "daemon", "node_modules", ENGINE_CLI);
mkdirSync(join(engine, ".."), { recursive: true });
writeFileSync(engine, "console.log(JSON.stringify([process.execPath, ...process.argv.slice(1)]));\n");
// .cmd files run through cmd.exe; /s strips only the outer quotes, so the quoted path and arguments stay as written.
const run = spawnSync(
"cmd.exe",
["/d", "/s", "/c", `""${join(moved, "bin", "pi.cmd")}" --version "a b" "wow!""`],
{ encoding: "utf8", windowsVerbatimArguments: true },
);
expect(run.stderr).toBe("");
expect(run.status).toBe(0);
expect(JSON.parse(run.stdout)).toEqual([
join(moved, "node", "bin", "node.exe"),
engine,
"--version",
"a b",
"wow!",
]);
},
);
it.skipIf(process.platform === "win32")(
"emits a `pi` wrapper that runs the runtime's node and engine wherever the runtime is",
() => {
expect(statSync(join(out, "bin", "pi")).mode & 0o111).toBe(0o111);
const moved = tempDir();
mkdirSync(join(moved, "bin"));
mkdirSync(join(moved, "node", "bin"), { recursive: true });
copyFileSync(join(out, "bin", "pi"), join(moved, "bin", "pi"));
writeFileSync(join(moved, "node", "bin", "node"), '#!/bin/sh\nprintf "%s\\n" "$0" "$@"\n', {
mode: 0o755,
});
const linked = join(tempDir(), "pi");
symlinkSync(join(moved, "bin", "pi"), linked);
const real = realpathSync(moved);
for (const entry of [join(moved, "bin", "pi"), linked]) {
const run = spawnSync(entry, ["--version", "a b"], { encoding: "utf8" });
expect(run.status).toBe(0);
expect(run.stdout.split("\n")).toEqual([
join(real, "node", "bin", "node"),
join(real, "daemon", "node_modules", ENGINE_CLI),
"--version",
"a b",
"",
]);
}
expect(existsSync(join(PACKAGE, "node_modules", ENGINE_CLI))).toBe(true);
},
);
});