77} from "@t3tools/contracts" ;
88import * as HostProcess from "@t3tools/shared/HostProcess" ;
99import * as Effect from "effect/Effect" ;
10+ import * as Deferred from "effect/Deferred" ;
11+ import * as SubscriptionRef from "effect/SubscriptionRef" ;
1012import * as FileSystem from "effect/FileSystem" ;
1113import * as Layer from "effect/Layer" ;
1214import * as Path from "effect/Path" ;
@@ -15,8 +17,9 @@ import * as Stream from "effect/Stream";
1517import * as ServerSettings from "../serverSettings.ts" ;
1618import * as CodexInstallation from "./CodexInstallation.ts" ;
1719import * as AntigravityInstallation from "./AntigravityInstallation.ts" ;
20+ import { ProviderDriverError } from "@t3tools/provider-core/server/errors" ;
1821import type { ProviderInstance } from "@t3tools/provider-core/server/driver" ;
19- import { makeProviderInstallation } from "./providerInstallation.ts" ;
22+ import * as ProviderInstallation from "./providerInstallation.ts" ;
2023import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts" ;
2124import * as ProviderRegistry from "./ProviderRegistry.ts" ;
2225
@@ -60,69 +63,145 @@ const makeHarness = Effect.fn("providerInstallation.test.makeHarness")(function*
6063 } = { } ,
6164) {
6265 const calls : string [ ] = [ ] ;
66+ const refreshed = yield * Deferred . make < void > ( ) ;
67+ const observed = yield * Deferred . make < void > ( ) ;
68+ const listening = yield * Deferred . make < void > ( ) ;
69+ const installationState = yield * SubscriptionRef . make ( state ) ;
6370 let protectedPaths : ReadonlyArray < string > = [ ] ;
6471 const configured = input . instance ?? instance ( ) ;
65- const router = yield * makeProviderInstallation ( ) . pipe (
66- Effect . provide (
67- Layer . mergeAll (
68- ServerSettings . layerTest ( input . settings ) ,
69- Layer . mock ( ProviderInstanceRegistry . ProviderInstanceRegistry ) ( {
70- getInstance : ( id ) =>
71- Effect . succeed ( id === configured . instanceId ? configured : undefined ) ,
72- listInstances : Effect . succeed ( [ configured ] ) ,
72+ const dependencies = Layer . mergeAll (
73+ ServerSettings . layerTest ( input . settings ) ,
74+ Layer . mock ( ProviderInstanceRegistry . ProviderInstanceRegistry ) ( {
75+ getInstance : ( id ) => Effect . succeed ( id === configured . instanceId ? configured : undefined ) ,
76+ listInstances : Effect . succeed ( [ configured ] ) ,
77+ } ) ,
78+ Layer . mock ( ProviderRegistry . ProviderRegistry ) ( {
79+ refreshInstance : ( ) =>
80+ Effect . sync ( ( ) => {
81+ calls . push ( "refresh" ) ;
82+ return [ ] ;
83+ } ) . pipe ( Effect . tap ( ( ) => Deferred . succeed ( refreshed , undefined ) ) ) ,
84+ } ) ,
85+ Layer . mock ( CodexInstallation . CodexInstallation ) ( {
86+ managedDirectory : "/unused-managed-codex" ,
87+ start : Effect . sync ( ( ) => {
88+ calls . push ( "codex-start" ) ;
89+ return { ...state , driver : ProviderDriverKind . make ( "codex" ) } ;
90+ } ) ,
91+ cancel : ( ) =>
92+ Effect . sync ( ( ) => {
93+ calls . push ( "codex-cancel" ) ;
94+ return { ...state , driver : ProviderDriverKind . make ( "codex" ) } ;
7395 } ) ,
74- Layer . mock ( ProviderRegistry . ProviderRegistry ) ( {
75- refreshInstance : ( ) =>
76- Effect . sync ( ( ) => {
77- calls . push ( "refresh" ) ;
78- return [ ] ;
79- } ) ,
96+ state : Effect . succeed ( { ... state , driver : ProviderDriverKind . make ( "codex" ) } ) ,
97+ changes : Stream . succeed ( { ... state , driver : ProviderDriverKind . make ( "codex" ) } ) ,
98+ remove : ( paths ) =>
99+ Effect . sync ( ( ) => {
100+ protectedPaths = paths ?? [ ] ;
101+ calls . push ( "codex-remove" ) ;
80102 } ) ,
81- Layer . mock ( CodexInstallation . CodexInstallation ) ( {
82- managedDirectory : "/unused-managed-codex" ,
83- start : Effect . sync ( ( ) => {
84- calls . push ( "codex-start" ) ;
85- return { ...state , driver : ProviderDriverKind . make ( "codex" ) } ;
86- } ) ,
87- cancel : ( ) =>
88- Effect . sync ( ( ) => {
89- calls . push ( "codex-cancel" ) ;
90- return { ...state , driver : ProviderDriverKind . make ( "codex" ) } ;
91- } ) ,
92- state : Effect . succeed ( { ...state , driver : ProviderDriverKind . make ( "codex" ) } ) ,
93- changes : Stream . succeed ( { ...state , driver : ProviderDriverKind . make ( "codex" ) } ) ,
94- remove : ( paths ) =>
95- Effect . sync ( ( ) => {
96- protectedPaths = paths ?? [ ] ;
97- calls . push ( "codex-remove" ) ;
98- } ) ,
103+ } ) ,
104+ Layer . mock ( AntigravityInstallation . AntigravityInstallation ) ( {
105+ managedDirectory : "/unused-managed-runtime" ,
106+ start : Effect . sync ( ( ) => {
107+ calls . push ( "start" ) ;
108+ return { ...state , operationId : "install-1" , phase : "downloading" as const } ;
109+ } ) . pipe ( Effect . tap ( ( started ) => SubscriptionRef . set ( installationState , started ) ) ) ,
110+ cancel : ( ) =>
111+ Effect . sync ( ( ) => {
112+ calls . push ( "cancel" ) ;
113+ return state ;
99114 } ) ,
100- Layer . mock ( AntigravityInstallation . AntigravityInstallation ) ( {
101- managedDirectory : "/unused-managed-runtime" ,
102- start : Effect . sync ( ( ) => {
103- calls . push ( "start" ) ;
104- return state ;
105- } ) ,
106- cancel : ( ) =>
107- Effect . sync ( ( ) => {
108- calls . push ( "cancel" ) ;
109- return state ;
110- } ) ,
111- state : Effect . succeed ( state ) ,
112- changes : Stream . succeed ( state ) ,
113- remove : ( paths ) =>
114- Effect . sync ( ( ) => {
115- protectedPaths = paths ?? [ ] ;
116- calls . push ( "remove" ) ;
117- } ) ,
115+ state : Effect . succeed ( state ) ,
116+ changes : input . instance ?. refreshModels
117+ ? SubscriptionRef . changes ( installationState ) . pipe (
118+ Stream . tap ( ( ) => Deferred . succeed ( listening , undefined ) ) ,
119+ Stream . ensuring ( Deferred . succeed ( observed , undefined ) ) ,
120+ )
121+ : Stream . succeed ( state ) ,
122+ remove : ( paths ) =>
123+ Effect . sync ( ( ) => {
124+ protectedPaths = paths ?? [ ] ;
125+ calls . push ( "remove" ) ;
118126 } ) ,
119- ) ,
120- ) ,
127+ } ) ,
121128 ) ;
122- return { router, calls, protectedPaths : ( ) => protectedPaths } ;
129+ const context = yield * Layer . build ( ProviderInstallation . layer . pipe ( Layer . provide ( dependencies ) ) ) ;
130+ const connect = Effect . service ( ProviderInstallation . ProviderInstallation ) . pipe (
131+ Effect . provide ( context ) ,
132+ ) ;
133+ const router = yield * connect ;
134+ return {
135+ router,
136+ connect,
137+ listening,
138+ calls,
139+ refreshed,
140+ observed,
141+ installationState,
142+ protectedPaths : ( ) => protectedPaths ,
143+ } ;
123144} ) ;
124145
125146describe ( "provider installation routing" , ( ) => {
147+ it . effect . each ( [
148+ { phase : "succeeded" , enabled : true , checkFails : false , expectedChecks : 1 } ,
149+ { phase : "succeeded" , enabled : true , checkFails : true , expectedChecks : 1 } ,
150+ { phase : "succeeded" , enabled : false , checkFails : false , expectedChecks : 0 } ,
151+ { phase : "cancelled" , enabled : true , checkFails : false , expectedChecks : 0 } ,
152+ { phase : "failed" , enabled : true , checkFails : false , expectedChecks : 0 } ,
153+ ] as const ) (
154+ "checks account access once after installation is $phase, enabled=$enabled, checkFails=$checkFails" ,
155+ ( { phase, enabled, checkFails, expectedChecks } ) =>
156+ Effect . gen ( function * ( ) {
157+ let checks = 0 ;
158+ const harness = yield * makeHarness ( {
159+ instance : Object . assign ( instance ( ) , {
160+ enabled,
161+ refreshModels : ( ) =>
162+ Effect . sync ( ( ) => {
163+ checks ++ ;
164+ } ) . pipe (
165+ Effect . andThen (
166+ checkFails
167+ ? Effect . fail (
168+ new ProviderDriverError ( {
169+ driver,
170+ instanceId,
171+ detail : "Sign in required." ,
172+ } ) ,
173+ )
174+ : Effect . void ,
175+ ) ,
176+ ) ,
177+ } ) ,
178+ } ) ;
179+ yield * Effect . gen ( function * ( ) {
180+ const first = yield * harness . connect ;
181+ const second = yield * harness . connect ;
182+ yield * Effect . all ( [ first . start ( { instanceId } ) , second . start ( { instanceId } ) ] , {
183+ concurrency : "unbounded" ,
184+ } ) ;
185+ yield * Deferred . await ( harness . listening ) ;
186+ } ) . pipe ( Effect . scoped ) ;
187+ assert . isFalse ( yield * Deferred . isDone ( harness . observed ) ) ;
188+ assert . strictEqual ( checks , 0 ) ;
189+ yield * SubscriptionRef . set ( harness . installationState , {
190+ ...state ,
191+ operationId : "install-1" ,
192+ phase,
193+ installedVersion : phase === "succeeded" ? "1.5.1" : null ,
194+ } ) ;
195+ yield * Deferred . await ( harness . observed ) ;
196+ if ( expectedChecks > 0 ) yield * Deferred . await ( harness . refreshed ) ;
197+ assert . strictEqual ( checks , expectedChecks ) ;
198+ assert . deepEqual (
199+ harness . calls ,
200+ expectedChecks > 0 ? [ "start" , "start" , "refresh" ] : [ "start" , "start" ] ,
201+ ) ;
202+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
203+ ) ;
204+
126205 it . effect ( "keeps external installs manual without hiding shared install status" , ( ) =>
127206 Effect . gen ( function * ( ) {
128207 const harness = yield * makeHarness ( {
0 commit comments