53 lines
2.0 KiB
TypeScript
53 lines
2.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { fetchStats, parseStats, type UsageStats } from "../src/lib/stats";
|
|
|
|
test("parseStats extracts totals and gen p95", () => {
|
|
const stats = parseStats({
|
|
total_requests: 1985,
|
|
total_input_tokens: 312327705,
|
|
total_output_tokens: 932710,
|
|
total_cache_tokens: 308144640,
|
|
gen_histogram: { p50: 341, p95: 378.86, p99: 392 },
|
|
});
|
|
assert.deepEqual(stats, { totalRequests: 1985, totalInputTokens: 312327705, totalOutputTokens: 932710, genP95: 378.86 });
|
|
});
|
|
|
|
test("parseStats defaults missing fields and rejects malformed input", () => {
|
|
assert.deepEqual(parseStats({}), { totalRequests: 0, totalInputTokens: 0, totalOutputTokens: 0, genP95: 0 });
|
|
assert.equal(parseStats({ total_requests: "nope" })!.totalRequests, 0);
|
|
assert.equal(parseStats(null), null);
|
|
assert.equal(parseStats("x"), null);
|
|
});
|
|
|
|
test("fetchStats calls /api/metrics/stats with a model query", async () => {
|
|
const calls: string[] = [];
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async (url: RequestInfo | URL) => {
|
|
calls.push(String(url));
|
|
return { ok: true, json: async () => ({ total_requests: 7 }) } as unknown as Response;
|
|
}) as typeof fetch;
|
|
try {
|
|
await fetchStats({ baseUrl: "http://x" }, "DeepSeek-V4-Flash-0731");
|
|
assert.equal(calls[0], "http://x/api/metrics/stats?model=DeepSeek-V4-Flash-0731");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
test("fetchStats omits the model query for 'all' and returns null on HTTP error", async () => {
|
|
const calls: string[] = [];
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async (url: RequestInfo | URL) => {
|
|
calls.push(String(url));
|
|
return { ok: false, status: 500 } as unknown as Response;
|
|
}) as typeof fetch;
|
|
try {
|
|
const stats = await fetchStats({ baseUrl: "http://x" }, "all");
|
|
assert.equal(stats, null);
|
|
assert.equal(calls[0], "http://x/api/metrics/stats");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|