flagon-io/g1t

public

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

g1t/apps/sudo/app/lib/pricing.test.ts

151 lines6,512 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

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