Skip to content

Commit 0d36f2b

Browse files
fix(mobile): subscription widget no longer expires on a window that already reset (#22993)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 996b43f commit 0d36f2b

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎apps/mobile/src/widgets/subscriptionUsageSnapshot.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,65 @@ describe("subscription widget snapshots", () => {
135135
});
136136
});
137137

138+
describe("subscription widget expiry", () => {
139+
const maxAgeDeadline = now + 15 * 60_000;
140+
function single(resetsAt: string) {
141+
return buildSubscriptionUsageSnapshot(
142+
presentations([provider({ usageLimits: { checkedAt, windows: [{ ...window, resetsAt }] } })]),
143+
deepLink,
144+
).providers[0];
145+
}
146+
147+
it.each(["2026-09-05T11:50:00.000Z", checkedAt])(
148+
"keeps usage whose reset at %s already happened when observed",
149+
(resetsAt) => {
150+
expect(single(resetsAt)).toMatchObject({
151+
detail: "Subscription remaining",
152+
expiresAt: maxAgeDeadline,
153+
windows: [{ remaining: 60, reset: expect.stringMatching(/^Reset /) }],
154+
});
155+
},
156+
);
157+
158+
it.each([
159+
{ resetAccount: 1, expiresAt: maxAgeDeadline, label: /^Reset / },
160+
{ resetAccount: 0, expiresAt: now + 2 * 60_000, label: /^Next reset / },
161+
])(
162+
"compares a pooled reset with its own account's observation (account $resetAccount)",
163+
({ resetAccount, expiresAt, label }) => {
164+
// Account 0 was observed at 12:00, account 1 at 12:05; the pooled reset is at 12:02.
165+
const snapshot = buildSubscriptionUsageSnapshot(
166+
presentations(
167+
[checkedAt, "2026-09-05T12:05:00.000Z"].map((accountCheckedAt, index) =>
168+
provider({
169+
instanceId: ProviderInstanceId.make(`codex-${index}`),
170+
auth: { status: "authenticated", email: `account-${index}@example.com` },
171+
usageLimits: {
172+
checkedAt: accountCheckedAt,
173+
windows: [
174+
{
175+
...window,
176+
resetsAt:
177+
index === resetAccount
178+
? "2026-09-05T12:02:00.000Z"
179+
: "2026-09-05T13:00:00.000Z",
180+
},
181+
],
182+
},
183+
}),
184+
),
185+
),
186+
deepLink,
187+
);
188+
expect(snapshot.providers[0]).toMatchObject({
189+
detail: "2 accounts · pooled",
190+
expiresAt,
191+
windows: [{ remaining: 60, reset: expect.stringMatching(label) }],
192+
});
193+
},
194+
);
195+
});
196+
138197
describe("widget refresh probes", () => {
139198
it("does not overlap a slow probe even after the refresh interval", async () => {
140199
let finish!: () => void;

‎apps/mobile/src/widgets/subscriptionUsageSnapshot.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
collectLimitAccounts,
33
collectLimitPools,
44
type LimitAccount,
5+
type LimitPoolWindow,
56
type LimitPresentations,
67
} from "@t3tools/shared/usageLimits";
78

@@ -44,6 +45,10 @@ export function createWidgetRefresher<Id>(refresh: (id: Id) => Promise<unknown>)
4445
};
4546
}
4647

48+
// A reset at or before its account's observation already happened, so it cannot expire the snapshot.
49+
const isUpcoming = (reset: LimitPoolWindow["resets"][number]) =>
50+
reset.at > Date.parse(reset.member.account.limits.checkedAt);
51+
4752
function subscriptionUsageProps(
4853
accounts: readonly LimitAccount[],
4954
now: number,
@@ -75,7 +80,9 @@ function subscriptionUsageProps(
7580
const checkedAt = Math.min(...pool.accounts.map((a) => Date.parse(a.limits.checkedAt)));
7681
const expiresAt = Math.min(
7782
checkedAt + SNAPSHOT_MAX_AGE,
78-
...pool.windows.flatMap((window) => window.resets.map((reset) => reset.at)),
83+
...pool.windows.flatMap((window) =>
84+
window.resets.filter(isUpcoming).map((reset) => reset.at),
85+
),
7986
);
8087
const fresh = Number.isFinite(expiresAt) && expiresAt > now;
8188
const sortedWindows = [...pool.windows].sort(
@@ -106,7 +113,9 @@ function subscriptionUsageProps(
106113
label: window.label,
107114
remaining: Math.round(window.remainingPercent),
108115
reset: window.resets[0]
109-
? `Next reset ${new Date(window.resets[0].at).toLocaleString(undefined, {
116+
? `${isUpcoming(window.resets[0]) ? "Next reset" : "Reset"} ${new Date(
117+
window.resets[0].at,
118+
).toLocaleString(undefined, {
110119
month: "short",
111120
day: "numeric",
112121
hour: "numeric",

0 commit comments

Comments
 (0)