Files
new-api-analytics/lib/queries.test.ts

47 lines
1.4 KiB
TypeScript

import { beforeEach, describe, expect, mock, test } from "bun:test";
const queryMock = mock(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,
},
]);
mock.module("./db", () => ({
query: queryMock,
}));
const { getTrends } = await import("./queries");
describe("getTrends", () => {
beforeEach(() => {
queryMock.mockClear();
});
test("adds optional detail filters to the trend query", async () => {
await getTrends("day", 101, 201, { username: "alice" });
expect(queryMock.mock.calls[0][0]).toContain("username = $3");
expect(queryMock.mock.calls[0][1]).toEqual([101, 201, "alice"]);
await getTrends("day", 102, 202, { model: "gpt-4.1" });
expect(queryMock.mock.calls[1][0]).toContain("= $3");
expect(queryMock.mock.calls[1][1]).toEqual([102, 202, "gpt-4.1"]);
await getTrends("day", 103, 203, { channelId: 7 });
expect(queryMock.mock.calls[2][0]).toContain("channel_id = $3");
expect(queryMock.mock.calls[2][1]).toEqual([103, 203, 7]);
});
test("keeps the hour in hourly trend buckets", async () => {
const trends = await getTrends("hour", 104, 204);
expect(queryMock.mock.calls[0][0]).toContain("date_trunc('hour'");
expect(trends[0].date).toBe("2026-04-01 13:00");
});
});