11// @vitest -environment jsdom
22import { act , renderHook } from "@testing-library/react" ;
33import { beforeEach , describe , expect , it , vi } from "vitest" ;
4- import type { TurnPlan } from "@/types" ;
4+ import type { RateLimitSnapshot , TurnPlan } from "@/types" ;
55import { interruptTurn } from "@services/tauri" ;
66import {
77 normalizePlanUpdate ,
@@ -26,6 +26,7 @@ type SetupOverrides = {
2626 pendingInterrupts ?: string [ ] ;
2727 planByThread ?: Record < string , TurnPlan | null > ;
2828 activeTurnByThread ?: Record < string , string | null > ;
29+ rateLimitsByWorkspace ?: Record < string , RateLimitSnapshot | null > ;
2930} ;
3031
3132const makeOptions = ( overrides : SetupOverrides = { } ) => {
@@ -38,6 +39,9 @@ const makeOptions = (overrides: SetupOverrides = {}) => {
3839 const getActiveTurnId = vi . fn (
3940 ( threadId : string ) => overrides . activeTurnByThread ?. [ threadId ] ?? null ,
4041 ) ;
42+ const getCurrentRateLimits = vi . fn (
43+ ( workspaceId : string ) => overrides . rateLimitsByWorkspace ?. [ workspaceId ] ?? null ,
44+ ) ;
4145 const pushThreadErrorMessage = vi . fn ( ) ;
4246 const safeMessageActivity = vi . fn ( ) ;
4347 const recordThreadActivity = vi . fn ( ) ;
@@ -52,6 +56,7 @@ const makeOptions = (overrides: SetupOverrides = {}) => {
5256 useThreadTurnEvents ( {
5357 dispatch,
5458 planByThreadRef,
59+ getCurrentRateLimits,
5560 getCustomName,
5661 isThreadHidden,
5762 markProcessing,
@@ -74,6 +79,7 @@ const makeOptions = (overrides: SetupOverrides = {}) => {
7479 markReviewing,
7580 setActiveTurnId,
7681 getActiveTurnId,
82+ getCurrentRateLimits,
7783 pushThreadErrorMessage,
7884 safeMessageActivity,
7985 recordThreadActivity,
@@ -557,7 +563,20 @@ describe("useThreadTurnEvents", () => {
557563 } ) ;
558564
559565 it ( "dispatches normalized rate limits updates" , ( ) => {
560- const { result, dispatch } = makeOptions ( ) ;
566+ const previousRateLimits = {
567+ primary : {
568+ usedPercent : 35 ,
569+ windowDurationMins : 60 ,
570+ resetsAt : 1_700_000_000 ,
571+ } ,
572+ secondary : null ,
573+ credits : null ,
574+ planType : null ,
575+ } satisfies RateLimitSnapshot ;
576+
577+ const { result, dispatch, getCurrentRateLimits } = makeOptions ( {
578+ rateLimitsByWorkspace : { "ws-1" : previousRateLimits } ,
579+ } ) ;
561580 const normalized = { primary : { usedPercent : 10 } } ;
562581
563582 vi . mocked ( normalizeRateLimits ) . mockReturnValue ( normalized as never ) ;
@@ -566,6 +585,11 @@ describe("useThreadTurnEvents", () => {
566585 result . current . onAccountRateLimitsUpdated ( "ws-1" , { primary : { } } ) ;
567586 } ) ;
568587
588+ expect ( getCurrentRateLimits ) . toHaveBeenCalledWith ( "ws-1" ) ;
589+ expect ( normalizeRateLimits ) . toHaveBeenCalledWith (
590+ { primary : { } } ,
591+ previousRateLimits ,
592+ ) ;
569593 expect ( dispatch ) . toHaveBeenCalledWith ( {
570594 type : "setRateLimits" ,
571595 workspaceId : "ws-1" ,
0 commit comments