@@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem";
1111import * as Layer from "effect/Layer" ;
1212import * as Path from "effect/Path" ;
1313import * as Redacted from "effect/Redacted" ;
14+ import * as Stream from "effect/Stream" ;
1415import * as TestClock from "effect/testing/TestClock" ;
1516import { FetchHttpClient , HttpClient , HttpClientResponse } from "effect/http" ;
1617import { describe } from "vite-plus/test" ;
@@ -165,6 +166,104 @@ describe("OpenCode2Server error details", () => {
165166 ) ;
166167} ) ;
167168
169+ describe ( "OpenCode2Server.settleModelCatalog" , ( ) => {
170+ const catalogEvent = ( type : string , directory : string ) =>
171+ ( { type, location : { directory } } ) as unknown as OpenCode2Client . OpenCode2StreamEvent ;
172+
173+ const settle = < StreamE , EventsE > ( input : {
174+ readonly reads : Array < string > ;
175+ readonly read ?: Effect . Effect < void > ;
176+ readonly events : Effect . Effect <
177+ Stream . Stream < OpenCode2Client . OpenCode2StreamEvent , StreamE > ,
178+ EventsE
179+ > ;
180+ } ) =>
181+ OpenCode2Server . settleModelCatalog ( {
182+ directory : "/project" ,
183+ read :
184+ input . read ??
185+ Effect . sync ( ( ) => {
186+ input . reads . push ( "read" ) ;
187+ } ) ,
188+ events : input . events ,
189+ } ) ;
190+
191+ it . effect ( "reads once and settles on the directory's model.updated" , ( ) =>
192+ Effect . gen ( function * ( ) {
193+ const reads : Array < string > = [ ] ;
194+ const settling = yield * settle ( {
195+ reads,
196+ events : Effect . succeed (
197+ Stream . make (
198+ catalogEvent ( "model.updated" , "/elsewhere" ) ,
199+ catalogEvent ( "provider.updated" , "/project" ) ,
200+ catalogEvent ( "model.updated" , "/project" ) ,
201+ ) . pipe ( Stream . concat ( Stream . never ) ) ,
202+ ) ,
203+ } ) . pipe ( Effect . forkChild ) ;
204+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
205+ // Settling on the matching event; waiting for the stream to end would hang.
206+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
207+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
208+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
209+ ) ;
210+
211+ it . effect ( "stops waiting after the timeout when no matching event arrives" , ( ) =>
212+ Effect . gen ( function * ( ) {
213+ const reads : Array < string > = [ ] ;
214+ const settling = yield * settle ( {
215+ reads,
216+ events : Effect . succeed (
217+ Stream . make ( catalogEvent ( "model.updated" , "/elsewhere" ) ) . pipe (
218+ Stream . concat ( Stream . never ) ,
219+ ) ,
220+ ) ,
221+ } ) . pipe ( Effect . forkChild ) ;
222+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
223+ assert . isUndefined ( settling . pollUnsafe ( ) ) ;
224+ yield * TestClock . adjust ( "5 seconds" ) ;
225+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
226+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
227+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
228+ ) ;
229+
230+ it . effect ( "shares one deadline between the read and the wait" , ( ) =>
231+ Effect . gen ( function * ( ) {
232+ const reads : Array < string > = [ ] ;
233+ const settling = yield * settle ( {
234+ reads,
235+ read : Effect . sleep ( "4 seconds" ) . pipe (
236+ Effect . andThen (
237+ Effect . sync ( ( ) => {
238+ reads . push ( "read" ) ;
239+ } ) ,
240+ ) ,
241+ ) ,
242+ events : Effect . succeed ( Stream . never ) ,
243+ } ) . pipe ( Effect . forkChild ) ;
244+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
245+ yield * TestClock . adjust ( "4 seconds" ) ;
246+ // The read is done and the wait is running; both share one five-second budget.
247+ assert . isUndefined ( settling . pollUnsafe ( ) ) ;
248+ yield * TestClock . adjust ( "1 second" ) ;
249+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
250+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
251+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
252+ ) ;
253+
254+ it . effect ( "lends the server when the event stream is unavailable" , ( ) =>
255+ Effect . gen ( function * ( ) {
256+ const reads : Array < string > = [ ] ;
257+ const settling = yield * settle ( { reads, events : Effect . fail ( "no event stream" ) } ) . pipe (
258+ Effect . forkChild ,
259+ ) ;
260+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
261+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
262+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
263+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
264+ ) ;
265+ } ) ;
266+
168267describe ( "OpenCode2Server passwords" , ( ) => {
169268 it . effect ( "generates a distinct 256-bit password per server" , ( ) =>
170269 Effect . gen ( function * ( ) {
0 commit comments