|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; |
| 4 | + |
| 5 | +import { makeOpenRouterApi } from "./openrouterApi.ts"; |
| 6 | + |
| 7 | +const config = { |
| 8 | + kind: "openrouter", |
| 9 | + managementKey: "sk-or-secret", |
| 10 | + enabled: true, |
| 11 | +} as const; |
| 12 | + |
| 13 | +/** One canned reply per endpoint path, plus the paths actually requested. */ |
| 14 | +function fixture(replies: Record<string, { status: number; body: unknown }>) { |
| 15 | + const paths: string[] = []; |
| 16 | + const http = HttpClient.make((request) => |
| 17 | + Effect.sync(() => { |
| 18 | + expect(request.headers.authorization).toBe("Bearer sk-or-secret"); |
| 19 | + const path = new URL(request.url).pathname; |
| 20 | + paths.push(path); |
| 21 | + const reply = replies[path] ?? { status: 404, body: {} }; |
| 22 | + return HttpClientResponse.fromWeb( |
| 23 | + request, |
| 24 | + Response.json(reply.body, { status: reply.status }), |
| 25 | + ); |
| 26 | + }), |
| 27 | + ); |
| 28 | + return { paths, api: makeOpenRouterApi.pipe(Effect.provideService(HttpClient.HttpClient, http)) }; |
| 29 | +} |
| 30 | + |
| 31 | +describe("OpenRouter credit reads", () => { |
| 32 | + it.effect("reports the account balance a provisioning key can see", () => |
| 33 | + Effect.gen(function* () { |
| 34 | + const test = fixture({ |
| 35 | + "/api/v1/credits": { |
| 36 | + status: 200, |
| 37 | + body: { data: { total_credits: 100.5, total_usage: 25.75 } }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + const api = yield* test.api; |
| 41 | + |
| 42 | + expect(yield* api.readCredits(config)).toEqual({ |
| 43 | + scope: "account", |
| 44 | + usedUsd: 25.75, |
| 45 | + purchasedUsd: 100.5, |
| 46 | + remainingUsd: 74.75, |
| 47 | + }); |
| 48 | + // The narrower read is never made when the account answers. |
| 49 | + expect(test.paths).toEqual(["/api/v1/credits"]); |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + it.effect("falls back to the key's own allowance when OpenRouter refuses with 403", () => |
| 54 | + Effect.gen(function* () { |
| 55 | + const test = fixture({ |
| 56 | + "/api/v1/credits": { |
| 57 | + status: 403, |
| 58 | + body: { |
| 59 | + error: { code: 403, message: "Only management keys can perform this operation" }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + "/api/v1/key": { |
| 63 | + status: 200, |
| 64 | + body: { data: { usage: 1.42, limit: 10, limit_remaining: 8.58, is_free_tier: false } }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + const api = yield* test.api; |
| 68 | + |
| 69 | + expect(yield* api.readCredits(config)).toEqual({ |
| 70 | + scope: "key", |
| 71 | + usedUsd: 1.42, |
| 72 | + limitUsd: 10, |
| 73 | + remainingUsd: 8.58, |
| 74 | + isFreeTier: false, |
| 75 | + }); |
| 76 | + expect(test.paths).toEqual(["/api/v1/credits", "/api/v1/key"]); |
| 77 | + }), |
| 78 | + ); |
| 79 | + |
| 80 | + it.effect("leaves an uncapped key with spend only, so no balance is invented", () => |
| 81 | + Effect.gen(function* () { |
| 82 | + const test = fixture({ |
| 83 | + "/api/v1/credits": { status: 403, body: {} }, |
| 84 | + "/api/v1/key": { |
| 85 | + status: 200, |
| 86 | + body: { data: { usage: 3.5, limit: null, limit_remaining: null, is_free_tier: true } }, |
| 87 | + }, |
| 88 | + }); |
| 89 | + const api = yield* test.api; |
| 90 | + |
| 91 | + expect(yield* api.readCredits(config)).toEqual({ |
| 92 | + scope: "key", |
| 93 | + usedUsd: 3.5, |
| 94 | + isFreeTier: true, |
| 95 | + }); |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + it.effect("reports a rejected key rather than retrying it against the other endpoint", () => |
| 100 | + Effect.gen(function* () { |
| 101 | + const test = fixture({ "/api/v1/credits": { status: 401, body: {} } }); |
| 102 | + const api = yield* test.api; |
| 103 | + const result = yield* api.readCredits(config).pipe(Effect.result); |
| 104 | + |
| 105 | + expect(result._tag).toBe("Failure"); |
| 106 | + if (result._tag === "Failure") { |
| 107 | + expect(result.failure.detail).toBe("OpenRouter rejected the API key."); |
| 108 | + } |
| 109 | + expect(test.paths).toEqual(["/api/v1/credits"]); |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + it.effect("reports a body it cannot read instead of publishing a partial balance", () => |
| 114 | + Effect.gen(function* () { |
| 115 | + const test = fixture({ |
| 116 | + "/api/v1/credits": { status: 200, body: { data: { total_credits: "lots" } } }, |
| 117 | + }); |
| 118 | + const api = yield* test.api; |
| 119 | + const result = yield* api.readCredits(config).pipe(Effect.result); |
| 120 | + |
| 121 | + expect(result._tag).toBe("Failure"); |
| 122 | + if (result._tag === "Failure") { |
| 123 | + expect(result.failure.detail).toBe("OpenRouter returned an unexpected balance."); |
| 124 | + } |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + it.effect("never calls OpenRouter without a key", () => |
| 129 | + Effect.gen(function* () { |
| 130 | + const test = fixture({}); |
| 131 | + const api = yield* test.api; |
| 132 | + const result = yield* api.readCredits({ ...config, managementKey: "" }).pipe(Effect.result); |
| 133 | + |
| 134 | + expect(result._tag).toBe("Failure"); |
| 135 | + expect(test.paths).toEqual([]); |
| 136 | + }), |
| 137 | + ); |
| 138 | +}); |
0 commit comments