Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

159 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 absorbedMicros,
6 age,
7 answerDueAt,
8 givenLabel,
9 marginApart,
10 givenParts,
11 givenTotal,
12 goodwillLine,
13 goodwillNeedsReason,
14 goodwillWarning,
15 goodwillChoices,
16 moneyApart,
17 isFast,
18 marginOfMonth,
19 parseRequestStatus,
20 ratioLabel,
21 requestsHref,
22 revenueOf,
23 span,
24 waiting,
25} from "./pricing.ts";
26
27test("the request filter is open unless another status is named", () => {
28 assert.equal(parseRequestStatus(null), "open");
29 assert.equal(parseRequestStatus("declined"), "declined");
30 assert.equal(parseRequestStatus("all"), "all");
31 assert.equal(parseRequestStatus("closed"), "open");
32 assert.equal(requestsHref("open"), "/requests");
33 assert.equal(requestsHref("approved"), "/requests?status=approved");
34});
35
36test("an answer is due one business day after the request", () => {
37 // Tuesday → Wednesday, same time.
38 assert.equal(answerDueAt("2026-10-06T15:00:00Z").toISOString(), "2026-10-07T15:00:00.000Z");
39 // Friday → Monday, same time.
40 assert.equal(answerDueAt("2026-10-09T15:00:00Z").toISOString(), "2026-10-12T15:00:00.000Z");
41 // Saturday or Sunday → the end of Monday.
42 assert.equal(answerDueAt("2026-10-10T09:00:00Z").toISOString(), "2026-10-12T23:59:59.000Z");
43 assert.equal(answerDueAt("2026-10-11T23:00:00Z").toISOString(), "2026-10-12T23:59:59.000Z");
44});
45
46test("how long a request has waited, and whether it is overdue", () => {
47 const asked = "2026-10-06T15:00:00Z";
48 assert.deepEqual(
49 { label: waiting(asked, new Date("2026-10-06T18:30:00Z")).label, overdue: waiting(asked, new Date("2026-10-06T18:30:00Z")).overdue },
50 { label: "3 h", overdue: false },
51 );
52 const late = waiting(asked, new Date("2026-10-07T19:00:00Z"));
53 assert.equal(late.label, "1 d 4 h");
54 assert.equal(late.overdue, true);
55 assert.equal(waiting("nonsense").label, "—");
56 assert.equal(span(25 * 60_000), "25 min");
57 assert.equal(span(48 * 3_600_000), "2 d");
58});
59
60test("a workspace's age in its largest whole unit", () => {
61 const now = new Date("2026-10-05T00:00:00Z");
62 assert.equal(age("2026-10-04T12:00:00Z", now), "less than a day");
63 assert.equal(age("2026-09-23T00:00:00Z", now), "12 days");
64 assert.equal(age("2026-01-05T00:00:00Z", now), "9 months");
65 assert.equal(age("2023-10-05T00:00:00Z", now), "3 years");
66 assert.equal(age(null, now), null);
67});
68
69test("goodwill: g1t absorbs only what is past its margin", () => {
70 assert.equal(absorbedMicros(30_000_000, 8_000_000), 22_000_000);
71 assert.equal(absorbedMicros(5_000_000, 8_000_000), 0);
72 assert.equal(absorbedMicros(5_000_000, -1), 5_000_000);
73});
74
75test("goodwill needs a reason past the one-click credit, or a second time in 12 months", () => {
76 const credit = 40_000_000;
77 assert.equal(goodwillNeedsReason(null, credit, null), false);
78 assert.equal(goodwillNeedsReason(40_000_000, credit, null), false);
79 assert.equal(goodwillNeedsReason(20_000_000, credit, null), false);
80 assert.equal(goodwillNeedsReason(40_000_001, credit, null), true);
81 assert.equal(goodwillNeedsReason(null, credit, "2026-03-01T00:00:00Z"), true);
82 assert.deepEqual(goodwillWarning(100_000_000, { creditMicros: credit, marginMicros: 8_000_000 }, null), {
83 needsReason: true,
84 absorbedMicros: 92_000_000,
85 overCap: true,
86 });
87 assert.deepEqual(goodwillWarning(null, { creditMicros: credit, marginMicros: 8_000_000 }, null), {
88 needsReason: false,
89 absorbedMicros: 32_000_000,
90 overCap: false,
91 });
92 // Past the cap only when what g1t absorbs is more than it.
93 assert.equal(goodwillWarning(58_000_000, { creditMicros: credit, marginMicros: 8_000_000 }, null).overCap, false);
94 assert.equal(goodwillWarning(58_000_001, { creditMicros: credit, marginMicros: 8_000_000 }, null).overCap, true);
95 assert.equal(goodwillWarning(20_000_000, { creditMicros: credit, marginMicros: 8_000_000 }, null, 10_000_000).overCap, true);
96 assert.deepEqual(goodwillChoices({ overageMicros: 100_000_000, creditMicros: credit }), [
97 { label: "One-click credit", micros: null },
98 { label: "The whole overage", micros: 100_000_000 },
99 ]);
100 assert.deepEqual(goodwillChoices({ overageMicros: 10_000_000, creditMicros: 10_000_000 }), [{ label: "One-click credit", micros: null }]);
101 assert.equal(goodwillLine("2026-10-03"), "Credit from g1t: accidental usage on 2026-10-03");
102});
103
104test("velocity: five times the usual hour is fast", () => {
105 assert.equal(isFast(5), true);
106 assert.equal(isFast(4.99), false);
107 assert.equal(isFast(Number.NaN), false);
108 assert.equal(ratioLabel(7.24, 1_000_000), "7.2×");
109 assert.equal(ratioLabel(12.6, 1_000_000), "13×");
110 assert.equal(ratioLabel(0, 0), "new");
111});
112
113test("margin counts the plan's price and never what g1t gave", () => {
114 const month = { chargedMicros: 300_000_000, plansMicros: 200_000_000, costMicros: 250_000_000, givenMicros: 90_000_000 };
115 assert.equal(revenueOf(month), 500_000_000);
116 assert.deepEqual(marginOfMonth(month), { micros: 250_000_000, percent: 50 });
117 assert.deepEqual(marginOfMonth({ chargedMicros: 0, costMicros: 1_000_000 }), { micros: -1_000_000, percent: null });
118});
119
120test("given: labelled by source, totalled at price and at cost", () => {
121 assert.equal(givenLabel({ source: "oss_pool", label: "" }), "Open-source pool");
122 assert.equal(givenLabel({ source: "internal", label: "Internal (g1t's own)" }), "Internal (g1t's own)");
123 assert.equal(givenLabel({ source: "new_thing", label: "" }), "new_thing");
124 assert.deepEqual(
125 givenTotal([
126 { micros: 10_000_000, costMicros: 8_000_000 },
127 { micros: 5_000_000, costMicros: 4_000_000 },
128 ]),
129 { micros: 15_000_000, costMicros: 12_000_000 },
130 );
131 assert.deepEqual(givenParts({ trialMicros: 1_000_000, ossMicros: 0 }), [{ label: "Trial", micros: 1_000_000 }]);
132 assert.deepEqual(givenParts({}), []);
133});
134
135test("margin on what was sold leaves out the cost of what g1t gave", () => {
136 const month = { chargedMicros: 120_000_000, plansMicros: 200_000_000, costMicros: 160_000_000 };
137 assert.deepEqual(moneyApart(month, 60_000_000), {
138 revenueMicros: 320_000_000,
139 soldCostMicros: 100_000_000,
140 marginMicros: 220_000_000,
141 marginPercent: 69,
142 netMicros: 160_000_000,
143 });
144 // Everything given, nothing sold: no margin percent, and net is the loss.
145 assert.deepEqual(moneyApart({ chargedMicros: 0, costMicros: 5_000_000 }, 5_000_000), {
146 revenueMicros: 0,
147 soldCostMicros: 0,
148 marginMicros: 0,
149 marginPercent: null,
150 netMicros: -5_000_000,
151 });
152});
153
154test("the month by the Costs model: given is cost already, and the margin is on what was sold", () => {
155 const parts = marginApart({ usageMicros: 720_000, plansMicros: 0, costMicros: 1_300_000, givenMicros: 700_000, soldMarginMicros: 120_000, soldMarginPercent: 16.67, marginMicros: -580_000 });
156 assert.deepEqual(parts, { revenueMicros: 720_000, soldCostMicros: 600_000, marginMicros: 120_000, marginPercent: 17, netMicros: -580_000 });
157 // Nothing sold: no percent.
158 assert.equal(marginApart({ usageMicros: 0, plansMicros: 0, costMicros: 30_080_000, givenMicros: 30_080_000, soldMarginMicros: 0, soldMarginPercent: null, marginMicros: -30_080_000 }).marginPercent, null);
159});