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

usageLimits.test.ts

122 lines4.4 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 const next = applyUsageLimitsUpdate({
26 previous: published,
27 checkedAt: "2026-09-03T12:00:05.000Z",
28 update: {
29 windows: [
30 { ...weekly },
31 { id: "five_hour", kind: "session", label: "Session", usedPercent: 40 },
32 ],
33 },
34 });
35 expect(next).toBe(published);
36 });
37​
38 it("upserts by id and keeps the reset a percent-only update omits", () => {
39 const next = applyUsageLimitsUpdate({
40 previous: published,
41 checkedAt: "2026-09-03T12:00:05.000Z",
42 update: {
43 windows: [{ id: "five_hour", kind: "session", label: "Session", usedPercent: 55 }],
44 },
45 });
46 expect(next).not.toBe(published);
47 expect(next).toEqual({
48 checkedAt: "2026-09-03T12:00:05.000Z",
49 windows: [{ ...session, usedPercent: 55 }, weekly],
50 });
51 });
52​
53 it("leaves an unsupported account and an empty update alone", () => {
54 const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
55 expect(
56 applyUsageLimitsUpdate({ previous: unsupported, checkedAt, update: { windows: [session] } }),
57 ).toBe(unsupported);
58 expect(
59 applyUsageLimitsUpdate({ previous: published, checkedAt, update: { windows: [] } }),
60 ).toBe(published);
61 });
62​
63 it("marks a first sparse update as unavailable", () => {
64 expect(
65 applyUsageLimitsUpdate({ previous: undefined, checkedAt, update: { windows: [weekly] } }),
66 ).toEqual({ checkedAt, windows: [weekly], unavailable: { reason: "probeFailed" } });
67 });
68​
69 it("preserves reset credits when a streamed window update changes usage", () => {
70 const resetCredits = { availableCount: 2, nextExpiresAt: "2026-10-01T00:00:00.000Z" };
71 const next = applyUsageLimitsUpdate({
72 previous: { ...published, resetCredits },
73 checkedAt: "2026-09-03T12:00:05.000Z",
74 update: { windows: [{ ...session, usedPercent: 55 }] },
75 });
76​
77 expect(next).toEqual({
78 checkedAt: "2026-09-03T12:00:05.000Z",
79 windows: [{ ...session, usedPercent: 55 }, weekly],
80 resetCredits,
81 });
82 });
83});
84​
85describe("resolveUsageLimitsAfterProbe", () => {
86 it("uses read order for windows while taking metadata from the successful probe", () => {
87 const live = {
88 checkedAt: "2026-09-03T12:00:05.000Z",
89 windows: [
90 { ...session, usedPercent: 95 },
91 { ...weekly, usedPercent: 70 },
92 ],
93 resetCredits: { availableCount: 2 },
94 externalUsage: { label: "Old dashboard", url: "https://example.com/old" },
95 };
96 const probed = {
97 ...published,
98 windows: [{ ...session, usedPercent: 0 }],
99 resetCredits: { availableCount: 1 },
100 externalUsage: { label: "Usage dashboard", url: "https://example.com/usage" },
101 };
102​
103 expect(resolveUsageLimitsAfterProbe({ published: live, probed })).toEqual({
104 ...probed,
105 checkedAt: live.checkedAt,
106 windows: live.windows,
107 });
108​
109 const sameTime = { ...probed, checkedAt: live.checkedAt };
110 const newer = { ...probed, checkedAt: "2026-09-03T12:00:06.000Z" };
111 expect(resolveUsageLimitsAfterProbe({ published: live, probed: sameTime })).toEqual(sameTime);
112 expect(resolveUsageLimitsAfterProbe({ published: live, probed: newer })).toEqual(newer);
113 });
114​
115 it("keeps the last good windows through a failed probe but not an unsupported one", () => {
116 const failed = { checkedAt, windows: [], unavailable: { reason: "probeFailed" as const } };
117 const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
118 expect(resolveUsageLimitsAfterProbe({ published, probed: failed })).toBe(published);
119 expect(resolveUsageLimitsAfterProbe({ published, probed: unsupported })).toBe(unsupported);
120 expect(resolveUsageLimitsAfterProbe({ published: undefined, probed: failed })).toBe(failed);
121 });
122});