refactor(test): centralize doctor e2e runtime and snapshot scaffolding

This commit is contained in:
Peter Steinberger
2026-02-16 16:32:22 +00:00
parent ffeeb835aa
commit 3a2fffefdb
5 changed files with 81 additions and 172 deletions

View File

@@ -129,6 +129,17 @@ export const runLegacyStateMigrations = vi.fn().mockResolvedValue({
warnings: [], warnings: [],
}) as unknown as MockFn; }) as unknown as MockFn;
const DEFAULT_CONFIG_SNAPSHOT = {
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
} as const;
vi.mock("@clack/prompts", () => ({ vi.mock("@clack/prompts", () => ({
confirm, confirm,
intro: vi.fn(), intro: vi.fn(),
@@ -261,29 +272,43 @@ vi.mock("./doctor-state-migrations.js", () => ({
runLegacyStateMigrations, runLegacyStateMigrations,
})); }));
export function mockDoctorConfigSnapshot(
params: {
config?: Record<string, unknown>;
parsed?: Record<string, unknown>;
valid?: boolean;
issues?: Array<{ path: string; message: string }>;
legacyIssues?: Array<{ path: string; message: string }>;
} = {},
) {
readConfigFileSnapshot.mockResolvedValue({
...DEFAULT_CONFIG_SNAPSHOT,
config: params.config ?? DEFAULT_CONFIG_SNAPSHOT.config,
parsed: params.parsed ?? DEFAULT_CONFIG_SNAPSHOT.parsed,
valid: params.valid ?? DEFAULT_CONFIG_SNAPSHOT.valid,
issues: params.issues ?? DEFAULT_CONFIG_SNAPSHOT.issues,
legacyIssues: params.legacyIssues ?? DEFAULT_CONFIG_SNAPSHOT.legacyIssues,
});
}
export function createDoctorRuntime() {
return {
log: vi.fn() as unknown as MockFn,
error: vi.fn() as unknown as MockFn,
exit: vi.fn() as unknown as MockFn,
};
}
export async function arrangeLegacyStateMigrationTest(): Promise<{ export async function arrangeLegacyStateMigrationTest(): Promise<{
doctorCommand: unknown; doctorCommand: unknown;
runtime: { log: MockFn; error: MockFn; exit: MockFn }; runtime: { log: MockFn; error: MockFn; exit: MockFn };
detectLegacyStateMigrations: MockFn; detectLegacyStateMigrations: MockFn;
runLegacyStateMigrations: MockFn; runLegacyStateMigrations: MockFn;
}> { }> {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot();
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
});
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { const runtime = createDoctorRuntime();
log: vi.fn() as unknown as MockFn,
error: vi.fn() as unknown as MockFn,
exit: vi.fn() as unknown as MockFn,
};
detectLegacyStateMigrations.mockClear(); detectLegacyStateMigrations.mockClear();
runLegacyStateMigrations.mockClear(); runLegacyStateMigrations.mockClear();

View File

@@ -1,6 +1,9 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it } from "vitest";
import { import {
createDoctorRuntime,
findLegacyGatewayServices, findLegacyGatewayServices,
migrateLegacyConfig,
mockDoctorConfigSnapshot,
note, note,
readConfigFileSnapshot, readConfigFileSnapshot,
resolveOpenClawPackageRoot, resolveOpenClawPackageRoot,
@@ -9,39 +12,20 @@ import {
serviceInstall, serviceInstall,
serviceIsLoaded, serviceIsLoaded,
uninstallLegacyGatewayServices, uninstallLegacyGatewayServices,
migrateLegacyConfig,
writeConfigFile, writeConfigFile,
} from "./doctor.e2e-harness.js"; } from "./doctor.e2e-harness.js";
describe("doctor command", () => { describe("doctor command", () => {
it("migrates routing.allowFrom to channels.whatsapp.allowFrom", { timeout: 60_000 }, async () => { it("migrates routing.allowFrom to channels.whatsapp.allowFrom", { timeout: 60_000 }, async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: { routing: { allowFrom: ["+15555550123"] } }, parsed: { routing: { allowFrom: ["+15555550123"] } },
valid: false, valid: false,
config: {}, issues: [{ path: "routing.allowFrom", message: "legacy" }],
issues: [ legacyIssues: [{ path: "routing.allowFrom", message: "legacy" }],
{
path: "routing.allowFrom",
message: "legacy",
},
],
legacyIssues: [
{
path: "routing.allowFrom",
message: "legacy",
},
],
}); });
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { const runtime = createDoctorRuntime();
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
migrateLegacyConfig.mockReturnValue({ migrateLegacyConfig.mockReturnValue({
config: { channels: { whatsapp: { allowFrom: ["+15555550123"] } } }, config: { channels: { whatsapp: { allowFrom: ["+15555550123"] } } },
@@ -59,16 +43,7 @@ describe("doctor command", () => {
}); });
it("skips legacy gateway services migration", { timeout: 60_000 }, async () => { it("skips legacy gateway services migration", { timeout: 60_000 }, async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot();
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
});
findLegacyGatewayServices.mockResolvedValueOnce([ findLegacyGatewayServices.mockResolvedValueOnce([
{ {
@@ -81,13 +56,7 @@ describe("doctor command", () => {
serviceInstall.mockClear(); serviceInstall.mockClear();
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { await doctorCommand(createDoctorRuntime());
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await doctorCommand(runtime);
expect(uninstallLegacyGatewayServices).not.toHaveBeenCalled(); expect(uninstallLegacyGatewayServices).not.toHaveBeenCalled();
expect(serviceInstall).not.toHaveBeenCalled(); expect(serviceInstall).not.toHaveBeenCalled();
@@ -113,25 +82,10 @@ describe("doctor command", () => {
durationMs: 1, durationMs: 1,
}); });
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot();
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
});
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { await doctorCommand(createDoctorRuntime());
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await doctorCommand(runtime);
expect(runGatewayUpdate).toHaveBeenCalledWith(expect.objectContaining({ cwd: root })); expect(runGatewayUpdate).toHaveBeenCalledWith(expect.objectContaining({ cwd: root }));
expect(readConfigFileSnapshot).not.toHaveBeenCalled(); expect(readConfigFileSnapshot).not.toHaveBeenCalled();

View File

@@ -1,9 +1,10 @@
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it } from "vitest";
import { import {
arrangeLegacyStateMigrationTest, arrangeLegacyStateMigrationTest,
confirm, confirm,
createDoctorRuntime,
ensureAuthProfileStore, ensureAuthProfileStore,
readConfigFileSnapshot, mockDoctorConfigSnapshot,
serviceIsLoaded, serviceIsLoaded,
serviceRestart, serviceRestart,
writeConfigFile, writeConfigFile,
@@ -31,16 +32,7 @@ describe("doctor command", () => {
}, 30_000); }, 30_000);
it("skips gateway restarts in non-interactive mode", async () => { it("skips gateway restarts in non-interactive mode", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot();
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
});
const { healthCommand } = await import("./health.js"); const { healthCommand } = await import("./health.js");
healthCommand.mockRejectedValueOnce(new Error("gateway closed")); healthCommand.mockRejectedValueOnce(new Error("gateway closed"));
@@ -50,25 +42,14 @@ describe("doctor command", () => {
confirm.mockClear(); confirm.mockClear();
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { await doctorCommand(createDoctorRuntime(), { nonInteractive: true });
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await doctorCommand(runtime, { nonInteractive: true });
expect(serviceRestart).not.toHaveBeenCalled(); expect(serviceRestart).not.toHaveBeenCalled();
expect(confirm).not.toHaveBeenCalled(); expect(confirm).not.toHaveBeenCalled();
}); });
it("migrates anthropic oauth config profile id when only email profile exists", async () => { it("migrates anthropic oauth config profile id when only email profile exists", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: { config: {
auth: { auth: {
profiles: { profiles: {
@@ -76,8 +57,6 @@ describe("doctor command", () => {
}, },
}, },
}, },
issues: [],
legacyIssues: [],
}); });
ensureAuthProfileStore.mockReturnValueOnce({ ensureAuthProfileStore.mockReturnValueOnce({
@@ -95,7 +74,7 @@ describe("doctor command", () => {
}); });
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
await doctorCommand({ log: vi.fn(), error: vi.fn(), exit: vi.fn() }, { yes: true }); await doctorCommand(createDoctorRuntime(), { yes: true });
const written = writeConfigFile.mock.calls.at(-1)?.[0] as Record<string, unknown>; const written = writeConfigFile.mock.calls.at(-1)?.[0] as Record<string, unknown>;
const profiles = (written.auth as { profiles: Record<string, unknown> }).profiles; const profiles = (written.auth as { profiles: Record<string, unknown> }).profiles;

View File

@@ -2,16 +2,11 @@ import fs from "node:fs";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { note, readConfigFileSnapshot } from "./doctor.e2e-harness.js"; import { createDoctorRuntime, mockDoctorConfigSnapshot, note } from "./doctor.e2e-harness.js";
describe("doctor command", () => { describe("doctor command", () => {
it("warns when per-agent sandbox docker/browser/prune overrides are ignored under shared scope", async () => { it("warns when per-agent sandbox docker/browser/prune overrides are ignored under shared scope", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: { config: {
agents: { agents: {
defaults: { defaults: {
@@ -35,20 +30,12 @@ describe("doctor command", () => {
], ],
}, },
}, },
issues: [],
legacyIssues: [],
}); });
note.mockClear(); note.mockClear();
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { await doctorCommand(createDoctorRuntime(), { nonInteractive: true });
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await doctorCommand(runtime, { nonInteractive: true });
expect( expect(
note.mock.calls.some(([message, title]) => { note.mock.calls.some(([message, title]) => {
@@ -65,17 +52,10 @@ describe("doctor command", () => {
}, 30_000); }, 30_000);
it("does not warn when only the active workspace is present", async () => { it("does not warn when only the active workspace is present", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: { config: {
agents: { defaults: { workspace: "/Users/steipete/openclaw" } }, agents: { defaults: { workspace: "/Users/steipete/openclaw" } },
}, },
issues: [],
legacyIssues: [],
}); });
note.mockClear(); note.mockClear();
@@ -95,13 +75,7 @@ describe("doctor command", () => {
}); });
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
const runtime = { await doctorCommand(createDoctorRuntime(), { nonInteractive: true });
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
await doctorCommand(runtime, { nonInteractive: true });
expect(note.mock.calls.some(([_, title]) => title === "Extra workspace")).toBe(false); expect(note.mock.calls.some(([_, title]) => title === "Extra workspace")).toBe(false);

View File

@@ -1,21 +1,12 @@
import fs from "node:fs"; import fs from "node:fs";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it } from "vitest";
import { note, readConfigFileSnapshot } from "./doctor.e2e-harness.js"; import { createDoctorRuntime, mockDoctorConfigSnapshot, note } from "./doctor.e2e-harness.js";
describe("doctor command", () => { describe("doctor command", () => {
it("warns when the state directory is missing", async () => { it("warns when the state directory is missing", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot();
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: {},
issues: [],
legacyIssues: [],
});
const missingDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-missing-state-")); const missingDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-missing-state-"));
fs.rmSync(missingDir, { recursive: true, force: true }); fs.rmSync(missingDir, { recursive: true, force: true });
@@ -23,10 +14,10 @@ describe("doctor command", () => {
note.mockClear(); note.mockClear();
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
await doctorCommand( await doctorCommand(createDoctorRuntime(), {
{ log: vi.fn(), error: vi.fn(), exit: vi.fn() }, nonInteractive: true,
{ nonInteractive: true, workspaceSuggestions: false }, workspaceSuggestions: false,
); });
const stateNote = note.mock.calls.find((call) => call[1] === "State integrity"); const stateNote = note.mock.calls.find((call) => call[1] === "State integrity");
expect(stateNote).toBeTruthy(); expect(stateNote).toBeTruthy();
@@ -34,12 +25,7 @@ describe("doctor command", () => {
}, 30_000); }, 30_000);
it("warns about opencode provider overrides", async () => { it("warns about opencode provider overrides", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: { config: {
models: { models: {
providers: { providers: {
@@ -50,15 +36,13 @@ describe("doctor command", () => {
}, },
}, },
}, },
issues: [],
legacyIssues: [],
}); });
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
await doctorCommand( await doctorCommand(createDoctorRuntime(), {
{ log: vi.fn(), error: vi.fn(), exit: vi.fn() }, nonInteractive: true,
{ nonInteractive: true, workspaceSuggestions: false }, workspaceSuggestions: false,
); });
const warned = note.mock.calls.some( const warned = note.mock.calls.some(
([message, title]) => ([message, title]) =>
@@ -68,17 +52,10 @@ describe("doctor command", () => {
}); });
it("skips gateway auth warning when OPENCLAW_GATEWAY_TOKEN is set", async () => { it("skips gateway auth warning when OPENCLAW_GATEWAY_TOKEN is set", async () => {
readConfigFileSnapshot.mockResolvedValue({ mockDoctorConfigSnapshot({
path: "/tmp/openclaw.json",
exists: true,
raw: "{}",
parsed: {},
valid: true,
config: { config: {
gateway: { mode: "local" }, gateway: { mode: "local" },
}, },
issues: [],
legacyIssues: [],
}); });
const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN; const prevToken = process.env.OPENCLAW_GATEWAY_TOKEN;
@@ -87,10 +64,10 @@ describe("doctor command", () => {
try { try {
const { doctorCommand } = await import("./doctor.js"); const { doctorCommand } = await import("./doctor.js");
await doctorCommand( await doctorCommand(createDoctorRuntime(), {
{ log: vi.fn(), error: vi.fn(), exit: vi.fn() }, nonInteractive: true,
{ nonInteractive: true, workspaceSuggestions: false }, workspaceSuggestions: false,
); });
} finally { } finally {
if (prevToken === undefined) { if (prevToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN; delete process.env.OPENCLAW_GATEWAY_TOKEN;