feat(cli)!: merge the daemon into pi2 as the daemon subcommand
`pi2 daemon [start] [flags]` now starts the web-console daemon. The `pi2d` executable, its bin entry and its entry module are removed: the daemon keeps its lock -> token -> plugin socket -> listen -> daemon.json lifecycle, its single stdout line (`pi2 daemon listening on ...`) and the signal handling that leaves managed nvim instances running. Service files are renamed to `pi2.service` / `com.pi2.daemon`, and the smoke and e2e harnesses start the daemon through `pi2 daemon`. BREAKING CHANGE: the `pi2d` command no longer exists; use `pi2 daemon` (or `pi2 daemon start`). Existing systemd/launchd units installed under the old names must be reinstalled with `pi2 install-service --write`.
This commit is contained in:
1 parent
b02f4e9070
commit
7e780515ac
38 files changed
+295
-232
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { DEFAULT_PORT_RETRIES, main, parseDaemonArgs } from "../../src/cli/pi2d.js";
|
||||
import { DEFAULT_PORT_RETRIES, main, parseDaemonArgs } from "../../src/cli/daemon.js";
|
||||
import { acquireLock } from "../../src/cli/lock.js";
|
||||
import { readCliToken, tokenPerms, writeCliToken } from "../../src/cli/token.js";
|
||||
import { UsageError } from "../../src/cli/util.js";
|
||||
@@ -44,12 +44,13 @@ describe("parseDaemonArgs", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("pi2d main (non-listening paths)", () => {
|
||||
describe("daemon main (non-listening paths)", () => {
|
||||
it("prints usage for --help", async () => {
|
||||
const out: string[] = [];
|
||||
const code = await main(["--help"], { out: (line) => out.push(line), err: () => undefined });
|
||||
expect(code).toBe(0);
|
||||
expect(out.join("\n")).toContain("Usage: pi2d [options]");
|
||||
expect(out[0]?.split("\n")[0]).toBe("pi2 daemon — pi2 daemon");
|
||||
expect(out.join("\n")).toContain("Usage: pi2 daemon [start] [options]");
|
||||
expect(out.join("\n")).toContain("--static-dir");
|
||||
});
|
||||
|
||||
@@ -57,7 +58,7 @@ describe("pi2d main (non-listening paths)", () => {
|
||||
const out: string[] = [];
|
||||
const code = await main(["--version"], { out: (line) => out.push(line), err: () => undefined });
|
||||
expect(code).toBe(0);
|
||||
expect(out[0]).toMatch(/^pi2d \d+\.\d+\.\d+/);
|
||||
expect(out[0]).toMatch(/^pi2 \d+\.\d+\.\d+/);
|
||||
});
|
||||
|
||||
it("exits 2 on bad arguments", async () => {
|
||||
@@ -112,12 +113,12 @@ describe("local CLI token", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("pi2d singleton lock vs cli.token", () => {
|
||||
describe("daemon singleton lock vs cli.token", () => {
|
||||
let dir: string;
|
||||
let tokenPath: string;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = tempDir("pi2d-main-lock-");
|
||||
dir = tempDir("pi2-daemon-main-lock-");
|
||||
setRuntimeEnv(dir);
|
||||
mkdirSync(join(dir, "web"), { recursive: true });
|
||||
tokenPath = join(dir, "run", "cli.token");
|
||||
@@ -147,7 +148,7 @@ describe("pi2d singleton lock vs cli.token", () => {
|
||||
const err: string[] = [];
|
||||
const code = await main([], { out: () => undefined, err: (line) => err.push(line) });
|
||||
expect(code).toBe(1);
|
||||
expect(err.join("\n")).toContain("another pi2d is running");
|
||||
expect(err.join("\n")).toContain("another pi2 daemon is running");
|
||||
expect(readCliToken(tokenPath)).toBe(original);
|
||||
} finally {
|
||||
holder.release();
|
||||
+3
-3
@@ -11,7 +11,7 @@ export const TEST_LIMITS: Limits = {
|
||||
maxConnections: 64,
|
||||
};
|
||||
|
||||
export function tempDir(prefix = "pi2d-cli-"): string {
|
||||
export function tempDir(prefix = "pi2-daemon-cli-"): string {
|
||||
return mkdtempSync(join(tmpdir(), prefix));
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ export function testConfig(overrides: Partial<Config> = {}): Config {
|
||||
staticDir: "/nonexistent-web-dist",
|
||||
corsOrigins: [],
|
||||
limits: TEST_LIMITS,
|
||||
tokenPath: "/tmp/pi2d-cli-test.sock.token",
|
||||
deviceStorePath: "/tmp/pi2d-cli-test-devices.json",
|
||||
tokenPath: "/tmp/pi2-daemon-cli-test.sock.token",
|
||||
deviceStorePath: "/tmp/pi2-daemon-cli-test-devices.json",
|
||||
};
|
||||
return { ...base, ...overrides, limits: { ...base.limits, ...(overrides.limits ?? {}) } };
|
||||
}
|
||||
|
||||
+56
-17
@@ -140,7 +140,7 @@ describe("resolveEndpoint / createClient", () => {
|
||||
});
|
||||
|
||||
it("explains a missing daemon or token", () => {
|
||||
expect(() => resolveEndpoint(dir)).toThrowError(/no running pi2d/);
|
||||
expect(() => resolveEndpoint(dir)).toThrowError(/no running pi2 daemon/);
|
||||
writeDaemonJson(DAEMON, join(dir, "daemon.json"));
|
||||
expect(() => resolveEndpoint(dir)).toThrowError(/missing CLI token/);
|
||||
});
|
||||
@@ -149,7 +149,7 @@ describe("resolveEndpoint / createClient", () => {
|
||||
writeDaemonJson({ ...DAEMON, port: 1 }, join(dir, "daemon.json"));
|
||||
writeCliToken(join(dir, "cli.token"));
|
||||
const client = createClient({ endpoint: resolveEndpoint(dir), timeoutMs: 500 });
|
||||
await expect(client.get("/api/v1/info")).rejects.toThrowError(/cannot reach pi2d/);
|
||||
await expect(client.get("/api/v1/info")).rejects.toThrowError(/cannot reach the pi2 daemon/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -255,11 +255,11 @@ describe("pi2 attach", () => {
|
||||
exec: recorded.exec,
|
||||
warn: (line) => warnings.push(line),
|
||||
client: () => {
|
||||
throw new CliError("no running pi2d (daemon.json missing)");
|
||||
throw new CliError("no running pi2 daemon (daemon.json missing)");
|
||||
},
|
||||
});
|
||||
expect(code).toBe(0);
|
||||
expect(warnings[0]).toContain("no running pi2d");
|
||||
expect(warnings[0]).toContain("no running pi2 daemon");
|
||||
expect(recorded.calls).toEqual([{ command: "nvim", args: [], cwd }]);
|
||||
});
|
||||
|
||||
@@ -274,12 +274,12 @@ describe("pi2 attach", () => {
|
||||
client: () => {
|
||||
const client = fakeClient();
|
||||
client.get = async () => {
|
||||
throw new CliError("cannot reach pi2d");
|
||||
throw new CliError("cannot reach the pi2 daemon");
|
||||
};
|
||||
return client;
|
||||
},
|
||||
});
|
||||
expect(warnings[0]).toContain("cannot reach pi2d");
|
||||
expect(warnings[0]).toContain("cannot reach the pi2 daemon");
|
||||
expect(recorded.calls).toEqual([{ command: "nvim", args: [], cwd }]);
|
||||
});
|
||||
});
|
||||
@@ -362,18 +362,18 @@ describe("pi2 doctor (daemon probe wiring)", () => {
|
||||
expect(daemon?.status).toBe("ok");
|
||||
expect(daemon?.detail).toContain("http://127.0.0.1:7712");
|
||||
expect(report.checks.find((check) => check.name === "protocol")?.status).toBe("ok");
|
||||
expect(report.checks.find((check) => check.name === "port")?.detail).toBe("7712 bound by pi2d");
|
||||
expect(report.checks.find((check) => check.name === "port")?.detail).toBe("7712 bound by the daemon");
|
||||
});
|
||||
|
||||
it("reports an unreachable daemon without throwing", async () => {
|
||||
const report = await runDoctorCommand({
|
||||
client: () => {
|
||||
throw new CliError("no running pi2d (daemon.json missing)");
|
||||
throw new CliError("no running pi2 daemon (daemon.json missing)");
|
||||
},
|
||||
});
|
||||
const daemon = report.checks.find((check) => check.name === "daemon");
|
||||
expect(daemon?.status).toBe("fail");
|
||||
expect(daemon?.detail).toContain("no running pi2d");
|
||||
expect(daemon?.detail).toContain("no running pi2 daemon");
|
||||
expect(report.ok).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -411,11 +411,11 @@ describe("pi2 main", () => {
|
||||
const code = await main(["status"], {
|
||||
err: (line) => err.push(line),
|
||||
client: () => {
|
||||
throw new CliError("no running pi2d");
|
||||
throw new CliError("no running pi2 daemon");
|
||||
},
|
||||
});
|
||||
expect(code).toBe(1);
|
||||
expect(err[0]).toBe("pi2: no running pi2d");
|
||||
expect(err[0]).toBe("pi2: no running pi2 daemon");
|
||||
});
|
||||
|
||||
it("routes attach through runAttach with an injected exec", async () => {
|
||||
@@ -430,6 +430,45 @@ describe("pi2 main", () => {
|
||||
expect(recorded.calls).toEqual([{ command: "nvim", args: [], cwd: process.cwd() }]);
|
||||
});
|
||||
|
||||
it("dispatches `daemon` to the daemon entry point", async () => {
|
||||
const out: string[] = [];
|
||||
const err: string[] = [];
|
||||
const seen: string[][] = [];
|
||||
const code = await main(["daemon", "--help"], {
|
||||
out: (line) => out.push(line),
|
||||
err: (line) => err.push(line),
|
||||
daemonMain: async (argv, io) => {
|
||||
seen.push([...argv]);
|
||||
io.out("daemon usage");
|
||||
io.err("daemon warning");
|
||||
return 7;
|
||||
},
|
||||
});
|
||||
expect(code).toBe(7);
|
||||
expect(seen).toEqual([["--help"]]);
|
||||
expect(out).toEqual(["daemon usage"]);
|
||||
expect(err).toEqual(["daemon warning"]);
|
||||
});
|
||||
|
||||
it("strips the optional `start` word before parsing daemon flags", async () => {
|
||||
const seen: string[][] = [];
|
||||
const code = await main(["daemon", "start", "--help"], {
|
||||
daemonMain: async (argv) => {
|
||||
seen.push([...argv]);
|
||||
return 0;
|
||||
},
|
||||
});
|
||||
expect(code).toBe(0);
|
||||
expect(seen).toEqual([["--help"]]);
|
||||
});
|
||||
|
||||
it("exits 2 on an unknown daemon subcommand", async () => {
|
||||
const err: string[] = [];
|
||||
const code = await main(["daemon", "bogus"], { out: () => undefined, err: (line) => err.push(line) });
|
||||
expect(code).toBe(2);
|
||||
expect(err.join("\n")).toContain("unexpected argument bogus");
|
||||
});
|
||||
|
||||
it("installs a service with --write through the injected writer", async () => {
|
||||
const out: string[] = [];
|
||||
const writes: { path: string; content: string }[] = [];
|
||||
@@ -438,7 +477,7 @@ describe("pi2 main", () => {
|
||||
platform: "linux",
|
||||
servicePaths: {
|
||||
nodePath: "/usr/bin/node",
|
||||
scriptPath: "/repo/dist/cli/pi2d.js",
|
||||
scriptPath: "/repo/dist/cli/pi2.js",
|
||||
stateDir: "/state",
|
||||
homeDir: "/home/u",
|
||||
pathEnv: "/usr/bin",
|
||||
@@ -446,9 +485,9 @@ describe("pi2 main", () => {
|
||||
writeFile: (path, content) => writes.push({ path, content }),
|
||||
});
|
||||
expect(code).toBe(0);
|
||||
expect(writes[0]?.path).toBe(join("/home/u/.config/systemd/user", "pi2d.service"));
|
||||
expect(writes[0]?.content).toContain("ExecStart=/usr/bin/node /repo/dist/cli/pi2d.js --foreground");
|
||||
expect(out.join("\n")).toContain("systemctl --user enable --now pi2d.service");
|
||||
expect(writes[0]?.path).toBe(join("/home/u/.config/systemd/user", "pi2.service"));
|
||||
expect(writes[0]?.content).toContain("ExecStart=/usr/bin/node /repo/dist/cli/pi2.js daemon --foreground");
|
||||
expect(out.join("\n")).toContain("systemctl --user enable --now pi2.service");
|
||||
});
|
||||
|
||||
it("prints the service on the default dry-run", async () => {
|
||||
@@ -458,7 +497,7 @@ describe("pi2 main", () => {
|
||||
platform: "darwin",
|
||||
servicePaths: {
|
||||
nodePath: "/usr/bin/node",
|
||||
scriptPath: "/repo/dist/cli/pi2d.js",
|
||||
scriptPath: "/repo/dist/cli/pi2.js",
|
||||
stateDir: "/state",
|
||||
homeDir: "/home/u",
|
||||
pathEnv: "/usr/bin",
|
||||
@@ -466,7 +505,7 @@ describe("pi2 main", () => {
|
||||
});
|
||||
expect(code).toBe(0);
|
||||
expect(out.join("\n")).toContain("<key>KeepAlive</key>");
|
||||
expect(out.join("\n")).toContain("would write /home/u/Library/LaunchAgents/com.pi2.pi2d.plist");
|
||||
expect(out.join("\n")).toContain("would write /home/u/Library/LaunchAgents/com.pi2.daemon.plist");
|
||||
});
|
||||
|
||||
it("renders doctor output and exits 1 when a check fails", async () => {
|
||||
|
||||
+24
-23
@@ -14,19 +14,19 @@ import { runInstallService, runUninstallService } from "../../src/cli/pi2.js";
|
||||
|
||||
const paths: ServicePaths = {
|
||||
nodePath: "/opt/node/bin/node",
|
||||
scriptPath: "/repo/dist/cli/pi2d.js",
|
||||
scriptPath: "/repo/dist/cli/pi2.js",
|
||||
stateDir: "/home/u/.local/state/pi2",
|
||||
homeDir: "/home/u",
|
||||
pathEnv: "/opt/node/bin:/usr/bin:/bin",
|
||||
};
|
||||
|
||||
const LINUX_UNIT = `[Unit]
|
||||
Description=pi2 daemon (pi2d)
|
||||
Description=pi2 daemon
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/opt/node/bin/node /repo/dist/cli/pi2d.js --foreground
|
||||
ExecStart=/opt/node/bin/node /repo/dist/cli/pi2.js daemon --foreground
|
||||
Environment=PATH=/opt/node/bin:/usr/bin:/bin
|
||||
Restart=on-failure
|
||||
RestartSec=2
|
||||
@@ -42,11 +42,12 @@ const MAC_PLIST = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.pi2.pi2d</string>
|
||||
<string>com.pi2.daemon</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/opt/node/bin/node</string>
|
||||
<string>/repo/dist/cli/pi2d.js</string>
|
||||
<string>/repo/dist/cli/pi2.js</string>
|
||||
<string>daemon</string>
|
||||
<string>--foreground</string>
|
||||
</array>
|
||||
<key>EnvironmentVariables</key>
|
||||
@@ -59,9 +60,9 @@ const MAC_PLIST = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<key>KeepAlive</key>
|
||||
<true/>
|
||||
<key>StandardOutPath</key>
|
||||
<string>/home/u/.local/state/pi2/pi2d.log</string>
|
||||
<string>/home/u/.local/state/pi2/pi2-daemon.log</string>
|
||||
<key>StandardErrorPath</key>
|
||||
<string>/home/u/.local/state/pi2/pi2d.err.log</string>
|
||||
<string>/home/u/.local/state/pi2/pi2-daemon.err.log</string>
|
||||
</dict>
|
||||
</plist>
|
||||
`;
|
||||
@@ -80,22 +81,22 @@ describe("systemd user unit", () => {
|
||||
});
|
||||
|
||||
it("quotes values with spaces", () => {
|
||||
const unit = renderSystemdUnit({ ...paths, scriptPath: "/repo dir/cli/pi2d.js" });
|
||||
expect(unit).toContain('ExecStart=/opt/node/bin/node "/repo dir/cli/pi2d.js" --foreground');
|
||||
const unit = renderSystemdUnit({ ...paths, scriptPath: "/repo dir/cli/pi2.js" });
|
||||
expect(unit).toContain('ExecStart=/opt/node/bin/node "/repo dir/cli/pi2.js" daemon --foreground');
|
||||
});
|
||||
|
||||
it("targets ~/.config/systemd/user and honours XDG_CONFIG_HOME", () => {
|
||||
expect(systemdUserDir(paths)).toBe("/home/u/.config/systemd/user");
|
||||
expect(systemdUserDir({ ...paths, configHome: "/xdg" })).toBe("/xdg/systemd/user");
|
||||
const service = renderService("linux", paths);
|
||||
expect(service.path).toBe("/home/u/.config/systemd/user/pi2d.service");
|
||||
expect(service.name).toBe("pi2d.service");
|
||||
expect(service.path).toBe("/home/u/.config/systemd/user/pi2.service");
|
||||
expect(service.name).toBe("pi2.service");
|
||||
expect(serviceDir(service)).toBe("/home/u/.config/systemd/user");
|
||||
expect(service.commands).toEqual([
|
||||
"systemctl --user daemon-reload",
|
||||
"systemctl --user enable --now pi2d.service",
|
||||
"systemctl --user enable --now pi2.service",
|
||||
]);
|
||||
expect(service.removeCommands).toContain("systemctl --user disable --now pi2d.service");
|
||||
expect(service.removeCommands).toContain("systemctl --user disable --now pi2.service");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -112,14 +113,14 @@ describe("LaunchAgent plist", () => {
|
||||
it("targets ~/Library/LaunchAgents", () => {
|
||||
expect(launchAgentDir(paths)).toBe("/home/u/Library/LaunchAgents");
|
||||
const service = renderService("darwin", paths);
|
||||
expect(service.path).toBe("/home/u/Library/LaunchAgents/com.pi2.pi2d.plist");
|
||||
expect(service.name).toBe("com.pi2.pi2d.plist");
|
||||
expect(service.path).toBe("/home/u/Library/LaunchAgents/com.pi2.daemon.plist");
|
||||
expect(service.name).toBe("com.pi2.daemon.plist");
|
||||
expect(service.commands[0]).toContain("launchctl bootstrap gui/$(id -u)");
|
||||
expect(service.removeCommands[0]).toContain("launchctl bootout");
|
||||
});
|
||||
|
||||
it("resolves dist/cli/pi2d.js next to the built CLI", () => {
|
||||
expect(defaultScriptPath()).toMatch(/dist\/cli\/pi2d\.js$/);
|
||||
it("resolves dist/cli/pi2.js next to the built CLI", () => {
|
||||
expect(defaultScriptPath()).toMatch(/dist\/cli\/pi2\.js$/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -137,8 +138,8 @@ describe("install-service / uninstall-service commands", () => {
|
||||
expect(code).toBe(0);
|
||||
expect(writes).toEqual([]);
|
||||
expect(lines.join("\n")).toContain("[Service]");
|
||||
expect(lines.join("\n")).toContain("would write /home/u/.config/systemd/user/pi2d.service");
|
||||
expect(lines.join("\n")).toContain("systemctl --user enable --now pi2d.service");
|
||||
expect(lines.join("\n")).toContain("would write /home/u/.config/systemd/user/pi2.service");
|
||||
expect(lines.join("\n")).toContain("systemctl --user enable --now pi2.service");
|
||||
});
|
||||
|
||||
it("writes the unit on --write and prints the enable commands", () => {
|
||||
@@ -152,8 +153,8 @@ describe("install-service / uninstall-service commands", () => {
|
||||
writeFile: (path, content) => writes.push({ path, content }),
|
||||
});
|
||||
expect(code).toBe(0);
|
||||
expect(writes).toEqual([{ path: join("/home/u/.config/systemd/user", "pi2d.service"), content: LINUX_UNIT }]);
|
||||
expect(lines[0]).toBe("wrote /home/u/.config/systemd/user/pi2d.service (mode 0644)");
|
||||
expect(writes).toEqual([{ path: join("/home/u/.config/systemd/user", "pi2.service"), content: LINUX_UNIT }]);
|
||||
expect(lines[0]).toBe("wrote /home/u/.config/systemd/user/pi2.service (mode 0644)");
|
||||
expect(lines.join("\n")).toContain("systemctl --user daemon-reload");
|
||||
});
|
||||
|
||||
@@ -170,7 +171,7 @@ describe("install-service / uninstall-service commands", () => {
|
||||
}),
|
||||
).toBe(0);
|
||||
expect(removed).toEqual([]);
|
||||
expect(dry.join("\n")).toContain("would remove /home/u/Library/LaunchAgents/com.pi2.pi2d.plist");
|
||||
expect(dry.join("\n")).toContain("would remove /home/u/Library/LaunchAgents/com.pi2.daemon.plist");
|
||||
|
||||
const lines: string[] = [];
|
||||
expect(
|
||||
@@ -182,7 +183,7 @@ describe("install-service / uninstall-service commands", () => {
|
||||
removeFile: (path) => removed.push(path),
|
||||
}),
|
||||
).toBe(0);
|
||||
expect(removed).toEqual(["/home/u/Library/LaunchAgents/com.pi2.pi2d.plist"]);
|
||||
expect(removed).toEqual(["/home/u/Library/LaunchAgents/com.pi2.daemon.plist"]);
|
||||
expect(lines.join("\n")).toContain("launchctl bootout");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user