|
| 1 | +// Configure wizard persistence tests protect config writes before local side effects. |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + intro: vi.fn(), |
| 7 | + outro: vi.fn(), |
| 8 | + select: vi.fn(), |
| 9 | + text: vi.fn(), |
| 10 | + note: vi.fn(), |
| 11 | + readConfigFileSnapshotForWrite: vi.fn(), |
| 12 | + writeWizardConfigFile: vi.fn(), |
| 13 | + probeGatewayReachable: vi.fn(), |
| 14 | + waitForGatewayReachable: vi.fn(), |
| 15 | + healthCommand: vi.fn(), |
| 16 | + maybeInstallDaemon: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("./configure.shared.js", () => ({ |
| 20 | + CONFIGURE_SECTION_OPTIONS: [ |
| 21 | + { value: "daemon", label: "Daemon", hint: "Manage the background service" }, |
| 22 | + { value: "health", label: "Health check", hint: "Run gateway checks" }, |
| 23 | + ], |
| 24 | + confirm: vi.fn(), |
| 25 | + intro: mocks.intro, |
| 26 | + outro: mocks.outro, |
| 27 | + select: mocks.select, |
| 28 | + text: mocks.text, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("../config/config.js", () => ({ |
| 32 | + readConfigFileSnapshotForWrite: mocks.readConfigFileSnapshotForWrite, |
| 33 | + resolveGatewayPort: () => 18789, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("../config/logging.js", () => ({ logConfigUpdated: vi.fn() })); |
| 37 | +vi.mock("../../packages/terminal-core/src/note.js", () => ({ note: mocks.note })); |
| 38 | +vi.mock("../wizard/clack-prompter.js", () => ({ createClackPrompter: () => ({}) })); |
| 39 | +vi.mock("../wizard/setup.shared.js", () => ({ |
| 40 | + writeWizardConfigFile: mocks.writeWizardConfigFile, |
| 41 | +})); |
| 42 | +vi.mock("../wizard/setup.secret-input.js", () => ({ |
| 43 | + resolveSetupSecretInputString: vi.fn(async () => undefined), |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("./onboard-helpers.js", () => ({ |
| 47 | + DEFAULT_WORKSPACE: "/tmp/openclaw-workspace", |
| 48 | + applyWizardMetadata: (config: OpenClawConfig) => config, |
| 49 | + guardCancel: (value: unknown) => value, |
| 50 | + probeGatewayReachable: mocks.probeGatewayReachable, |
| 51 | + resolveAdvertisedControlUiLinks: vi.fn(async () => ({ |
| 52 | + httpUrl: "http://127.0.0.1:18789/", |
| 53 | + wsUrl: "ws://127.0.0.1:18789", |
| 54 | + })), |
| 55 | + resolveLocalControlUiProbeLinks: vi.fn(() => ({ |
| 56 | + httpUrl: "http://127.0.0.1:18789/", |
| 57 | + wsUrl: "ws://127.0.0.1:18789", |
| 58 | + })), |
| 59 | + summarizeExistingConfig: vi.fn(() => "Gateway: remote"), |
| 60 | + waitForGatewayReachable: mocks.waitForGatewayReachable, |
| 61 | +})); |
| 62 | + |
| 63 | +vi.mock("./onboard-agent-target.js", () => ({ |
| 64 | + ensureOnboardingAgentWorkspace: vi.fn(), |
| 65 | + resolveOnboardingAgentTarget: () => ({ |
| 66 | + agentId: "main", |
| 67 | + agentDir: "/tmp/openclaw-agent", |
| 68 | + workspaceDir: "/tmp/openclaw-workspace", |
| 69 | + }), |
| 70 | +})); |
| 71 | + |
| 72 | +vi.mock("../plugins/install-record-commit.js", () => ({ |
| 73 | + commitConfigWithPendingPluginInstalls: vi.fn(), |
| 74 | +})); |
| 75 | +vi.mock("../plugins/plugin-registry.js", () => ({ resolvePluginContributionOwners: vi.fn() })); |
| 76 | +vi.mock("./configure.channels.js", () => ({ removeChannelConfigWizard: vi.fn() })); |
| 77 | +vi.mock("./configure.daemon.js", () => ({ maybeInstallDaemon: mocks.maybeInstallDaemon })); |
| 78 | +vi.mock("./configure.gateway-auth.js", () => ({ promptAuthConfig: vi.fn() })); |
| 79 | +vi.mock("./configure.gateway.js", () => ({ promptGatewayConfig: vi.fn() })); |
| 80 | +vi.mock("./health.js", () => ({ healthCommand: mocks.healthCommand })); |
| 81 | +vi.mock("./onboard-channels.js", () => ({ setupChannels: vi.fn() })); |
| 82 | +vi.mock("./onboard-remote.js", () => ({ promptRemoteGatewayConfig: vi.fn() })); |
| 83 | +vi.mock("./onboard-skills.js", () => ({ setupSkills: vi.fn() })); |
| 84 | + |
| 85 | +import { runConfigureWizard } from "./configure.wizard.js"; |
| 86 | + |
| 87 | +const runtime = { |
| 88 | + log: vi.fn(), |
| 89 | + error: vi.fn(), |
| 90 | + exit: vi.fn(), |
| 91 | +}; |
| 92 | + |
| 93 | +describe("configure wizard persistence before local side effects", () => { |
| 94 | + beforeEach(() => { |
| 95 | + vi.clearAllMocks(); |
| 96 | + mocks.readConfigFileSnapshotForWrite.mockResolvedValue({ |
| 97 | + snapshot: { |
| 98 | + exists: true, |
| 99 | + valid: true, |
| 100 | + config: { gateway: { mode: "remote" } }, |
| 101 | + issues: [], |
| 102 | + }, |
| 103 | + writeOptions: { |
| 104 | + expectedConfigPath: "/tmp/openclaw.json", |
| 105 | + ownedConfigPathForWrite: "/tmp/openclaw.json", |
| 106 | + }, |
| 107 | + }); |
| 108 | + mocks.probeGatewayReachable.mockResolvedValue({ ok: false }); |
| 109 | + mocks.text.mockResolvedValue("18789"); |
| 110 | + }); |
| 111 | + |
| 112 | + it.each([ |
| 113 | + ["health", "succeeded"], |
| 114 | + ["daemon", "succeeded"], |
| 115 | + ["health", "failed"], |
| 116 | + ["daemon", "failed"], |
| 117 | + ] as const)("persists Local before %s reports %s", async (section, outcome) => { |
| 118 | + const choices = ["local", section, "__continue"]; |
| 119 | + const events: string[] = []; |
| 120 | + const writes: OpenClawConfig[] = []; |
| 121 | + mocks.select.mockImplementation(async () => choices.shift()); |
| 122 | + mocks.writeWizardConfigFile.mockImplementation(async (config: OpenClawConfig) => { |
| 123 | + events.push("commit"); |
| 124 | + writes.push(config); |
| 125 | + return config; |
| 126 | + }); |
| 127 | + if (section === "health") { |
| 128 | + mocks.waitForGatewayReachable.mockImplementationOnce(async () => { |
| 129 | + events.push("health"); |
| 130 | + return { ok: outcome === "succeeded" }; |
| 131 | + }); |
| 132 | + } else { |
| 133 | + mocks.maybeInstallDaemon.mockImplementationOnce(async () => { |
| 134 | + events.push("daemon"); |
| 135 | + return outcome; |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + await runConfigureWizard({ command: "configure" }, runtime); |
| 140 | + |
| 141 | + expect(writes).toHaveLength(1); |
| 142 | + expect(writes[0]?.gateway?.mode).toBe("local"); |
| 143 | + expect(events).toEqual(["commit", section]); |
| 144 | + if (outcome === "failed") { |
| 145 | + expect(mocks.outro).toHaveBeenLastCalledWith( |
| 146 | + `Configuration updated, but ${section === "health" ? "health check" : "daemon setup"} failed.`, |
| 147 | + ); |
| 148 | + expect(mocks.outro).not.toHaveBeenCalledWith("Configuration updated."); |
| 149 | + } |
| 150 | + }); |
| 151 | +}); |
0 commit comments