fix(state): auto-migrate legacy agent dir

This commit is contained in:
Peter Steinberger
2026-01-06 22:04:23 +00:00
parent 7aa7fa79d0
commit 2771001720
6 changed files with 570 additions and 457 deletions

View File

@@ -2,11 +2,13 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import {
autoMigrateLegacyAgentDir,
detectLegacyStateMigrations,
resetAutoMigrateLegacyAgentDirForTest,
runLegacyStateMigrations,
} from "./doctor-state-migrations.js";
@@ -21,6 +23,7 @@ async function makeTempRoot() {
}
afterEach(async () => {
resetAutoMigrateLegacyAgentDirForTest();
if (!tempRoot) return;
await fs.promises.rm(tempRoot, { recursive: true, force: true });
tempRoot = null;
@@ -98,6 +101,28 @@ describe("doctor legacy state migrations", () => {
expect(fs.existsSync(path.join(backupDir, "foo.txt"))).toBe(true);
});
it("auto-migrates legacy agent dir on startup", async () => {
const root = await makeTempRoot();
const cfg: ClawdbotConfig = {};
const legacyAgentDir = path.join(root, "agent");
fs.mkdirSync(legacyAgentDir, { recursive: true });
fs.writeFileSync(path.join(legacyAgentDir, "auth.json"), "{}", "utf-8");
const log = { info: vi.fn(), warn: vi.fn() };
const result = await autoMigrateLegacyAgentDir({
cfg,
env: { CLAWDBOT_STATE_DIR: root } as NodeJS.ProcessEnv,
log,
});
const targetAgentDir = path.join(root, "agents", "main", "agent");
expect(fs.existsSync(path.join(targetAgentDir, "auth.json"))).toBe(true);
expect(result.migrated).toBe(true);
expect(log.info).toHaveBeenCalled();
});
it("migrates legacy WhatsApp auth files without touching oauth.json", async () => {
const root = await makeTempRoot();
const cfg: ClawdbotConfig = {};