From 5f55a53f0e25d6c6c88c3f18287278f8375f6c2f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 20:58:25 +0000 Subject: [PATCH] refactor(test): share doctor legacy migration setup --- src/commands/doctor.e2e-harness.ts | 96 ++++++++++++++++--- ...k-legacy-sandbox-image-missing.e2e.test.ts | 56 +---------- ...te-migrations-yes-mode-without.e2e.test.ts | 54 +---------- 3 files changed, 89 insertions(+), 117 deletions(-) diff --git a/src/commands/doctor.e2e-harness.ts b/src/commands/doctor.e2e-harness.ts index 6aef868d555..c59d060d5bd 100644 --- a/src/commands/doctor.e2e-harness.ts +++ b/src/commands/doctor.e2e-harness.ts @@ -85,6 +85,45 @@ export const callGateway = vi .fn() .mockRejectedValue(new Error("gateway closed")) as unknown as MockFn; +export const autoMigrateLegacyStateDir = vi.fn().mockResolvedValue({ + migrated: false, + skipped: false, + changes: [], + warnings: [], +}) as unknown as MockFn; + +export const detectLegacyStateMigrations = vi.fn().mockResolvedValue({ + targetAgentId: "main", + targetMainKey: "main", + targetScope: undefined, + stateDir: "/tmp/state", + oauthDir: "/tmp/oauth", + sessions: { + legacyDir: "/tmp/state/sessions", + legacyStorePath: "/tmp/state/sessions/sessions.json", + targetDir: "/tmp/state/agents/main/sessions", + targetStorePath: "/tmp/state/agents/main/sessions/sessions.json", + hasLegacy: false, + legacyKeys: [], + }, + agentDir: { + legacyDir: "/tmp/state/agent", + targetDir: "/tmp/state/agents/main/agent", + hasLegacy: false, + }, + whatsappAuth: { + legacyDir: "/tmp/oauth", + targetDir: "/tmp/oauth/whatsapp/default", + hasLegacy: false, + }, + preview: [], +}) as unknown as MockFn; + +export const runLegacyStateMigrations = vi.fn().mockResolvedValue({ + changes: [], + warnings: [], +}) as unknown as MockFn; + vi.mock("@clack/prompts", () => ({ confirm, intro: vi.fn(), @@ -212,13 +251,33 @@ vi.mock("./onboard-helpers.js", () => ({ })); vi.mock("./doctor-state-migrations.js", () => ({ - autoMigrateLegacyStateDir: vi.fn().mockResolvedValue({ - migrated: false, - skipped: false, - changes: [], - warnings: [], - }), - detectLegacyStateMigrations: vi.fn().mockResolvedValue({ + autoMigrateLegacyStateDir, + detectLegacyStateMigrations, + runLegacyStateMigrations, +})); + +export async function arrangeLegacyStateMigrationTest() { + readConfigFileSnapshot.mockResolvedValue({ + path: "/tmp/openclaw.json", + exists: true, + raw: "{}", + parsed: {}, + valid: true, + config: {}, + issues: [], + legacyIssues: [], + }); + + const { doctorCommand } = await import("./doctor.js"); + const runtime = { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn(), + }; + + detectLegacyStateMigrations.mockClear(); + runLegacyStateMigrations.mockClear(); + detectLegacyStateMigrations.mockResolvedValueOnce({ targetAgentId: "main", targetMainKey: "main", targetScope: undefined, @@ -229,7 +288,7 @@ vi.mock("./doctor-state-migrations.js", () => ({ legacyStorePath: "/tmp/state/sessions/sessions.json", targetDir: "/tmp/state/agents/main/sessions", targetStorePath: "/tmp/state/agents/main/sessions/sessions.json", - hasLegacy: false, + hasLegacy: true, legacyKeys: [], }, agentDir: { @@ -242,13 +301,22 @@ vi.mock("./doctor-state-migrations.js", () => ({ targetDir: "/tmp/oauth/whatsapp/default", hasLegacy: false, }, - preview: [], - }), - runLegacyStateMigrations: vi.fn().mockResolvedValue({ - changes: [], + preview: ["- Legacy sessions detected"], + }); + runLegacyStateMigrations.mockResolvedValueOnce({ + changes: ["migrated"], warnings: [], - }), -})); + }); + + confirm.mockClear(); + + return { + doctorCommand, + runtime, + detectLegacyStateMigrations, + runLegacyStateMigrations, + }; +} beforeEach(() => { confirm.mockReset().mockResolvedValue(true); diff --git a/src/commands/doctor.falls-back-legacy-sandbox-image-missing.e2e.test.ts b/src/commands/doctor.falls-back-legacy-sandbox-image-missing.e2e.test.ts index 8bd3b36ae8f..79c3b3ec1e1 100644 --- a/src/commands/doctor.falls-back-legacy-sandbox-image-missing.e2e.test.ts +++ b/src/commands/doctor.falls-back-legacy-sandbox-image-missing.e2e.test.ts @@ -1,58 +1,10 @@ -import { describe, expect, it, vi } from "vitest"; -import { confirm, readConfigFileSnapshot } from "./doctor.e2e-harness.js"; +import { describe, expect, it } from "vitest"; +import { arrangeLegacyStateMigrationTest, confirm } from "./doctor.e2e-harness.js"; describe("doctor command", () => { it("runs legacy state migrations in non-interactive mode without prompting", async () => { - readConfigFileSnapshot.mockResolvedValue({ - path: "/tmp/openclaw.json", - exists: true, - raw: "{}", - parsed: {}, - valid: true, - config: {}, - issues: [], - legacyIssues: [], - }); - - const { doctorCommand } = await import("./doctor.js"); - const runtime = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(), - }; - - const { detectLegacyStateMigrations, runLegacyStateMigrations } = - await import("./doctor-state-migrations.js"); - detectLegacyStateMigrations.mockResolvedValueOnce({ - targetAgentId: "main", - targetMainKey: "main", - stateDir: "/tmp/state", - oauthDir: "/tmp/oauth", - sessions: { - legacyDir: "/tmp/state/sessions", - legacyStorePath: "/tmp/state/sessions/sessions.json", - targetDir: "/tmp/state/agents/main/sessions", - targetStorePath: "/tmp/state/agents/main/sessions/sessions.json", - hasLegacy: true, - }, - agentDir: { - legacyDir: "/tmp/state/agent", - targetDir: "/tmp/state/agents/main/agent", - hasLegacy: false, - }, - whatsappAuth: { - legacyDir: "/tmp/oauth", - targetDir: "/tmp/oauth/whatsapp/default", - hasLegacy: false, - }, - preview: ["- Legacy sessions detected"], - }); - runLegacyStateMigrations.mockResolvedValueOnce({ - changes: ["migrated"], - warnings: [], - }); - - confirm.mockClear(); + const { doctorCommand, runtime, runLegacyStateMigrations } = + await arrangeLegacyStateMigrationTest(); await doctorCommand(runtime, { nonInteractive: true }); diff --git a/src/commands/doctor.runs-legacy-state-migrations-yes-mode-without.e2e.test.ts b/src/commands/doctor.runs-legacy-state-migrations-yes-mode-without.e2e.test.ts index 6cf24b90e66..635cf8a05f7 100644 --- a/src/commands/doctor.runs-legacy-state-migrations-yes-mode-without.e2e.test.ts +++ b/src/commands/doctor.runs-legacy-state-migrations-yes-mode-without.e2e.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import { + arrangeLegacyStateMigrationTest, confirm, ensureAuthProfileStore, readConfigFileSnapshot, @@ -10,57 +11,8 @@ import { describe("doctor command", () => { it("runs legacy state migrations in yes mode without prompting", async () => { - readConfigFileSnapshot.mockResolvedValue({ - path: "/tmp/openclaw.json", - exists: true, - raw: "{}", - parsed: {}, - valid: true, - config: {}, - issues: [], - legacyIssues: [], - }); - - const { doctorCommand } = await import("./doctor.js"); - const runtime = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(), - }; - - const { detectLegacyStateMigrations, runLegacyStateMigrations } = - await import("./doctor-state-migrations.js"); - detectLegacyStateMigrations.mockResolvedValueOnce({ - targetAgentId: "main", - targetMainKey: "main", - stateDir: "/tmp/state", - oauthDir: "/tmp/oauth", - sessions: { - legacyDir: "/tmp/state/sessions", - legacyStorePath: "/tmp/state/sessions/sessions.json", - targetDir: "/tmp/state/agents/main/sessions", - targetStorePath: "/tmp/state/agents/main/sessions/sessions.json", - hasLegacy: true, - }, - agentDir: { - legacyDir: "/tmp/state/agent", - targetDir: "/tmp/state/agents/main/agent", - hasLegacy: false, - }, - whatsappAuth: { - legacyDir: "/tmp/oauth", - targetDir: "/tmp/oauth/whatsapp/default", - hasLegacy: false, - }, - preview: ["- Legacy sessions detected"], - }); - runLegacyStateMigrations.mockResolvedValueOnce({ - changes: ["migrated"], - warnings: [], - }); - - runLegacyStateMigrations.mockClear(); - confirm.mockClear(); + const { doctorCommand, runtime, runLegacyStateMigrations } = + await arrangeLegacyStateMigrationTest(); await doctorCommand(runtime, { yes: true });