Files
openclaw/src/cli/models-cli.test.ts
2026-02-15 19:29:27 +00:00

117 lines
3.7 KiB
TypeScript

import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const githubCopilotLoginCommand = vi.fn();
const modelsStatusCommand = vi.fn().mockResolvedValue(undefined);
const noopAsync = vi.fn(async () => undefined);
vi.mock("../commands/models.js", () => ({
githubCopilotLoginCommand,
modelsStatusCommand,
modelsAliasesAddCommand: noopAsync,
modelsAliasesListCommand: noopAsync,
modelsAliasesRemoveCommand: noopAsync,
modelsAuthAddCommand: noopAsync,
modelsAuthLoginCommand: noopAsync,
modelsAuthOrderClearCommand: noopAsync,
modelsAuthOrderGetCommand: noopAsync,
modelsAuthOrderSetCommand: noopAsync,
modelsAuthPasteTokenCommand: noopAsync,
modelsAuthSetupTokenCommand: noopAsync,
modelsFallbacksAddCommand: noopAsync,
modelsFallbacksClearCommand: noopAsync,
modelsFallbacksListCommand: noopAsync,
modelsFallbacksRemoveCommand: noopAsync,
modelsImageFallbacksAddCommand: noopAsync,
modelsImageFallbacksClearCommand: noopAsync,
modelsImageFallbacksListCommand: noopAsync,
modelsImageFallbacksRemoveCommand: noopAsync,
modelsListCommand: noopAsync,
modelsScanCommand: noopAsync,
modelsSetCommand: noopAsync,
modelsSetImageCommand: noopAsync,
}));
describe("models cli", () => {
let Command: typeof import("commander").Command;
let registerModelsCli: (typeof import("./models-cli.js"))["registerModelsCli"];
beforeAll(async () => {
// Load once; vi.mock above ensures command handlers are already mocked.
({ Command } = await import("commander"));
({ registerModelsCli } = await import("./models-cli.js"));
});
beforeEach(() => {
githubCopilotLoginCommand.mockClear();
modelsStatusCommand.mockClear();
});
function createProgram() {
const program = new Command();
registerModelsCli(program);
return program;
}
it("registers github-copilot login command", async () => {
const program = createProgram();
const models = program.commands.find((cmd) => cmd.name() === "models");
expect(models).toBeTruthy();
const auth = models?.commands.find((cmd) => cmd.name() === "auth");
expect(auth).toBeTruthy();
const login = auth?.commands.find((cmd) => cmd.name() === "login-github-copilot");
expect(login).toBeTruthy();
await program.parseAsync(["models", "auth", "login-github-copilot", "--yes"], {
from: "user",
});
expect(githubCopilotLoginCommand).toHaveBeenCalledTimes(1);
expect(githubCopilotLoginCommand).toHaveBeenCalledWith(
expect.objectContaining({ yes: true }),
expect.any(Object),
);
});
it("passes --agent to models status", async () => {
const program = createProgram();
await program.parseAsync(["models", "status", "--agent", "poe"], { from: "user" });
expect(modelsStatusCommand).toHaveBeenCalledWith(
expect.objectContaining({ agent: "poe" }),
expect.any(Object),
);
});
it("passes parent --agent to models status", async () => {
const program = createProgram();
await program.parseAsync(["models", "--agent", "poe", "status"], { from: "user" });
expect(modelsStatusCommand).toHaveBeenCalledWith(
expect.objectContaining({ agent: "poe" }),
expect.any(Object),
);
});
it("shows help for models auth without error exit", async () => {
const program = new Command();
program.exitOverride();
program.configureOutput({
writeOut: () => {},
writeErr: () => {},
});
registerModelsCli(program);
try {
await program.parseAsync(["models", "auth"], { from: "user" });
expect.fail("expected help to exit");
} catch (err) {
const error = err as { exitCode?: number };
expect(error.exitCode).toBe(0);
}
});
});