|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { runDoctorHealthFlow } from "./doctor-health.js"; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + outro: vi.fn(), |
| 6 | + writeUpdatePostInstallDoctorResult: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("@clack/prompts", () => ({ |
| 10 | + intro: vi.fn(), |
| 11 | + outro: mocks.outro, |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../config/paths.js", () => ({ |
| 15 | + resolveStateDir: () => "/tmp/openclaw-doctor-state-does-not-exist", |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("../state/openclaw-database-preflight.js", () => ({ |
| 19 | + OpenClawDatabaseSchemaPreflightError: class extends Error {}, |
| 20 | + preflightOpenClawDatabaseSchemas: () => ({ incompatible: [] }), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("../state/openclaw-agent-db.js", () => ({ |
| 24 | + OPENCLAW_AGENT_SCHEMA_VERSION: 1, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("../state/openclaw-state-db.js", () => ({ |
| 28 | + OPENCLAW_STATE_SCHEMA_VERSION: 1, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("../commands/doctor-prompter.js", () => ({ |
| 32 | + createDoctorPrompter: () => ({}), |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock("../infra/openclaw-root.js", () => ({ |
| 36 | + resolveOpenClawPackageRoot: async () => undefined, |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("../commands/doctor-update.js", () => ({ |
| 40 | + maybeOfferUpdateBeforeDoctor: async () => ({ handled: false }), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock("../commands/doctor-ui.js", () => ({ |
| 44 | + maybeRepairUiProtocolFreshness: async () => undefined, |
| 45 | +})); |
| 46 | + |
| 47 | +vi.mock("../commands/doctor-install.js", () => ({ |
| 48 | + noteSourceInstallIssues: () => undefined, |
| 49 | +})); |
| 50 | + |
| 51 | +vi.mock("../commands/doctor/shared/plugin-runtime-symlinks.js", () => ({ |
| 52 | + noteStalePluginRuntimeSymlinks: async () => undefined, |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock("../commands/doctor-platform-notes.js", () => ({ |
| 56 | + noteStartupOptimizationHints: () => undefined, |
| 57 | +})); |
| 58 | + |
| 59 | +vi.mock("../commands/doctor-config-flow.js", () => ({ |
| 60 | + loadAndMaybeMigrateDoctorConfig: async () => ({ cfg: {}, shouldWriteConfig: true }), |
| 61 | +})); |
| 62 | + |
| 63 | +vi.mock("../config/config.js", () => ({ |
| 64 | + CONFIG_PATH: "/tmp/openclaw.json", |
| 65 | +})); |
| 66 | + |
| 67 | +vi.mock("../infra/update-doctor-result.js", () => ({ |
| 68 | + UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE: 86, |
| 69 | + UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH_ENV: "OPENCLAW_UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH", |
| 70 | + writeUpdatePostInstallDoctorResult: mocks.writeUpdatePostInstallDoctorResult, |
| 71 | +})); |
| 72 | + |
| 73 | +vi.mock("./doctor-health-contributions.js", () => ({ |
| 74 | + runDoctorHealthContributions: async (ctx: { |
| 75 | + configWriteRefusal?: "cron-owner-safety"; |
| 76 | + postInstallDoctorResult?: { |
| 77 | + status: "advisory"; |
| 78 | + advisory: { |
| 79 | + kind: "package-post-install-doctor"; |
| 80 | + message: string; |
| 81 | + reason: "deferred-configured-plugin-repair"; |
| 82 | + details: string[]; |
| 83 | + }; |
| 84 | + }; |
| 85 | + }) => { |
| 86 | + ctx.configWriteRefusal = "cron-owner-safety"; |
| 87 | + ctx.postInstallDoctorResult = { |
| 88 | + status: "advisory", |
| 89 | + advisory: { |
| 90 | + kind: "package-post-install-doctor", |
| 91 | + message: "recoverable plugin repair", |
| 92 | + reason: "deferred-configured-plugin-repair", |
| 93 | + details: ["plugin repair deferred"], |
| 94 | + }, |
| 95 | + }; |
| 96 | + }, |
| 97 | +})); |
| 98 | + |
| 99 | +describe("runDoctorHealthFlow", () => { |
| 100 | + beforeEach(() => { |
| 101 | + mocks.outro.mockClear(); |
| 102 | + mocks.writeUpdatePostInstallDoctorResult.mockClear(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("reports a cron ownership refusal instead of a recoverable post-install advisory", async () => { |
| 106 | + const runtime = { |
| 107 | + log: vi.fn(), |
| 108 | + error: vi.fn(), |
| 109 | + exit: vi.fn(), |
| 110 | + }; |
| 111 | + vi.stubEnv( |
| 112 | + "OPENCLAW_UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH", |
| 113 | + "/tmp/openclaw-update-doctor-result.json", |
| 114 | + ); |
| 115 | + |
| 116 | + try { |
| 117 | + await runDoctorHealthFlow(runtime, {}); |
| 118 | + } finally { |
| 119 | + vi.unstubAllEnvs(); |
| 120 | + } |
| 121 | + |
| 122 | + expect(mocks.outro).toHaveBeenCalledWith("Doctor finished, but config fixes were not applied."); |
| 123 | + expect(mocks.outro).not.toHaveBeenCalledWith("Doctor complete."); |
| 124 | + expect(runtime.exit).toHaveBeenCalledWith(1); |
| 125 | + expect(runtime.exit).not.toHaveBeenCalledWith(86); |
| 126 | + expect(mocks.writeUpdatePostInstallDoctorResult).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments