|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { PostCorePluginUpdateResult } from "./update-command-plugins.js"; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + readConfig: vi.fn(), |
| 6 | + resolveEntrypoint: vi.fn(), |
| 7 | + runExec: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("../../config/config.js", async (importOriginal) => ({ |
| 11 | + ...(await importOriginal<typeof import("../../config/config.js")>()), |
| 12 | + readConfigFileSnapshot: mocks.readConfig, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../../daemon/gateway-entrypoint.js", () => ({ |
| 16 | + resolveGatewayInstallEntrypoint: mocks.resolveEntrypoint, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../../process/exec.js", () => ({ |
| 20 | + runExec: mocks.runExec, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("../../runtime.js", () => ({ |
| 24 | + defaultRuntime: { error: vi.fn(), log: vi.fn() }, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("./shared.js", async (importOriginal) => ({ |
| 28 | + ...(await importOriginal<typeof import("./shared.js")>()), |
| 29 | + resolveNodeRunner: vi.fn(() => "/usr/bin/node"), |
| 30 | +})); |
| 31 | + |
| 32 | +import { completePostCorePluginUpdate } from "./update-command-fresh-doctor.js"; |
| 33 | + |
| 34 | +const pluginUpdate: PostCorePluginUpdateResult = { |
| 35 | + status: "ok", |
| 36 | + changed: true, |
| 37 | + sync: { |
| 38 | + changed: false, |
| 39 | + switchedToBundled: [], |
| 40 | + switchedToNpm: [], |
| 41 | + warnings: [], |
| 42 | + errors: [], |
| 43 | + }, |
| 44 | + npm: { changed: false, outcomes: [] }, |
| 45 | + integrityDrifts: [], |
| 46 | + warnings: [], |
| 47 | +}; |
| 48 | + |
| 49 | +const validConfigSnapshot = { |
| 50 | + valid: true as const, |
| 51 | + parsed: {}, |
| 52 | + config: {}, |
| 53 | + runtimeConfig: {}, |
| 54 | + sourceConfig: {}, |
| 55 | + warnings: [], |
| 56 | + issues: [], |
| 57 | + legacyIssues: [], |
| 58 | +}; |
| 59 | + |
| 60 | +describe("post-plugin update readiness", () => { |
| 61 | + beforeEach(() => { |
| 62 | + mocks.readConfig.mockReset().mockResolvedValue(validConfigSnapshot); |
| 63 | + mocks.resolveEntrypoint.mockReset().mockResolvedValue("/opt/openclaw/dist/index.js"); |
| 64 | + mocks.runExec.mockReset().mockImplementation(async (_command, args: string[]) => ({ |
| 65 | + stdout: args.includes("--lint") |
| 66 | + ? `${JSON.stringify({ ok: true, checksRun: 1, checksSkipped: 0, findings: [] })}\n` |
| 67 | + : "", |
| 68 | + stderr: "", |
| 69 | + })); |
| 70 | + }); |
| 71 | + |
| 72 | + it("runs declared readiness checks in the updated process before accepting restart", async () => { |
| 73 | + await completePostCorePluginUpdate({ |
| 74 | + root: "/opt/openclaw", |
| 75 | + pluginUpdate, |
| 76 | + freshDoctorRequired: true, |
| 77 | + yes: true, |
| 78 | + json: true, |
| 79 | + timeoutMs: 5_000, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(mocks.runExec.mock.calls.map(([, args]) => args)).toEqual([ |
| 83 | + [ |
| 84 | + "/opt/openclaw/dist/index.js", |
| 85 | + "doctor", |
| 86 | + "--repair", |
| 87 | + "--non-interactive", |
| 88 | + "--no-workspace-suggestions", |
| 89 | + "--yes", |
| 90 | + ], |
| 91 | + ["/opt/openclaw/dist/index.js", "config", "validate", "--json"], |
| 92 | + ["/opt/openclaw/dist/index.js", "doctor", "--lint", "--json", "--severity-min", "error"], |
| 93 | + ]); |
| 94 | + expect(mocks.runExec.mock.calls[2]?.[2]).toMatchObject({ |
| 95 | + env: { OPENCLAW_UPDATE_POST_CORE_CONVERGENCE: "1" }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("runs updated readiness checks even when no plugin package changed", async () => { |
| 100 | + await completePostCorePluginUpdate({ |
| 101 | + root: "/opt/openclaw", |
| 102 | + pluginUpdate: { ...pluginUpdate, changed: false }, |
| 103 | + freshDoctorRequired: false, |
| 104 | + yes: true, |
| 105 | + json: true, |
| 106 | + timeoutMs: 5_000, |
| 107 | + }); |
| 108 | + |
| 109 | + expect(mocks.runExec.mock.calls.map(([, args]) => args)).toEqual([ |
| 110 | + ["/opt/openclaw/dist/index.js", "doctor", "--lint", "--json", "--severity-min", "error"], |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns the owner-provided remediation and refuses restart when readiness fails", async () => { |
| 115 | + mocks.runExec.mockImplementation(async (_command, args: string[]) => { |
| 116 | + if (args.includes("--lint")) { |
| 117 | + throw Object.assign(new Error("readiness failed"), { |
| 118 | + exitCode: 1, |
| 119 | + stdout: `${JSON.stringify({ |
| 120 | + ok: false, |
| 121 | + checksRun: 1, |
| 122 | + checksSkipped: 0, |
| 123 | + findings: [ |
| 124 | + { |
| 125 | + checkId: "memory-core/managed-local-embedding-setup", |
| 126 | + severity: "error", |
| 127 | + source: "memory-core", |
| 128 | + message: "Managed local embeddings are unavailable.", |
| 129 | + fixHint: |
| 130 | + "Run `openclaw models --agent main auth login --provider llama-cpp --method local`.", |
| 131 | + }, |
| 132 | + ], |
| 133 | + })}\n`, |
| 134 | + stderr: "", |
| 135 | + }); |
| 136 | + } |
| 137 | + return { stdout: "", stderr: "" }; |
| 138 | + }); |
| 139 | + |
| 140 | + const result = await completePostCorePluginUpdate({ |
| 141 | + root: "/opt/openclaw", |
| 142 | + pluginUpdate, |
| 143 | + freshDoctorRequired: true, |
| 144 | + yes: true, |
| 145 | + json: true, |
| 146 | + timeoutMs: 5_000, |
| 147 | + }); |
| 148 | + |
| 149 | + expect(result.pluginUpdate).toMatchObject({ |
| 150 | + status: "error", |
| 151 | + reason: "post-plugin-update-readiness-failed", |
| 152 | + warnings: [ |
| 153 | + { |
| 154 | + pluginId: "memory-core", |
| 155 | + reason: "memory-core/managed-local-embedding-setup", |
| 156 | + message: "Managed local embeddings are unavailable.", |
| 157 | + guidance: [ |
| 158 | + "Run `openclaw models --agent main auth login --provider llama-cpp --method local`.", |
| 159 | + ], |
| 160 | + }, |
| 161 | + ], |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it.each([ |
| 166 | + { |
| 167 | + label: "malformed output", |
| 168 | + stdout: "{not-json\n", |
| 169 | + }, |
| 170 | + { |
| 171 | + label: "no declared check", |
| 172 | + stdout: `${JSON.stringify({ ok: true, checksRun: 0, checksSkipped: 0, findings: [] })}\n`, |
| 173 | + }, |
| 174 | + ])("fails closed on $label from the updated readiness child", async ({ stdout }) => { |
| 175 | + mocks.runExec.mockImplementation(async (_command, args: string[]) => ({ |
| 176 | + stdout: args.includes("--lint") ? stdout : "", |
| 177 | + stderr: "", |
| 178 | + })); |
| 179 | + |
| 180 | + const result = await completePostCorePluginUpdate({ |
| 181 | + root: "/opt/openclaw", |
| 182 | + pluginUpdate, |
| 183 | + freshDoctorRequired: true, |
| 184 | + yes: true, |
| 185 | + json: true, |
| 186 | + timeoutMs: 5_000, |
| 187 | + }); |
| 188 | + |
| 189 | + expect(result.pluginUpdate).toMatchObject({ |
| 190 | + status: "error", |
| 191 | + reason: "post-plugin-update-readiness-execution-failed", |
| 192 | + warnings: [ |
| 193 | + expect.objectContaining({ |
| 194 | + message: "Updated plugin readiness checks could not be completed before restart.", |
| 195 | + }), |
| 196 | + ], |
| 197 | + }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments