mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 20:36:38 +00:00
test(agents): dedupe copilot models-config token setup
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
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 { captureEnv } from "../test-utils/env.js";
|
||||||
import {
|
import {
|
||||||
installModelsConfigTestHooks,
|
installModelsConfigTestHooks,
|
||||||
|
mockCopilotTokenExchangeSuccess,
|
||||||
|
withCopilotGithubToken,
|
||||||
withModelsTempHome as withTempHome,
|
withModelsTempHome as withTempHome,
|
||||||
} from "./models-config.e2e-harness.js";
|
} from "./models-config.e2e-harness.js";
|
||||||
import { ensureOpenClawModelsJson } from "./models-config.js";
|
import { ensureOpenClawModelsJson } from "./models-config.js";
|
||||||
@@ -13,19 +15,7 @@ installModelsConfigTestHooks({ restoreFetch: true });
|
|||||||
describe("models-config", () => {
|
describe("models-config", () => {
|
||||||
it("auto-injects github-copilot provider when token is present", async () => {
|
it("auto-injects github-copilot provider when token is present", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]);
|
await withCopilotGithubToken("gh-token", async () => {
|
||||||
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 {
|
|
||||||
const agentDir = path.join(home, "agent-default-base-url");
|
const agentDir = path.join(home, "agent-default-base-url");
|
||||||
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
|
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"]?.baseUrl).toBe("https://api.copilot.example");
|
||||||
expect(parsed.providers["github-copilot"]?.models?.length ?? 0).toBe(0);
|
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.GH_TOKEN = "gh-token";
|
||||||
process.env.GITHUB_TOKEN = "github-token";
|
process.env.GITHUB_TOKEN = "github-token";
|
||||||
|
|
||||||
const fetchMock = vi.fn().mockResolvedValue({
|
const fetchMock = mockCopilotTokenExchangeSuccess();
|
||||||
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 {
|
try {
|
||||||
await ensureOpenClawModelsJson({ models: { providers: {} } });
|
await ensureOpenClawModelsJson({ models: { providers: {} } });
|
||||||
|
|||||||
@@ -71,6 +71,17 @@ export function mockCopilotTokenExchangeSuccess(): MockFn {
|
|||||||
return fetchMock;
|
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 = [
|
export const MODELS_CONFIG_IMPLICIT_ENV_VARS = [
|
||||||
"CLOUDFLARE_AI_GATEWAY_API_KEY",
|
"CLOUDFLARE_AI_GATEWAY_API_KEY",
|
||||||
"COPILOT_GITHUB_TOKEN",
|
"COPILOT_GITHUB_TOKEN",
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
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 { resolveOpenClawAgentDir } from "./agent-paths.js";
|
import { resolveOpenClawAgentDir } from "./agent-paths.js";
|
||||||
import {
|
import {
|
||||||
installModelsConfigTestHooks,
|
installModelsConfigTestHooks,
|
||||||
mockCopilotTokenExchangeSuccess,
|
mockCopilotTokenExchangeSuccess,
|
||||||
|
withCopilotGithubToken,
|
||||||
withUnsetCopilotTokenEnv,
|
withUnsetCopilotTokenEnv,
|
||||||
withModelsTempHome as withTempHome,
|
withModelsTempHome as withTempHome,
|
||||||
} from "./models-config.e2e-harness.js";
|
} from "./models-config.e2e-harness.js";
|
||||||
@@ -53,19 +53,7 @@ describe("models-config", () => {
|
|||||||
|
|
||||||
it("does not override explicit github-copilot provider config", async () => {
|
it("does not override explicit github-copilot provider config", async () => {
|
||||||
await withTempHome(async () => {
|
await withTempHome(async () => {
|
||||||
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]);
|
await withCopilotGithubToken("gh-token", async () => {
|
||||||
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 ensureOpenClawModelsJson({
|
await ensureOpenClawModelsJson({
|
||||||
models: {
|
models: {
|
||||||
providers: {
|
providers: {
|
||||||
@@ -85,9 +73,7 @@ describe("models-config", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://copilot.local");
|
expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://copilot.local");
|
||||||
} finally {
|
});
|
||||||
envSnapshot.restore();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user