File size: 5,197 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
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { mockSystemAccountHome } from "../../daemon/service.test-helpers.js";
import {
  lifecycleTestRuntime,
  resetLifecycleRuntimeLogs,
  resetLifecycleServiceMocks,
  service,
  stubEmptyGatewayEnv,
} from "./test-helpers/lifecycle-core-harness.js";

const owner = vi.hoisted(() => ({
  owner: "foreground-generation",
  pid: 9472,
  host: "fixture-host",
  startedAt: 100,
  port: 18789,
  mode: "foreground" as const,
  supervisor: null,
  state: "live" as const,
  expired: false,
}));
const mocks = vi.hoisted(() => ({
  readOwner: vi.fn<(params?: { env?: NodeJS.ProcessEnv }) => typeof owner | undefined>(() => owner),
  callGatewayCli: vi.fn(async () => ({ pid: owner.pid })),
  waitForGatewayHealthyListener: vi.fn(async () => ({ healthy: true })),
}));

vi.mock("../../runtime.js", () => ({ defaultRuntime: lifecycleTestRuntime }));
vi.mock("../../daemon/service.js", async (original) => ({
  ...(await original<typeof import("../../daemon/service.js")>()),
  resolveGatewayService: () => service,
}));
vi.mock("../../config/config.js", async (original) => ({
  ...(await original<typeof import("../../config/config.js")>()),
  readBestEffortConfig: async () => ({ gateway: { port: 18789 } }),
}));
vi.mock("../../config/io.js", () => ({
  createConfigIO: () => ({
    readBestEffortConfig: async () => ({ gateway: { port: 18789 } }),
  }),
}));
vi.mock("./lifecycle-action-preflight.js", () => ({
  getServiceActionPreflightFailure: async () => null,
}));
vi.mock("../../infra/gateway-owner-lease.js", () => ({
  readGatewayOwnerLease: mocks.readOwner,
}));
vi.mock("../../infra/gateway-lock.js", () => ({
  readActiveGatewayLockPort: async () => owner.port,
  readActiveGatewayLockIdentity: async () => ({
    ownerId: owner.owner,
    pid: owner.pid,
    port: owner.port,
    createdAt: "2026-09-13T02:27:25Z",
    startTime: owner.startedAt,
  }),
  isSameGatewayLockIdentity: (a: { ownerId?: string }, b: { ownerId?: string }) =>
    a.ownerId === b.ownerId,
}));
vi.mock("../../infra/gateway-processes.js", () => ({
  findVerifiedGatewayListenerPidsOnPortSync: () => [owner.pid],
  signalVerifiedGatewayPidSync: vi.fn(),
  formatGatewayPidList: (pids: number[]) => pids.join(", "),
}));
vi.mock("../../gateway/probe.js", () => ({
  probeGateway: async () => ({ ok: true, configSnapshot: { commands: { restart: true } } }),
}));
vi.mock("../../gateway/call.js", () => ({ callGatewayCli: mocks.callGatewayCli }));
vi.mock("./restart-health.js", async (original) => ({
  ...(await original<typeof import("./restart-health.js")>()),
  waitForGatewayHealthyListener: mocks.waitForGatewayHealthyListener,
}));
vi.mock("./lifecycle-audit.js", () => ({
  appendGatewayLifecycleAudit: vi.fn(),
  createGatewayLifecycleMutationAudit: () => undefined,
  createServiceLifecycleMutationAudit: () => undefined,
  appendServiceLifecycleRepairAudit: vi.fn(),
}));
vi.mock("../../infra/restart-intent.js", async (original) => ({
  ...(await original<typeof import("../../infra/restart-intent.js")>()),
  writeGatewayRestartIntentSync: () => true,
  clearGatewayRestartIntentSync: vi.fn(),
}));

beforeEach(() => {
  vi.clearAllMocks();
  resetLifecycleRuntimeLogs();
  resetLifecycleServiceMocks();
  stubEmptyGatewayEnv();
  mockSystemAccountHome();
  vi.stubEnv("OPENCLAW_SUPERVISOR_MODE", "");
  vi.stubEnv("OPENCLAW_CONTAINER_HINT", "");
  vi.stubEnv("OPENCLAW_UPDATE_IN_PROGRESS", "");
  mocks.readOwner.mockImplementation((params) =>
    params?.env?.OPENCLAW_STATE_DIR === "foreign-service-state" ? undefined : owner,
  );
  vi.spyOn(process, "platform", "get").mockReturnValue("win32");
  service.readCommand.mockResolvedValue({
    programArguments: [
      "node",
      "C:\\OpenClaw\\dist\\index.js",
      "gateway",
      "--port",
      "18789",
      "--task-supervisor",
    ],
    environment: {},
  });
  service.readRuntime.mockResolvedValue({ status: "stopped" });
  service.restart.mockRejectedValue(new Error("gateway port 18789 is still busy before restart"));
});

afterEach(() => {
  vi.restoreAllMocks();
  vi.unstubAllEnvs();
});

it.each([false, true])(
  "restarts the selected foreground owner despite an installed task (different state=%s)",
  async (differentState) => {
    if (differentState) {
      const command = await service.readCommand(process.env);
      if (!command) {
        throw new Error("Expected installed task fixture");
      }
      service.readCommand.mockResolvedValue({
        ...command,
        environment: { OPENCLAW_STATE_DIR: "foreign-service-state" },
      });
    }
    const { runDaemonRestart } = await import("./lifecycle.js");
    await expect(runDaemonRestart({ json: true })).resolves.toBe(true);
    expect(mocks.callGatewayCli).toHaveBeenCalledWith(
      expect.objectContaining({
        method: "gateway.restart.request",
        params: expect.objectContaining({
          target: { pid: owner.pid, ownerId: owner.owner, port: owner.port },
        }),
      }),
    );
    expect(service.restart).not.toHaveBeenCalled();
    expect(service.install).not.toHaveBeenCalled();
    expect(mocks.waitForGatewayHealthyListener).toHaveBeenCalled();
  },
);