11import { EnvironmentId , UsageDay , USAGE_CONTRACT_VERSION } from "@t3tools/contracts" ;
2+ import { AsyncResult } from "effect/unstable/reactivity" ;
3+ import { serverEnvironment } from "./server" ;
24import { act , useLayoutEffect } from "react" ;
35import { create , type ReactTestRenderer } from "react-test-renderer" ;
46import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
@@ -21,10 +23,11 @@ const testState = vi.hoisted(() => ({
2123 runAtomCommand : vi . fn ( ) ,
2224 refreshUsage : vi . fn ( ) ,
2325 refreshAtom : vi . fn ( ) ,
26+ readSummary : vi . fn ( ) ,
2427} ) ) ;
2528vi . mock ( "@t3tools/client-runtime/state/usage" , ( ) => ( { refreshUsage : testState . refreshUsage } ) ) ;
2629vi . mock ( "../rpc/atomRegistry" , ( ) => ( {
27- appAtomRegistry : { refresh : testState . refreshAtom } ,
30+ appAtomRegistry : { refresh : testState . refreshAtom , get : testState . readSummary } ,
2831} ) ) ;
2932vi . mock ( "@effect/atom-react" , async ( importOriginal ) => ( {
3033 ...( await importOriginal < typeof import ( "@effect/atom-react" ) > ( ) ) ,
@@ -122,6 +125,9 @@ beforeEach(async () => {
122125 testState . runAtomCommand . mockReset ( ) ;
123126 testState . refreshUsage . mockReset ( ) . mockResolvedValue ( undefined ) ;
124127 testState . refreshAtom . mockReset ( ) ;
128+ testState . readSummary
129+ . mockReset ( )
130+ . mockImplementation ( ( ) => AsyncResult . success ( testState . environments [ 0 ] ?. summary ) ) ;
125131
126132 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
127133 testState . environments = [ environment ( "a" , 10 ) , environment ( "b" , 20 ) , environment ( "slow" , null ) ] ;
@@ -223,3 +229,36 @@ describe("thread breakdown refresh", () => {
223229 } ,
224230 ) ;
225231} ) ;
232+
233+ it ( "refreshes thread keys with the new window and newly published providers" , async ( ) => {
234+ await act ( ( ) =>
235+ renderer ?. update ( < Probe selected = { new Set ( [ EnvironmentId . make ( "a" ) ] ) } refreshThreads = { true } /> ) ,
236+ ) ;
237+ const nextInput = {
238+ ...input ,
239+ sinceDay : UsageDay . make ( "2026-09-05" ) ,
240+ untilDay : UsageDay . make ( "2026-09-06" ) ,
241+ } ;
242+ const changed = environment ( "a" , 50 ) . summary ! ;
243+ testState . readSummary . mockReturnValue (
244+ AsyncResult . success ( {
245+ ...changed ,
246+ ...nextInput ,
247+ buckets : changed . buckets . map ( ( bucket ) => ( { ...bucket , provider : "claude" } ) ) ,
248+ sources : changed . sources . map ( ( source ) => ( {
249+ ...source ,
250+ fingerprint : { ...source . fingerprint , provider : "claude" } ,
251+ } ) ) ,
252+ } ) ,
253+ ) ;
254+ const query = vi . spyOn ( serverEnvironment , "usageThreadBreakdown" ) ;
255+ try {
256+ await latest . refresh ( nextInput ) ;
257+ expect ( query ) . toHaveBeenCalledWith ( {
258+ environmentId : EnvironmentId . make ( "a" ) ,
259+ input : expect . objectContaining ( { ...nextInput , providers : [ "claude" ] } ) ,
260+ } ) ;
261+ } finally {
262+ query . mockRestore ( ) ;
263+ }
264+ } ) ;
0 commit comments