Files
oh-my-pi/packages/ai/test/github-copilot-openai-base-url.test.ts
T
can1357 1b9d9d0851 refactor(catalog)!: split model catalog from pi-ai
Move bundled models, model cache/manager, thinking metadata, effort helpers,
provider descriptors/discovery, wire constants, and model identity utilities
into the new @oh-my-pi/pi-catalog package.

Update pi-ai to keep provider runtime/auth concerns, move catalog provider
metadata into CATALOG_PROVIDERS, and migrate coding-agent, agent, stats, docs,
and tests to import catalog values from pi-catalog.

Split coding-agent model registry helpers into discovery, roles, and models
config modules while preserving registry orchestration.

BREAKING CHANGE: @oh-my-pi/pi-ai no longer exports catalog subpaths such as
/models, /model-cache, /model-manager, /model-thinking, /effort,
/provider-models*, discovery helpers, and provider wire constants; use the
matching @oh-my-pi/pi-catalog subpaths instead.
2026-06-10 04:06:57 +02:00

154 lines
5.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "bun:test";
import { streamOpenAICompletions } from "@oh-my-pi/pi-ai/providers/openai-completions";
import { streamOpenAIResponses } from "@oh-my-pi/pi-ai/providers/openai-responses";
import type { Context, Model } from "@oh-my-pi/pi-ai/types";
import { getBundledModel } from "@oh-my-pi/pi-catalog/models";
afterEach(() => {
vi.restoreAllMocks();
});
const testContext: Context = {
messages: [{ role: "user", content: "hello", timestamp: Date.now() }],
};
function getRequestUrl(input: string | URL | Request): string {
if (input instanceof Request) {
return input.url;
}
return typeof input === "string" ? input : input.toString();
}
function getRequestHeader(
input: string | URL | Request,
init: RequestInit | undefined,
headerName: string,
): string | null {
if (input instanceof Request) {
return input.headers.get(headerName);
}
return new Headers(init?.headers).get(headerName);
}
function createUnauthorizedResponse(): Response {
return new Response(JSON.stringify({ error: { message: "Unauthorized" } }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
const testToken = "ghu_test_copilot_token";
const enterpriseApiKey = JSON.stringify({ token: testToken, enterpriseUrl: "ghe.example.com" });
describe("GitHub Copilot OpenAI transport base URL", () => {
it("uses model baseUrl for chat completions", async () => {
const requestedUrls: string[] = [];
const fetchMock = vi.fn(async (input: string | URL | Request) => {
requestedUrls.push(getRequestUrl(input));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-4o") as Model<"openai-completions">;
const result = await streamOpenAICompletions(model, testContext, {
apiKey: testToken,
fetch: fetchMock as unknown as typeof fetch,
}).result();
expect(result.stopReason).toBe("error");
expect(requestedUrls[0]).toBe("https://api.githubcopilot.com/chat/completions");
});
it("uses model baseUrl for responses API", async () => {
const requestedUrls: string[] = [];
const fetchMock = vi.fn(async (input: string | URL | Request) => {
requestedUrls.push(getRequestUrl(input));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-5-mini") as Model<"openai-responses">;
const result = await streamOpenAIResponses(model, testContext, {
apiKey: testToken,
fetch: fetchMock as unknown as typeof fetch,
}).result();
expect(result.stopReason).toBe("error");
expect(requestedUrls[0]).toBe("https://api.githubcopilot.com/responses");
});
it("routes structured enterprise credentials to the enterprise chat completions host", async () => {
const requestedUrls: string[] = [];
const requestedAuthHeaders: Array<string | null> = [];
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
requestedUrls.push(getRequestUrl(input));
requestedAuthHeaders.push(getRequestHeader(input, init, "Authorization"));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-4o") as Model<"openai-completions">;
const result = await streamOpenAICompletions(model, testContext, {
apiKey: enterpriseApiKey,
fetch: fetchMock as unknown as typeof fetch,
}).result();
expect(result.stopReason).toBe("error");
expect(requestedUrls[0]).toBe("https://copilot-api.ghe.example.com/chat/completions");
expect(requestedAuthHeaders[0]).toBe(`Bearer ${testToken}`);
});
it("routes structured enterprise credentials to the enterprise responses host", async () => {
const requestedUrls: string[] = [];
const requestedAuthHeaders: Array<string | null> = [];
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
requestedUrls.push(getRequestUrl(input));
requestedAuthHeaders.push(getRequestHeader(input, init, "Authorization"));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-5-mini") as Model<"openai-responses">;
const result = await streamOpenAIResponses(model, testContext, {
apiKey: enterpriseApiKey,
fetch: fetchMock as unknown as typeof fetch,
}).result();
expect(result.stopReason).toBe("error");
expect(requestedUrls[0]).toBe("https://copilot-api.ghe.example.com/responses");
expect(requestedAuthHeaders[0]).toBe(`Bearer ${testToken}`);
});
it("forwards initiatorOverride to chat completions requests", async () => {
const requestedInitiators: Array<string | null> = [];
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
requestedInitiators.push(getRequestHeader(input, init, "X-Initiator"));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-4o") as Model<"openai-completions">;
const result = await streamOpenAICompletions(model, testContext, {
apiKey: testToken,
fetch: fetchMock as unknown as typeof fetch,
initiatorOverride: "agent",
}).result();
expect(result.stopReason).toBe("error");
expect(requestedInitiators[0]).toBe("agent");
});
it("forwards initiatorOverride to responses requests", async () => {
const requestedInitiators: Array<string | null> = [];
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
requestedInitiators.push(getRequestHeader(input, init, "X-Initiator"));
return createUnauthorizedResponse();
});
const model = getBundledModel("github-copilot", "gpt-5-mini") as Model<"openai-responses">;
const result = await streamOpenAIResponses(model, testContext, {
apiKey: testToken,
fetch: fetchMock as unknown as typeof fetch,
initiatorOverride: "agent",
}).result();
expect(result.stopReason).toBe("error");
expect(requestedInitiators[0]).toBe("agent");
});
});