g1t/services/deployments/src/metering.test.ts

46 lines2,311 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.

status.g1t.sh with incident management, invites that land you in the workspace, settings as pages, usage without quotas1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { amount, monthCost } from "./metering.ts";
5
6/** Cloudflare's prices, at cost: $0.30 per million requests, $0.02 per million CPU ms, $0.10 a custom domain a month. */
7const COSTS = { millionRequests: 300_000, millionCpuMs: 20_000, domainMonth: 100_000 };
8
9test("every request and CPU millisecond is metered from the first, with nothing included", () => {
10 // What used to be inside a 1 million request, 3 million CPU ms allowance now costs what it costs.
11 const month = monthCost({ requests: 1_000_000, cpu_ms: 3_000_000, peak_domains: 0 }, COSTS);
12 assert.equal(month.traffic.micros, 300_000 + 60_000);
13 assert.equal(month.domains.micros, 0);
14 assert.equal(month.micros, 360_000);
15 assert.equal(month.traffic.detail, "1 million requests and 3 million CPU ms");
16 // A single request is a fraction of a millionth, rounded up.
17 assert.equal(monthCost({ requests: 1, cpu_ms: 0, peak_domains: 0 }, COSTS).micros, 1);
18});
19
20test("a busy workspace on the plan is never capped, only charged", () => {
21 // 500 million requests in a month: $150 to g1t, no count stops it.
22 const month = monthCost({ requests: 500_000_000, cpu_ms: 2_000_000_000, peak_domains: 40 }, COSTS);
23 assert.equal(month.traffic.micros, 150_000_000 + 40_000_000);
24 assert.equal(month.domains.micros, 4_000_000);
25 assert.equal(month.description, "500,000,000 requests, 2,000,000,000 CPU ms, 40 custom domains");
26});
27
28test("custom domains are charged from the first, by the month", () => {
29 const month = monthCost({ requests: 0, cpu_ms: 0, peak_domains: 1 }, COSTS);
30 assert.equal(month.domains.micros, 100_000);
31 assert.equal(month.domains.detail, "1 custom domain");
32 assert.equal(month.traffic.detail, null);
33 assert.equal(monthCost({ requests: 0, cpu_ms: 0, peak_domains: 3 }, COSTS).domains.detail, "3 custom domains");
34});
35
36test("apps are not metered: a month with only apps up and no traffic costs nothing", () => {
37 const month = monthCost({ requests: 0, cpu_ms: 0, peak_domains: null }, COSTS);
38 assert.equal(month.micros, 0);
39 assert.equal(month.description, "");
40});
41
42test("amounts read plainly", () => {
43 assert.equal(amount(840_000), "840,000");
44 assert.equal(amount(1_240_000), "1.2 million");
45 assert.equal(amount(0), "0");
46});