77 ProviderInstanceId ,
88 type ServerProvider ,
99} from "@t3tools/contracts" ;
10+ import * as Cause from "effect/Cause" ;
1011import { AsyncResult , Atom } from "effect/reactivity" ;
1112import { useEffect } from "react" ;
1213import { expect , it , vi } from "vite-plus/test" ;
@@ -21,6 +22,8 @@ const state = vi.hoisted(() => ({
2122 } [ ] ,
2223 providers : new Map < EnvironmentId , ReadonlyArray < ServerProvider > > ( ) ,
2324 requests : [ ] as EnvironmentId [ ] ,
25+ checks : [ ] as { environmentId : EnvironmentId ; input : unknown } [ ] ,
26+ checkResult : null as AsyncResult . AsyncResult < unknown , unknown > | null ,
2427 sessions : new Map < EnvironmentId , Atom . Writable < AsyncResult . AsyncResult < AuthSessionState > > > ( ) ,
2528} ) ) ;
2629
@@ -41,18 +44,23 @@ vi.mock(import("@t3tools/client-runtime/state/session"), async (original) => {
4144 } ;
4245} ) ;
4346vi . mock ( import ( "~/state/server" ) , ( ) => ( {
44- serverEnvironment : { updateProvider : { } } as never ,
47+ serverEnvironment : { updateProvider : "update" , refreshProviders : "refresh" } as never ,
4548} ) ) ;
4649vi . mock ( import ( "~/state/use-atom-command" ) , ( ) => ( {
47- useAtomCommand : ( ( ) =>
48- async ( { environmentId } : { environmentId : EnvironmentId } ) => {
49- state . requests . push ( environmentId ) ;
50- return AsyncResult . success ( undefined ) ;
50+ useAtomCommand : ( ( command : string ) =>
51+ async ( { environmentId, input } : { environmentId : EnvironmentId ; input : unknown } ) => {
52+ if ( command !== "refresh" ) {
53+ state . requests . push ( environmentId ) ;
54+ return AsyncResult . success ( undefined ) ;
55+ }
56+ state . checks . push ( { environmentId, input } ) ;
57+ return state . checkResult ?? AsyncResult . success ( undefined ) ;
5158 } ) as never ,
5259} ) ) ;
5360
5461import { appAtomRegistry } from "~/rpc/atomRegistry" ;
55- import { useProviderUpdates } from "./ProviderUpdatesAction" ;
62+ import { toastManager } from "./ui/toast" ;
63+ import { useCheckProviderUpdates , useProviderUpdates } from "./ProviderUpdatesAction" ;
5664
5765const provider : ServerProvider = {
5866 instanceId : ProviderInstanceId . make ( "codex" ) ,
@@ -102,6 +110,8 @@ function setup() {
102110 state . environments . map ( ( { environmentId } ) => [ environmentId , [ provider ] ] ) ,
103111 ) ;
104112 state . requests = [ ] ;
113+ state . checks = [ ] ;
114+ state . checkResult = null ;
105115 const sessions = new Map < EnvironmentId , Atom . Writable < AsyncResult . AsyncResult < AuthSessionState > > > (
106116 [
107117 [ state . environments [ 0 ] ! . environmentId , Atom . make ( session ( [ "orchestration:read" ] ) ) ] ,
@@ -118,14 +128,17 @@ function setup() {
118128 return { sessions } ;
119129}
120130
121- it ( "bulk provider updates exclude connections without a confirmed management grant" , async ( ) => {
131+ it ( "provider updates and checks exclude connections without a confirmed management grant" , async ( ) => {
122132 const fixture = setup ( ) ;
123133 let updates ! : ReturnType < typeof useProviderUpdates > ;
134+ let check ! : ReturnType < typeof useCheckProviderUpdates > ;
124135 function Probe ( ) {
125136 const nextUpdates = useProviderUpdates ( ) ;
137+ const nextCheck = useCheckProviderUpdates ( ) ;
126138 useEffect ( ( ) => {
127139 updates = nextUpdates ;
128- } , [ nextUpdates ] ) ;
140+ check = nextCheck ;
141+ } , [ nextUpdates , nextCheck ] ) ;
129142 return null ;
130143 }
131144 await using _screen = await render (
@@ -140,6 +153,29 @@ it("bulk provider updates exclude connections without a confirmed management gra
140153 await updates . updateAll ( { quiet : true } ) ;
141154 } ) ;
142155 expect ( state . requests ) . toEqual ( [ writableId ] ) ;
156+ await act ( async ( ) => {
157+ await check ( ) ;
158+ } ) ;
159+ // A model refresh always drops the server's cached latest provider versions.
160+ expect ( state . checks ) . toEqual ( [ { environmentId : writableId , input : { refreshModels : true } } ] ) ;
161+
162+ using toast = vi . spyOn ( toastManager , "add" ) ;
163+ state . checkResult = AsyncResult . failure ( Cause . interrupt ( ) ) ;
164+ await act ( async ( ) => {
165+ await check ( ) ;
166+ } ) ;
167+ expect ( toast ) . not . toHaveBeenCalled ( ) ;
168+ state . checkResult = AsyncResult . failure ( Cause . fail ( new Error ( "offline" ) ) ) ;
169+ await act ( async ( ) => {
170+ await check ( { quiet : true } ) ;
171+ } ) ;
172+ expect ( toast ) . not . toHaveBeenCalled ( ) ;
173+ await act ( async ( ) => {
174+ await check ( ) ;
175+ } ) ;
176+ expect ( toast ) . toHaveBeenCalledWith (
177+ expect . objectContaining ( { title : "Could not check provider updates" } ) ,
178+ ) ;
143179
144180 await act ( ( ) => {
145181 appAtomRegistry . set ( fixture . sessions . get ( writableId ) ! , session ( [ "orchestration:read" ] ) ) ;
0 commit comments