|
| 1 | +import { vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 3 | +import type { DoctorHealthFlowContext } from "./doctor-health-contributions.js"; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + outro: vi.fn(), |
| 7 | + config: vi.fn<() => OpenClawConfig>(), |
| 8 | + runContributions: vi.fn<(ctx: DoctorHealthFlowContext) => Promise<void>>(), |
| 9 | + writeUpdatePostInstallDoctorResult: vi.fn(), |
| 10 | + service: vi.fn(), |
| 11 | + packageRoot: vi.fn<() => string | undefined>(), |
| 12 | + restartedHealthy: true, |
| 13 | + emulateNativeInstall: true, |
| 14 | + servicePlatform: undefined as NodeJS.Platform | undefined, |
| 15 | + taskDefinitelyStopped: vi.fn(() => true), |
| 16 | + startupFallbackRuntime: vi.fn<() => Promise<{ status: string } | null>>(async () => null), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("@clack/prompts", () => ({ |
| 20 | + intro: vi.fn(), |
| 21 | + note: vi.fn(), |
| 22 | + outro: mocks.outro, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("../commands/doctor-prompter.js", () => ({ |
| 26 | + createDoctorPrompter: () => ({}), |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock("../infra/openclaw-root.js", async (importOriginal) => ({ |
| 30 | + ...(await importOriginal<typeof import("../infra/openclaw-root.js")>()), |
| 31 | + resolveOpenClawPackageRoot: async () => mocks.packageRoot(), |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("../daemon/service.js", async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<typeof import("../daemon/service.js")>()), |
| 36 | + resolveGatewayService: () => mocks.service(), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("../config/paths.js", async (importOriginal) => { |
| 40 | + const actual = await importOriginal<typeof import("../config/paths.js")>(); |
| 41 | + return { |
| 42 | + ...actual, |
| 43 | + // Native-manager cases use isolated storage; runtime-only coverage retains |
| 44 | + // the real install-identity policy instead of adopting the host service. |
| 45 | + isDefaultInstallIdentity: (env: NodeJS.ProcessEnv) => |
| 46 | + mocks.emulateNativeInstall || actual.isDefaultInstallIdentity(env), |
| 47 | + }; |
| 48 | +}); |
| 49 | + |
| 50 | +vi.mock("../daemon/schtasks-runtime.js", async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<typeof import("../daemon/schtasks-runtime.js")>()), |
| 52 | + isScheduledTaskDefinitelyNotRunning: mocks.taskDefinitelyStopped, |
| 53 | + readWindowsStartupFallbackRuntimeForUpdate: mocks.startupFallbackRuntime, |
| 54 | +})); |
| 55 | + |
| 56 | +vi.mock("../cli/update-cli/update-command-service-maintenance.js", async (importOriginal) => { |
| 57 | + const actual = |
| 58 | + await importOriginal< |
| 59 | + typeof import("../cli/update-cli/update-command-service-maintenance.js") |
| 60 | + >(); |
| 61 | + return { |
| 62 | + ...actual, |
| 63 | + maybeStopManagedServiceBeforeMutableUpdate: async ( |
| 64 | + params: Parameters<typeof actual.maybeStopManagedServiceBeforeMutableUpdate>[0], |
| 65 | + ) => { |
| 66 | + // Emulate the native manager only; workspace and SQLite identities must |
| 67 | + // retain the host filesystem's case semantics during real migration. |
| 68 | + const platform = mocks.servicePlatform |
| 69 | + ? vi.spyOn(process, "platform", "get").mockReturnValue(mocks.servicePlatform) |
| 70 | + : undefined; |
| 71 | + try { |
| 72 | + return await actual.maybeStopManagedServiceBeforeMutableUpdate(params); |
| 73 | + } finally { |
| 74 | + platform?.mockRestore(); |
| 75 | + } |
| 76 | + }, |
| 77 | + }; |
| 78 | +}); |
| 79 | + |
| 80 | +vi.mock("../cli/update-cli/update-command-service-plan.js", async (importOriginal) => ({ |
| 81 | + ...(await importOriginal<typeof import("../cli/update-cli/update-command-service-plan.js")>()), |
| 82 | + // The fixture owns an in-memory manager; native machine profile policy is |
| 83 | + // covered at the updater boundary and must not select a host service here. |
| 84 | + assertGatewayServiceManagementAllowedForUpdate: () => undefined, |
| 85 | + resolveGatewayServiceManagementBlockMessageForUpdate: () => undefined, |
| 86 | +})); |
| 87 | + |
| 88 | +vi.mock("../cli/daemon-cli/restart-health.js", async (importOriginal) => ({ |
| 89 | + ...(await importOriginal<typeof import("../cli/daemon-cli/restart-health.js")>()), |
| 90 | + waitForGatewayHealthyRestart: async () => ({ healthy: mocks.restartedHealthy }), |
| 91 | + renderRestartDiagnostics: () => ["synthetic readiness failure"], |
| 92 | +})); |
| 93 | + |
| 94 | +vi.mock("../commands/doctor-update.js", () => ({ |
| 95 | + maybeOfferUpdateBeforeDoctor: async () => ({ handled: false }), |
| 96 | +})); |
| 97 | + |
| 98 | +vi.mock("../commands/doctor-ui.js", () => ({ |
| 99 | + maybeRepairUiProtocolFreshness: async () => undefined, |
| 100 | +})); |
| 101 | + |
| 102 | +vi.mock("../commands/doctor-install.js", () => ({ |
| 103 | + noteSourceInstallIssues: () => undefined, |
| 104 | +})); |
| 105 | + |
| 106 | +vi.mock("../commands/doctor/shared/plugin-runtime-symlinks.js", () => ({ |
| 107 | + noteStalePluginRuntimeSymlinks: async () => undefined, |
| 108 | +})); |
| 109 | + |
| 110 | +vi.mock("../commands/doctor-platform-notes.js", () => ({ |
| 111 | + noteStartupOptimizationHints: () => undefined, |
| 112 | +})); |
| 113 | + |
| 114 | +vi.mock("../commands/doctor-config-flow.js", () => ({ |
| 115 | + loadAndMaybeMigrateDoctorConfig: async () => ({ cfg: mocks.config(), shouldWriteConfig: true }), |
| 116 | +})); |
| 117 | + |
| 118 | +vi.mock("../config/config.js", async (importOriginal) => ({ |
| 119 | + ...(await importOriginal<typeof import("../config/config.js")>()), |
| 120 | + CONFIG_PATH: "/tmp/openclaw.json", |
| 121 | +})); |
| 122 | + |
| 123 | +vi.mock("../infra/update-doctor-result.js", () => ({ |
| 124 | + UPDATE_POST_INSTALL_DOCTOR_ADVISORY_EXIT_CODE: 86, |
| 125 | + UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH_ENV: "OPENCLAW_UPDATE_POST_INSTALL_DOCTOR_RESULT_PATH", |
| 126 | + writeUpdatePostInstallDoctorResult: mocks.writeUpdatePostInstallDoctorResult, |
| 127 | +})); |
| 128 | + |
| 129 | +vi.mock("./doctor-health-contributions.js", () => ({ |
| 130 | + runDoctorHealthContributions: mocks.runContributions, |
| 131 | +})); |
| 132 | + |
| 133 | +export { mocks }; |
0 commit comments