Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageLimits.test.ts

124 lines4.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { applyUsageLimitsUpdate, resolveUsageLimitsAfterProbe } from "./usageLimits.ts";
4​
5const checkedAt = "2026-09-03T12:00:00.000Z";
6const session = {
7 id: "five_hour",
8 kind: "session",
9 label: "Session",
10 usedPercent: 40,
11 windowDurationMins: 300,
12 resetsAt: "2026-09-03T14:00:00.000Z",
13} as const;
14const weekly = {
15 id: "seven_day",
16 kind: "weekly",
17 label: "Weekly",
18 usedPercent: 20,
19 windowDurationMins: 10_080,
20} as const;
21const published = { checkedAt, windows: [session, weekly] };
22​
23describe("applyUsageLimitsUpdate", () => {
24 it("returns the published object itself when no window moved", () => {
25 // Codex repeats the same numbers beside every token-usage tick; the
26 // ingestion path relies on identity to skip the publish.
27 const next = applyUsageLimitsUpdate({
28 previous: published,
29 checkedAt: "2026-09-03T12:00:05.000Z",
30 update: {
31 windows: [
32 { ...weekly },
33 { id: "five_hour", kind: "session", label: "Session", usedPercent: 40 },
34 ],
35 },
36 });
37 expect(next).toBe(published);
38 });
39​
40 it("upserts by id and keeps the reset a percent-only update omits", () => {
41 const next = applyUsageLimitsUpdate({
42 previous: published,
43 checkedAt: "2026-09-03T12:00:05.000Z",
44 update: {
45 windows: [{ id: "five_hour", kind: "session", label: "Session", usedPercent: 55 }],
46 },
47 });
48 expect(next).not.toBe(published);
49 expect(next).toEqual({
50 checkedAt: "2026-09-03T12:00:05.000Z",
51 windows: [{ ...session, usedPercent: 55 }, weekly],
52 });
53 });
54​
55 it("leaves an unsupported account and an empty update alone", () => {
56 const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
57 expect(
58 applyUsageLimitsUpdate({ previous: unsupported, checkedAt, update: { windows: [session] } }),
59 ).toBe(unsupported);
60 expect(
61 applyUsageLimitsUpdate({ previous: published, checkedAt, update: { windows: [] } }),
62 ).toBe(published);
63 });
64​
65 it("marks a first sparse update as unavailable", () => {
66 expect(
67 applyUsageLimitsUpdate({ previous: undefined, checkedAt, update: { windows: [weekly] } }),
68 ).toEqual({ checkedAt, windows: [weekly], unavailable: { reason: "probeFailed" } });
69 });
70​
71 it("preserves reset credits when a streamed window update changes usage", () => {
72 const resetCredits = { availableCount: 2, nextExpiresAt: "2026-10-01T00:00:00.000Z" };
73 const next = applyUsageLimitsUpdate({
74 previous: { ...published, resetCredits },
75 checkedAt: "2026-09-03T12:00:05.000Z",
76 update: { windows: [{ ...session, usedPercent: 55 }] },
77 });
78​
79 expect(next).toEqual({
80 checkedAt: "2026-09-03T12:00:05.000Z",
81 windows: [{ ...session, usedPercent: 55 }, weekly],
82 resetCredits,
83 });
84 });
85});
86​
87describe("resolveUsageLimitsAfterProbe", () => {
88 it("uses read order for windows while taking metadata from the successful probe", () => {
89 const live = {
90 checkedAt: "2026-09-03T12:00:05.000Z",
91 windows: [
92 { ...session, usedPercent: 95 },
93 { ...weekly, usedPercent: 70 },
94 ],
95 resetCredits: { availableCount: 2 },
96 externalUsage: { label: "Old dashboard", url: "https://example.com/old" },
97 };
98 const probed = {
99 ...published,
100 windows: [{ ...session, usedPercent: 0 }],
101 resetCredits: { availableCount: 1 },
102 externalUsage: { label: "Usage dashboard", url: "https://example.com/usage" },
103 };
104​
105 expect(resolveUsageLimitsAfterProbe({ published: live, probed })).toEqual({
106 ...probed,
107 checkedAt: live.checkedAt,
108 windows: live.windows,
109 });
110​
111 const sameTime = { ...probed, checkedAt: live.checkedAt };
112 const newer = { ...probed, checkedAt: "2026-09-03T12:00:06.000Z" };
113 expect(resolveUsageLimitsAfterProbe({ published: live, probed: sameTime })).toEqual(sameTime);
114 expect(resolveUsageLimitsAfterProbe({ published: live, probed: newer })).toEqual(newer);
115 });
116​
117 it("keeps the last good windows through a failed probe but not an unsupported one", () => {
118 const failed = { checkedAt, windows: [], unavailable: { reason: "probeFailed" as const } };
119 const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
120 expect(resolveUsageLimitsAfterProbe({ published, probed: failed })).toBe(published);
121 expect(resolveUsageLimitsAfterProbe({ published, probed: unsupported })).toBe(unsupported);
122 expect(resolveUsageLimitsAfterProbe({ published: undefined, probed: failed })).toBe(failed);
123 });
124});