Skip to content

Commit 5c9e4cd

Browse files
committed
refactor: clarify doctor repair flow
1 parent e8ba55a commit 5c9e4cd

29 files changed

Lines changed: 387 additions & 134 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const resolveGatewayProgramArguments = vi.fn(async (_opts?: unknown) => ({
88
programArguments: ["/bin/node", "cli", "gateway", "--port", "18789"],
99
}));
1010
const serviceInstall = vi.fn().mockResolvedValue(undefined);
11+
const serviceStage = vi.fn().mockResolvedValue(undefined);
1112
const serviceUninstall = vi.fn().mockResolvedValue(undefined);
1213
const serviceStop = vi.fn().mockResolvedValue(undefined);
1314
const serviceRestart = vi.fn().mockResolvedValue({ outcome: "completed" });
@@ -56,6 +57,7 @@ vi.mock("../daemon/service.js", async (importOriginal) => {
5657
label: "LaunchAgent",
5758
loadedText: "loaded",
5859
notLoadedText: "not loaded",
60+
stage: serviceStage,
5961
install: serviceInstall,
6062
uninstall: serviceUninstall,
6163
stop: serviceStop,

‎src/cli/daemon-cli/install.integration.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const serviceMock = vi.hoisted(() => ({
1111
label: "Gateway",
1212
loadedText: "loaded",
1313
notLoadedText: "not loaded",
14+
stage: vi.fn(async (_opts?: { environment?: Record<string, string | undefined> }) => {}),
1415
install: vi.fn(async (_opts?: { environment?: Record<string, string | undefined> }) => {}),
1516
uninstall: vi.fn(async () => {}),
1617
stop: vi.fn(async () => {}),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const service = vi.hoisted(() => ({
4444
loadedText: "loaded",
4545
notLoadedText: "not loaded",
4646
isLoaded: vi.fn(async () => false),
47+
stage: vi.fn(async () => {}),
4748
install: vi.fn(async () => {}),
4849
uninstall: vi.fn(async () => {}),
4950
restart: vi.fn(async () => {}),
@@ -167,6 +168,7 @@ describe("runDaemonInstall", () => {
167168
isGatewayDaemonRuntimeMock.mockReset();
168169
installDaemonServiceAndEmitMock.mockReset();
169170
service.isLoaded.mockReset();
171+
service.stage.mockReset();
170172
resetRuntimeCapture();
171173
actionState.warnings.length = 0;
172174
actionState.emitted.length = 0;
@@ -194,6 +196,7 @@ describe("runDaemonInstall", () => {
194196
isGatewayDaemonRuntimeMock.mockReturnValue(true);
195197
installDaemonServiceAndEmitMock.mockResolvedValue(undefined);
196198
service.isLoaded.mockResolvedValue(false);
199+
service.stage.mockResolvedValue(undefined);
197200
service.readCommand.mockResolvedValue(null);
198201
resolveNodeStartupTlsEnvironmentMock.mockReturnValue({
199202
NODE_EXTRA_CA_CERTS: undefined,

‎src/cli/daemon-cli/status.gather.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ vi.mock("../../daemon/service.js", () => ({
104104
label: "LaunchAgent",
105105
loadedText: "loaded",
106106
notLoadedText: "not loaded",
107+
stage: vi.fn(async () => {}),
108+
install: vi.fn(async () => {}),
109+
uninstall: vi.fn(async () => {}),
110+
stop: vi.fn(async () => {}),
111+
restart: vi.fn(async () => ({ outcome: "completed" as const })),
107112
isLoaded: serviceIsLoaded,
108113
readCommand: serviceReadCommand,
109114
readRuntime: serviceReadRuntime,

‎src/cli/daemon-cli/test-helpers/lifecycle-core-harness.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const runtimeLogs = lifecycleRuntimeCapture.runtimeLogs;
88
type LifecycleRuntimeHarness = typeof lifecycleRuntimeCapture.defaultRuntime;
99

1010
type LifecycleServiceHarness = GatewayService & {
11+
stage: MockFn<GatewayService["stage"]>;
1112
install: MockFn<GatewayService["install"]>;
1213
uninstall: MockFn<GatewayService["uninstall"]>;
1314
stop: MockFn<GatewayService["stop"]>;
@@ -23,6 +24,7 @@ export const service: LifecycleServiceHarness = {
2324
label: "TestService",
2425
loadedText: "loaded",
2526
notLoadedText: "not loaded",
27+
stage: vi.fn(),
2628
install: vi.fn(),
2729
uninstall: vi.fn(),
2830
stop: vi.fn(),
@@ -37,6 +39,7 @@ export function resetLifecycleRuntimeLogs() {
3739
}
3840

3941
export function resetLifecycleServiceMocks() {
42+
service.stage.mockClear();
4043
service.isLoaded.mockClear();
4144
service.readCommand.mockClear();
4245
service.restart.mockClear();

‎src/cli/gateway-cli.coverage.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ vi.mock("../daemon/service.js", () => ({
6565
label: "LaunchAgent",
6666
loadedText: "loaded",
6767
notLoadedText: "not loaded",
68+
stage: vi.fn(),
6869
install: vi.fn(),
6970
uninstall: vi.fn(),
7071
stop: vi.fn(),

‎src/commands/doctor-auth.deprecated-cli-profiles.test.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import type { OpenClawConfig } from "../config/config.js";
66
import { captureEnv } from "../test-utils/env.js";
77
import { maybeRemoveDeprecatedCliAuthProfiles } from "./doctor-auth.js";
88
import type { DoctorPrompter } from "./doctor-prompter.js";
9+
import type { DoctorRepairMode } from "./doctor-repair-mode.js";
910

1011
let envSnapshot: ReturnType<typeof captureEnv>;
1112
let tempAgentDir: string | undefined;
1213

1314
function makePrompter(confirmValue: boolean): DoctorPrompter {
15+
const repairMode: DoctorRepairMode = {
16+
shouldRepair: confirmValue,
17+
shouldForce: false,
18+
nonInteractive: false,
19+
canPrompt: true,
20+
updateInProgress: false,
21+
};
1422
return {
1523
confirm: vi.fn().mockResolvedValue(confirmValue),
16-
confirmRepair: vi.fn().mockResolvedValue(confirmValue),
17-
confirmAggressive: vi.fn().mockResolvedValue(confirmValue),
18-
confirmSkipInNonInteractive: vi.fn().mockResolvedValue(confirmValue),
24+
confirmAutoFix: vi.fn().mockResolvedValue(confirmValue),
25+
confirmAggressiveAutoFix: vi.fn().mockResolvedValue(confirmValue),
26+
confirmRuntimeRepair: vi.fn().mockResolvedValue(confirmValue),
1927
select: vi.fn().mockResolvedValue(""),
20-
shouldRepair: confirmValue,
21-
shouldForce: false,
28+
shouldRepair: repairMode.shouldRepair,
29+
shouldForce: repairMode.shouldForce,
30+
repairMode,
2231
};
2332
}
2433

‎src/commands/doctor-auth.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export async function maybeRemoveDeprecatedCliAuthProfiles(
153153
}
154154
note(lines.join("\n"), "Auth profiles");
155155

156-
const shouldRemove = await prompter.confirmRepair({
156+
const shouldRemove = await prompter.confirmAutoFix({
157157
message: "Remove deprecated CLI auth profiles now?",
158158
initialValue: true,
159159
});
@@ -316,7 +316,7 @@ export async function noteAuthProfileHealth(params: {
316316
return;
317317
}
318318

319-
const shouldRefresh = await params.prompter.confirmRepair({
319+
const shouldRefresh = await params.prompter.confirmAutoFix({
320320
message: "Refresh expiring OAuth tokens now? (static tokens need re-auth)",
321321
initialValue: true,
322322
});

‎src/commands/doctor-gateway-daemon-flow.test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const service = vi.hoisted(() => ({
55
isLoaded: vi.fn(),
66
readRuntime: vi.fn(),
77
restart: vi.fn(),
8+
stage: vi.fn(),
89
install: vi.fn(),
910
readCommand: vi.fn(),
1011
}));
@@ -143,14 +144,19 @@ describe("maybeRepairGatewayDaemon", () => {
143144
function createPrompter(confirmImpl: (message: string) => boolean) {
144145
return {
145146
confirm: vi.fn(),
146-
confirmRepair: vi.fn(),
147-
confirmAggressive: vi.fn(),
148-
confirmSkipInNonInteractive: vi.fn(async ({ message }: { message: string }) =>
149-
confirmImpl(message),
150-
),
147+
confirmAutoFix: vi.fn(),
148+
confirmAggressiveAutoFix: vi.fn(),
149+
confirmRuntimeRepair: vi.fn(async ({ message }: { message: string }) => confirmImpl(message)),
151150
select: vi.fn(),
152151
shouldRepair: false,
153152
shouldForce: false,
153+
repairMode: {
154+
shouldRepair: false,
155+
shouldForce: false,
156+
nonInteractive: false,
157+
canPrompt: true,
158+
updateInProgress: false,
159+
},
154160
};
155161
}
156162

‎src/commands/doctor-gateway-daemon-flow.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function maybeRepairLaunchAgentBootstrap(params: {
5959

6060
note("LaunchAgent is listed but not loaded in launchd.", `${params.title} LaunchAgent`);
6161

62-
const shouldFix = await params.prompter.confirmSkipInNonInteractive({
62+
const shouldFix = await params.prompter.confirmRuntimeRepair({
6363
message: `Repair ${params.title} LaunchAgent bootstrap now?`,
6464
initialValue: true,
6565
});
@@ -159,7 +159,7 @@ export async function maybeRepairGatewayDaemon(params: {
159159
}
160160
note("Gateway service not installed.", "Gateway");
161161
if (params.cfg.gateway?.mode !== "remote") {
162-
const install = await params.prompter.confirmSkipInNonInteractive({
162+
const install = await params.prompter.confirmRuntimeRepair({
163163
message: "Install gateway service now?",
164164
initialValue: true,
165165
});
@@ -230,7 +230,7 @@ export async function maybeRepairGatewayDaemon(params: {
230230
}
231231

232232
if (serviceRuntime?.status !== "running") {
233-
const start = await params.prompter.confirmSkipInNonInteractive({
233+
const start = await params.prompter.confirmRuntimeRepair({
234234
message: "Start gateway service now?",
235235
initialValue: true,
236236
});
@@ -257,7 +257,7 @@ export async function maybeRepairGatewayDaemon(params: {
257257
}
258258

259259
if (serviceRuntime?.status === "running") {
260-
const restart = await params.prompter.confirmSkipInNonInteractive({
260+
const restart = await params.prompter.confirmRuntimeRepair({
261261
message: "Restart gateway service now?",
262262
initialValue: true,
263263
});

0 commit comments

Comments
 (0)