|
| 1 | +/// <reference types="bun" /> |
| 2 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 3 | +import { OpenRouterProvider } from "../src/provider/openrouter.provider.js"; |
| 4 | + |
| 5 | +const originalFetch = globalThis.fetch; |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + globalThis.fetch = originalFetch; |
| 9 | +}); |
| 10 | + |
| 11 | +describe("OpenRouterProvider", () => { |
| 12 | + it("parses JSON response and exposes usage metrics", async () => { |
| 13 | + globalThis.fetch = async () => |
| 14 | + new Response( |
| 15 | + JSON.stringify({ |
| 16 | + choices: [{ message: { content: '{"projectName":"acme"}' } }], |
| 17 | + usage: { prompt_tokens: 120, completion_tokens: 80, total_tokens: 200 }, |
| 18 | + model: "openai/gpt-4o-mini", |
| 19 | + }), |
| 20 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 21 | + ); |
| 22 | + |
| 23 | + const provider = new OpenRouterProvider({ apiKey: "test-key" }); |
| 24 | + |
| 25 | + const result = await provider.call({ |
| 26 | + agentName: "planner", |
| 27 | + input: { prompt: "build app", projectName: "acme" }, |
| 28 | + options: { |
| 29 | + systemPrompt: "Return JSON", |
| 30 | + userPrompt: "{\"prompt\":\"build app\"}", |
| 31 | + model: "openai/gpt-4o-mini", |
| 32 | + maxInputTokens: 300, |
| 33 | + maxOutputTokens: 300, |
| 34 | + temperature: 0.1, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + expect(result.output).toEqual({ projectName: "acme" }); |
| 39 | + expect(result.inputTokens).toBe(120); |
| 40 | + expect(result.outputTokens).toBe(80); |
| 41 | + expect(result.tokensUsed).toBe(200); |
| 42 | + expect(result.model).toBe("openai/gpt-4o-mini"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("throws on non-JSON model content", async () => { |
| 46 | + globalThis.fetch = async () => |
| 47 | + new Response( |
| 48 | + JSON.stringify({ |
| 49 | + choices: [{ message: { content: "not-json" } }], |
| 50 | + }), |
| 51 | + { status: 200, headers: { "content-type": "application/json" } }, |
| 52 | + ); |
| 53 | + |
| 54 | + const provider = new OpenRouterProvider({ apiKey: "test-key" }); |
| 55 | + |
| 56 | + await expect( |
| 57 | + provider.call({ |
| 58 | + agentName: "planner", |
| 59 | + input: { prompt: "build app", projectName: "acme" }, |
| 60 | + options: { |
| 61 | + systemPrompt: "Return JSON", |
| 62 | + userPrompt: "{}", |
| 63 | + model: "openai/gpt-4o-mini", |
| 64 | + maxInputTokens: 300, |
| 65 | + maxOutputTokens: 300, |
| 66 | + temperature: 0.1, |
| 67 | + }, |
| 68 | + }), |
| 69 | + ).rejects.toThrow("OpenRouter returned non-JSON content"); |
| 70 | + }); |
| 71 | +}); |
0 commit comments