perf(test): remove resetModules from auth/models/subagent suites

This commit is contained in:
Peter Steinberger
2026-02-13 15:53:26 +00:00
parent c179f71f42
commit 02fe0c840e
9 changed files with 118 additions and 191 deletions

View File

@@ -3,6 +3,7 @@ import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
import { ensureOpenClawModelsJson } from "./models-config.js";
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withTempHomeBase(fn, { prefix: "openclaw-models-" });
@@ -34,6 +35,7 @@ const _MODELS_CONFIG: OpenClawConfig = {
describe("models-config", () => {
let previousHome: string | undefined;
const originalFetch = globalThis.fetch;
beforeEach(() => {
previousHome = process.env.HOME;
@@ -41,28 +43,26 @@ describe("models-config", () => {
afterEach(() => {
process.env.HOME = previousHome;
if (originalFetch) {
globalThis.fetch = originalFetch;
}
});
it("auto-injects github-copilot provider when token is present", async () => {
await withTempHome(async (home) => {
const previous = process.env.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 {
vi.resetModules();
vi.doMock("../providers/github-copilot-token.js", () => ({
DEFAULT_COPILOT_API_BASE_URL: "https://api.individual.githubcopilot.com",
resolveCopilotApiToken: vi.fn().mockResolvedValue({
token: "copilot",
expiresAt: Date.now() + 60 * 60 * 1000,
source: "mock",
baseUrl: "https://api.copilot.example",
}),
}));
const { ensureOpenClawModelsJson } = await import("./models-config.js");
const agentDir = path.join(home, "agent-default-base-url");
await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir);
@@ -78,6 +78,7 @@ describe("models-config", () => {
}
});
});
it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => {
await withTempHome(async () => {
const previous = process.env.COPILOT_GITHUB_TOKEN;
@@ -87,28 +88,21 @@ 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;
try {
vi.resetModules();
const resolveCopilotApiToken = vi.fn().mockResolvedValue({
token: "copilot",
expiresAt: Date.now() + 60 * 60 * 1000,
source: "mock",
baseUrl: "https://api.copilot.example",
});
vi.doMock("../providers/github-copilot-token.js", () => ({
DEFAULT_COPILOT_API_BASE_URL: "https://api.individual.githubcopilot.com",
resolveCopilotApiToken,
}));
const { ensureOpenClawModelsJson } = await import("./models-config.js");
await ensureOpenClawModelsJson({ models: { providers: {} } });
expect(resolveCopilotApiToken).toHaveBeenCalledWith(
expect.objectContaining({ githubToken: "copilot-token" }),
);
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
} finally {
process.env.COPILOT_GITHUB_TOKEN = previous;
process.env.GH_TOKEN = previousGh;