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
@@ -0,0 +1,168 @@
|
||||
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/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";
|
||||
import { PROTOCOL_VERSION } from "../../src/protocol/types.js";
|
||||
import { clearRuntimeEnv, removeDir, setRuntimeEnv, tempDir } from "./helpers.js";
|
||||
|
||||
describe("parseDaemonArgs", () => {
|
||||
it("defaults to nothing set (foreground daemon, config defaults)", () => {
|
||||
expect(parseDaemonArgs([])).toEqual({ force: false, foreground: false, help: false, version: false });
|
||||
});
|
||||
|
||||
it("parses every flag, in both value syntaxes", () => {
|
||||
expect(parseDaemonArgs(["--port", "7676", "--host", "0.0.0.0", "--static-dir=/srv/ui", "--force", "--foreground"])).toEqual({
|
||||
port: 7676,
|
||||
host: "0.0.0.0",
|
||||
staticDir: "/srv/ui",
|
||||
force: true,
|
||||
foreground: true,
|
||||
help: false,
|
||||
version: false,
|
||||
});
|
||||
expect(parseDaemonArgs(["-p", "0"]).port).toBe(0);
|
||||
expect(parseDaemonArgs(["-f"]).force).toBe(true);
|
||||
expect(parseDaemonArgs(["--help"]).help).toBe(true);
|
||||
expect(parseDaemonArgs(["-h"]).help).toBe(true);
|
||||
expect(parseDaemonArgs(["-V"]).version).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects bad ports and stray positionals", () => {
|
||||
for (const port of ["abc", "-1", "70000", "1.5"]) {
|
||||
expect(() => parseDaemonArgs(["--port", port])).toThrow(UsageError);
|
||||
}
|
||||
expect(() => parseDaemonArgs(["extra"])).toThrow(/unexpected argument extra/);
|
||||
expect(() => parseDaemonArgs(["--nope"])).toThrow(/unknown option --nope/);
|
||||
expect(() => parseDaemonArgs(["--port"])).toThrow(/requires a value/);
|
||||
});
|
||||
|
||||
it("retries the default port at most +9", () => {
|
||||
expect(DEFAULT_PORT_RETRIES).toBe(9);
|
||||
});
|
||||
});
|
||||
|
||||
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[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");
|
||||
});
|
||||
|
||||
it("prints the version for --version", async () => {
|
||||
const out: string[] = [];
|
||||
const code = await main(["--version"], { out: (line) => out.push(line), err: () => undefined });
|
||||
expect(code).toBe(0);
|
||||
expect(out[0]).toMatch(/^pi2 \d+\.\d+\.\d+/);
|
||||
});
|
||||
|
||||
it("exits 2 on bad arguments", async () => {
|
||||
const err: string[] = [];
|
||||
const code = await main(["--port", "nope"], {
|
||||
out: () => undefined,
|
||||
err: (line) => err.push(line),
|
||||
});
|
||||
expect(code).toBe(2);
|
||||
expect(err[0]).toContain("--port must be an integer");
|
||||
});
|
||||
});
|
||||
|
||||
describe("local CLI token", () => {
|
||||
let dir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = tempDir("pi2-token-");
|
||||
});
|
||||
|
||||
afterEach(() => removeDir(dir));
|
||||
it("is 32 bytes of hex, written atomically with mode 0600", () => {
|
||||
const path = join(dir, "run", "cli.token");
|
||||
const token = writeCliToken(path);
|
||||
expect(token).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect(statSync(path).mode & 0o777).toBe(0o600);
|
||||
expect(readFileSync(path, "utf8").trim()).toBe(token);
|
||||
expect(readCliToken(path)).toBe(token);
|
||||
expect(tokenPerms(path)).toEqual({ exists: true, mode: 0o600 });
|
||||
});
|
||||
|
||||
it("rotates on every start", () => {
|
||||
const path = join(dir, "cli.token");
|
||||
const first = writeCliToken(path);
|
||||
const second = writeCliToken(path);
|
||||
expect(second).not.toBe(first);
|
||||
expect(readCliToken(path)).toBe(second);
|
||||
});
|
||||
|
||||
it("reads as undefined when missing", () => {
|
||||
const path = join(dir, "missing.token");
|
||||
expect(readCliToken(path)).toBeUndefined();
|
||||
expect(tokenPerms(path)).toEqual({ exists: false, mode: undefined });
|
||||
});
|
||||
|
||||
it("tightens the mode of a pre-existing world-readable token", () => {
|
||||
const path = join(dir, "cli.token");
|
||||
writeCliToken(path);
|
||||
chmodSync(path, 0o644);
|
||||
writeCliToken(path);
|
||||
expect(statSync(path).mode & 0o777).toBe(0o600);
|
||||
});
|
||||
});
|
||||
|
||||
describe("daemon singleton lock vs cli.token", () => {
|
||||
let dir: string;
|
||||
let tokenPath: string;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = tempDir("pi2-daemon-main-lock-");
|
||||
setRuntimeEnv(dir);
|
||||
mkdirSync(join(dir, "web"), { recursive: true });
|
||||
tokenPath = join(dir, "run", "cli.token");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
clearRuntimeEnv();
|
||||
removeDir(dir);
|
||||
});
|
||||
|
||||
function holdLock(): ReturnType<typeof acquireLock> {
|
||||
return acquireLock({
|
||||
payload: {
|
||||
port: 0,
|
||||
daemonId: "d-holder",
|
||||
startedAt: Date.now(),
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
it("does not rotate cli.token when another daemon holds the lock", async () => {
|
||||
mkdirSync(join(dir, "run"), { recursive: true });
|
||||
const original = writeCliToken(tokenPath);
|
||||
const holder = holdLock();
|
||||
try {
|
||||
const err: string[] = [];
|
||||
const code = await main([], { out: () => undefined, err: (line) => err.push(line) });
|
||||
expect(code).toBe(1);
|
||||
expect(err.join("\n")).toContain("another pi2 daemon is running");
|
||||
expect(readCliToken(tokenPath)).toBe(original);
|
||||
} finally {
|
||||
holder.release();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not create cli.token before the lock is acquired", async () => {
|
||||
const holder = holdLock();
|
||||
try {
|
||||
const code = await main([], { out: () => undefined, err: () => undefined });
|
||||
expect(code).toBe(1);
|
||||
expect(existsSync(tokenPath)).toBe(false);
|
||||
} finally {
|
||||
holder.release();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user