Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageExport.test.ts

92 lines2.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { mergeUsage } from "@t3tools/shared/usageMerge";
4import { UsageDay } from "@t3tools/contracts";
5import { usageToCsv, usageToJson } from "./usageExport";
6​
7const input = {
8 sinceDay: UsageDay.make("2026-08-10"),
9 untilDay: UsageDay.make("2026-08-11"),
10 timeZone: "UTC",
11 resolution: "day" as const,
12};
13​
14describe("usage exports", () => {
15 it("exports model rows as escaped CSV", () => {
16 const usage = mergeUsage(
17 [
18 {
19 environmentId: "env-a" as never,
20 label: "Local",
21 summary: {
22 contractVersion: 6,
23 readAt: "2026-08-11T00:00:00.000Z",
24 timeZone: "UTC",
25 sinceDay: input.sinceDay,
26 untilDay: input.untilDay,
27 buckets: [
28 {
29 day: input.sinceDay,
30 provider: "devin",
31 model: "gpt,5.6",
32 totals: {
33 uncachedInputTokens: 10,
34 cachedInputTokens: 2,
35 cacheCreationTokens: 0,
36 outputTokens: 5,
37 reasoningTokens: 1,
38 },
39 costUsd: 0.25,
40 cacheSavingsUsd: 0,
41 costSource: "providerReported",
42 records: 1,
43 unpricedRecords: 0,
44 sessions: 1,
45 },
46 ],
47 sources: [
48 {
49 fingerprint: {
50 hostId: "host",
51 provider: "devin",
52 resolvedHomePath: "/logs",
53 volumeId: "1:2",
54 },
55 status: "ok",
56 scannedFiles: 1,
57 skippedFiles: 0,
58 malformedRecords: 0,
59 distinctSessions: 1,
60 message: null,
61 },
62 ],
63 pricing: {
64 status: "unavailable",
65 source: "test",
66 fetchedAt: null,
67 knownModels: 0,
68 },
69 scanDurationMs: 1,
70 },
71 },
72 ],
73 6,
74 );
75 const csv = usageToCsv(usage, input);
76​
77 expect(csv).toContain("Provider,Model,Tokens,Cost (USD),Records,Cost share");
78 expect(csv).toContain('devin,"gpt,5.6"');
79 expect(csv).toContain("Total tokens,17");
80 });
81​
82 it("serializes provider maps in JSON instead of dropping them", () => {
83 const usage = mergeUsage([], 6);
84 const parsed = JSON.parse(usageToJson(usage, input)) as {
85 readonly daily: readonly unknown[];
86 readonly window: { readonly timeZone: string };
87 };
88​
89 expect(parsed.window.timeZone).toBe("UTC");
90 expect(parsed.daily).toEqual([]);
91 });
92});