|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { ApiKeyPool } from '../ApiKeyPool.js'; |
| 3 | + |
| 4 | +describe('ApiKeyPool', () => { |
| 5 | + describe('construction', () => { |
| 6 | + it('parses comma-separated string into keys', () => { |
| 7 | + const pool = new ApiKeyPool('sk_a,sk_b,sk_c'); |
| 8 | + expect(pool.size).toBe(3); |
| 9 | + expect(pool.hasKeys).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('accepts a single key string', () => { |
| 13 | + const pool = new ApiKeyPool('sk_a'); |
| 14 | + expect(pool.size).toBe(1); |
| 15 | + }); |
| 16 | + |
| 17 | + it('accepts an array of keys', () => { |
| 18 | + const pool = new ApiKeyPool(['sk_a', 'sk_b']); |
| 19 | + expect(pool.size).toBe(2); |
| 20 | + }); |
| 21 | + |
| 22 | + it('trims whitespace from keys', () => { |
| 23 | + const pool = new ApiKeyPool(' sk_a , sk_b '); |
| 24 | + expect(pool.next()).toBe('sk_a'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('filters empty segments', () => { |
| 28 | + const pool = new ApiKeyPool('sk_a,,sk_b,'); |
| 29 | + expect(pool.size).toBe(2); |
| 30 | + }); |
| 31 | + |
| 32 | + it('handles empty string', () => { |
| 33 | + const pool = new ApiKeyPool(''); |
| 34 | + expect(pool.size).toBe(0); |
| 35 | + expect(pool.hasKeys).toBe(false); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('round-robin', () => { |
| 40 | + it('single key always returns the same key', () => { |
| 41 | + const pool = new ApiKeyPool('sk_only'); |
| 42 | + expect(pool.next()).toBe('sk_only'); |
| 43 | + expect(pool.next()).toBe('sk_only'); |
| 44 | + expect(pool.next()).toBe('sk_only'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rotates through keys in order', () => { |
| 48 | + const pool = new ApiKeyPool('sk_a,sk_b,sk_c', { primaryWeight: 1 }); |
| 49 | + expect(pool.next()).toBe('sk_a'); |
| 50 | + expect(pool.next()).toBe('sk_b'); |
| 51 | + expect(pool.next()).toBe('sk_c'); |
| 52 | + expect(pool.next()).toBe('sk_a'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('gives first key higher weight with default primaryWeight=2', () => { |
| 56 | + const pool = new ApiKeyPool('sk_a,sk_b'); |
| 57 | + const counts: Record<string, number> = { sk_a: 0, sk_b: 0 }; |
| 58 | + for (let i = 0; i < 90; i++) counts[pool.next()]++; |
| 59 | + expect(counts.sk_a).toBeGreaterThan(counts.sk_b); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('exhaustion', () => { |
| 64 | + beforeEach(() => { vi.useFakeTimers(); }); |
| 65 | + afterEach(() => { vi.useRealTimers(); }); |
| 66 | + |
| 67 | + it('skips exhausted key', () => { |
| 68 | + const pool = new ApiKeyPool('sk_a,sk_b', { primaryWeight: 1 }); |
| 69 | + pool.next(); // sk_a |
| 70 | + pool.markExhausted('sk_a'); |
| 71 | + expect(pool.next()).toBe('sk_b'); |
| 72 | + expect(pool.next()).toBe('sk_b'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('re-includes key after cooldown expires', () => { |
| 76 | + const pool = new ApiKeyPool('sk_a,sk_b', { primaryWeight: 1, cooldownMs: 1000 }); |
| 77 | + pool.next(); // sk_a |
| 78 | + pool.markExhausted('sk_a'); |
| 79 | + expect(pool.next()).toBe('sk_b'); |
| 80 | + |
| 81 | + vi.advanceTimersByTime(1001); |
| 82 | + const next3 = [pool.next(), pool.next()]; |
| 83 | + expect(next3).toContain('sk_a'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns least-exhausted key when all are exhausted', () => { |
| 87 | + const pool = new ApiKeyPool('sk_a,sk_b', { primaryWeight: 1, cooldownMs: 10_000 }); |
| 88 | + pool.markExhausted('sk_a'); |
| 89 | + vi.advanceTimersByTime(5000); |
| 90 | + pool.markExhausted('sk_b'); |
| 91 | + expect(pool.next()).toBe('sk_a'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('edge cases', () => { |
| 96 | + it('next() returns empty string for empty pool', () => { |
| 97 | + const pool = new ApiKeyPool(''); |
| 98 | + expect(pool.next()).toBe(''); |
| 99 | + }); |
| 100 | + |
| 101 | + it('markExhausted on unknown key is a no-op', () => { |
| 102 | + const pool = new ApiKeyPool('sk_a'); |
| 103 | + pool.markExhausted('sk_unknown'); |
| 104 | + expect(pool.next()).toBe('sk_a'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments