feat: add user token breakdown data
This commit is contained in:
@@ -16,11 +16,22 @@ mock.module("./db", () => ({
|
||||
query: queryMock,
|
||||
}));
|
||||
|
||||
const { getTrends } = await import("./queries");
|
||||
const { getTrends, getUserDetail } = await import("./queries");
|
||||
|
||||
describe("getTrends", () => {
|
||||
beforeEach(() => {
|
||||
queryMock.mockClear();
|
||||
queryMock.mockImplementation(async () => [
|
||||
{
|
||||
date: "2026-04-01 13:00:00",
|
||||
calls: 1,
|
||||
prompt_tokens: 10,
|
||||
completion_tokens: 20,
|
||||
cache_creation_tokens: 3,
|
||||
cache_read_tokens: 4,
|
||||
quota: 100,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("adds optional detail filters to the trend query", async () => {
|
||||
@@ -44,3 +55,71 @@ describe("getTrends", () => {
|
||||
expect(trends[0].date).toBe("2026-04-01 13:00");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserDetail", () => {
|
||||
beforeEach(() => {
|
||||
queryMock.mockClear();
|
||||
});
|
||||
|
||||
test("returns token breakdown with nested model rows for user details", async () => {
|
||||
queryMock.mockImplementation(async (sql: string) => {
|
||||
if (sql.includes("token_models AS")) {
|
||||
return [
|
||||
{ token_name: "prod-key", model: "claude-sonnet-4", calls: 3, tokens: 100, cache_creation: 5, cache_read: 7, quota: 50 },
|
||||
{ token_name: "", model: "gpt-4o", calls: 1, tokens: 20, cache_creation: 0, cache_read: 0, quota: 10 },
|
||||
];
|
||||
}
|
||||
|
||||
if (sql.includes("GROUP BY token_name")) {
|
||||
return [
|
||||
{ token_name: "prod-key", calls: 3, tokens: 100, cache_creation: 5, cache_read: 7, quota: 50 },
|
||||
{ token_name: "", calls: 1, tokens: 20, cache_creation: 0, cache_read: 0, quota: 10 },
|
||||
];
|
||||
}
|
||||
|
||||
if (sql.includes("GROUP BY model")) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (sql.includes("SELECT id, display_name FROM users")) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (sql.includes("SELECT id FROM users WHERE username")) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (sql.includes("SELECT COUNT(*)::int as calls")) {
|
||||
return [
|
||||
{ calls: 4, prompt: 120, completion: 0, cache_creation: 5, cache_read: 7, quota: 60 },
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
});
|
||||
|
||||
const detail = await getUserDetail("token-user", 301, 401);
|
||||
|
||||
expect(detail.tokens).toEqual([
|
||||
{
|
||||
name: "prod-key",
|
||||
calls: 3,
|
||||
total_tokens: 112,
|
||||
quota: 50,
|
||||
models: [
|
||||
{ name: "claude-sonnet-4", calls: 3, total_tokens: 112, quota: 50 },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "",
|
||||
calls: 1,
|
||||
total_tokens: 20,
|
||||
quota: 10,
|
||||
models: [
|
||||
{ name: "gpt-4o", calls: 1, total_tokens: 20, quota: 10 },
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(queryMock.mock.calls.some(([sql]) => String(sql).includes("token_name"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user