import { fileURLToPath } from "node:url"; import { zstdDecompressSync } from "node:zlib"; import { type Model, normalizeContext } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { _test } from "../index.ts"; let session: PiSession | undefined; afterEach(async () => { await session?.dispose(); session = undefined; }); function fastPath(): string { return fileURLToPath(new URL("../index.ts", import.meta.url)); } describe("fast contract", () => { it("loads through the owned fixture", async () => { const extensionPath = fastPath(); session = await createPiSession({ extensionPaths: [extensionPath] }); expect(session.errors).toEqual([]); expect(session.extensions).toHaveLength(1); expect(session.extensions[0]!.path).toBe(extensionPath); }); it.each([ ["enabled", { fast: { enable: true } }, "priority"], ["disabled by default", {}, undefined], ] as const)( "dispatches %s requests with expected service tier", async (_label, userSettings, expectedTier) => { session = await createPiSession({ extensionPaths: [fastPath()], userSettings, }); const provider = session.session.modelRuntime.getProvider("openai-codex"); if (!provider) throw new Error("missing registered openai-codex provider"); // Catalog-backed model metadata, supplied directly because the fixture registry only lists Faux. const model: Model<"openai-codex-responses"> = { id: "gpt-6-luna", name: "GPT-6 Luna", api: "openai-codex-responses", provider: "openai-codex", baseUrl: "https://chatgpt.com/backend-api", reasoning: true, input: ["text", "image"], contextWindow: 272000, maxTokens: 128000, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, }; const claim = Buffer.from( JSON.stringify({ "https://api.openai.com/auth": { chatgpt_account_id: "synthetic-account", }, }), ).toString("base64url"); let sentBody: Record | undefined; const fetch = vi.fn( async (url: string | URL | Request, init?: RequestInit) => { const request = new Request(url, init); const bytes = Buffer.from(await request.arrayBuffer()); const encoding = request.headers.get("content-encoding"); if (encoding && encoding !== "zstd") throw new Error(`Unexpected fixture encoding: ${encoding}`); const decoded = encoding === "zstd" ? zstdDecompressSync(bytes) : bytes; sentBody = JSON.parse(decoded.toString("utf8")) as Record< string, unknown >; return Response.json( { error: { message: "fixture transport reached" } }, { status: 400 }, ); }, ); const stream = provider.streamSimple( model, normalizeContext({ messages: [] }), { apiKey: `header.${claim}.signature`, fetch, transport: "sse", maxRetries: 0, }, ); const errors: string[] = []; for await (const event of stream) { if (event.type === "error") errors.push(event.error.errorMessage ?? "Unknown stream error"); } expect(fetch, errors.join("\n")).toHaveBeenCalledOnce(); expect(errors).toEqual([ expect.stringContaining("fixture transport reached"), ]); expect(sentBody).toBeDefined(); expect(sentBody?.["service_tier"]).toBe(expectedTier); }, ); it("owns fast activation", () => { expect( _test.fastActive({ model: { provider: "openai-codex" } } as never, true), ).toBe(true); expect( _test.fastActive({ model: { provider: "openai" } } as never, true), ).toBe(false); expect( _test.fastActive({ model: { provider: "openai-codex" } } as never, false), ).toBe(false); }); });