@@ -9,7 +9,6 @@ import { describe, expect, it, vi } from "vite-plus/test";
99import {
1010 buildSubscriptionUsageSnapshot ,
1111 createWidgetRefresher ,
12- subscriptionUsageTimeline ,
1312 WIDGET_REFRESH_INTERVAL ,
1413} from "./subscriptionUsageSnapshot" ;
1514
@@ -137,140 +136,35 @@ describe("subscription widget snapshots", () => {
137136} ) ;
138137
139138describe ( "subscription widget expiry" , ( ) => {
140- const observedAt = "2026-10-21T16:45:00.000Z" ;
141- const observedNow = Date . parse ( observedAt ) ;
142- const maxAgeDeadline = Date . parse ( "2026-10-21T17:00:00.000Z" ) ;
143-
144- function snapshot (
145- sessionReset = "2026-10-21T16:30:00.000Z" ,
146- weeklyReset = "2026-10-28T16:45:00.000Z" ,
147- maxWindowsPerProvider = 6 ,
148- ) {
139+ const maxAgeDeadline = now + 15 * 60_000 ;
140+ function single ( resetsAt : string ) {
149141 return buildSubscriptionUsageSnapshot (
150- presentations ( [
151- provider ( {
152- usageLimits : {
153- checkedAt : observedAt ,
154- windows : [
155- {
156- id : "session" ,
157- kind : "session" ,
158- label : "5 hours" ,
159- usedPercent : 0 ,
160- resetsAt : sessionReset ,
161- } ,
162- {
163- id : "weekly" ,
164- kind : "weekly" ,
165- label : "Weekly" ,
166- usedPercent : 30 ,
167- resetsAt : weeklyReset ,
168- } ,
169- ] ,
170- } ,
171- } ) ,
172- ] ) ,
142+ presentations ( [ provider ( { usageLimits : { checkedAt, windows : [ { ...window , resetsAt } ] } } ) ] ) ,
173143 deepLink ,
174- maxWindowsPerProvider ,
175- ) ;
144+ ) . providers [ 0 ] ;
176145 }
177146
178- it . each ( [
179- { platform : "iOS" , maxWindowsPerProvider : 6 } ,
180- { platform : "Android" , maxWindowsPerProvider : Infinity } ,
181- ] ) (
182- "$platform keeps both freshly observed rows when only the session reset is in the past" ,
183- ( { maxWindowsPerProvider } ) => {
184- const pastReset = snapshot ( undefined , undefined , maxWindowsPerProvider ) ;
185- const futureReset = snapshot ( "2026-10-21T17:30:00.000Z" , undefined , maxWindowsPerProvider ) ;
186- expect ( futureReset . providers [ 0 ] ) . toMatchObject ( {
187- detail : "Subscription remaining" ,
188- expiresAt : maxAgeDeadline ,
189- totalWindows : 2 ,
190- windows : [
191- { kind : "weekly" , remaining : 70 } ,
192- { kind : "session" , remaining : 100 } ,
193- ] ,
194- } ) ;
195- expect ( pastReset . checkedAt ) . toBe ( observedNow ) ;
196- expect ( pastReset . providers [ 0 ] ) . toMatchObject ( {
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 ( {
197151 detail : "Subscription remaining" ,
198152 expiresAt : maxAgeDeadline ,
199- totalWindows : 2 ,
200- windows : [
201- { kind : "weekly" , remaining : 70 } ,
202- { kind : "session" , remaining : 100 } ,
203- ] ,
153+ windows : [ { remaining : 60 , reset : expect . stringMatching ( / ^ R e s e t / ) } ] ,
204154 } ) ;
205- expect ( pastReset . providers [ 0 ] ?. windows [ 1 ] ?. reset ) . toMatch ( / ^ R e s e t / ) ;
206- expect ( futureReset . providers [ 0 ] ?. windows [ 1 ] ?. reset ) . toMatch ( / ^ N e x t r e s e t / ) ;
207155 } ,
208156 ) ;
209157
210- it ( "ignores resets at observation time and preserves reported past-reset usage" , ( ) => {
211- const stored = buildSubscriptionUsageSnapshot (
212- presentations ( [
213- provider ( {
214- usageLimits : {
215- checkedAt : observedAt ,
216- windows : [ { ...window , usedPercent : 40 , resetsAt : observedAt } ] ,
217- } ,
218- } ) ,
219- ] ) ,
220- deepLink ,
221- ) ;
222- expect ( stored . providers [ 0 ] ) . toMatchObject ( {
223- expiresAt : maxAgeDeadline ,
224- windows : [ { remaining : 60 , reset : expect . stringMatching ( / ^ R e s e t / ) } ] ,
225- } ) ;
226- } ) ;
227-
228- it ( "compares resets against each provider's own observation time" , ( ) => {
229- const resetAt = "2026-10-21T16:50:00.000Z" ;
230- const stored = buildSubscriptionUsageSnapshot (
231- presentations ( [
232- provider ( {
233- usageLimits : {
234- checkedAt : observedAt ,
235- windows : [ { ...window , resetsAt : resetAt } ] ,
236- } ,
237- } ) ,
238- provider ( {
239- instanceId : ProviderInstanceId . make ( "claude" ) ,
240- driver : ProviderDriverKind . make ( "claudeAgent" ) ,
241- usageLimits : {
242- checkedAt : "2026-10-21T16:55:00.000Z" ,
243- windows : [ { ...window , resetsAt : resetAt } ] ,
244- } ,
245- } ) ,
246- ] ) ,
247- deepLink ,
248- ) ;
249- expect ( stored . providers ) . toMatchObject ( [
250- { name : "Codex" , expiresAt : Date . parse ( resetAt ) } ,
251- { name : "Claude" , expiresAt : Date . parse ( "2026-10-21T17:10:00.000Z" ) } ,
252- ] ) ;
253- } ) ;
254-
255158 it . each ( [
256- {
257- observation : "newer" ,
258- resetAccount : 1 ,
259- expiresAt : "2026-10-21T16:55:00.000Z" ,
260- resetLabel : / ^ R e s e t / ,
261- } ,
262- {
263- observation : "older" ,
264- resetAccount : 0 ,
265- expiresAt : "2026-10-21T16:42:00.000Z" ,
266- resetLabel : / ^ N e x t r e s e t / ,
267- } ,
159+ { resetAccount : 1 , expiresAt : maxAgeDeadline , label : / ^ R e s e t / } ,
160+ { resetAccount : 0 , expiresAt : now + 2 * 60_000 , label : / ^ N e x t r e s e t / } ,
268161 ] ) (
269- "compares a pooled reset with its $observation account observation" ,
270- ( { resetAccount, expiresAt, resetLabel } ) => {
271- const stored = buildSubscriptionUsageSnapshot (
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 (
272166 presentations (
273- [ "2026-10-21T16:40 :00.000Z" , observedAt ] . map ( ( accountCheckedAt , index ) =>
167+ [ checkedAt , "2026-09-05T12:05 :00.000Z" ] . map ( ( accountCheckedAt , index ) =>
274168 provider ( {
275169 instanceId : ProviderInstanceId . make ( `codex-${ index } ` ) ,
276170 auth : { status : "authenticated" , email : `account-${ index } @example.com` } ,
@@ -281,8 +175,8 @@ describe("subscription widget expiry", () => {
281175 ...window ,
282176 resetsAt :
283177 index === resetAccount
284- ? "2026-10-21T16:42 :00.000Z"
285- : "2026-10-21T17:30 :00.000Z" ,
178+ ? "2026-09-05T12:02 :00.000Z"
179+ : "2026-09-05T13:00 :00.000Z" ,
286180 } ,
287181 ] ,
288182 } ,
@@ -291,67 +185,13 @@ describe("subscription widget expiry", () => {
291185 ) ,
292186 deepLink ,
293187 ) ;
294- expect ( stored . checkedAt ) . toBe ( Date . parse ( "2026-10-21T16:40:00.000Z" ) ) ;
295- expect ( stored . providers [ 0 ] ) . toMatchObject ( {
188+ expect ( snapshot . providers [ 0 ] ) . toMatchObject ( {
296189 detail : "2 accounts · pooled" ,
297- expiresAt : Date . parse ( expiresAt ) ,
298- totalWindows : 1 ,
299- windows : [ { remaining : 60 , reset : expect . stringMatching ( resetLabel ) } ] ,
190+ expiresAt,
191+ windows : [ { remaining : 60 , reset : expect . stringMatching ( label ) } ] ,
300192 } ) ;
301- expect (
302- subscriptionUsageTimeline ( stored , observedNow ) [ 0 ] ?. props . providers [ 0 ] ?. windows ,
303- ) . toHaveLength ( resetAccount === 1 ? 1 : 0 ) ;
304193 } ,
305194 ) ;
306-
307- it ( "keeps both rows in the current timeline entry despite the past session reset" , ( ) => {
308- const timeline = subscriptionUsageTimeline ( snapshot ( ) , observedNow ) ;
309- expect ( timeline . map ( ( entry ) => entry . date . toISOString ( ) ) ) . toEqual ( [
310- observedAt ,
311- "2026-10-21T17:00:00.000Z" ,
312- ] ) ;
313- expect ( timeline [ 0 ] ?. props . providers [ 0 ] ) . toMatchObject ( {
314- detail : "Subscription remaining" ,
315- totalWindows : 2 ,
316- windows : [
317- { kind : "weekly" , remaining : 70 } ,
318- { kind : "session" , remaining : 100 } ,
319- ] ,
320- } ) ;
321- expect ( timeline [ 1 ] ?. props . providers [ 0 ] ) . toMatchObject ( {
322- detail : "Open T3 to refresh" ,
323- totalWindows : 0 ,
324- windows : [ ] ,
325- } ) ;
326- } ) ;
327-
328- it ( "expires a snapshot older than 15 minutes even with future reset times" , ( ) => {
329- const stored = snapshot ( "2026-10-21T17:30:00.000Z" ) ;
330- expect ( stored . providers [ 0 ] ?. expiresAt ) . toBe ( maxAgeDeadline ) ;
331- expect ( subscriptionUsageTimeline ( stored , Date . parse ( "2026-10-21T17:00:00.001Z" ) ) ) . toMatchObject (
332- [
333- {
334- props : {
335- providers : [ { detail : "Open T3 to refresh" , totalWindows : 0 , windows : [ ] } ] ,
336- } ,
337- } ,
338- ] ,
339- ) ;
340- } ) ;
341-
342- it ( "expires at a future reset sooner than the 15 minute age limit" , ( ) => {
343- const resetAt = "2026-10-21T16:50:00.000Z" ;
344- const stored = snapshot ( resetAt ) ;
345- expect ( stored . providers [ 0 ] ?. expiresAt ) . toBe ( Date . parse ( resetAt ) ) ;
346- const timeline = subscriptionUsageTimeline ( stored , observedNow ) ;
347- expect ( timeline . map ( ( entry ) => entry . date . toISOString ( ) ) ) . toEqual ( [ observedAt , resetAt ] ) ;
348- expect ( timeline [ 0 ] ?. props . providers [ 0 ] ?. windows ) . toHaveLength ( 2 ) ;
349- expect ( timeline [ 1 ] ?. props . providers [ 0 ] ) . toMatchObject ( {
350- detail : "Open T3 to refresh" ,
351- totalWindows : 0 ,
352- windows : [ ] ,
353- } ) ;
354- } ) ;
355195} ) ;
356196
357197describe ( "widget refresh probes" , ( ) => {
0 commit comments