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" ) > ( ) ) ,
@@ -127,6 +130,9 @@ beforeEach(async () => {
127130 testState . runAtomCommand . mockReset ( ) ;
128131 testState . refreshUsage . mockReset ( ) . mockResolvedValue ( undefined ) ;
129132 testState . refreshAtom . mockReset ( ) ;
133+ testState . readSummary
134+ . mockReset ( )
135+ . mockImplementation ( ( ) => AsyncResult . success ( testState . environments [ 0 ] ?. summary ) ) ;
130136
131137 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
132138 testState . environments = [ environment ( "a" , 10 ) , environment ( "b" , 20 ) , environment ( "slow" , null ) ] ;
@@ -261,3 +267,36 @@ describe("snapshot refresh scope", () => {
261267 expect ( latest . refreshError ) . toBe ( "Refresh failed. Showing the last successful usage snapshot." ) ;
262268 } ) ;
263269} ) ;
270+
271+ it ( "refreshes thread keys with the new window and newly published providers" , async ( ) => {
272+ await act ( ( ) =>
273+ renderer ?. update ( < Probe selected = { new Set ( [ EnvironmentId . make ( "a" ) ] ) } refreshThreads = { true } /> ) ,
274+ ) ;
275+ const nextInput = {
276+ ...input ,
277+ sinceDay : UsageDay . make ( "2026-09-05" ) ,
278+ untilDay : UsageDay . make ( "2026-09-06" ) ,
279+ } ;
280+ const changed = environment ( "a" , 50 ) . summary ! ;
281+ testState . readSummary . mockReturnValue (
282+ AsyncResult . success ( {
283+ ...changed ,
284+ ...nextInput ,
285+ buckets : changed . buckets . map ( ( bucket ) => ( { ...bucket , provider : "claude" } ) ) ,
286+ sources : changed . sources . map ( ( source ) => ( {
287+ ...source ,
288+ fingerprint : { ...source . fingerprint , provider : "claude" } ,
289+ } ) ) ,
290+ } ) ,
291+ ) ;
292+ const query = vi . spyOn ( serverEnvironment , "usageThreadBreakdown" ) ;
293+ try {
294+ await latest . refresh ( nextInput ) ;
295+ expect ( query ) . toHaveBeenCalledWith ( {
296+ environmentId : EnvironmentId . make ( "a" ) ,
297+ input : expect . objectContaining ( { ...nextInput , providers : [ "claude" ] } ) ,
298+ } ) ;
299+ } finally {
300+ query . mockRestore ( ) ;
301+ }
302+ } ) ;
0 commit comments