Repository navigation
Expand file tree
/
Copy pathapi-key-health.spec.ts
More file actions
325 lines (265 loc) · 10.1 KB
/
Copy pathapi-key-health.spec.ts
File metadata and controls
325 lines (265 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { describe, it, expect, beforeEach } from "vitest";
import {
isKeyHealthy,
reportKeySuccess,
reportKeyError,
getKeyHealth,
getKeyMetrics,
getAllKeyMetrics,
calculateUptimePenalty,
resetKeyHealth,
UPTIME_PENALTY_THRESHOLD,
} from "./api-key-health.js";
describe("api-key-health", () => {
beforeEach(() => {
resetKeyHealth();
});
describe("isKeyHealthy", () => {
it("should return true for keys with no health data", () => {
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(true);
});
it("should return true after successful requests", () => {
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(true);
});
it("should return true after fewer than threshold errors", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(true);
});
it("should return false after reaching error threshold", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(false);
});
it("should return false for permanently blacklisted keys (401)", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 401);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(false);
});
it("should return false for permanently blacklisted keys (403)", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 403);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(false);
});
it("should track different keys independently", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(false);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 1)).toBe(true);
expect(isKeyHealthy("LLM_ANTHROPIC_API_KEY", 0)).toBe(true);
});
});
describe("reportKeySuccess", () => {
it("should reset consecutive errors", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.consecutiveErrors).toBe(0);
});
it("should not reset permanently blacklisted keys", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 401);
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
expect(isKeyHealthy("LLM_OPENAI_API_KEY", 0)).toBe(false);
});
});
describe("reportKeyError", () => {
it("should increment consecutive errors", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.consecutiveErrors).toBe(1);
});
it("should update last error time", () => {
const before = Date.now();
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
const after = Date.now();
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.lastErrorTime).toBeGreaterThanOrEqual(before);
expect(health?.lastErrorTime).toBeLessThanOrEqual(after);
});
it("should permanently blacklist on 401", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 401);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.permanentlyBlacklisted).toBe(true);
});
it("should permanently blacklist on 403", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 403);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.permanentlyBlacklisted).toBe(true);
});
it("should not permanently blacklist on other errors", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 429);
reportKeyError("LLM_OPENAI_API_KEY", 0, 502);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.permanentlyBlacklisted).toBe(false);
});
it("should ignore non-exempt 4xx errors for uptime routing", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 400);
reportKeyError("LLM_OPENAI_API_KEY", 0, 422);
expect(getKeyHealth("LLM_OPENAI_API_KEY", 0)).toBeDefined();
expect(getKeyMetrics("LLM_OPENAI_API_KEY", 0)).toMatchObject({
uptime: 100,
totalRequests: 0,
consecutiveErrors: 0,
permanentlyBlacklisted: false,
});
});
it("should still track 404 and 429 for uptime routing", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 404);
reportKeyError("LLM_OPENAI_API_KEY", 0, 429);
expect(getKeyMetrics("LLM_OPENAI_API_KEY", 0)).toMatchObject({
uptime: 0,
totalRequests: 2,
consecutiveErrors: 2,
permanentlyBlacklisted: false,
});
});
it("should still permanently blacklist ignored 4xx with permanent auth text", () => {
reportKeyError(
"LLM_OPENAI_API_KEY",
0,
400,
"API Key not found. Please pass a valid API key.",
);
expect(getKeyMetrics("LLM_OPENAI_API_KEY", 0)).toMatchObject({
uptime: 0,
totalRequests: 1,
consecutiveErrors: 0,
permanentlyBlacklisted: true,
});
});
it("should permanently blacklist ignored 4xx with invalid key text", () => {
reportKeyError(
"LLM_OPENAI_API_KEY",
0,
400,
"API key not valid. Please pass a valid API key.",
);
expect(getKeyMetrics("LLM_OPENAI_API_KEY", 0)).toMatchObject({
uptime: 0,
totalRequests: 1,
consecutiveErrors: 0,
permanentlyBlacklisted: true,
});
});
});
describe("getKeyHealth", () => {
it("should return undefined for unknown keys", () => {
expect(getKeyHealth("LLM_UNKNOWN", 0)).toBeUndefined();
});
it("should return health data for known keys", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health).toBeDefined();
expect(health?.consecutiveErrors).toBe(1);
});
});
describe("resetKeyHealth", () => {
it("should clear all health data", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_ANTHROPIC_API_KEY", 1, 401);
resetKeyHealth();
expect(getKeyHealth("LLM_OPENAI_API_KEY", 0)).toBeUndefined();
expect(getKeyHealth("LLM_ANTHROPIC_API_KEY", 1)).toBeUndefined();
});
});
describe("getKeyMetrics", () => {
it("should return 100% uptime for unknown keys", () => {
const metrics = getKeyMetrics("LLM_UNKNOWN", 0);
expect(metrics.uptime).toBe(100);
expect(metrics.totalRequests).toBe(0);
expect(metrics.consecutiveErrors).toBe(0);
expect(metrics.permanentlyBlacklisted).toBe(false);
});
it("should return 100% uptime for all successful requests", () => {
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
const metrics = getKeyMetrics("LLM_OPENAI_API_KEY", 0);
expect(metrics.uptime).toBe(100);
expect(metrics.totalRequests).toBe(3);
});
it("should calculate uptime based on success/error ratio", () => {
// 7 successes, 3 errors = 70% uptime
for (let i = 0; i < 7; i++) {
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
}
for (let i = 0; i < 3; i++) {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
}
const metrics = getKeyMetrics("LLM_OPENAI_API_KEY", 0);
expect(metrics.uptime).toBe(70);
expect(metrics.totalRequests).toBe(10);
});
it("should track permanently blacklisted status", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 401);
const metrics = getKeyMetrics("LLM_OPENAI_API_KEY", 0);
expect(metrics.permanentlyBlacklisted).toBe(true);
});
it("should track consecutive errors", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
const metrics = getKeyMetrics("LLM_OPENAI_API_KEY", 0);
expect(metrics.consecutiveErrors).toBe(2);
});
});
describe("getAllKeyMetrics", () => {
it("should return metrics for all key indices", () => {
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
reportKeyError("LLM_OPENAI_API_KEY", 1, 500);
const metrics = getAllKeyMetrics("LLM_OPENAI_API_KEY", 3);
expect(metrics).toHaveLength(3);
expect(metrics[0].totalRequests).toBe(1);
expect(metrics[0].uptime).toBe(100);
expect(metrics[1].totalRequests).toBe(1);
expect(metrics[1].uptime).toBe(0);
expect(metrics[2].totalRequests).toBe(0);
expect(metrics[2].uptime).toBe(100); // No data = 100%
});
});
describe("calculateUptimePenalty", () => {
it("should return 0 penalty for uptime >= threshold", () => {
expect(calculateUptimePenalty(100)).toBe(0);
expect(calculateUptimePenalty(UPTIME_PENALTY_THRESHOLD)).toBe(0);
});
it("should return increasing penalty for lower uptime", () => {
const penalty90 = calculateUptimePenalty(90);
const penalty80 = calculateUptimePenalty(80);
const penalty70 = calculateUptimePenalty(70);
const penalty50 = calculateUptimePenalty(50);
expect(penalty90).toBeGreaterThan(0);
expect(penalty80).toBeGreaterThan(penalty90);
expect(penalty70).toBeGreaterThan(penalty80);
expect(penalty50).toBeGreaterThan(penalty70);
});
it("should have approximately expected penalty values", () => {
// Based on the formula: ((95 - uptime) / 95 * 5)^2
expect(calculateUptimePenalty(90)).toBeCloseTo(0.069, 1);
expect(calculateUptimePenalty(80)).toBeCloseTo(0.62, 1);
expect(calculateUptimePenalty(70)).toBeCloseTo(1.73, 1);
});
});
describe("history tracking", () => {
it("should track success in history", () => {
reportKeySuccess("LLM_OPENAI_API_KEY", 0);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.history).toHaveLength(1);
expect(health?.history[0].success).toBe(true);
});
it("should track errors in history", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 500);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.history).toHaveLength(1);
expect(health?.history[0].success).toBe(false);
});
it("should track permanent blacklist errors in history", () => {
reportKeyError("LLM_OPENAI_API_KEY", 0, 401);
const health = getKeyHealth("LLM_OPENAI_API_KEY", 0);
expect(health?.history).toHaveLength(1);
expect(health?.history[0].success).toBe(false);
expect(health?.permanentlyBlacklisted).toBe(true);
});
});
});