|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { requireNodeSqlite } from "../infra/node-sqlite.js"; |
| 5 | +import { OPENCLAW_STATE_SCHEMA_VERSION } from "../state/openclaw-state-db.js"; |
| 6 | +import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js"; |
| 7 | +import { |
| 8 | + autoMigrateLegacyStateDir, |
| 9 | + createDoctorRuntime, |
| 10 | + mockDoctorConfigSnapshot, |
| 11 | + readConfigFileSnapshot, |
| 12 | + resolveOpenClawPackageRoot, |
| 13 | + runCommandWithTimeout, |
| 14 | + runGatewayUpdate, |
| 15 | +} from "./doctor.e2e-harness.js"; |
| 16 | + |
| 17 | +let doctorCommand: typeof import("./doctor.js").doctorCommand; |
| 18 | + |
| 19 | +describe("doctor database schema preflight", () => { |
| 20 | + beforeEach(async () => { |
| 21 | + vi.resetModules(); |
| 22 | + ({ doctorCommand } = await import("./doctor.js")); |
| 23 | + vi.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("lets a successful interactive update replace the stale doctor", async () => { |
| 27 | + writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1); |
| 28 | + mockDoctorConfigSnapshot(); |
| 29 | + mockInteractiveGitUpdate("ok"); |
| 30 | + |
| 31 | + await expect(doctorCommand(createDoctorRuntime())).resolves.toBeUndefined(); |
| 32 | + |
| 33 | + expect(runGatewayUpdate).toHaveBeenCalledOnce(); |
| 34 | + expect(autoMigrateLegacyStateDir).not.toHaveBeenCalled(); |
| 35 | + expect(readConfigFileSnapshot).not.toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("refuses after an interactive update does not handle doctor", async () => { |
| 39 | + writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1); |
| 40 | + mockDoctorConfigSnapshot(); |
| 41 | + mockInteractiveGitUpdate("skipped"); |
| 42 | + |
| 43 | + await expect(doctorCommand(createDoctorRuntime())).rejects.toThrow( |
| 44 | + /Doctor refused to continue.*database schema.*newer than this build/iu, |
| 45 | + ); |
| 46 | + |
| 47 | + expect(runGatewayUpdate).toHaveBeenCalledOnce(); |
| 48 | + expect(autoMigrateLegacyStateDir).not.toHaveBeenCalled(); |
| 49 | + expect(readConfigFileSnapshot).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("refuses before config repair flows when updates are disabled", async () => { |
| 53 | + writeStateSchemaVersion(OPENCLAW_STATE_SCHEMA_VERSION + 1); |
| 54 | + mockDoctorConfigSnapshot(); |
| 55 | + |
| 56 | + await expect(doctorCommand(createDoctorRuntime(), { nonInteractive: true })).rejects.toThrow( |
| 57 | + /Doctor refused to continue.*database schema.*newer than this build/iu, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(runGatewayUpdate).not.toHaveBeenCalled(); |
| 61 | + expect(autoMigrateLegacyStateDir).not.toHaveBeenCalled(); |
| 62 | + expect(readConfigFileSnapshot).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +function mockInteractiveGitUpdate(status: "ok" | "skipped"): void { |
| 67 | + delete process.env.OPENCLAW_UPDATE_IN_PROGRESS; |
| 68 | + resolveOpenClawPackageRoot.mockResolvedValue("/repo"); |
| 69 | + runCommandWithTimeout.mockResolvedValue({ |
| 70 | + stdout: "/repo\n", |
| 71 | + stderr: "", |
| 72 | + code: 0, |
| 73 | + signal: null, |
| 74 | + killed: false, |
| 75 | + }); |
| 76 | + runGatewayUpdate.mockResolvedValue({ |
| 77 | + status, |
| 78 | + mode: "git", |
| 79 | + root: "/repo", |
| 80 | + steps: [], |
| 81 | + durationMs: 0, |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function writeStateSchemaVersion(version: number): void { |
| 86 | + const statePath = resolveOpenClawStateSqlitePath(process.env); |
| 87 | + fs.mkdirSync(path.dirname(statePath), { recursive: true }); |
| 88 | + const { DatabaseSync } = requireNodeSqlite(); |
| 89 | + const database = new DatabaseSync(statePath); |
| 90 | + try { |
| 91 | + database.exec(`PRAGMA user_version = ${version};`); |
| 92 | + } finally { |
| 93 | + database.close(); |
| 94 | + } |
| 95 | +} |
0 commit comments