refactor(test): fix copilot env restore

This commit is contained in:
Peter Steinberger 2026-02-16 00:15:20 +00:00
parent 76015aab23
commit 72baa58edd

View File

@ -1,6 +1,7 @@
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, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { import {
installModelsConfigTestHooks, installModelsConfigTestHooks,
withModelsTempHome as withTempHome, withModelsTempHome as withTempHome,
@ -12,7 +13,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 previous = process.env.COPILOT_GITHUB_TOKEN; const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]);
process.env.COPILOT_GITHUB_TOKEN = "gh-token"; process.env.COPILOT_GITHUB_TOKEN = "gh-token";
const fetchMock = vi.fn().mockResolvedValue({ const fetchMock = vi.fn().mockResolvedValue({
ok: true, ok: true,
@ -36,20 +37,14 @@ 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 { } finally {
if (previous === undefined) { envSnapshot.restore();
delete process.env.COPILOT_GITHUB_TOKEN;
} else {
process.env.COPILOT_GITHUB_TOKEN = previous;
}
} }
}); });
}); });
it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => { it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => {
await withTempHome(async () => { await withTempHome(async () => {
const previous = process.env.COPILOT_GITHUB_TOKEN; const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"]);
const previousGh = process.env.GH_TOKEN;
const previousGithub = process.env.GITHUB_TOKEN;
process.env.COPILOT_GITHUB_TOKEN = "copilot-token"; process.env.COPILOT_GITHUB_TOKEN = "copilot-token";
process.env.GH_TOKEN = "gh-token"; process.env.GH_TOKEN = "gh-token";
process.env.GITHUB_TOKEN = "github-token"; process.env.GITHUB_TOKEN = "github-token";
@ -70,9 +65,7 @@ describe("models-config", () => {
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }]; const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token"); expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
} finally { } finally {
process.env.COPILOT_GITHUB_TOKEN = previous; envSnapshot.restore();
process.env.GH_TOKEN = previousGh;
process.env.GITHUB_TOKEN = previousGithub;
} }
}); });
}); });