Files
openclaw/src/cli/program/register.maintenance.test.ts
2026-02-19 15:19:38 +00:00

77 lines
1.9 KiB
TypeScript

import { Command } from "commander";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const doctorCommand = vi.fn();
const dashboardCommand = vi.fn();
const resetCommand = vi.fn();
const uninstallCommand = vi.fn();
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
vi.mock("../../commands/doctor.js", () => ({
doctorCommand,
}));
vi.mock("../../commands/dashboard.js", () => ({
dashboardCommand,
}));
vi.mock("../../commands/reset.js", () => ({
resetCommand,
}));
vi.mock("../../commands/uninstall.js", () => ({
uninstallCommand,
}));
vi.mock("../../runtime.js", () => ({
defaultRuntime: runtime,
}));
let registerMaintenanceCommands: typeof import("./register.maintenance.js").registerMaintenanceCommands;
beforeAll(async () => {
({ registerMaintenanceCommands } = await import("./register.maintenance.js"));
});
describe("registerMaintenanceCommands doctor action", () => {
async function runMaintenanceCli(args: string[]) {
const program = new Command();
registerMaintenanceCommands(program);
await program.parseAsync(args, { from: "user" });
}
beforeEach(() => {
vi.clearAllMocks();
});
it("exits with code 0 after successful doctor run", async () => {
doctorCommand.mockResolvedValue(undefined);
await runMaintenanceCli(["doctor", "--non-interactive", "--yes"]);
expect(doctorCommand).toHaveBeenCalledWith(
runtime,
expect.objectContaining({
nonInteractive: true,
yes: true,
}),
);
expect(runtime.exit).toHaveBeenCalledWith(0);
});
it("exits with code 1 when doctor fails", async () => {
doctorCommand.mockRejectedValue(new Error("doctor failed"));
await runMaintenanceCli(["doctor"]);
expect(runtime.error).toHaveBeenCalledWith("Error: doctor failed");
expect(runtime.exit).toHaveBeenCalledWith(1);
expect(runtime.exit).not.toHaveBeenCalledWith(0);
});
});