11import { EnvironmentId } from "@t3tools/contracts" ;
2+ import * as Option from "effect/Option" ;
23import { Atom , AtomRegistry } from "effect/reactivity" ;
34import { describe , expect , it , vi } from "vite-plus/test" ;
45
@@ -38,6 +39,18 @@ vi.mock("../state/presentation", async () => {
3839 } ;
3940} ) ;
4041
42+ vi . mock ( "../state/shell" , async ( ) => {
43+ const { Atom } = await import ( "effect/reactivity" ) ;
44+ const Option = await import ( "effect/Option" ) ;
45+ return {
46+ environmentShell : {
47+ stateValueAtom : Atom . family ( ( _id : string ) =>
48+ Atom . make ( { status : "synchronizing" , error : Option . none < string > ( ) } ) ,
49+ ) ,
50+ } ,
51+ } ;
52+ } ) ;
53+
4154vi . mock ( "../state/server" , ( ) => ( {
4255 serverEnvironment : { refreshProviders : { label : "Refresh providers" } } ,
4356} ) ) ;
@@ -56,6 +69,7 @@ vi.mock("@t3tools/client-runtime/state/runtime", () => ({
5669import { environmentCatalog } from "../connection/catalog" ;
5770import { appAtomRegistry } from "../state/atom-registry" ;
5871import { environmentPresentations } from "../state/presentation" ;
72+ import { environmentShell } from "../state/shell" ;
5973import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage" ;
6074
6175const registry = appAtomRegistry as AtomRegistry . AtomRegistry ;
@@ -66,8 +80,16 @@ const presentationsAtom = environmentPresentations.presentationsAtom as unknown
6680 Map < string , { connection : { phase : string } } >
6781> ;
6882
83+ const shellAtom = ( environmentId : EnvironmentId ) =>
84+ environmentShell . stateValueAtom ( environmentId ) as unknown as Atom . Writable < {
85+ status : string ;
86+ error : Option . Option < string > ;
87+ } > ;
88+
6989const connected = EnvironmentId . make ( "env-connected" ) ;
7090const connecting = EnvironmentId . make ( "env-connecting" ) ;
91+ const syncing = EnvironmentId . make ( "env-syncing" ) ;
92+ const failed = EnvironmentId . make ( "env-failed" ) ;
7193
7294describe ( "installSubscriptionUsageRefresh" , ( ) => {
7395 it ( "refreshes connected environments once the catalog is ready and the app is active" , ( ) => {
@@ -79,8 +101,13 @@ describe("installSubscriptionUsageRefresh", () => {
79101 new Map ( [
80102 [ connected , { connection : { phase : "connected" } } ] ,
81103 [ connecting , { connection : { phase : "connecting" } } ] ,
104+ [ syncing , { connection : { phase : "connected" } } ] ,
105+ [ failed , { connection : { phase : "connected" } } ] ,
82106 ] ) ,
83107 ) ;
108+ registry . set ( shellAtom ( connected ) , { status : "live" , error : Option . none ( ) } ) ;
109+ // A shell that failed never goes live; its quotas still refresh.
110+ registry . set ( shellAtom ( failed ) , { status : "cached" , error : Option . some ( "Shell failed" ) } ) ;
84111
85112 const dispose = installSubscriptionUsageRefresh ( ) ;
86113 registry . set ( catalogAtom , { isReady : true } ) ;
@@ -89,7 +116,11 @@ describe("installSubscriptionUsageRefresh", () => {
89116
90117 fixture . appState = "active" ;
91118 for ( const listener of fixture . appStateListeners ) listener ( ) ;
92- expect ( fixture . refreshed ) . toEqual ( [ connected ] ) ;
119+ expect ( fixture . refreshed ) . toEqual ( [ connected , failed ] ) ;
120+
121+ // The refresh waits for the thread list, so it never competes with it on a cold start.
122+ registry . set ( shellAtom ( syncing ) , { status : "live" , error : Option . none ( ) } ) ;
123+ expect ( fixture . refreshed ) . toEqual ( [ connected , failed , syncing ] ) ;
93124
94125 dispose ( ) ;
95126 expect ( fixture . appStateListeners . size ) . toBe ( 0 ) ;
0 commit comments