-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathadmin-parsing.test.js
More file actions
381 lines (332 loc) · 12.3 KB
/
admin-parsing.test.js
File metadata and controls
381 lines (332 loc) · 12.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Unit tests for admin.js parsing and data extraction functions.
*/
import { describe, test, expect } from "vitest";
import { parseUriTemplate } from "../../mcpgateway/admin_ui/utils.js";
import { parseThinkTags } from "../../mcpgateway/admin_ui/llmChat.js";
import { orderCertificateChain, parseCertificateInfo } from "../../mcpgateway/admin_ui/caCertificate.js";
import { extractTeamId } from "../../mcpgateway/admin_ui/teams.js";
import { extractApiError } from "../../mcpgateway/admin_ui/security.js";
import { extractKPIData } from "../../mcpgateway/admin_ui/metrics.js";
// ---------------------------------------------------------------------------
// parseUriTemplate
// ---------------------------------------------------------------------------
describe("parseUriTemplate", () => {
test("extracts single template variable", () => {
expect(parseUriTemplate("/users/{id}")).toEqual(["id"]);
});
test("extracts multiple template variables", () => {
expect(parseUriTemplate("/users/{userId}/posts/{postId}")).toEqual(["userId", "postId"]);
});
test("returns empty array when no variables", () => {
expect(parseUriTemplate("/users/list")).toEqual([]);
});
test("returns empty array for empty string", () => {
expect(parseUriTemplate("")).toEqual([]);
});
test("handles adjacent variables", () => {
expect(parseUriTemplate("{a}{b}")).toEqual(["a", "b"]);
});
test("handles variable with underscores", () => {
expect(parseUriTemplate("/api/{user_name}")).toEqual(["user_name"]);
});
});
// ---------------------------------------------------------------------------
// parseThinkTags
// ---------------------------------------------------------------------------
describe("parseThinkTags", () => {
test("parses single think block", () => {
const result = parseThinkTags("<think>reasoning here</think>The answer");
expect(result.thinkingSteps).toHaveLength(1);
expect(result.thinkingSteps[0].content).toBe("reasoning here");
expect(result.finalAnswer).toBe("The answer");
});
test("parses multiple think blocks", () => {
const input =
"<think>step 1</think>middle<think>step 2</think>final answer";
const result = parseThinkTags(input);
expect(result.thinkingSteps).toHaveLength(2);
expect(result.thinkingSteps[0].content).toBe("step 1");
expect(result.thinkingSteps[1].content).toBe("step 2");
expect(result.finalAnswer).toBe("middlefinal answer");
});
test("returns empty thinkingSteps when no think tags", () => {
const result = parseThinkTags("just a normal response");
expect(result.thinkingSteps).toHaveLength(0);
expect(result.finalAnswer).toBe("just a normal response");
});
test("preserves rawContent", () => {
const input = "<think>thought</think>answer";
const result = parseThinkTags(input);
expect(result.rawContent).toBe(input);
});
test("skips empty think blocks", () => {
const result = parseThinkTags("<think></think>answer");
expect(result.thinkingSteps).toHaveLength(0);
expect(result.finalAnswer).toBe("answer");
});
test("skips whitespace-only think blocks", () => {
const result = parseThinkTags("<think> \n </think>answer");
expect(result.thinkingSteps).toHaveLength(0);
});
test("handles multiline think content", () => {
const input = "<think>line1\nline2\nline3</think>answer";
const result = parseThinkTags(input);
expect(result.thinkingSteps[0].content).toContain("line1");
expect(result.thinkingSteps[0].content).toContain("line3");
});
test("trims final answer whitespace", () => {
const result = parseThinkTags("<think>thought</think> answer ");
expect(result.finalAnswer).toBe("answer");
});
});
// ---------------------------------------------------------------------------
// parseCertificateInfo
// ---------------------------------------------------------------------------
describe("parseCertificateInfo", () => {
test("identifies root cert (subject === issuer)", () => {
const content = "Subject: CN=Root CA\nIssuer: CN=Root CA\n";
const result = parseCertificateInfo(content);
expect(result.isRoot).toBe(true);
expect(result.subject).toBe("CN=Root CA");
expect(result.issuer).toBe("CN=Root CA");
});
test("identifies non-root cert (subject !== issuer)", () => {
const content = "Subject: CN=Server\nIssuer: CN=Root CA\n";
const result = parseCertificateInfo(content);
expect(result.isRoot).toBe(false);
expect(result.subject).toBe("CN=Server");
expect(result.issuer).toBe("CN=Root CA");
});
test("returns isRoot=false when Subject is missing", () => {
const content = "Issuer: CN=Root CA\n";
expect(parseCertificateInfo(content).isRoot).toBe(false);
});
test("returns isRoot=false when Issuer is missing", () => {
const content = "Subject: CN=Root CA\n";
expect(parseCertificateInfo(content).isRoot).toBe(false);
});
test("returns isRoot=false for empty string", () => {
expect(parseCertificateInfo("").isRoot).toBe(false);
});
});
// ---------------------------------------------------------------------------
// orderCertificateChain
// ---------------------------------------------------------------------------
describe("orderCertificateChain", () => {
test("puts root certs first", () => {
const certs = [
{ certInfo: { isRoot: false }, name: "leaf" },
{ certInfo: { isRoot: true }, name: "root" },
{ certInfo: { isRoot: false }, name: "intermediate" },
];
const ordered = orderCertificateChain(certs);
expect(ordered[0].name).toBe("root");
expect(ordered).toHaveLength(3);
});
test("handles all roots", () => {
const certs = [
{ certInfo: { isRoot: true }, name: "root1" },
{ certInfo: { isRoot: true }, name: "root2" },
];
const ordered = orderCertificateChain(certs);
expect(ordered).toHaveLength(2);
});
test("handles no roots", () => {
const certs = [
{ certInfo: { isRoot: false }, name: "leaf1" },
{ certInfo: { isRoot: false }, name: "leaf2" },
];
const ordered = orderCertificateChain(certs);
expect(ordered).toHaveLength(2);
});
test("handles empty array", () => {
expect(orderCertificateChain([])).toEqual([]);
});
test("filters out entries without certInfo", () => {
const certs = [
{ certInfo: { isRoot: true }, name: "root" },
{ name: "no-cert-info" },
];
const ordered = orderCertificateChain(certs);
expect(ordered).toHaveLength(1);
});
});
// ---------------------------------------------------------------------------
// extractTeamId
// ---------------------------------------------------------------------------
describe("extractTeamId", () => {
test("extracts team ID from element ID", () => {
expect(extractTeamId("team-row-", "team-row-abc123")).toBe("abc123");
});
test("returns null when elementId is null", () => {
expect(extractTeamId("prefix-", null)).toBeNull();
});
test("returns null when elementId does not start with prefix", () => {
expect(extractTeamId("team-row-", "other-abc123")).toBeNull();
});
test("returns empty string when ID equals prefix", () => {
expect(extractTeamId("prefix-", "prefix-")).toBe("");
});
test("handles empty prefix", () => {
expect(extractTeamId("", "anything")).toBe("anything");
});
});
// ---------------------------------------------------------------------------
// extractApiError
// ---------------------------------------------------------------------------
describe("extractApiError", () => {
test("returns fallback for null error", () => {
expect(extractApiError(null)).toBe("An error occurred");
});
test("returns fallback for undefined error", () => {
expect(extractApiError(undefined)).toBe("An error occurred");
});
test("returns fallback for error without detail or message", () => {
expect(extractApiError({})).toBe("An error occurred");
});
test("uses custom fallback", () => {
expect(extractApiError(null, "Custom error")).toBe("Custom error");
});
test("extracts message property", () => {
expect(extractApiError({ message: "Something failed" })).toBe("Something failed");
});
test("extracts string detail", () => {
expect(extractApiError({ detail: "Not found" })).toBe("Not found");
});
test("prefers message over detail", () => {
expect(extractApiError({ message: "msg", detail: "det" })).toBe("msg");
});
test("formats Pydantic validation errors (array detail)", () => {
const error = {
detail: [{ msg: "field required" }, { msg: "invalid value" }],
};
const result = extractApiError(error);
expect(result).toContain("field required");
expect(result).toContain("invalid value");
expect(result).toContain("; ");
});
test("JSON-stringifies array detail items without msg", () => {
const error = {
detail: [{ loc: ["body", "name"], type: "missing" }],
};
const result = extractApiError(error);
expect(result).toContain("loc");
});
});
// ---------------------------------------------------------------------------
// extractKPIData
// ---------------------------------------------------------------------------
describe("extractKPIData", () => {
test("extracts KPIs from standard format", () => {
const data = {
tools: {
"Total Executions": 100,
"Successful Executions": 90,
"Failed Executions": 10,
"Average Response Time": 1.5,
},
};
const result = extractKPIData(data);
expect(result.totalExecutions).toBe(100);
expect(result.successRate).toBe(90);
expect(result.errorRate).toBe(10);
expect(result.avgResponseTime).toBeCloseTo(1500, 1);
});
test("aggregates across multiple categories", () => {
const data = {
tools: {
"Total Executions": 50,
"Successful Executions": 45,
"Failed Executions": 5,
},
resources: {
"Total Executions": 50,
"Successful Executions": 40,
"Failed Executions": 10,
},
};
const result = extractKPIData(data);
expect(result.totalExecutions).toBe(100);
expect(result.successRate).toBe(85);
expect(result.errorRate).toBe(15);
});
test("returns zeros for empty data", () => {
const result = extractKPIData({});
expect(result.totalExecutions).toBe(0);
expect(result.successRate).toBe(0);
expect(result.errorRate).toBe(0);
expect(result.avgResponseTime).toBeNull();
});
test("returns zeros for null data", () => {
const result = extractKPIData(null);
expect(result.totalExecutions).toBe(0);
});
test("handles camelCase key aliases", () => {
const data = {
tools_metrics: {
totalexecutions: 200,
successfulexecutions: 180,
failedexecutions: 20,
avgresponsetime: 2.0,
},
};
const result = extractKPIData(data);
expect(result.totalExecutions).toBe(200);
expect(result.successRate).toBe(90);
});
test("handles snake_case key aliases", () => {
const data = {
tools: {
total_executions: 100,
successful_executions: 80,
failed_executions: 20,
},
};
const result = extractKPIData(data);
expect(result.totalExecutions).toBe(100);
});
test("returns null avgResponseTime when no executions", () => {
const data = {
tools: {
"Total Executions": 0,
"Successful Executions": 0,
"Failed Executions": 0,
"Average Response Time": 5.0,
},
};
const result = extractKPIData(data);
expect(result.avgResponseTime).toBeNull();
});
test("computes weighted average response time across categories", () => {
const data = {
tools: {
"Total Executions": 100,
"Successful Executions": 100,
"Failed Executions": 0,
"Average Response Time": 2.0,
},
resources: {
"Total Executions": 100,
"Successful Executions": 100,
"Failed Executions": 0,
"Average Response Time": 4.0,
},
};
const result = extractKPIData(data);
// Weighted avg = (100*2.0 + 100*4.0) / 200 = 3.0s = 3000ms
expect(result.avgResponseTime).toBeCloseTo(3000, 1);
});
test("ignores N/A response time values", () => {
const data = {
tools: {
"Total Executions": 100,
"Successful Executions": 100,
"Failed Executions": 0,
"Average Response Time": "N/A",
},
};
const result = extractKPIData(data);
expect(result.avgResponseTime).toBeNull();
});
});