test(agents): dedupe copilot models-config token setup

This commit is contained in:
Peter Steinberger
2026-02-19 09:03:32 +00:00
parent b4dbe03298
commit 0900ec38a9
3 changed files with 21 additions and 44 deletions

View File

@@ -1,9 +1,11 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import {
installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess,
withCopilotGithubToken,
withModelsTempHome as withTempHome,
} from "./models-config.e2e-harness.js";
import { ensureOpenClawModelsJson } from "./models-config.js";
@@ -13,19 +15,7 @@ installModelsConfigTestHooks({ restoreFetch: true });
describe("models-config", () => {
it("auto-injects github-copilot provider when token is present", async () => {
await withTempHome(async (home) => {
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]);
process.env.COPILOT_GITHUB_TOKEN = "gh-token";
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
token: "copilot-token;proxy-ep=proxy.copilot.example",
expires_at: Math.floor(Date.now() / 1000) + 3600,
}),
});
globalThis.fetch = fetchMock as unknown as typeof fetch;
try {
await withCopilotGithubToken("gh-token", async () => {
const agentDir = path.join(home, "agent-default-base-url");
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
@@ -36,9 +26,7 @@ describe("models-config", () => {
expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://api.copilot.example");
expect(parsed.providers["github-copilot"]?.models?.length ?? 0).toBe(0);
} finally {
envSnapshot.restore();
}
});
});
});
@@ -49,15 +37,7 @@ describe("models-config", () => {
process.env.GH_TOKEN = "gh-token";
process.env.GITHUB_TOKEN = "github-token";
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
token: "copilot-token;proxy-ep=proxy.copilot.example",
expires_at: Math.floor(Date.now() / 1000) + 3600,
}),
});
globalThis.fetch = fetchMock as unknown as typeof fetch;
const fetchMock = mockCopilotTokenExchangeSuccess();
try {
await ensureOpenClawModelsJson({ models: { providers: {} } });

View File

@@ -71,6 +71,17 @@ export function mockCopilotTokenExchangeSuccess(): MockFn {
return fetchMock;
}
export async function withCopilotGithubToken<T>(
token: string,
fn: (fetchMock: MockFn) => Promise<T>,
): Promise<T> {
return withTempEnv(["COPILOT_GITHUB_TOKEN"], async () => {
process.env.COPILOT_GITHUB_TOKEN = token;
const fetchMock = mockCopilotTokenExchangeSuccess();
return fn(fetchMock);
});
}
export const MODELS_CONFIG_IMPLICIT_ENV_VARS = [
"CLOUDFLARE_AI_GATEWAY_API_KEY",
"COPILOT_GITHUB_TOKEN",

View File

@@ -1,11 +1,11 @@
import fs from "node:fs/promises";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { describe, expect, it } from "vitest";
import { resolveOpenClawAgentDir } from "./agent-paths.js";
import {
installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess,
withCopilotGithubToken,
withUnsetCopilotTokenEnv,
withModelsTempHome as withTempHome,
} from "./models-config.e2e-harness.js";
@@ -53,19 +53,7 @@ describe("models-config", () => {
it("does not override explicit github-copilot provider config", async () => {
await withTempHome(async () => {
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]);
process.env.COPILOT_GITHUB_TOKEN = "gh-token";
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
token: "copilot-token;proxy-ep=proxy.copilot.example",
expires_at: Math.floor(Date.now() / 1000) + 3600,
}),
});
globalThis.fetch = fetchMock as unknown as typeof fetch;
try {
await withCopilotGithubToken("gh-token", async () => {
await ensureOpenClawModelsJson({
models: {
providers: {
@@ -85,9 +73,7 @@ describe("models-config", () => {
};
expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://copilot.local");
} finally {
envSnapshot.restore();
}
});
});
});
});