mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
370 lines
11 KiB
TypeScript
370 lines
11 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => {
|
|
const printModelTable = vi.fn();
|
|
const sourceConfig = {
|
|
agents: { defaults: { model: { primary: "openai-codex/gpt-5.4" } } },
|
|
models: {
|
|
providers: {
|
|
openai: {
|
|
apiKey: "$OPENAI_API_KEY", // pragma: allowlist secret
|
|
},
|
|
},
|
|
},
|
|
};
|
|
const resolvedConfig = {
|
|
agents: { defaults: { model: { primary: "openai-codex/gpt-5.4" } } },
|
|
models: {
|
|
providers: {
|
|
openai: {
|
|
apiKey: "sk-resolved-runtime-value", // pragma: allowlist secret
|
|
},
|
|
},
|
|
},
|
|
};
|
|
return {
|
|
loadConfig: vi.fn().mockReturnValue({
|
|
agents: { defaults: { model: { primary: "openai-codex/gpt-5.4" } } },
|
|
models: { providers: {} },
|
|
}),
|
|
sourceConfig,
|
|
resolvedConfig,
|
|
loadModelsConfigWithSource: vi.fn().mockResolvedValue({
|
|
sourceConfig,
|
|
resolvedConfig,
|
|
diagnostics: [],
|
|
}),
|
|
ensureAuthProfileStore: vi.fn().mockReturnValue({ version: 1, profiles: {}, order: {} }),
|
|
loadModelRegistry: vi
|
|
.fn()
|
|
.mockResolvedValue({ models: [], availableKeys: new Set(), registry: {} }),
|
|
loadModelCatalog: vi.fn().mockResolvedValue([]),
|
|
resolveConfiguredEntries: vi.fn().mockReturnValue({
|
|
entries: [
|
|
{
|
|
key: "openai-codex/gpt-5.4",
|
|
ref: { provider: "openai-codex", model: "gpt-5.4" },
|
|
tags: new Set(["configured"]),
|
|
aliases: [],
|
|
},
|
|
],
|
|
}),
|
|
printModelTable,
|
|
listProfilesForProvider: vi.fn().mockReturnValue([]),
|
|
resolveModelWithRegistry: vi.fn().mockReturnValue({
|
|
provider: "openai-codex",
|
|
id: "gpt-5.4",
|
|
name: "GPT-5.4",
|
|
api: "openai-codex-responses",
|
|
baseUrl: "https://chatgpt.com/backend-api",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
maxTokens: 128000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
}),
|
|
};
|
|
});
|
|
|
|
vi.mock("../../config/config.js", () => ({
|
|
loadConfig: mocks.loadConfig,
|
|
getRuntimeConfigSnapshot: vi.fn().mockReturnValue(null),
|
|
getRuntimeConfigSourceSnapshot: vi.fn().mockReturnValue(null),
|
|
}));
|
|
|
|
vi.mock("../../agents/auth-profiles.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../../agents/auth-profiles.js")>();
|
|
return {
|
|
...actual,
|
|
ensureAuthProfileStore: mocks.ensureAuthProfileStore,
|
|
listProfilesForProvider: mocks.listProfilesForProvider,
|
|
};
|
|
});
|
|
|
|
vi.mock("../../agents/model-catalog.js", () => ({
|
|
loadModelCatalog: mocks.loadModelCatalog,
|
|
}));
|
|
|
|
vi.mock("./list.registry.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("./list.registry.js")>();
|
|
return {
|
|
...actual,
|
|
loadModelRegistry: mocks.loadModelRegistry,
|
|
};
|
|
});
|
|
|
|
vi.mock("./load-config.js", () => ({
|
|
loadModelsConfigWithSource: mocks.loadModelsConfigWithSource,
|
|
}));
|
|
|
|
vi.mock("./list.configured.js", () => ({
|
|
resolveConfiguredEntries: mocks.resolveConfiguredEntries,
|
|
}));
|
|
|
|
vi.mock("./list.table.js", () => ({
|
|
printModelTable: mocks.printModelTable,
|
|
}));
|
|
|
|
vi.mock("../../agents/pi-embedded-runner/model.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../../agents/pi-embedded-runner/model.js")>();
|
|
return {
|
|
...actual,
|
|
resolveModelWithRegistry: mocks.resolveModelWithRegistry,
|
|
};
|
|
});
|
|
|
|
import { modelsListCommand } from "./list.list-command.js";
|
|
|
|
describe("modelsListCommand forward-compat", () => {
|
|
it("does not mark configured codex model as missing when forward-compat can build a fallback", async () => {
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
await modelsListCommand({ json: true }, runtime as never);
|
|
|
|
expect(mocks.printModelTable).toHaveBeenCalled();
|
|
const rows = mocks.printModelTable.mock.calls[0]?.[0] as Array<{
|
|
key: string;
|
|
tags: string[];
|
|
missing: boolean;
|
|
}>;
|
|
|
|
const codex = rows.find((r) => r.key === "openai-codex/gpt-5.4");
|
|
expect(codex).toBeTruthy();
|
|
expect(codex?.missing).toBe(false);
|
|
expect(codex?.tags).not.toContain("missing");
|
|
});
|
|
|
|
it("passes source config to model registry loading for persistence safety", async () => {
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
await modelsListCommand({ json: true }, runtime as never);
|
|
|
|
expect(mocks.loadModelRegistry).toHaveBeenCalledWith(mocks.resolvedConfig, {
|
|
sourceConfig: mocks.sourceConfig,
|
|
});
|
|
});
|
|
|
|
it("keeps configured local openai gpt-5.4 entries visible in --local output", async () => {
|
|
mocks.resolveConfiguredEntries.mockReturnValueOnce({
|
|
entries: [
|
|
{
|
|
key: "openai/gpt-5.4",
|
|
ref: { provider: "openai", model: "gpt-5.4" },
|
|
tags: new Set(["configured"]),
|
|
aliases: [],
|
|
},
|
|
],
|
|
});
|
|
mocks.resolveModelWithRegistry.mockReturnValueOnce({
|
|
provider: "openai",
|
|
id: "gpt-5.4",
|
|
name: "GPT-5.4",
|
|
api: "openai-responses",
|
|
baseUrl: "http://localhost:4000/v1",
|
|
input: ["text", "image"],
|
|
contextWindow: 1_050_000,
|
|
maxTokens: 128_000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
});
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
await modelsListCommand({ json: true, local: true }, runtime as never);
|
|
|
|
expect(mocks.printModelTable).toHaveBeenCalled();
|
|
const rows = mocks.printModelTable.mock.calls.at(-1)?.[0] as Array<{ key: string }>;
|
|
expect(rows).toEqual([
|
|
expect.objectContaining({
|
|
key: "openai/gpt-5.4",
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("marks synthetic codex gpt-5.4 rows as available when provider auth exists", async () => {
|
|
mocks.loadModelRegistry.mockResolvedValueOnce({
|
|
models: [],
|
|
availableKeys: new Set(),
|
|
registry: {},
|
|
});
|
|
mocks.listProfilesForProvider.mockImplementation((_: unknown, provider: string) =>
|
|
provider === "openai-codex" ? ([{ id: "profile-1" }] as Array<Record<string, unknown>>) : [],
|
|
);
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
try {
|
|
await modelsListCommand({ json: true }, runtime as never);
|
|
|
|
expect(mocks.printModelTable).toHaveBeenCalled();
|
|
const rows = mocks.printModelTable.mock.calls.at(-1)?.[0] as Array<{
|
|
key: string;
|
|
available: boolean;
|
|
}>;
|
|
|
|
expect(rows).toContainEqual(
|
|
expect.objectContaining({
|
|
key: "openai-codex/gpt-5.4",
|
|
available: true,
|
|
}),
|
|
);
|
|
} finally {
|
|
mocks.listProfilesForProvider.mockReturnValue([]);
|
|
}
|
|
});
|
|
|
|
it("includes synthetic codex gpt-5.4 in --all output when catalog supports it", async () => {
|
|
mocks.resolveConfiguredEntries.mockReturnValueOnce({ entries: [] });
|
|
mocks.loadModelRegistry.mockResolvedValueOnce({
|
|
models: [
|
|
{
|
|
provider: "openai-codex",
|
|
id: "gpt-5.3-codex",
|
|
name: "GPT-5.3 Codex",
|
|
api: "openai-codex-responses",
|
|
baseUrl: "https://chatgpt.com/backend-api",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
maxTokens: 128000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
},
|
|
],
|
|
availableKeys: new Set(["openai-codex/gpt-5.3-codex"]),
|
|
registry: {},
|
|
});
|
|
mocks.loadModelCatalog.mockResolvedValueOnce([
|
|
{
|
|
provider: "openai-codex",
|
|
id: "gpt-5.3-codex",
|
|
name: "GPT-5.3 Codex",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
},
|
|
{
|
|
provider: "openai-codex",
|
|
id: "gpt-5.4",
|
|
name: "GPT-5.4",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
},
|
|
]);
|
|
mocks.listProfilesForProvider.mockImplementation((_: unknown, provider: string) =>
|
|
provider === "openai-codex" ? ([{ id: "profile-1" }] as Array<Record<string, unknown>>) : [],
|
|
);
|
|
mocks.resolveModelWithRegistry.mockImplementation(
|
|
({ provider, modelId }: { provider: string; modelId: string }) => {
|
|
if (provider !== "openai-codex") {
|
|
return undefined;
|
|
}
|
|
if (modelId === "gpt-5.3-codex") {
|
|
return {
|
|
provider: "openai-codex",
|
|
id: "gpt-5.3-codex",
|
|
name: "GPT-5.3 Codex",
|
|
api: "openai-codex-responses",
|
|
baseUrl: "https://chatgpt.com/backend-api",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
maxTokens: 128000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
};
|
|
}
|
|
if (modelId === "gpt-5.4") {
|
|
return {
|
|
provider: "openai-codex",
|
|
id: "gpt-5.4",
|
|
name: "GPT-5.4",
|
|
api: "openai-codex-responses",
|
|
baseUrl: "https://chatgpt.com/backend-api",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
maxTokens: 128000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
};
|
|
}
|
|
return undefined;
|
|
},
|
|
);
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
try {
|
|
await modelsListCommand(
|
|
{ all: true, provider: "openai-codex", json: true },
|
|
runtime as never,
|
|
);
|
|
|
|
expect(mocks.printModelTable).toHaveBeenCalled();
|
|
const rows = mocks.printModelTable.mock.calls.at(-1)?.[0] as Array<{
|
|
key: string;
|
|
available: boolean;
|
|
}>;
|
|
|
|
expect(rows).toEqual([
|
|
expect.objectContaining({
|
|
key: "openai-codex/gpt-5.3-codex",
|
|
}),
|
|
expect.objectContaining({
|
|
key: "openai-codex/gpt-5.4",
|
|
available: true,
|
|
}),
|
|
]);
|
|
} finally {
|
|
mocks.listProfilesForProvider.mockReturnValue([]);
|
|
}
|
|
});
|
|
|
|
it("keeps discovered rows in --all output when catalog lookup is empty", async () => {
|
|
mocks.resolveConfiguredEntries.mockReturnValueOnce({ entries: [] });
|
|
mocks.loadModelRegistry.mockResolvedValueOnce({
|
|
models: [
|
|
{
|
|
provider: "openai-codex",
|
|
id: "gpt-5.3-codex",
|
|
name: "GPT-5.3 Codex",
|
|
api: "openai-codex-responses",
|
|
baseUrl: "https://chatgpt.com/backend-api",
|
|
input: ["text"],
|
|
contextWindow: 272000,
|
|
maxTokens: 128000,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
},
|
|
],
|
|
availableKeys: new Set(["openai-codex/gpt-5.3-codex"]),
|
|
registry: {},
|
|
});
|
|
mocks.loadModelCatalog.mockResolvedValueOnce([]);
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
|
|
await modelsListCommand({ all: true, provider: "openai-codex", json: true }, runtime as never);
|
|
|
|
expect(mocks.printModelTable).toHaveBeenCalled();
|
|
const rows = mocks.printModelTable.mock.calls.at(-1)?.[0] as Array<{ key: string }>;
|
|
|
|
expect(rows).toEqual([
|
|
expect.objectContaining({
|
|
key: "openai-codex/gpt-5.3-codex",
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("exits with an error when configured-mode listing has no model registry", async () => {
|
|
vi.clearAllMocks();
|
|
const previousExitCode = process.exitCode;
|
|
process.exitCode = undefined;
|
|
mocks.loadModelRegistry.mockResolvedValueOnce({
|
|
models: [],
|
|
availableKeys: new Set<string>(),
|
|
registry: undefined,
|
|
});
|
|
const runtime = { log: vi.fn(), error: vi.fn() };
|
|
let observedExitCode: number | undefined;
|
|
|
|
try {
|
|
await modelsListCommand({ json: true }, runtime as never);
|
|
observedExitCode = process.exitCode;
|
|
} finally {
|
|
process.exitCode = previousExitCode;
|
|
}
|
|
|
|
expect(runtime.error).toHaveBeenCalledWith("Model registry unavailable.");
|
|
expect(observedExitCode).toBe(1);
|
|
expect(mocks.printModelTable).not.toHaveBeenCalled();
|
|
});
|
|
});
|