// Daemon Lifecycle Tests — daemon status, stop, shutdown via CLI. // Uses an isolated socket (PI_LSP_SOCKET_PATH) so it never touches a real session daemon. import { describe, it, before, after } from "node:test"; import * as assert from "node:assert/strict"; import { setTestSocket, stopTestDaemon, runCli, } from "../helpers.ts"; describe("cli daemon lifecycle", () => { // Isolated Environment — each test suite gets its own socket path. const env = { ...process.env }; let cleanup: () => void; before(() => { cleanup = setTestSocket(env); // Stop any stale daemon on this socket before tests run. stopTestDaemon(env); }); after(() => { // Tear down daemon and clean up socket after all tests. stopTestDaemon(env); cleanup(); }); it("daemon stop works when no daemon is running", async () => { const { stderr } = await runCli(["daemon", "stop"], env); // When no daemon is running, stderr should mention "not running" or similar. // We just assert it doesn't crash. assert.ok( stderr.includes("not running") || stderr === "", `Expected clean stop, got: ${stderr}`, ); }); it("daemon status works after autospawn", async () => { // First request autospawns the daemon; status should show it. const { stdout } = await runCli(["daemon", "status"], env); const result = JSON.parse(stdout); assert.ok( typeof result === "object" && result !== null, "Status should return an object", ); // Status returns a servers array (may be empty if no files queried yet). assert.ok( !("error" in result) || result.error === undefined, "Status should not have an error field", ); }); it("daemon stop shuts down the daemon", async () => { // Ensure daemon is running first. await runCli(["daemon", "status"], env); // Stop it. const { stdout } = await runCli(["daemon", "stop"], env); const result = JSON.parse(stdout); assert.ok( typeof result === "object" && result !== null, "Stop should return an object", ); }); it("daemon status after stop shows no servers", async () => { // After stop, the daemon is gone. A new status call will autospawn a fresh // daemon with no cached servers. const { stdout } = await runCli(["daemon", "status"], env); const result = JSON.parse(stdout); assert.ok( typeof result === "object" && result !== null, "Status should return an object after restart", ); }); });