Skip to content
89 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { AgentSession, PriceBook } from "@g1t/contracts";
5
6import { daySeries, markupLabel, percentLabel, pricingOf, readPeriod, readScope, receiptOf, spanFor, tokenCount } from "./spend.ts";
7
8const OCT_8 = new Date("2026-10-08T15:00:00Z");
9
10test("a period's days match the agents service's, both ends included", () => {
11 assert.deepEqual(spanFor("month", OCT_8), { from: "2026-10-01", until: "2026-10-08" });
12 assert.deepEqual(spanFor("last_month", OCT_8), { from: "2026-09-01", until: "2026-09-30" });
13 assert.deepEqual(spanFor("last_month", new Date("2026-01-15T00:00:00Z")), { from: "2025-12-01", until: "2025-12-31" });
14 assert.deepEqual(spanFor("7d", OCT_8), { from: "2026-10-02", until: "2026-10-08" });
15 assert.deepEqual(spanFor("30d", OCT_8), { from: "2026-09-09", until: "2026-10-08" });
16});
17
18test("only someone who may see the workspace's spend gets it; anything else is their own", () => {
19 assert.equal(readScope(null, true), "workspace");
20 assert.equal(readScope("me", true), "me");
21 assert.equal(readScope("workspace", false), "me");
22 assert.equal(readPeriod("7d"), "7d");
23 assert.equal(readPeriod("year"), "month");
24});
25
26test("a day series has every day, products added together, zeros where nothing was spent", () => {
27 const days = daySeries("2026-10-01", "2026-10-03", [
28 { day: "2026-10-01", micros: 5 },
29 { day: "2026-10-01", micros: 7 },
30 { day: "2026-10-03", micros: 1 },
31 ]);
32 assert.deepEqual(days, [
33 { day: "2026-10-01", micros: 12 },
34 { day: "2026-10-02", micros: 0 },
35 { day: "2026-10-03", micros: 1 },
36 ]);
37});
38
39test("pricing comes from the price book: the model markup, the run markup, the agent rate", () => {
40 const book = {
41 modelMarginPercent: 0,
42 changes: [],
43 prices: [
44 { meter: "sandbox_second", costMicros: 10, markupPercent: 20, priceMicros: 12 },
45 { meter: "build_second", costMicros: 10, markupPercent: 20, priceMicros: 12 },
46 { meter: "agent_tokens", costMicros: 0, markupPercent: 0, priceMicros: 250_000 },
47 { meter: "agent_tokens_own", costMicros: 0, markupPercent: 0, priceMicros: 250_000 },
48 ],
49 } as unknown as PriceBook;
50 const pricing = pricingOf(book);
51 assert.equal(pricing.modelMarkupPercent, 0);
52 assert.deepEqual(pricing.markup, { min: 20, max: 20 });
53 assert.equal(markupLabel(pricing.markup!), "20%");
54 assert.equal(markupLabel({ min: 15, max: 20 }), "15–20%");
55 assert.equal(pricing.agentRateMicros, 250_000);
56});
57
58const session = (over: Partial<AgentSession>): AgentSession =>
59 ({ id: "s", parent_id: null, root_id: "r", charged_micros: 0, cost_micros: 0, input_tokens: 0, output_tokens: 0, steps: 0, tool_calls: 0, created_at: "2026-10-08T00:00:00Z", ...over }) as AgentSession;
60
61test("a receipt gives the root its own share, children under their parents, and the tree's totals", () => {
62 const tree = [
63 session({ id: "r", charged_micros: 1_840_000, cost_micros: 1_000_000, input_tokens: 400_000, output_tokens: 30_000, steps: 6, tool_calls: 20 }),
64 session({ id: "c1", parent_id: "r", charged_micros: 300_000, cost_micros: 200_000, input_tokens: 10_000, steps: 2, created_at: "2026-10-08T00:01:00Z" }),
65 session({ id: "g1", parent_id: "c1", charged_micros: 40_000, cost_micros: 30_000, steps: 1, created_at: "2026-10-08T00:02:00Z" }),
66 ];
67 const receipt = receiptOf(tree, "r")!;
68 assert.deepEqual(
69 receipt.lines.map((l) => [l.session.id, l.depth, l.ownMicros]),
70 [
71 ["r", 0, 1_500_000],
72 ["c1", 1, 300_000],
73 ["g1", 2, 40_000],
74 ],
75 );
76 assert.equal(receipt.chargedMicros, 1_840_000);
77 assert.equal(receipt.providerMicros, 1_230_000);
78 assert.equal(receipt.steps, 9);
79 assert.equal(receiptOf([], "r"), null);
80});
81
82test("token counts and percents read short", () => {
83 assert.equal(tokenCount(830), "830");
84 assert.equal(tokenCount(1_200), "1.2K");
85 assert.equal(tokenCount(412_000), "412K");
86 assert.equal(tokenCount(1_250_000), "1.3M");
87 assert.equal(percentLabel(62, 100), "62%");
88 assert.equal(percentLabel(62, null), "");
89});