|
| 1 | +import { describe, test, expect, beforeEach } from 'bun:test' |
| 2 | +import { anthropicAdapter } from '../adapters/anthropic.js' |
| 3 | +import { openaiAdapter } from '../adapters/openai.js' |
| 4 | +import { bedrockAdapter } from '../adapters/bedrock.js' |
| 5 | +import { |
| 6 | + getProviderUsage, |
| 7 | + resetProviderUsage, |
| 8 | + setProviderBalance, |
| 9 | + subscribeProviderUsage, |
| 10 | + updateProviderBuckets, |
| 11 | +} from '../store.js' |
| 12 | + |
| 13 | +function headers(pairs: Record<string, string>): Headers { |
| 14 | + const h = new Headers() |
| 15 | + for (const [k, v] of Object.entries(pairs)) h.set(k, v) |
| 16 | + return h |
| 17 | +} |
| 18 | + |
| 19 | +describe('anthropicAdapter', () => { |
| 20 | + test('parses both 5h and 7d buckets', () => { |
| 21 | + const h = headers({ |
| 22 | + 'anthropic-ratelimit-unified-5h-utilization': '0.42', |
| 23 | + 'anthropic-ratelimit-unified-5h-reset': '1800000000', |
| 24 | + 'anthropic-ratelimit-unified-7d-utilization': '0.1', |
| 25 | + 'anthropic-ratelimit-unified-7d-reset': '1800100000', |
| 26 | + }) |
| 27 | + const out = anthropicAdapter.parseHeaders(h) |
| 28 | + expect(out).toHaveLength(2) |
| 29 | + expect(out[0]).toMatchObject({ |
| 30 | + kind: 'session', |
| 31 | + label: 'Session', |
| 32 | + utilization: 0.42, |
| 33 | + resetsAt: 1800000000, |
| 34 | + }) |
| 35 | + expect(out[1]).toMatchObject({ |
| 36 | + kind: 'weekly', |
| 37 | + label: 'Weekly', |
| 38 | + utilization: 0.1, |
| 39 | + resetsAt: 1800100000, |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + test('returns [] when headers absent (API key user)', () => { |
| 44 | + expect(anthropicAdapter.parseHeaders(new Headers())).toEqual([]) |
| 45 | + }) |
| 46 | + |
| 47 | + test('drops bucket with non-numeric utilization', () => { |
| 48 | + const h = headers({ |
| 49 | + 'anthropic-ratelimit-unified-5h-utilization': 'xx', |
| 50 | + 'anthropic-ratelimit-unified-5h-reset': '0', |
| 51 | + }) |
| 52 | + expect(anthropicAdapter.parseHeaders(h)).toEqual([]) |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('openaiAdapter', () => { |
| 57 | + test('computes RPM and TPM utilization from limit+remaining', () => { |
| 58 | + const h = headers({ |
| 59 | + 'x-ratelimit-limit-requests': '1000', |
| 60 | + 'x-ratelimit-remaining-requests': '250', |
| 61 | + 'x-ratelimit-limit-tokens': '100000', |
| 62 | + 'x-ratelimit-remaining-tokens': '25000', |
| 63 | + 'x-ratelimit-reset-requests': '6m', |
| 64 | + }) |
| 65 | + const out = openaiAdapter.parseHeaders(h) |
| 66 | + expect(out).toHaveLength(2) |
| 67 | + expect(out[0].kind).toBe('requests') |
| 68 | + expect(out[0].label).toBe('RPM') |
| 69 | + expect(out[0].utilization).toBeCloseTo(0.75, 5) |
| 70 | + expect(out[1].kind).toBe('tokens') |
| 71 | + expect(out[1].utilization).toBeCloseTo(0.75, 5) |
| 72 | + }) |
| 73 | + |
| 74 | + test('returns [] when no relevant headers', () => { |
| 75 | + expect(openaiAdapter.parseHeaders(new Headers())).toEqual([]) |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +describe('bedrockAdapter', () => { |
| 80 | + test('inverts quota-remaining into utilization', () => { |
| 81 | + const h = headers({ |
| 82 | + 'x-amzn-bedrock-quota-remaining': '0.3', |
| 83 | + 'x-amzn-bedrock-quota-reset': '1800000000', |
| 84 | + }) |
| 85 | + const out = bedrockAdapter.parseHeaders(h) |
| 86 | + expect(out).toHaveLength(1) |
| 87 | + expect(out[0].kind).toBe('throttle') |
| 88 | + expect(out[0].utilization).toBeCloseTo(0.7, 5) |
| 89 | + expect(out[0].resetsAt).toBe(1800000000) |
| 90 | + }) |
| 91 | + |
| 92 | + test('returns [] without header', () => { |
| 93 | + expect(bedrockAdapter.parseHeaders(new Headers())).toEqual([]) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +describe('providerUsage store', () => { |
| 98 | + beforeEach(() => { |
| 99 | + resetProviderUsage() |
| 100 | + }) |
| 101 | + |
| 102 | + test('updateProviderBuckets replaces buckets and notifies', () => { |
| 103 | + const seen: string[] = [] |
| 104 | + const unsub = subscribeProviderUsage(u => seen.push(u.providerId)) |
| 105 | + updateProviderBuckets('openai', [ |
| 106 | + { kind: 'tokens', label: 'TPM', utilization: 0.5 }, |
| 107 | + ]) |
| 108 | + expect(getProviderUsage().providerId).toBe('openai') |
| 109 | + expect(getProviderUsage().buckets).toHaveLength(1) |
| 110 | + expect(seen).toEqual(['openai']) |
| 111 | + unsub() |
| 112 | + }) |
| 113 | + |
| 114 | + test('setProviderBalance stores and clears', () => { |
| 115 | + setProviderBalance('deepseek', { currency: 'USD', remaining: 3.5 }) |
| 116 | + expect(getProviderUsage().balance?.remaining).toBe(3.5) |
| 117 | + setProviderBalance('deepseek', null) |
| 118 | + expect(getProviderUsage().balance).toBeUndefined() |
| 119 | + }) |
| 120 | +}) |
0 commit comments