diff --git a/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts b/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts index c5e9ac64369..77b4c63e94d 100644 --- a/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts +++ b/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts @@ -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: {} } }); diff --git a/src/agents/models-config.e2e-harness.ts b/src/agents/models-config.e2e-harness.ts index 9b8ba534aa6..3c1e59d9730 100644 --- a/src/agents/models-config.e2e-harness.ts +++ b/src/agents/models-config.e2e-harness.ts @@ -71,6 +71,17 @@ export function mockCopilotTokenExchangeSuccess(): MockFn { return fetchMock; } +export async function withCopilotGithubToken( + token: string, + fn: (fetchMock: MockFn) => Promise, +): Promise { + 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", diff --git a/src/agents/models-config.uses-first-github-copilot-profile-env-tokens.e2e.test.ts b/src/agents/models-config.uses-first-github-copilot-profile-env-tokens.e2e.test.ts index ff55eb8e697..50b80f2eb0e 100644 --- a/src/agents/models-config.uses-first-github-copilot-profile-env-tokens.e2e.test.ts +++ b/src/agents/models-config.uses-first-github-copilot-profile-env-tokens.e2e.test.ts @@ -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(); - } + }); }); }); });