Skip to content
100 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { AgentSession, PriceBook } from "@g1t/contracts";
5
6import { agentRateLabel, 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: models at the provider's price, what g1t runs at its markup", () => {
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_models", costMicros: 1_000_000, markupPercent: 0, priceMicros: 1_000_000 },
47 { meter: "gateway_models", costMicros: 1_000_000, markupPercent: 0, priceMicros: 1_000_000 },
48 { meter: "card_fee_percent", costMicros: 29_000, markupPercent: 0, priceMicros: 29_000 },
49 { meter: "agent_token_weight_input", costMicros: 1_000_000, markupPercent: 0, priceMicros: 1_000_000 },
50 { meter: "agent_tokens", costMicros: 0, markupPercent: 0, priceMicros: 0 },
51 { meter: "agent_tokens_own", costMicros: 0, markupPercent: 0, priceMicros: 0 },
52 ],
53 } as unknown as PriceBook;
54 (book as { changes: unknown[] }).changes = [{ meter: "agent_tokens", newCostMicros: 250_000, effectiveAt: "2026-10-22T00:00:00Z" }];
55 const pricing = pricingOf(book);
56 assert.equal(pricing.modelMarkupPercent, 0);
57 // Models, the gateway, card fees and weights are not things g1t runs: the markup is one figure.
58 assert.deepEqual(pricing.markup, { min: 20, max: 20 });
59 assert.equal(markupLabel(pricing.markup!), "20%");
60 assert.equal(markupLabel({ min: 15, max: 20 }), "15–20%");
61 // The agent rate, $0 until its dated version starts.
62 assert.equal(pricing.agentRateMicros, 0);
63 assert.deepEqual(pricing.agentRateComing, { micros: 250_000, from: "2026-10-22" });
64 const dollars = (m: number) => `$${(m / 1_000_000).toFixed(2)}`;
65 assert.equal(agentRateLabel(pricing, dollars), "$0.25 per million tokens from 2026-10-22");
66 assert.equal(agentRateLabel({ agentRateMicros: 250_000, agentRateComing: null }, dollars), "$0.25 per million tokens");
67});
68
69const session = (over: Partial<AgentSession>): AgentSession =>
70 ({ 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;
71
72test("a receipt gives the root its own share, children under their parents, and the tree's totals", () => {
73 const tree = [
74 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 }),
75 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" }),
76 session({ id: "g1", parent_id: "c1", charged_micros: 40_000, cost_micros: 30_000, steps: 1, created_at: "2026-10-08T00:02:00Z" }),
77 ];
78 const receipt = receiptOf(tree, "r")!;
79 assert.deepEqual(
80 receipt.lines.map((l) => [l.session.id, l.depth, l.ownMicros]),
81 [
82 ["r", 0, 1_500_000],
83 ["c1", 1, 300_000],
84 ["g1", 2, 40_000],
85 ],
86 );
87 assert.equal(receipt.chargedMicros, 1_840_000);
88 assert.equal(receipt.providerMicros, 1_230_000);
89 assert.equal(receipt.steps, 9);
90 assert.equal(receiptOf([], "r"), null);
91});
92
93test("token counts and percents read short", () => {
94 assert.equal(tokenCount(830), "830");
95 assert.equal(tokenCount(1_200), "1.2K");
96 assert.equal(tokenCount(412_000), "412K");
97 assert.equal(tokenCount(1_250_000), "1.3M");
98 assert.equal(percentLabel(62, 100), "62%");
99 assert.equal(percentLabel(62, null), "");
100});