Files
oh-my-pi/packages/ai/test/api-registry.test.ts
T
can1357 d6bd208c25 fix: deferred model resolution and extension provider registration
- Fixes deferred `--model` resolution to match extension-provided models before fallback
- Fixes CLI `--api-key` handling to support deferred model selection
- Adds OAuth provider support for extensions with source-scoped registration cleanup
- Adds custom API registration helpers with built-in collision checks
- Expands `Api` type to support extension-defined identifiers
- Adds tests for runtime provider registration and model selection
2026-02-16 04:52:33 +01:00

52 lines
1.6 KiB
TypeScript

import { afterEach, describe, expect, test } from "bun:test";
import {
type CustomStreamSimpleFn,
clearCustomApis,
getCustomApi,
registerCustomApi,
unregisterCustomApis,
} from "../src/api-registry";
import type { AssistantMessageEventStream } from "../src/types";
afterEach(() => {
clearCustomApis();
});
describe("custom API registry", () => {
const streamSimple: CustomStreamSimpleFn = () => ({}) as unknown as AssistantMessageEventStream;
test("registers and resolves a custom API provider", () => {
registerCustomApi("custom-provider", streamSimple, "ext-a");
const provider = getCustomApi("custom-provider");
expect(provider).toBeDefined();
expect(provider?.streamSimple).toBe(streamSimple);
expect(provider?.sourceId).toBe("ext-a");
});
test("rejects registrations that collide with built-in API names", () => {
expect(() => registerCustomApi("openai-responses", streamSimple)).toThrow(
'Cannot register custom API "openai-responses": built-in API names are reserved.',
);
});
test("unregisterCustomApis removes only matching source registrations", () => {
registerCustomApi("custom-a", streamSimple, "ext-a");
registerCustomApi("custom-b", streamSimple, "ext-b");
unregisterCustomApis("ext-a");
expect(getCustomApi("custom-a")).toBeUndefined();
expect(getCustomApi("custom-b")).toBeDefined();
});
test("clearCustomApis removes all custom APIs", () => {
registerCustomApi("custom-a", streamSimple, "ext-a");
registerCustomApi("custom-b", streamSimple, "ext-b");
clearCustomApis();
expect(getCustomApi("custom-a")).toBeUndefined();
expect(getCustomApi("custom-b")).toBeUndefined();
});
});