File size: 37,059 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 | // Daemon lifecycle core tests cover service lifecycle transitions and platform adapters.
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import type { GatewayServiceControlArgs } from "../../daemon/service-types.js";
import type { GatewayService } from "../../daemon/service.js";
import { mockSystemAccountHome } from "../../daemon/service.test-helpers.js";
import { withEnvAsync } from "../../test-utils/env.js";
import {
createGatewayServiceRunArgs as createServiceRunArgs,
createGatewayUninstallArgs,
lifecycleTestRuntime,
resetLifecycleRuntimeLogs,
resetLifecycleServiceMocks,
lifecycleRuntimeLogs,
service,
stubEmptyGatewayEnv,
} from "./test-helpers/lifecycle-core-harness.js";
const loadConfig = vi.fn<() => OpenClawConfig>(() => ({
gateway: {
auth: {
token: "config-token",
},
},
}));
const writeGatewayRestartIntentSync = vi.fn();
const clearGatewayRestartIntentSync = vi.fn();
const appendGatewayLifecycleAudit = vi.fn();
const MISSING_SERVICE_PROGRAM = "/openclaw-test-missing-runtime/node";
const SERVICE_REPAIR_COMMAND_CASES = [
["Gateway", "", "", "openclaw gateway", "restart"],
["Node", "", "", "openclaw node", "install --force"],
["Node", "work", "", "openclaw --profile work node", "install --force"],
["Node", "work", "demo", "openclaw --container demo node", "install --force"],
] as const;
const createGatewayLifecycleMutationAudit = vi.fn(
(params: { action: string; source?: string }) => (mutation: { mode: string; pid?: number }) =>
appendGatewayLifecycleAudit({
action: params.action,
source: params.source ?? "cli",
...mutation,
}),
);
vi.mock("../../config/config.js", () => ({
getRuntimeConfig: () => loadConfig(),
loadConfig: () => loadConfig(),
readBestEffortConfig: async () => loadConfig(),
}));
vi.mock("../../runtime.js", () => ({
defaultRuntime: lifecycleTestRuntime,
}));
vi.mock("../../infra/restart-intent.js", () => ({
clearGatewayRestartIntentSync: () => clearGatewayRestartIntentSync(),
writeGatewayRestartIntentSync: (opts: unknown) => writeGatewayRestartIntentSync(opts),
}));
vi.mock("./lifecycle-audit.js", () => ({
appendGatewayLifecycleAudit: (params: unknown) => appendGatewayLifecycleAudit(params),
createGatewayLifecycleMutationAudit: (params: { action: string; source?: string }) =>
createGatewayLifecycleMutationAudit(params),
createServiceLifecycleMutationAudit: (params: { serviceNoun: string; action: string }) =>
params.serviceNoun === "Gateway" ? createGatewayLifecycleMutationAudit(params) : undefined,
appendServiceLifecycleRepairAudit: (params: {
serviceNoun: string;
action: string;
pid?: number;
}) => {
if (params.serviceNoun === "Gateway") {
appendGatewayLifecycleAudit({
action: params.action,
source: "cli",
mode: "service-repair",
...(params.pid === undefined ? {} : { pid: params.pid }),
});
}
},
}));
let runServiceRestart: typeof import("./lifecycle-core.js").runServiceRestart;
let runServiceStart: typeof import("./lifecycle-core.js").runServiceStart;
let runServiceStop: typeof import("./lifecycle-core.js").runServiceStop;
let runServiceUninstall: typeof import("./lifecycle-core.js").runServiceUninstall;
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- Test helper lets assertions ascribe logged JSON shape.
function readJsonLog<T extends object>() {
const jsonLine = lifecycleRuntimeLogs.find((line) => line.trim().startsWith("{"));
return JSON.parse(jsonLine ?? "{}") as T;
}
function stubConfigSecretRefGatewayToken() {
loadConfig.mockReturnValue({
secrets: {
providers: {
default: { source: "env" },
},
},
gateway: {
auth: {
mode: "token",
token: {
source: "env",
provider: "default",
id: "SERVICE_GATEWAY_TOKEN",
},
},
},
});
}
function stubServiceGatewayTokenEnv() {
service.readCommand.mockResolvedValue({
programArguments: [],
environment: {
OPENCLAW_GATEWAY_TOKEN: "service-token",
SERVICE_GATEWAY_TOKEN: "service-token",
},
});
}
async function withUnsupportedGatewayService(
run: (unsupportedService: GatewayService) => Promise<void>,
) {
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("aix");
try {
const { resolveGatewayService } = await import("../../daemon/service.js");
await run(resolveGatewayService());
} finally {
platformSpy.mockRestore();
}
}
function expectUnsupportedServiceCheckFailure() {
const payload = readJsonLog<{ ok?: boolean; error?: string }>();
expect(payload.ok).toBe(false);
expect(payload.error).toContain(
"Gateway service check failed: Error: Gateway service install not supported on aix",
);
}
describe("runServiceRestart token drift", () => {
beforeAll(async () => {
({ runServiceRestart, runServiceStart, runServiceStop, runServiceUninstall } =
await import("./lifecycle-core.js"));
});
afterEach(() => {
vi.restoreAllMocks();
});
beforeEach(() => {
mockSystemAccountHome();
appendGatewayLifecycleAudit.mockClear();
createGatewayLifecycleMutationAudit.mockClear();
resetLifecycleRuntimeLogs();
loadConfig.mockReset();
loadConfig.mockReturnValue({
gateway: {
auth: {
token: "config-token",
},
},
});
resetLifecycleServiceMocks();
writeGatewayRestartIntentSync.mockClear();
clearGatewayRestartIntentSync.mockClear();
service.readCommand.mockResolvedValue({
programArguments: [],
environment: { OPENCLAW_GATEWAY_TOKEN: "service-token" },
});
stubEmptyGatewayEnv();
});
it("rejects unsupported-platform start before not-loaded recovery", async () => {
const onNotLoaded = vi.fn(async () => ({
result: "started" as const,
message: "should not run",
loaded: true,
}));
await withUnsupportedGatewayService(async (unsupportedService) => {
await expect(
runServiceStart({
serviceNoun: "Gateway",
service: unsupportedService,
renderStartHints: () => ["openclaw gateway install"],
opts: { json: true },
onNotLoaded,
}),
).rejects.toThrow("__exit__:1");
});
expect(onNotLoaded).not.toHaveBeenCalled();
expectUnsupportedServiceCheckFailure();
});
it("rejects unsupported-platform stop before unmanaged fallback", async () => {
const onNotLoaded = vi.fn(async () => ({
result: "stopped" as const,
message: "should not run",
}));
await withUnsupportedGatewayService(async (unsupportedService) => {
await expect(
runServiceStop({
serviceNoun: "Gateway",
service: unsupportedService,
opts: { json: true },
onNotLoaded,
}),
).rejects.toThrow("__exit__:1");
});
expect(onNotLoaded).not.toHaveBeenCalled();
expectUnsupportedServiceCheckFailure();
});
it("rejects unsupported-platform restart before unmanaged fallback", async () => {
const onNotLoaded = vi.fn(async () => ({
result: "restarted" as const,
message: "should not run",
}));
const postRestartCheck = vi.fn(async () => {});
await withUnsupportedGatewayService(async (unsupportedService) => {
await expect(
runServiceRestart({
serviceNoun: "Gateway",
service: unsupportedService,
renderStartHints: () => ["openclaw gateway install"],
opts: { json: true },
onNotLoaded,
postRestartCheck,
}),
).rejects.toThrow("__exit__:1");
});
expect(onNotLoaded).not.toHaveBeenCalled();
expect(postRestartCheck).not.toHaveBeenCalled();
expectUnsupportedServiceCheckFailure();
});
it.each([
{
name: "initial uninstall inspection",
arrange: () => service.isLoaded.mockRejectedValue(new Error("initial inspection failed")),
run: () => runServiceUninstall(createGatewayUninstallArgs()),
action: "uninstall",
detail: "initial inspection failed",
stopCalls: 0,
uninstallCalls: 0,
},
{
name: "post-uninstall verification",
arrange: () =>
service.isLoaded
.mockResolvedValueOnce(false)
.mockRejectedValueOnce(new Error("uninstall verification failed")),
run: () => runServiceUninstall(createGatewayUninstallArgs()),
action: "uninstall",
detail: "uninstall verification failed",
stopCalls: 0,
uninstallCalls: 1,
},
{
name: "post-stop verification",
arrange: () =>
service.isLoaded
.mockResolvedValueOnce(true)
.mockRejectedValueOnce(new Error("stop verification failed")),
run: () => runServiceStop({ serviceNoun: "Gateway", service, opts: { json: true } }),
action: "stop",
detail: "stop verification failed",
stopCalls: 1,
uninstallCalls: 0,
},
])("fails $name without reporting false absence", async (testCase) => {
testCase.arrange();
await expect(testCase.run()).rejects.toThrow("__exit__:1");
expect(
readJsonLog<{ action?: string; ok?: boolean; result?: string; error?: string }>(),
).toEqual(
expect.objectContaining({
action: testCase.action,
ok: false,
error: expect.stringContaining(testCase.detail),
}),
);
expect(service.stop).toHaveBeenCalledTimes(testCase.stopCalls);
expect(service.uninstall).toHaveBeenCalledTimes(testCase.uninstallCalls);
});
it("fails restart with the container hint when no service is installed", async () => {
service.isLoaded.mockResolvedValue(false);
service.readCommand.mockResolvedValue(null);
const hasInstalledDefinition = vi.fn(async () => false);
vi.stubEnv("OPENCLAW_CONTAINER_HINT", "openclaw-demo-container");
await expect(
runServiceRestart({
serviceNoun: "Gateway",
service: { ...service, hasInstalledDefinition } as GatewayService,
renderStartHints: () => [
"Restart the container or the service that manages it for openclaw-demo-container.",
"openclaw gateway install",
],
opts: { json: true },
}),
).rejects.toThrow("__exit__:1");
const payload = readJsonLog<{
action?: string;
ok?: boolean;
error?: string;
hints?: string[];
hintItems?: Array<{ kind: string; text: string }>;
}>();
expect(payload).toMatchObject({
action: "restart",
ok: false,
error: "Gateway service not loaded.",
});
expect(payload.hints).toContain(
"Restart the container or the service that manages it for openclaw-demo-container.",
);
expect(payload.hintItems).toContainEqual(
expect.objectContaining({ kind: "container-restart" }),
);
expect(hasInstalledDefinition).toHaveBeenCalledWith({ env: process.env });
});
it("restarts a disabled installed service through its native manager", async () => {
service.isLoaded.mockResolvedValue(false);
const hasInstalledDefinition = vi.fn(async () => true);
const onNotLoaded = vi.fn(async () => null);
const renderStartHints = vi.fn(() => ["openclaw gateway install"]);
service.restart.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.onMutation?.({ mode: "systemctl-restart" });
return { outcome: "completed" };
});
await expect(
runServiceRestart({
...createServiceRunArgs(),
service: { ...service, hasInstalledDefinition } as GatewayService,
renderStartHints,
onNotLoaded,
}),
).resolves.toBe(true);
expect(hasInstalledDefinition).toHaveBeenCalledWith({ env: process.env });
expect(service.restart).toHaveBeenCalledTimes(1);
expect(onNotLoaded).not.toHaveBeenCalled();
expect(renderStartHints).not.toHaveBeenCalled();
expect(appendGatewayLifecycleAudit).toHaveBeenCalledWith({
action: "restart",
source: "cli",
mode: "systemctl-restart",
});
expect(readJsonLog<{ ok?: boolean; result?: string; hints?: string[] }>()).toMatchObject({
ok: true,
result: "restarted",
});
});
it("runs the service mutation guard before restarting a loaded service", async () => {
const beforeServiceMutation = vi.fn();
await runServiceRestart({
...createServiceRunArgs(),
beforeServiceMutation,
});
expect(beforeServiceMutation).toHaveBeenCalledTimes(1);
expect(beforeServiceMutation.mock.invocationCallOrder[0]).toBeLessThan(
service.restart.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
);
});
it("fails restart when an installed service cannot be inspected", async () => {
service.isLoaded.mockRejectedValue(
new Error(
"systemctl is-enabled unavailable: Command failed during launch or output capture (EACCES)",
),
);
service.readCommand.mockResolvedValue(null);
const hasInstalledDefinition = vi.fn(async () => true);
const postRestartCheck = vi.fn(async () => {});
await expect(
runServiceRestart({
...createServiceRunArgs(),
service: { ...service, hasInstalledDefinition } as GatewayService,
postRestartCheck,
}),
).rejects.toThrow("__exit__:1");
expect(hasInstalledDefinition).not.toHaveBeenCalled();
expect(service.restart).not.toHaveBeenCalled();
expect(postRestartCheck).not.toHaveBeenCalled();
expect(readJsonLog<{ ok?: boolean; error?: string }>()).toMatchObject({
ok: false,
error: expect.stringContaining("systemctl is-enabled unavailable"),
});
});
it("aborts loaded-service mutation when the service guard rejects", async () => {
const repairLoadedService = vi.fn();
await expect(
runServiceRestart({
...createServiceRunArgs(),
beforeServiceMutation: () => {
throw new Error("service mutation denied");
},
repairLoadedService,
}),
).rejects.toThrow("service mutation denied");
expect(writeGatewayRestartIntentSync).not.toHaveBeenCalled();
expect(repairLoadedService).not.toHaveBeenCalled();
expect(service.restart).not.toHaveBeenCalled();
});
it("does not run the service mutation guard before not-loaded recovery", async () => {
service.isLoaded.mockResolvedValue(false);
service.readCommand.mockResolvedValue(null);
const beforeServiceMutation = vi.fn();
await runServiceRestart({
...createServiceRunArgs(),
beforeServiceMutation,
onNotLoaded: async () => ({
result: "restarted",
message: "Gateway restart signal sent to unmanaged process on port 18789: 4200.",
}),
});
expect(beforeServiceMutation).not.toHaveBeenCalled();
expect(service.restart).not.toHaveBeenCalled();
});
it("repairs managed port drift before restarting", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 1234 });
service.readCommand.mockResolvedValue({
programArguments: ["openclaw", "gateway", "--port", "18789"],
environment: { OPENCLAW_GATEWAY_PORT: "18789" },
});
type RepairLoadedService = NonNullable<
Parameters<typeof runServiceRestart>[0]["repairLoadedService"]
>;
const repairLoadedService = vi.fn<RepairLoadedService>(async () => ({
result: "restarted" as const,
message: "Gateway service definition repaired and restarted.",
loaded: true,
}));
await runServiceRestart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true, restartIntent: { waitMs: 2_500 } },
expectedPort: 19_001,
repairLoadedService,
});
expect(repairLoadedService).toHaveBeenCalledWith(
expect.objectContaining({
issues: [
{
code: "port-mismatch",
message: "service port 18789 does not match current gateway config port 19001",
},
],
}),
);
expect(service.restart).not.toHaveBeenCalled();
expect(writeGatewayRestartIntentSync).toHaveBeenCalledWith({
targetPid: 1234,
reason: "gateway.restart",
intent: { waitMs: 2_500 },
});
expect(readJsonLog<{ result?: string; message?: string }>()).toMatchObject({
result: "restarted",
message: "Gateway service definition repaired and restarted.",
});
});
it.each([true, false])(
"keeps Nix restart available without suggesting a forbidden token reinstall (json=%s)",
async (json) => {
await withEnvAsync({ OPENCLAW_NIX_MODE: "1" }, async () => {
await expect(
runServiceRestart({ ...createServiceRunArgs(true), opts: { json } }),
).resolves.toBe(true);
expect(service.restart).toHaveBeenCalledOnce();
const output = json
? readJsonLog<{ warnings: string[] }>().warnings.join("\n")
: lifecycleRuntimeLogs.join("\n");
expect(output).toContain("Config token differs from service token");
expect(output).toContain("Nix mode detected; service install is disabled.");
expect(output).not.toContain("gateway install --force");
});
},
);
it("emits drift warning when enabled", async () => {
await runServiceRestart(createServiceRunArgs(true));
expect(loadConfig).toHaveBeenCalledTimes(1);
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings?.some((warning) => warning.includes("gateway install --force"))).toBe(
true,
);
});
it("compares restart drift against config token even when caller env is set", async () => {
loadConfig.mockReturnValue({
gateway: {
auth: {
token: "config-token",
},
},
});
service.readCommand.mockResolvedValue({
programArguments: [],
environment: { OPENCLAW_GATEWAY_TOKEN: "env-token" },
});
vi.stubEnv("OPENCLAW_GATEWAY_TOKEN", "env-token");
await runServiceRestart(createServiceRunArgs(true));
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings?.some((warning) => warning.includes("gateway install --force"))).toBe(
true,
);
});
it("resolves config token SecretRefs using service command env before drift checks", async () => {
stubConfigSecretRefGatewayToken();
stubServiceGatewayTokenEnv();
await runServiceRestart(createServiceRunArgs(true));
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings).toBeUndefined();
});
it("prefers service command env over process env for SecretRef token drift resolution", async () => {
stubConfigSecretRefGatewayToken();
stubServiceGatewayTokenEnv();
vi.stubEnv("SERVICE_GATEWAY_TOKEN", "process-token");
await runServiceRestart(createServiceRunArgs(true));
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings).toBeUndefined();
});
it("skips drift warning when disabled", async () => {
await runServiceRestart({
serviceNoun: "Node",
service,
renderStartHints: () => [],
opts: { json: true },
});
expect(loadConfig).not.toHaveBeenCalled();
expect(service.readCommand).not.toHaveBeenCalled();
expect(writeGatewayRestartIntentSync).not.toHaveBeenCalled();
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings).toBeUndefined();
});
it("emits stopped when an unmanaged process handles stop", async () => {
service.isLoaded.mockResolvedValue(false);
await runServiceStop({
serviceNoun: "Gateway",
service,
opts: { json: true },
onNotLoaded: async () => ({
result: "stopped",
message: "Gateway stop signal sent to unmanaged process on port 18789: 4200.",
}),
});
const payload = readJsonLog<{ result?: string; message?: string }>();
expect(payload.result).toBe("stopped");
expect(payload.message).toContain("unmanaged process");
expect(service.stop).not.toHaveBeenCalled();
});
it("runs a requested managed stop even when the service is not loaded", async () => {
const onNotLoaded = vi.fn(async () => ({
result: "stopped" as const,
message: "Gateway stop signal sent to unmanaged process on port 18789: 4200.",
}));
service.isLoaded.mockResolvedValue(false);
await runServiceStop({
serviceNoun: "Gateway",
service,
opts: { json: true, disable: true },
stopWhenNotLoaded: true,
onNotLoaded,
});
const payload = readJsonLog<{ result?: string; service?: { loaded?: boolean } }>();
expect(payload.result).toBe("stopped");
expect(payload.service?.loaded).toBe(false);
expect(service.stop).toHaveBeenCalledTimes(1);
const [stopOptions] = service.stop.mock.calls[0] ?? [];
expect(stopOptions?.env).toBe(process.env);
expect(stopOptions?.disable).toBe(true);
expect(onNotLoaded).not.toHaveBeenCalled();
});
it("emits started when a not-loaded start path repairs the service", async () => {
service.isLoaded.mockResolvedValue(false);
await runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
onNotLoaded: async () => ({
result: "started",
message:
"Gateway LaunchAgent was installed but not loaded; re-bootstrapped launchd service.",
loaded: true,
}),
});
const payload = readJsonLog<{
result?: string;
message?: string;
service?: { loaded?: boolean };
}>();
expect(payload.result).toBe("started");
expect(payload.message).toContain("re-bootstrapped");
expect(payload.service?.loaded).toBe(true);
expect(service.start).not.toHaveBeenCalled();
});
it("runs restart health checks after an unmanaged restart signal", async () => {
const postRestartCheck = vi.fn(async () => {});
service.isLoaded.mockResolvedValue(false);
service.readCommand.mockResolvedValue(null);
await runServiceRestart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
onNotLoaded: async () => ({
result: "restarted",
message: "Gateway restart signal sent to unmanaged process on port 18789: 4200.",
}),
postRestartCheck,
});
expect(postRestartCheck).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({ activationAccepted: true }),
);
expect(service.restart).not.toHaveBeenCalled();
const payload = readJsonLog<{ result?: string; message?: string }>();
expect(payload.result).toBe("restarted");
expect(payload.message).toContain("unmanaged process");
});
it("emits loaded restart state when launchd repair handles a not-loaded restart", async () => {
const postRestartCheck = vi.fn(async () => {});
service.isLoaded.mockResolvedValue(false);
service.readCommand.mockResolvedValue(null);
await runServiceRestart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
onNotLoaded: async () => ({
result: "restarted",
message:
"Gateway LaunchAgent was installed but not loaded; re-bootstrapped launchd service.",
loaded: true,
}),
postRestartCheck,
});
expect(postRestartCheck).toHaveBeenCalledTimes(1);
expect(service.restart).not.toHaveBeenCalled();
const payload = readJsonLog<{
result?: string;
message?: string;
service?: { loaded?: boolean };
}>();
expect(payload.result).toBe("restarted");
expect(payload.message).toContain("re-bootstrapped");
expect(payload.service?.loaded).toBe(true);
});
it("skips restart health checks when restart is only scheduled", async () => {
const postRestartCheck = vi.fn(async () => {});
service.restart.mockResolvedValue({ outcome: "scheduled" });
const result = await runServiceRestart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
postRestartCheck,
});
expect(result).toBe(true);
expect(postRestartCheck).not.toHaveBeenCalled();
const payload = readJsonLog<{ result?: string; message?: string }>();
expect(payload.result).toBe("scheduled");
expect(payload.message).toBe("restart scheduled, gateway will restart momentarily");
});
it("writes a restart intent before service-manager restart", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 1234 });
await runServiceRestart(createServiceRunArgs());
expect(writeGatewayRestartIntentSync).toHaveBeenCalledWith({
targetPid: 1234,
reason: "gateway.restart",
});
expect(clearGatewayRestartIntentSync).not.toHaveBeenCalled();
expect(service.restart).toHaveBeenCalledTimes(1);
});
it("captures service restart warnings in json restart output", async () => {
service.restart.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.warn?.(
"Existing generated LaunchAgent env wrapper contains custom behavior and will be overwritten.",
);
return { outcome: "completed" };
});
await runServiceRestart(createServiceRunArgs());
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings).toContain(
"Existing generated LaunchAgent env wrapper contains custom behavior and will be overwritten.",
);
expect(service.restart).toHaveBeenCalledWith(
expect.objectContaining({ warn: expect.any(Function) }),
);
});
it("writes restart force and wait options into the service-manager intent", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 1234 });
await runServiceRestart({
...createServiceRunArgs(),
opts: {
json: true,
restartIntent: {
waitMs: 2_500,
},
},
});
expect(writeGatewayRestartIntentSync).toHaveBeenCalledWith({
targetPid: 1234,
reason: "gateway.restart",
intent: {
waitMs: 2_500,
},
});
});
it("clears restart intent when service-manager restart fails before signaling", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 1234 });
writeGatewayRestartIntentSync.mockReturnValueOnce(true);
service.restart.mockRejectedValueOnce(new Error("launchctl failed before signaling"));
await expect(runServiceRestart(createServiceRunArgs())).rejects.toThrow("__exit__:1");
expect(writeGatewayRestartIntentSync).toHaveBeenCalledWith({
targetPid: 1234,
reason: "gateway.restart",
});
expect(clearGatewayRestartIntentSync).toHaveBeenCalledOnce();
});
it("reports an already-running gateway without starting it", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 4242 });
await runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
const payload = readJsonLog<{ ok?: boolean; result?: string; message?: string }>();
expect(payload).toMatchObject({
ok: true,
result: "already-running",
message: "Gateway service already running (pid 4242).",
});
expect(service.start).not.toHaveBeenCalled();
expect(appendGatewayLifecycleAudit).not.toHaveBeenCalled();
});
it.each(SERVICE_REPAIR_COMMAND_CASES)(
"warns in json with the %s service repair command and active context",
async (serviceNoun, profile, container, command, repairAction) => {
vi.stubEnv("OPENCLAW_PROFILE", profile);
vi.stubEnv("OPENCLAW_CONTAINER_HINT", container);
service.readRuntime.mockResolvedValue({ status: "running", pid: 4242 });
service.readCommand.mockResolvedValue({
programArguments: [MISSING_SERVICE_PROGRAM, "openclaw", serviceNoun.toLowerCase()],
});
await runServiceStart({
...createServiceRunArgs(),
serviceNoun,
repairLoadedService: serviceNoun === "Gateway" ? vi.fn(async () => null) : undefined,
});
const payload = readJsonLog<{ result?: string; warnings?: string[] }>();
expect(payload.result).toBe("already-running");
expect(payload.warnings).toEqual([
`${serviceNoun} service already running, but its installed service definition needs repair: service command points at a missing path: ${MISSING_SERVICE_PROGRAM}; run \`${command} ${repairAction}\` to apply.`,
]);
expect(service.start).not.toHaveBeenCalled();
},
);
it.each([
["Gateway", "restart"],
["Node", "install --force"],
])(
"prints one warning line when an already-running %s service needs repair",
async (serviceNoun, repairAction) => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 4242 });
service.readCommand.mockResolvedValue({
programArguments: [MISSING_SERVICE_PROGRAM, "openclaw", serviceNoun.toLowerCase()],
});
await runServiceStart({
serviceNoun,
service,
renderStartHints: () => [],
repairLoadedService: serviceNoun === "Gateway" ? vi.fn(async () => null) : undefined,
});
const repairWarnings = lifecycleRuntimeLogs.filter((line) =>
line.startsWith(
`${serviceNoun} service already running, but its installed service definition needs repair:`,
),
);
expect(repairWarnings).toHaveLength(1);
expect(repairWarnings[0]).toContain(
`run \`openclaw ${serviceNoun.toLowerCase()} ${repairAction}\` to apply.`,
);
expect(service.start).not.toHaveBeenCalled();
},
);
it("audits a service start that actually mutates the gateway", async () => {
service.start.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.onMutation?.({ mode: "kickstart" });
});
await runServiceStart(createServiceRunArgs());
expect(appendGatewayLifecycleAudit).toHaveBeenCalledWith({
action: "start",
source: "cli",
mode: "kickstart",
});
});
it("audits direct managed restart mutations", async () => {
service.readRuntime.mockResolvedValue({ status: "running", pid: 4242 });
service.restart.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.onMutation?.({ mode: "kickstart" });
return { outcome: "completed" };
});
await runServiceRestart(createServiceRunArgs());
expect(appendGatewayLifecycleAudit).toHaveBeenCalledWith({
action: "restart",
source: "cli",
mode: "kickstart",
});
});
it("audits direct managed stop mutations", async () => {
service.stop.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.onMutation?.({ mode: "bootout" });
});
await runServiceStop({
serviceNoun: "Gateway",
service,
opts: { json: true },
});
expect(appendGatewayLifecycleAudit).toHaveBeenCalledWith({
action: "stop",
source: "cli",
mode: "bootout",
});
});
it("captures service start warnings in json start output", async () => {
service.start.mockImplementationOnce(async (args?: GatewayServiceControlArgs) => {
args?.warn?.(
"Existing generated LaunchAgent env wrapper contains custom behavior and will be overwritten.",
);
});
await runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
});
const payload = readJsonLog<{ warnings?: string[] }>();
expect(payload.warnings).toContain(
"Existing generated LaunchAgent env wrapper contains custom behavior and will be overwritten.",
);
expect(service.start).toHaveBeenCalledWith(
expect.objectContaining({ warn: expect.any(Function) }),
);
});
it("repairs loaded services with port drift during start before reporting success", async () => {
service.readCommand.mockResolvedValue({
programArguments: ["openclaw", "gateway", "--port", "18789"],
});
type RepairLoadedService = NonNullable<
Parameters<typeof runServiceStart>[0]["repairLoadedService"]
>;
const repairLoadedService = vi.fn<RepairLoadedService>(async (ctx) => {
ctx.warn?.(
"Existing generated LaunchAgent env wrapper contains custom behavior and will be overwritten.",
);
return {
result: "started" as const,
message: "Gateway service definition repaired and started.",
warnings: ["service port 18789 does not match current gateway config port 19001"],
loaded: true,
};
});
await runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => [],
opts: { json: true },
repairLoadedService,
expectedPort: 19_001,
});
expect(repairLoadedService).toHaveBeenCalledTimes(1);
expect(service.start).not.toHaveBeenCalled();
const payload = readJsonLog<{
result?: string;
message?: string;
warnings?: string[];
service?: { loaded?: boolean };
}>();
expect(payload.result).toBe("started");
expect(payload.message).toBe("Gateway service definition repaired and started.");
expect(payload.warnings).toEqual(
expect.arrayContaining([
expect.stringContaining("service port 18789"),
expect.stringContaining("custom behavior and will be overwritten"),
]),
);
expect(payload.service?.loaded).toBe(true);
});
it.each(SERVICE_REPAIR_COMMAND_CASES)(
"fails %s service start with its own install hint when repair is required",
async (serviceNoun, profile, container, command) => {
vi.stubEnv("OPENCLAW_PROFILE", profile);
vi.stubEnv("OPENCLAW_CONTAINER_HINT", container);
service.readCommand.mockResolvedValue({
programArguments: [MISSING_SERVICE_PROGRAM, "openclaw", serviceNoun.toLowerCase()],
});
await expect(runServiceStart({ ...createServiceRunArgs(), serviceNoun })).rejects.toThrow(
"__exit__:1",
);
const payload = readJsonLog<{
ok?: boolean;
error?: string;
hints?: string[];
hintItems?: Array<{ kind: string; text: string }>;
}>();
expect(payload.ok).toBe(false);
expect(payload.error).toContain("service needs repair");
expect(payload.hints).toEqual([`${command} install --force`]);
expect(payload.hintItems).toEqual([{ kind: "install", text: `${command} install --force` }]);
resetLifecycleRuntimeLogs();
await expect(
runServiceStart({
...createServiceRunArgs(),
serviceNoun,
opts: { json: false },
}),
).rejects.toThrow("__exit__:1");
expect(lifecycleRuntimeLogs).toContain(`Tip: ${command} install --force`);
expect(service.start).not.toHaveBeenCalled();
},
);
it("fails start when starting a stopped installed service errors", async () => {
service.isLoaded.mockResolvedValue(false);
service.start.mockRejectedValue(new Error("launchctl kickstart failed: permission denied"));
await expect(runServiceStart(createServiceRunArgs())).rejects.toThrow("__exit__:1");
const payload = readJsonLog<{ ok?: boolean; error?: string }>();
expect(payload.ok).toBe(false);
expect(payload.error).toContain("launchctl kickstart failed: permission denied");
});
it("runs the start health check before reporting success", async () => {
service.isLoaded.mockResolvedValue(false);
const postStartCheck = vi.fn(async () => {});
await runServiceStart({ ...createServiceRunArgs(), postStartCheck });
expect(postStartCheck).toHaveBeenCalledOnce();
const payload = readJsonLog<{ ok?: boolean; result?: string }>();
expect(payload).toMatchObject({ ok: true, result: "started" });
});
it("fails start with install hints when no service is installed", async () => {
service.isLoaded.mockResolvedValue(false);
service.readCommand.mockResolvedValue(null);
await expect(
runServiceStart({
serviceNoun: "Gateway",
service,
renderStartHints: () => ["openclaw gateway install"],
opts: { json: true },
}),
).rejects.toThrow("__exit__:1");
const payload = readJsonLog<{
ok?: boolean;
error?: string;
hints?: string[];
hintItems?: Array<{ kind: string; text: string }>;
}>();
expect(payload.ok).toBe(false);
expect(payload.error).toBe("Gateway service not loaded.");
expect(payload.hints?.includes("openclaw gateway install")).toBe(true);
expect(
payload.hintItems?.some(
(item) => item.kind === "install" && item.text === "openclaw gateway install",
),
).toBe(true);
expect(service.start).not.toHaveBeenCalled();
});
});
|