|
| 1 | +import fs from "node:fs"; |
| 2 | +import { afterEach, expect, it, vi } from "vitest"; |
| 3 | +import { loadPersistedSharedAuthProfileStore } from "../agents/auth-profiles/persisted.js"; |
| 4 | +import { deletePersistedAuthProfileStoreRaw } from "../agents/auth-profiles/sqlite.js"; |
| 5 | +import { writeConfigMachineState } from "../state/config-machine-state.js"; |
| 6 | +import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js"; |
| 7 | +import { |
| 8 | + closeOpenClawStateDatabaseForTest, |
| 9 | + openOpenClawStateDatabase, |
| 10 | +} from "../state/openclaw-state-db.js"; |
| 11 | +import { |
| 12 | + createOpenClawTestState, |
| 13 | + type OpenClawTestState, |
| 14 | +} from "../test-utils/openclaw-test-state.js"; |
| 15 | +import { maybeMigrateAuthProfileJsonStoresToSqlite } from "./doctor-auth-flat-profiles.js"; |
| 16 | + |
| 17 | +let state: OpenClawTestState; |
| 18 | + |
| 19 | +afterEach(async () => { |
| 20 | + vi.restoreAllMocks(); |
| 21 | + closeOpenClawAgentDatabasesForTest(); |
| 22 | + closeOpenClawStateDatabaseForTest(); |
| 23 | + await state?.cleanup(); |
| 24 | +}); |
| 25 | + |
| 26 | +it.each([ |
| 27 | + ["legacy-main", "auth-profiles.json"], |
| 28 | + ["legacy-main", "auth.json"], |
| 29 | + ["state-db", "auth-profiles.json"], |
| 30 | + ["state-db", "auth.json"], |
| 31 | +] as const)("recovers %s credentials after interrupted %s archival", async (location, filename) => { |
| 32 | + state = await createOpenClawTestState({ layout: "state-only" }); |
| 33 | + if (location === "state-db") { |
| 34 | + writeConfigMachineState("auth.sharedStore", { location }, { env: state.env }); |
| 35 | + } |
| 36 | + const credential = { type: "api_key", provider: "openai", key: "synthetic-migration-key" }; |
| 37 | + const source = await state.writeJson( |
| 38 | + `agents/main/agent/${filename}`, |
| 39 | + filename === "auth-profiles.json" |
| 40 | + ? { version: 1, profiles: { "openai:default": credential } } |
| 41 | + : { openai: credential }, |
| 42 | + ); |
| 43 | + const sourceBytes = fs.readFileSync(source); |
| 44 | + const migrate = () => |
| 45 | + maybeMigrateAuthProfileJsonStoresToSqlite({ |
| 46 | + cfg: {}, |
| 47 | + prompter: { confirmAutoFix: async () => true }, |
| 48 | + env: state.env, |
| 49 | + }); |
| 50 | + const rename = fs.renameSync; |
| 51 | + const interruptedRename = vi.spyOn(fs, "renameSync").mockImplementation((from, to) => { |
| 52 | + rename(from, to); |
| 53 | + if (String(from) === source) { |
| 54 | + throw new Error("simulated interruption after archive rename"); |
| 55 | + } |
| 56 | + }); |
| 57 | + const interrupted = await migrate(); |
| 58 | + interruptedRename.mockRestore(); |
| 59 | + expect(interrupted.warnings).toEqual([ |
| 60 | + expect.stringContaining("simulated interruption after archive rename"), |
| 61 | + ]); |
| 62 | + expect(fs.existsSync(source)).toBe(false); |
| 63 | + const db = openOpenClawStateDatabase({ env: state.env }).db; |
| 64 | + expect( |
| 65 | + db.prepare("SELECT status FROM migration_sources WHERE source_path = ?").get(source), |
| 66 | + ).toEqual({ status: "imported" }); |
| 67 | + |
| 68 | + // Model a lost destination after the import commit but before terminal completion. |
| 69 | + deletePersistedAuthProfileStoreRaw(location === "legacy-main" ? state.agentDir() : undefined); |
| 70 | + expect(loadPersistedSharedAuthProfileStore(state.env)?.profiles ?? {}).toEqual({}); |
| 71 | + const repaired = await migrate(); |
| 72 | + |
| 73 | + expect(repaired.warnings).toEqual([]); |
| 74 | + expect(loadPersistedSharedAuthProfileStore(state.env)?.profiles).toEqual({ |
| 75 | + "openai:default": credential, |
| 76 | + }); |
| 77 | + expect(repaired.changes).toContain("Reset an interrupted auth migration receipt for retry."); |
| 78 | + const receipt = db |
| 79 | + .prepare("SELECT status, report_json FROM migration_sources WHERE source_path = ?") |
| 80 | + .get(source) as { status: string; report_json: string }; |
| 81 | + expect(receipt.status).toBe("completed"); |
| 82 | + expect(fs.readFileSync(JSON.parse(receipt.report_json).archivePath)).toEqual(sourceBytes); |
| 83 | + expect((await migrate()).changes).toEqual([]); |
| 84 | +}); |
0 commit comments