-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex-usage.test.ts
More file actions
93 lines (86 loc) · 3.44 KB
/
Copy pathcodex-usage.test.ts
File metadata and controls
93 lines (86 loc) · 3.44 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
import { test, expect, describe } from "bun:test";
import {
formatCodexUsage,
formatCodexUsageCompact,
type CodexUsage,
} from "../../src/auth/codex/usage.js";
describe("formatCodexUsage", () => {
test("renders both windows with percent and reset, no dollar figures", () => {
const usage: CodexUsage = {
planType: "team",
allowed: true,
limitReached: false,
primary: { usedPercent: 42, windowSeconds: 18000, resetAfterSeconds: 3600, resetAt: 0 },
secondary: { usedPercent: 74, windowSeconds: 604800, resetAfterSeconds: 266511, resetAt: 0 },
hasCredits: true,
};
const out = formatCodexUsage(usage);
expect(out).toContain("Codex (team) — active");
expect(out).toContain("5-hour: 42% used · resets in 1h 0m");
expect(out).toContain("weekly: 74% used · resets in 3d 2h");
expect(out).not.toContain("$");
});
test("surfaces the blocked reason when the limit is reached", () => {
const usage: CodexUsage = {
planType: "team",
allowed: false,
limitReached: true,
primary: { usedPercent: 100, windowSeconds: 18000, resetAfterSeconds: 562, resetAt: 0 },
hasCredits: false,
reachedType: "workspace_member_credits_depleted",
};
const out = formatCodexUsage(usage);
expect(out).toContain("limit reached");
expect(out).toContain("5-hour: 100% used · resets in 9m");
expect(out).toContain("blocked: workspace member credits depleted");
});
test("omits windows that are absent", () => {
const usage: CodexUsage = { planType: "pro", allowed: true, limitReached: false, hasCredits: true };
expect(formatCodexUsage(usage)).toBe("Codex (pro) — active");
});
});
describe("formatCodexUsageCompact", () => {
test("renders a one-line status-bar form", () => {
const usage: CodexUsage = {
planType: "team",
allowed: true,
limitReached: false,
primary: { usedPercent: 42, windowSeconds: 18000, resetAfterSeconds: 0, resetAt: 0 },
secondary: { usedPercent: 74, windowSeconds: 604800, resetAfterSeconds: 0, resetAt: 0 },
hasCredits: true,
};
expect(formatCodexUsageCompact(usage)).toBe("Codex 5h 42% · wk 74%");
});
test("derives the window label from the actual duration", () => {
const usage: CodexUsage = {
planType: "team",
allowed: true,
limitReached: false,
primary: { usedPercent: 10, windowSeconds: 10800, resetAfterSeconds: 0, resetAt: 0 },
secondary: { usedPercent: 5, windowSeconds: 86400, resetAfterSeconds: 0, resetAt: 0 },
hasCredits: true,
};
expect(formatCodexUsageCompact(usage)).toBe("Codex 3h 10% · 1d 5%");
});
test("falls back to default labels when the window duration is missing", () => {
const usage: CodexUsage = {
planType: "team",
allowed: true,
limitReached: false,
primary: { usedPercent: 10, windowSeconds: 0, resetAfterSeconds: 0, resetAt: 0 },
secondary: { usedPercent: 5, windowSeconds: 0, resetAfterSeconds: 0, resetAt: 0 },
hasCredits: true,
};
expect(formatCodexUsageCompact(usage)).toBe("Codex 5h 10% · wk 5%");
});
test("marks the limit-reached state", () => {
const usage: CodexUsage = {
planType: "team",
allowed: false,
limitReached: true,
primary: { usedPercent: 100, windowSeconds: 18000, resetAfterSeconds: 0, resetAt: 0 },
hasCredits: false,
};
expect(formatCodexUsageCompact(usage)).toBe("Codex 5h 100% · (limit reached)");
});
});