test: move copilot models-json injection coverage to plan tests

This commit is contained in:
Peter Steinberger
2026-04-09 01:29:52 +01:00
parent a8c47db668
commit 691e2aa856
3 changed files with 61 additions and 178 deletions

View File

@@ -1,77 +0,0 @@
import fs from "node:fs/promises";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withEnvAsync } from "../test-utils/env.js";
import {
installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess,
withCopilotGithubToken,
withModelsTempHome as withTempHome,
} from "./models-config.e2e-harness.js";
vi.unmock("./models-config.js");
vi.unmock("./agent-paths.js");
vi.unmock("../plugins/manifest-registry.js");
vi.unmock("../plugins/provider-runtime.js");
vi.unmock("../plugins/provider-runtime.runtime.js");
vi.unmock("../secrets/provider-env-vars.js");
installModelsConfigTestHooks({ restoreFetch: true });
let ensureOpenClawModelsJson: typeof import("./models-config.js").ensureOpenClawModelsJson;
async function loadModelsConfigForTest(): Promise<void> {
vi.resetModules();
vi.doUnmock("./models-config.js");
vi.doUnmock("./agent-paths.js");
vi.doUnmock("../plugins/manifest-registry.js");
vi.doUnmock("../plugins/provider-runtime.js");
vi.doUnmock("../plugins/provider-runtime.runtime.js");
vi.doUnmock("../secrets/provider-env-vars.js");
({ ensureOpenClawModelsJson } = await import("./models-config.js"));
}
beforeEach(loadModelsConfigForTest);
describe("models-config", () => {
it("auto-injects github-copilot provider when token is present", async () => {
await withTempHome(async (home) => {
await withCopilotGithubToken("gh-token", async () => {
const agentDir = path.join(home, "agent-default-base-url");
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
const raw = await fs.readFile(path.join(agentDir, "models.json"), "utf8");
const parsed = JSON.parse(raw) as {
providers: Record<string, { baseUrl?: string; models?: unknown[] }>;
};
expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://api.copilot.example");
expect(parsed.providers["github-copilot"]?.models?.length ?? 0).toBe(0);
});
});
});
it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => {
await withTempHome(async () => {
await withEnvAsync(
{
COPILOT_GITHUB_TOKEN: "copilot-token",
GH_TOKEN: "gh-token",
GITHUB_TOKEN: "github-token",
OPENCLAW_TEST_ONLY_PROVIDER_PLUGIN_IDS: "github-copilot",
},
async () => {
const fetchMock = mockCopilotTokenExchangeSuccess();
await ensureOpenClawModelsJson({ models: { providers: {} } });
const [, opts] = fetchMock.mock.calls[0] as [
string,
{ headers?: Record<string, string> },
];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
},
);
});
});
});

View File

@@ -1,100 +0,0 @@
import fs from "node:fs/promises";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withEnvAsync } from "../test-utils/env.js";
import { DEFAULT_COPILOT_API_BASE_URL } from "./github-copilot-token.js";
import {
installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess,
withUnsetCopilotTokenEnv,
withModelsTempHome as withTempHome,
} from "./models-config.e2e-harness.js";
vi.unmock("./models-config.js");
vi.unmock("./agent-paths.js");
vi.unmock("../plugins/manifest-registry.js");
vi.unmock("../plugins/provider-runtime.js");
vi.unmock("../plugins/provider-runtime.runtime.js");
vi.unmock("../secrets/provider-env-vars.js");
installModelsConfigTestHooks({ restoreFetch: true });
let ensureOpenClawModelsJson: typeof import("./models-config.js").ensureOpenClawModelsJson;
async function loadModelsConfigForTest(): Promise<void> {
vi.resetModules();
vi.doUnmock("./models-config.js");
vi.doUnmock("./agent-paths.js");
vi.doUnmock("../plugins/manifest-registry.js");
vi.doUnmock("../plugins/provider-runtime.js");
vi.doUnmock("../plugins/provider-runtime.runtime.js");
vi.doUnmock("../secrets/provider-env-vars.js");
({ ensureOpenClawModelsJson } = await import("./models-config.js"));
}
beforeEach(loadModelsConfigForTest);
async function readCopilotBaseUrl(agentDir: string) {
const raw = await fs.readFile(path.join(agentDir, "models.json"), "utf8");
const parsed = JSON.parse(raw) as {
providers: Record<string, { baseUrl?: string }>;
};
return parsed.providers["github-copilot"]?.baseUrl;
}
describe("models-config", () => {
it("falls back to default baseUrl when token exchange fails", async () => {
await withTempHome(async () => {
await withEnvAsync(
{
COPILOT_GITHUB_TOKEN: "gh-token",
GH_TOKEN: undefined,
GITHUB_TOKEN: undefined,
OPENCLAW_TEST_ONLY_PROVIDER_PLUGIN_IDS: "github-copilot",
},
async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 500,
json: async () => ({ message: "boom" }),
});
globalThis.fetch = fetchMock as unknown as typeof fetch;
const { agentDir } = await ensureOpenClawModelsJson({ models: { providers: {} } });
expect(await readCopilotBaseUrl(agentDir)).toBe(DEFAULT_COPILOT_API_BASE_URL);
},
);
});
});
it("uses agentDir override auth profiles for copilot injection", async () => {
await withTempHome(async (home) => {
await withUnsetCopilotTokenEnv(async () => {
mockCopilotTokenExchangeSuccess();
const agentDir = path.join(home, "agent-override");
await fs.mkdir(agentDir, { recursive: true });
await fs.writeFile(
path.join(agentDir, "auth-profiles.json"),
JSON.stringify(
{
version: 1,
profiles: {
"github-copilot:github": {
type: "token",
provider: "github-copilot",
token: "gh-profile-token",
},
},
},
null,
2,
),
);
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
expect(await readCopilotBaseUrl(agentDir)).toBe("https://api.copilot.example");
});
});
});
});

View File

@@ -1,5 +1,10 @@
import { describe, expect, it, vi } from "vitest";
import { planOpenClawModelsJson } from "./models-config.plan.js";
import {
planOpenClawModelsJson,
planOpenClawModelsJsonWithDeps,
type ResolveImplicitProvidersForModelsJson,
} from "./models-config.plan.js";
import type { ProviderConfig } from "./models-config.providers.secrets.js";
import { createProviderAuthResolver } from "./models-config.providers.secrets.js";
vi.mock("./models-config.providers.js", () => ({
@@ -96,4 +101,59 @@ describe("models-config", () => {
profileId: "github-copilot:default",
});
});
it("writes an implicit github-copilot provider discovered from a token exchange", async () => {
const plan = await planCopilotWithImplicitProvider({
provider: { baseUrl: "https://api.copilot.example", models: [] },
});
expectCopilotProviderFromPlan(plan).toEqual({
baseUrl: "https://api.copilot.example",
models: [],
});
});
it("writes default github-copilot baseUrl when the token exchange fails", async () => {
const plan = await planCopilotWithImplicitProvider({
provider: { baseUrl: "https://api.individual.githubcopilot.com", models: [] },
});
expectCopilotProviderFromPlan(plan)?.toEqual({
baseUrl: "https://api.individual.githubcopilot.com",
models: [],
});
});
});
function createCopilotImplicitResolver(
provider: ProviderConfig,
): ResolveImplicitProvidersForModelsJson {
return async () => ({ "github-copilot": provider });
}
async function planCopilotWithImplicitProvider(params: { provider: ProviderConfig }) {
return await planOpenClawModelsJsonWithDeps(
{
cfg: { models: { providers: {} } },
agentDir: "/tmp/openclaw-agent",
env: {} as NodeJS.ProcessEnv,
existingRaw: "",
existingParsed: null,
},
{
resolveImplicitProviders: createCopilotImplicitResolver(params.provider),
},
);
}
function expectCopilotProviderFromPlan(
plan: Awaited<ReturnType<typeof planCopilotWithImplicitProvider>>,
) {
expect(plan.action).toBe("write");
const parsed =
plan.action === "write"
? (JSON.parse(plan.contents) as { providers?: Record<string, unknown> })
: {};
expect(parsed.providers?.["github-copilot"]).toBeDefined();
return expect(parsed.providers?.["github-copilot"]);
}