|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../../infra/session-cost-usage.js", async () => { |
| 4 | + const actual = await vi.importActual<typeof import("../../infra/session-cost-usage.js")>( |
| 5 | + "../../infra/session-cost-usage.js", |
| 6 | + ); |
| 7 | + return { |
| 8 | + ...actual, |
| 9 | + loadCostUsageSummary: vi.fn(async () => ({ |
| 10 | + updatedAt: Date.now(), |
| 11 | + startDate: "2026-02-01", |
| 12 | + endDate: "2026-02-02", |
| 13 | + daily: [], |
| 14 | + totals: { totalTokens: 1, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalCost: 0 }, |
| 15 | + })), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +import { loadCostUsageSummary } from "../../infra/session-cost-usage.js"; |
| 20 | +import { __test } from "./usage.js"; |
| 21 | + |
| 22 | +describe("gateway usage helpers", () => { |
| 23 | + beforeEach(() => { |
| 24 | + __test.costUsageCache.clear(); |
| 25 | + vi.useRealTimers(); |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("parseDateToMs accepts YYYY-MM-DD and rejects invalid input", () => { |
| 30 | + expect(__test.parseDateToMs("2026-02-05")).toBe(Date.UTC(2026, 1, 5)); |
| 31 | + expect(__test.parseDateToMs(" 2026-02-05 ")).toBe(Date.UTC(2026, 1, 5)); |
| 32 | + expect(__test.parseDateToMs("2026-2-5")).toBeUndefined(); |
| 33 | + expect(__test.parseDateToMs("nope")).toBeUndefined(); |
| 34 | + expect(__test.parseDateToMs(undefined)).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("parseDays coerces strings/numbers to integers", () => { |
| 38 | + expect(__test.parseDays(7.9)).toBe(7); |
| 39 | + expect(__test.parseDays("30")).toBe(30); |
| 40 | + expect(__test.parseDays("")).toBeUndefined(); |
| 41 | + expect(__test.parseDays("nope")).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("parseDateRange uses explicit start/end (inclusive end of day)", () => { |
| 45 | + const range = __test.parseDateRange({ startDate: "2026-02-01", endDate: "2026-02-02" }); |
| 46 | + expect(range.startMs).toBe(Date.UTC(2026, 1, 1)); |
| 47 | + expect(range.endMs).toBe(Date.UTC(2026, 1, 2) + 24 * 60 * 60 * 1000 - 1); |
| 48 | + }); |
| 49 | + |
| 50 | + it("parseDateRange clamps days to at least 1 and defaults to 30 days", () => { |
| 51 | + vi.useFakeTimers(); |
| 52 | + vi.setSystemTime(new Date("2026-02-05T12:34:56.000Z")); |
| 53 | + const oneDay = __test.parseDateRange({ days: 0 }); |
| 54 | + expect(oneDay.endMs).toBe(Date.UTC(2026, 1, 5) + 24 * 60 * 60 * 1000 - 1); |
| 55 | + expect(oneDay.startMs).toBe(Date.UTC(2026, 1, 5)); |
| 56 | + |
| 57 | + const def = __test.parseDateRange({}); |
| 58 | + expect(def.endMs).toBe(Date.UTC(2026, 1, 5) + 24 * 60 * 60 * 1000 - 1); |
| 59 | + expect(def.startMs).toBe(Date.UTC(2026, 1, 5) - 29 * 24 * 60 * 60 * 1000); |
| 60 | + }); |
| 61 | + |
| 62 | + it("loadCostUsageSummaryCached caches within TTL", async () => { |
| 63 | + vi.useFakeTimers(); |
| 64 | + vi.setSystemTime(new Date("2026-02-05T00:00:00.000Z")); |
| 65 | + |
| 66 | + const config = {} as unknown as ReturnType<import("../../config/config.js").loadConfig>; |
| 67 | + const a = await __test.loadCostUsageSummaryCached({ |
| 68 | + startMs: 1, |
| 69 | + endMs: 2, |
| 70 | + config, |
| 71 | + }); |
| 72 | + const b = await __test.loadCostUsageSummaryCached({ |
| 73 | + startMs: 1, |
| 74 | + endMs: 2, |
| 75 | + config, |
| 76 | + }); |
| 77 | + |
| 78 | + expect(a.totals.totalTokens).toBe(1); |
| 79 | + expect(b.totals.totalTokens).toBe(1); |
| 80 | + expect(vi.mocked(loadCostUsageSummary)).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | +}); |
0 commit comments