import { assertEquals, assertRejects, assertStrictEquals } from "@std/assert"; import type { PayloadHook } from "@berry/api"; import { testExtension } from "@berry/api/testing.ts"; import extension from "./mod.ts"; type Model = Parameters[1]; const openrouter = { provider: "openrouter" } as Model; const direct = { provider: "lithosai" } as Model; Deno.test("default routing requirements leave the request alone", async () => { const ext = testExtension(extension); const payload = { model: "deepseek/deepseek-v4.1-flash:nitro", messages: [] }; assertStrictEquals(await ext.payload(payload, openrouter), payload); await ext.configure({ allowFallbacks: "default" }); assertStrictEquals(await ext.payload(payload, openrouter), payload); assertEquals(await ext.payload(null, openrouter), null); assertEquals(await ext.payload([], openrouter), []); }); Deno.test("any non-default field applies routing, to OpenRouter object payloads only", async () => { const ext = testExtension(extension); await ext.configure({ sort: "throughput", maxCompletionPrice: "1.00" }); const payload = { model: "deepseek/deepseek-v4.1-flash:nitro", messages: [] }; assertEquals(await ext.payload(payload, openrouter), { ...payload, provider: { sort: "throughput", max_price: { completion: 1 } }, }); assertStrictEquals(await ext.payload(payload, direct), payload); await ext.configure({ sort: "default", maxCompletionPrice: "" }); assertStrictEquals(await ext.payload(payload, openrouter), payload); }); Deno.test("requirements override their fields without losing other routing or mutating the request", async () => { const ext = testExtension(extension); await ext.configure({ only: " baseten/fast, together, baseten/fast ", ignore: " deepinfra, ,novita ", maxCompletionPrice: ".75", allowFallbacks: "no", }); const payload = { model: "deepseek/deepseek-v4.1-flash", provider: { sort: "latency", only: ["fireworks"], allow_fallbacks: true, data_collection: "deny", max_price: { prompt: 0.2, completion: 5, request: 0.01 }, }, tools: [{ type: "function" }], }; const before = structuredClone(payload); assertEquals(await ext.payload(payload, openrouter), { ...payload, provider: { sort: "latency", only: ["baseten/fast", "together"], ignore: ["deepinfra", "novita"], allow_fallbacks: false, data_collection: "deny", max_price: { prompt: 0.2, completion: 0.75, request: 0.01 }, }, }); assertEquals(payload, before); }); Deno.test("zero is a price cap and clearing caps removes the conversation override", async () => { const ext = testExtension(extension); await ext.configure({ maxPromptPrice: " 0 ", maxCompletionPrice: "0.5" }); assertEquals(await ext.payload({}, openrouter), { provider: { max_price: { prompt: 0, completion: 0.5 } }, }); await ext.configure({ maxPromptPrice: "", maxCompletionPrice: " " }); const payload = { provider: { max_price: { prompt: 2 } } }; assertStrictEquals(await ext.payload(payload, openrouter), payload); }); Deno.test("invalid prices refuse configuration without discarding the current requirements", async () => { const ext = testExtension(extension); await ext.configure({ maxCompletionPrice: "1" }); await assertRejects( () => ext.configure({ maxCompletionPrice: "-0.5" }), Error, ); await assertRejects( () => ext.configure({ maxPromptPrice: "9".repeat(400) }), Error, ); assertEquals(await ext.payload({}, openrouter), { provider: { max_price: { completion: 1 } }, }); }); Deno.test("settings replay independently, and one recorded before a field was dropped no longer routes", async () => { const ext = testExtension(extension); const events = await ext.seed({ sort: "throughput", only: "parasail/fast", allowFallbacks: "yes", }); const restored = testExtension(extension, { events }); const expected = { provider: { sort: "throughput", only: ["parasail/fast"], allow_fallbacks: true, }, }; assertEquals(await restored.payload({}, openrouter), expected); const untouched = {}; assertStrictEquals(await ext.payload(untouched, openrouter), untouched); // a conversation that recorded the dropped fields folds to the defaults, without routing const older = testExtension(extension, { events: [{ type: "configured", values: { enabled: true, order: "together", requireParameters: true }, }] as never, }); assertEquals(older.state, { sort: "default", only: "", ignore: "", maxPromptPrice: "", maxCompletionPrice: "", allowFallbacks: "default", }); assertStrictEquals(await older.payload(untouched, openrouter), untouched); });