-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathusageDisplay.test.ts
More file actions
196 lines (181 loc) · 5.42 KB
/
Copy pathusageDisplay.test.ts
File metadata and controls
196 lines (181 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { describe, expect, it } from "vitest";
import type { UsageOutput } from "../usage/schemas";
import {
codeUsageMeter,
formatResetTime,
formatUsdAmount,
isCodeUsageFreeTier,
isUsageExceeded,
} from "./usageDisplay";
function makeUsage(
overrides: Partial<{
sustained: boolean;
burst: boolean;
isRateLimited: boolean;
}> = {},
): UsageOutput {
return {
product: "posthog_code",
user_id: 1,
sustained: {
used_percent: 50,
reset_at: "2026-05-01T13:00:00.000Z",
exceeded: overrides.sustained ?? false,
},
burst: {
used_percent: 30,
reset_at: "2026-05-01T12:10:00.000Z",
exceeded: overrides.burst ?? false,
},
is_rate_limited: overrides.isRateLimited ?? false,
is_pro: false,
};
}
describe("isUsageExceeded", () => {
it("returns false when nothing is exceeded", () => {
expect(isUsageExceeded(makeUsage())).toBe(false);
});
it("returns true when sustained is exceeded", () => {
expect(isUsageExceeded(makeUsage({ sustained: true }))).toBe(true);
});
it("returns true when burst is exceeded", () => {
expect(isUsageExceeded(makeUsage({ burst: true }))).toBe(true);
});
it("returns true when rate limited", () => {
expect(isUsageExceeded(makeUsage({ isRateLimited: true }))).toBe(true);
});
it("returns true when all flags are set", () => {
expect(
isUsageExceeded(
makeUsage({ sustained: true, burst: true, isRateLimited: true }),
),
).toBe(true);
});
});
describe("isCodeUsageFreeTier", () => {
it.each([
[false, true],
[true, false],
// Absent means unknown, never free.
[undefined, false],
] as const)("code_usage_subscribed=%s -> %s", (subscribed, expected) => {
expect(isCodeUsageFreeTier({ code_usage_subscribed: subscribed })).toBe(
expected,
);
});
it("treats missing usage as not confirmed free", () => {
expect(isCodeUsageFreeTier(null)).toBe(false);
expect(isCodeUsageFreeTier(undefined)).toBe(false);
});
});
describe("codeUsageMeter", () => {
it("prefers billing's org dollars when both numbers are present", () => {
const meter = codeUsageMeter({
...makeUsage(),
code_usage_subscribed: true,
ai_credits: { exhausted: false, used_usd: 12.4, limit_usd: 50 },
billing_period_end: "2026-06-01T00:00:00.000Z",
});
expect(meter).toEqual({
kind: "dollars",
usedUsd: 12.4,
limitUsd: 50,
percent: 25,
exceeded: false,
resetAt: "2026-06-01T00:00:00.000Z",
});
});
it("marks the dollars meter exceeded from the org bucket and falls back to the sustained reset", () => {
const meter = codeUsageMeter({
...makeUsage(),
code_usage_subscribed: false,
ai_credits: { exhausted: true, used_usd: 20, limit_usd: 20 },
});
expect(meter).toMatchObject({
kind: "dollars",
percent: 100,
exceeded: true,
resetAt: "2026-05-01T13:00:00.000Z",
});
});
it.each([
["missing numbers", { exhausted: false }],
["null numbers", { exhausted: false, used_usd: null, limit_usd: null }],
["a zero limit", { exhausted: false, used_usd: 0, limit_usd: 0 }],
])("falls back to the free-tier valve bucket with %s", (_name, aiCredits) => {
const usage: UsageOutput = {
...makeUsage(),
code_usage_subscribed: false,
ai_credits: aiCredits,
};
expect(codeUsageMeter(usage)).toEqual({
kind: "bucket",
bucket: usage.sustained,
});
});
it("hides the meter for a subscribed or unknown org without dollars", () => {
expect(
codeUsageMeter({ ...makeUsage(), code_usage_subscribed: true }),
).toEqual({ kind: "hidden" });
expect(codeUsageMeter(makeUsage())).toEqual({ kind: "hidden" });
expect(codeUsageMeter(null)).toEqual({ kind: "hidden" });
});
});
describe("formatUsdAmount", () => {
it.each([
[50, "$50"],
[12.4, "$12.40"],
[0.5, "$0.50"],
[0, "$0"],
])("formats %s as %s", (amount, expected) => {
expect(formatUsdAmount(amount)).toBe(expected);
});
});
describe("formatResetTime", () => {
const NOW = Date.parse("2026-05-01T12:00:00.000Z");
const isoAt = (msFromNow: number) => new Date(NOW + msFromNow).toISOString();
it.each([
{
name: "returns minutes-only under 1h",
resetAt: isoAt(30 * 60 * 1000),
expected: "Resets in 30m" as string | RegExp,
},
{
name: "returns hours + minutes under 24h",
resetAt: isoAt((4 * 3600 + 30 * 60) * 1000),
expected: "Resets in 4h 30m",
},
{
name: "returns hours only when minutes round to 0",
resetAt: isoAt(4 * 3600 * 1000),
expected: "Resets in 4h",
},
{
name: "rolls the hour instead of showing 60 minutes",
resetAt: isoAt((23 * 3600 + 59 * 60 + 40) * 1000),
expected: "Resets in 24h",
},
{
name: "returns localized date when over 24h away",
resetAt: isoAt(30 * 86400 * 1000),
expected: /^Resets [A-Za-z]+ \d+ at /,
},
{
name: "treats an already-past reset_at as shortly",
resetAt: isoAt(-60_000),
expected: "Resets shortly",
},
{
name: "treats an unparseable reset_at as shortly",
resetAt: "not-a-date",
expected: "Resets shortly",
},
])("$name", ({ resetAt, expected }) => {
const result = formatResetTime(resetAt, NOW);
if (expected instanceof RegExp) {
expect(result).toMatch(expected);
} else {
expect(result).toBe(expected);
}
});
});