usage.test.ts109 lines · main
1import { describe, expect, test } from 'bun:test';
2
3import { TIERS } from './tiers.js';
4import { currentMonthBounds } from './usage.js';
5
6describe('currentMonthBounds', () => {
7 test('first day of January gives Jan 1 → Feb 1 in UTC', () => {
8 const now = new Date('2026-01-01T05:30:00.000Z');
9 const { periodStart, periodEnd } = currentMonthBounds(now);
10 expect(periodStart.toISOString()).toBe('2026-01-01T00:00:00.000Z');
11 expect(periodEnd.toISOString()).toBe('2026-02-01T00:00:00.000Z');
12 });
13
14 test('mid-month date gives the same bounds as start-of-month', () => {
15 const now = new Date('2026-05-09T18:42:11.000Z');
16 const { periodStart, periodEnd } = currentMonthBounds(now);
17 expect(periodStart.toISOString()).toBe('2026-05-01T00:00:00.000Z');
18 expect(periodEnd.toISOString()).toBe('2026-06-01T00:00:00.000Z');
19 });
20
21 test('last second of December rolls into January next year', () => {
22 const now = new Date('2026-12-31T23:59:59.999Z');
23 const { periodStart, periodEnd } = currentMonthBounds(now);
24 expect(periodStart.toISOString()).toBe('2026-12-01T00:00:00.000Z');
25 expect(periodEnd.toISOString()).toBe('2027-01-01T00:00:00.000Z');
26 });
27
28 test('first millisecond of a month is in that month, not the prior one', () => {
29 const now = new Date('2026-07-01T00:00:00.000Z');
30 const { periodStart, periodEnd } = currentMonthBounds(now);
31 expect(periodStart.toISOString()).toBe('2026-07-01T00:00:00.000Z');
32 expect(periodEnd.toISOString()).toBe('2026-08-01T00:00:00.000Z');
33 });
34
35 test('handles February in non-leap year (28 days)', () => {
36 const now = new Date('2026-02-15T12:00:00.000Z');
37 const { periodStart, periodEnd } = currentMonthBounds(now);
38 expect(periodStart.toISOString()).toBe('2026-02-01T00:00:00.000Z');
39 expect(periodEnd.toISOString()).toBe('2026-03-01T00:00:00.000Z');
40 });
41
42 test('handles February in leap year (29 days, end is still Mar 1)', () => {
43 const now = new Date('2028-02-29T12:00:00.000Z');
44 const { periodStart, periodEnd } = currentMonthBounds(now);
45 expect(periodStart.toISOString()).toBe('2028-02-01T00:00:00.000Z');
46 expect(periodEnd.toISOString()).toBe('2028-03-01T00:00:00.000Z');
47 });
48
49 test('non-UTC inputs still produce UTC bounds', () => {
50 // 2026-05-31T23:00:00 in a +02:00 zone is 2026-05-31T21:00:00 UTC,
51 // so the month is still May.
52 const now = new Date('2026-05-31T23:00:00.000+02:00');
53 const { periodStart } = currentMonthBounds(now);
54 expect(periodStart.toISOString()).toBe('2026-05-01T00:00:00.000Z');
55 });
56});
57
58describe('TIERS.storageBytes', () => {
59 // The storage caps are surfaced as both a hard ceiling for future
60 // deploy-time enforcement and a soft cap rendered on the dashboard
61 // usage widget. The ordering invariant matters — a paid tier must
62 // never offer less storage than a free one.
63 test('storage caps are strictly increasing free < pro < team', () => {
64 expect(TIERS.free.storageBytes).toBeLessThan(TIERS.pro.storageBytes);
65 expect(TIERS.pro.storageBytes).toBeLessThan(TIERS.team.storageBytes);
66 });
67
68 test('free tier ships at exactly 1 GiB', () => {
69 expect(TIERS.free.storageBytes).toBe(1024 * 1024 * 1024);
70 });
71
72 test('pro tier ships at exactly 10 GiB', () => {
73 expect(TIERS.pro.storageBytes).toBe(10 * 1024 * 1024 * 1024);
74 });
75
76 test('team tier ships at exactly 100 GiB', () => {
77 expect(TIERS.team.storageBytes).toBe(100 * 1024 * 1024 * 1024);
78 });
79});
80
81describe('TIERS.connectionSecondsPerMonth', () => {
82 test('caps are strictly increasing free < pro < team', () => {
83 expect(TIERS.free.connectionSecondsPerMonth).toBeLessThan(
84 TIERS.pro.connectionSecondsPerMonth,
85 );
86 expect(TIERS.pro.connectionSecondsPerMonth).toBeLessThan(
87 TIERS.team.connectionSecondsPerMonth,
88 );
89 });
90
91 test('pro is 10x free, team is 100x free (matches the other scaling pattern)', () => {
92 expect(TIERS.pro.connectionSecondsPerMonth).toBe(TIERS.free.connectionSecondsPerMonth * 10);
93 expect(TIERS.team.connectionSecondsPerMonth).toBe(TIERS.free.connectionSecondsPerMonth * 100);
94 });
95});
96
97describe('TIERS.concurrentSubscriptions', () => {
98 // Sized so the year-one 10k-concurrent target is reached at the team
99 // tier's hard cap, not by stacking many smaller projects. A bad
100 // free-tier project can't take down the platform.
101 test('caps are strictly increasing free < pro < team', () => {
102 expect(TIERS.free.concurrentSubscriptions).toBeLessThan(TIERS.pro.concurrentSubscriptions);
103 expect(TIERS.pro.concurrentSubscriptions).toBeLessThan(TIERS.team.concurrentSubscriptions);
104 });
105
106 test('free tier hard-caps at 100 (single bad project cant exhaust the platform)', () => {
107 expect(TIERS.free.concurrentSubscriptions).toBe(100);
108 });
109});