Skip to content

Commit 08b7fcc

Browse files
test(cli): cover node daemon adapters (#122562)
Punchcard-Session: golden-valley-cedar-5x Co-authored-by: xin zhuang <[email protected]>
1 parent 6600930 commit 08b7fcc

2 files changed

Lines changed: 146 additions & 5 deletions

File tree

‎src/cli/node-cli/daemon.test.ts‎

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import type { GatewayServiceRuntime } from "../../daemon/service-runtime.js";
44
import type { GatewayServiceCommandConfig } from "../../daemon/service-types.js";
5-
import { runNodeDaemonInstall, runNodeDaemonStatus } from "./daemon.js";
5+
import {
6+
runNodeDaemonInstall,
7+
runNodeDaemonRestart,
8+
runNodeDaemonStart,
9+
runNodeDaemonStatus,
10+
runNodeDaemonStop,
11+
runNodeDaemonUninstall,
12+
} from "./daemon.js";
613

714
const mocks = vi.hoisted(() => {
815
const service = {
@@ -31,6 +38,7 @@ const mocks = vi.hoisted(() => {
3138
environment: {},
3239
environmentValueSources: {},
3340
})),
41+
failIfNixDaemonInstallMode: vi.fn(() => false),
3442
loadNodeHostConfig: vi.fn(),
3543
isSystemdUserServiceAvailable: vi.fn(async () => true),
3644
resolveSystemdUserServiceAccount: vi.fn(() => "pi"),
@@ -40,6 +48,10 @@ const mocks = vi.hoisted(() => {
4048
linger: "no",
4149
}),
4250
),
51+
runServiceRestart: vi.fn(),
52+
runServiceStart: vi.fn(),
53+
runServiceStop: vi.fn(),
54+
runServiceUninstall: vi.fn(),
4355
};
4456
});
4557

@@ -59,6 +71,13 @@ vi.mock("../../node-host/config.js", () => ({
5971
loadNodeHostConfig: mocks.loadNodeHostConfig,
6072
}));
6173

74+
vi.mock("../daemon-cli/lifecycle-core.js", () => ({
75+
runServiceRestart: mocks.runServiceRestart,
76+
runServiceStart: mocks.runServiceStart,
77+
runServiceStop: mocks.runServiceStop,
78+
runServiceUninstall: mocks.runServiceUninstall,
79+
}));
80+
6281
vi.mock("../../daemon/runtime-hints.js", () => ({
6382
buildPlatformRuntimeLogHints: () => [
6483
"Logs: node service log",
@@ -104,7 +123,7 @@ vi.mock("../daemon-cli/shared.js", async () => {
104123
}),
105124
formatRuntimeStatus: (runtime: GatewayServiceRuntime | undefined) => runtime?.status ?? "",
106125
resolveRuntimeStatusColor: () => "",
107-
failIfNixDaemonInstallMode: () => false,
126+
failIfNixDaemonInstallMode: mocks.failIfNixDaemonInstallMode,
108127
};
109128
});
110129

@@ -114,6 +133,7 @@ describe("runNodeDaemonInstall", () => {
114133
mocks.runtime.error.mockClear();
115134
mocks.runtime.writeJson.mockClear();
116135
mocks.runtime.exit.mockClear();
136+
mocks.failIfNixDaemonInstallMode.mockReset().mockReturnValue(false);
117137
mocks.service.install.mockReset().mockResolvedValue(undefined);
118138
mocks.service.isLoaded.mockReset().mockResolvedValue(false);
119139
mocks.buildNodeInstallPlan.mockReset().mockResolvedValue({
@@ -205,6 +225,26 @@ describe("runNodeDaemonInstall", () => {
205225
);
206226
});
207227

228+
it.each([
229+
["an invalid explicit port", { port: "abc" }, "Invalid --port"],
230+
["an unsupported runtime", { runtime: "deno" }, 'Invalid --runtime (use "node"'],
231+
])("rejects %s before building an install plan", async (_name, opts, error) => {
232+
await runNodeDaemonInstall(opts);
233+
234+
expect(mocks.runtime.error).toHaveBeenCalledWith(expect.stringContaining(error));
235+
expect(mocks.buildNodeInstallPlan).not.toHaveBeenCalled();
236+
expect(mocks.service.install).not.toHaveBeenCalled();
237+
});
238+
239+
it("does not build or install a service in Nix daemon mode", async () => {
240+
mocks.failIfNixDaemonInstallMode.mockReturnValue(true);
241+
242+
await runNodeDaemonInstall({});
243+
244+
expect(mocks.buildNodeInstallPlan).not.toHaveBeenCalled();
245+
expect(mocks.service.install).not.toHaveBeenCalled();
246+
});
247+
208248
it("warns about disabled systemd lingering after a fresh install (text mode)", async () => {
209249
// isLoaded=true so the service-load verification passes and the linger
210250
// diagnostic runs on the verified-success path.
@@ -309,6 +349,59 @@ describe("runNodeDaemonInstall", () => {
309349
});
310350
});
311351

352+
describe("node daemon lifecycle adapters", () => {
353+
beforeEach(() => {
354+
mocks.runServiceRestart.mockReset();
355+
mocks.runServiceStart.mockReset();
356+
mocks.runServiceStop.mockReset();
357+
mocks.runServiceUninstall.mockReset();
358+
});
359+
360+
it.each([
361+
{
362+
name: "start",
363+
action: runNodeDaemonStart,
364+
delegate: mocks.runServiceStart,
365+
expected: { renderStartHints: expect.any(Function) },
366+
},
367+
{
368+
name: "stop",
369+
action: runNodeDaemonStop,
370+
delegate: mocks.runServiceStop,
371+
expected: {},
372+
},
373+
{
374+
name: "restart",
375+
action: runNodeDaemonRestart,
376+
delegate: mocks.runServiceRestart,
377+
expected: { renderStartHints: expect.any(Function) },
378+
},
379+
{
380+
name: "uninstall",
381+
action: runNodeDaemonUninstall,
382+
delegate: mocks.runServiceUninstall,
383+
expected: {
384+
stopBeforeUninstall: false,
385+
assertNotLoadedAfterUninstall: false,
386+
},
387+
},
388+
])(
389+
"delegates $name with node-specific service options",
390+
async ({ action, delegate, expected }) => {
391+
await action({ json: true });
392+
393+
expect(delegate).toHaveBeenCalledWith(
394+
expect.objectContaining({
395+
serviceNoun: "Node",
396+
service: mocks.service,
397+
opts: { json: true },
398+
...expected,
399+
}),
400+
);
401+
},
402+
);
403+
});
404+
312405
describe("runNodeDaemonStatus", () => {
313406
function stdout(): string {
314407
return mocks.runtime.log.mock.calls.map(([line]) => line).join("\n");
@@ -353,6 +446,18 @@ describe("runNodeDaemonStatus", () => {
353446
expect(mocks.runtime.error).not.toHaveBeenCalled();
354447
});
355448

449+
it("reports an unknown runtime when runtime inspection fails", async () => {
450+
mocks.service.readRuntime.mockRejectedValue(new Error("permission denied"));
451+
452+
await runNodeDaemonStatus({ json: true });
453+
454+
expect(mocks.runtime.writeJson).toHaveBeenCalledWith({
455+
service: expect.objectContaining({
456+
runtime: { status: "unknown", detail: "Error: permission denied" },
457+
}),
458+
});
459+
});
460+
356461
it("keeps missing service-unit status on stderr and prints recovery hints on stdout", async () => {
357462
mocks.service.readRuntime.mockResolvedValue({ status: "stopped", missingUnit: true });
358463

‎src/cli/node-cli/register.test.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,48 @@ describe("registerNodeCli", () => {
6262
daemonMocks.runNodeDaemonUninstall.mockClear();
6363
});
6464

65-
it("registers node start for the macOS app node service manager", async () => {
65+
it.each([
66+
["status", daemonMocks.runNodeDaemonStatus],
67+
["uninstall", daemonMocks.runNodeDaemonUninstall],
68+
["stop", daemonMocks.runNodeDaemonStop],
69+
["start", daemonMocks.runNodeDaemonStart],
70+
["restart", daemonMocks.runNodeDaemonRestart],
71+
])("registers node %s and forwards --json", async (command, action) => {
6672
const program = createProgram();
6773

68-
await program.parseAsync(["node", "start", "--json"], { from: "user" });
74+
await program.parseAsync(["node", command, "--json"], { from: "user" });
75+
76+
expect(action.mock.calls[0]?.[0]?.json).toBe(true);
77+
});
78+
79+
it("forwards node install options to the daemon adapter", async () => {
80+
const program = createProgram();
81+
82+
await program.parseAsync(
83+
[
84+
"node",
85+
"install",
86+
"--port",
87+
"19000",
88+
"--host",
89+
"gateway.example",
90+
"--runtime",
91+
"node",
92+
"--force",
93+
"--json",
94+
],
95+
{ from: "user" },
96+
);
6997

70-
expect(daemonMocks.runNodeDaemonStart.mock.calls[0]?.[0]?.json).toBe(true);
98+
expect(daemonMocks.runNodeDaemonInstall).toHaveBeenCalledWith(
99+
expect.objectContaining({
100+
port: "19000",
101+
host: "gateway.example",
102+
runtime: "node",
103+
force: true,
104+
json: true,
105+
}),
106+
);
71107
});
72108

73109
it("rejects an explicit invalid node run port", async () => {

0 commit comments

Comments
 (0)