@@ -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,193 @@ describe("OpenCode2Server error details", () => {
165166 ) ;
166167} ) ;
167168
169+ describe ( "OpenCode2Server external server" , ( ) => {
170+ // An external server was not started here, so charging startup a catalog wait
171+ // would only add latency: one /api/info check, no event subscription.
172+ it . effect ( "returns without waiting for a model catalog it did not build" , ( ) => {
173+ const requests : Array < string > = [ ] ;
174+ const httpClient = Layer . succeed (
175+ HttpClient . HttpClient ,
176+ HttpClient . make ( ( request ) =>
177+ Effect . sync ( ( ) => {
178+ requests . push ( String ( request . url ) ) ;
179+ return HttpClientResponse . fromWeb (
180+ request ,
181+ new Response ( INFO_BODY , {
182+ status : 200 ,
183+ headers : { "content-type" : "application/json" } ,
184+ } ) ,
185+ ) ;
186+ } ) ,
187+ ) ,
188+ ) ;
189+ return Effect . gen ( function * ( ) {
190+ const server = yield * OpenCode2Server . make ( {
191+ binaryPath : "opencode" ,
192+ serverUrl : "http://127.0.0.1:4096" ,
193+ serverPassword : "secret" ,
194+ directory : "/project" ,
195+ environment : { } ,
196+ } ) ;
197+ const connection = yield * server . withConnection ( ( value ) => Effect . succeed ( value ) ) ;
198+ assert . isTrue ( connection . external ) ;
199+ assert . strictEqual ( requests . length , 1 ) ;
200+ assert . include ( requests [ 0 ] , "/api/info" ) ;
201+ } ) . pipe (
202+ Effect . provide (
203+ Layer . mergeAll (
204+ OpenCode2Client . layer . pipe ( Layer . provide ( httpClient ) ) ,
205+ OpenCodeRuntime . OpenCodeRuntimeLive . pipe ( Layer . provide ( OpenCodeServerLedger . layerTest ) ) ,
206+ ) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ,
207+ ) ,
208+ ) ;
209+ } ) ;
210+ } ) ;
211+
212+ describe ( "OpenCode2Server.settleModelCatalog" , ( ) => {
213+ const catalogEvent = ( type : string , directory : string ) =>
214+ ( { type, location : { directory } } ) as unknown as OpenCode2Client . OpenCode2StreamEvent ;
215+
216+ const settle = < ReadE , StreamE , EventsE > ( input : {
217+ readonly reads : Array < string > ;
218+ readonly read ?: Effect . Effect < void , ReadE > ;
219+ readonly events : Effect . Effect <
220+ Stream . Stream < OpenCode2Client . OpenCode2StreamEvent , StreamE > ,
221+ EventsE
222+ > ;
223+ } ) =>
224+ OpenCode2Server . settleModelCatalog ( {
225+ directory : "/project" ,
226+ read :
227+ input . read ??
228+ Effect . sync ( ( ) => {
229+ input . reads . push ( "read" ) ;
230+ } ) ,
231+ events : input . events ,
232+ } ) ;
233+
234+ it . effect ( "reads once and settles on the directory's model.updated" , ( ) =>
235+ Effect . gen ( function * ( ) {
236+ const reads : Array < string > = [ ] ;
237+ const settling = yield * settle ( {
238+ reads,
239+ events : Effect . succeed (
240+ Stream . make (
241+ catalogEvent ( "model.updated" , "/elsewhere" ) ,
242+ catalogEvent ( "provider.updated" , "/project" ) ,
243+ catalogEvent ( "model.updated" , "/project" ) ,
244+ ) . pipe ( Stream . concat ( Stream . never ) ) ,
245+ ) ,
246+ } ) . pipe ( Effect . forkChild ) ;
247+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
248+ // Settling on the matching event; waiting for the stream to end would hang.
249+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
250+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
251+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
252+ ) ;
253+
254+ it . effect ( "stops waiting after the timeout when no matching event arrives" , ( ) =>
255+ Effect . gen ( function * ( ) {
256+ const reads : Array < string > = [ ] ;
257+ const settling = yield * settle ( {
258+ reads,
259+ events : Effect . succeed (
260+ Stream . make ( catalogEvent ( "model.updated" , "/elsewhere" ) ) . pipe (
261+ Stream . concat ( Stream . never ) ,
262+ ) ,
263+ ) ,
264+ } ) . pipe ( Effect . forkChild ) ;
265+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
266+ assert . isUndefined ( settling . pollUnsafe ( ) ) ;
267+ yield * TestClock . adjust ( "5 seconds" ) ;
268+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
269+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
270+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
271+ ) ;
272+
273+ it . effect ( "shares one deadline between the read and the wait" , ( ) =>
274+ Effect . gen ( function * ( ) {
275+ const reads : Array < string > = [ ] ;
276+ const settling = yield * settle ( {
277+ reads,
278+ read : Effect . sleep ( "4 seconds" ) . pipe (
279+ Effect . andThen (
280+ Effect . sync ( ( ) => {
281+ reads . push ( "read" ) ;
282+ } ) ,
283+ ) ,
284+ ) ,
285+ events : Effect . succeed ( Stream . never ) ,
286+ } ) . pipe ( Effect . forkChild ) ;
287+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
288+ yield * TestClock . adjust ( "4 seconds" ) ;
289+ // The read is done and the wait is running; both share one five-second budget.
290+ assert . isUndefined ( settling . pollUnsafe ( ) ) ;
291+ yield * TestClock . adjust ( "1 second" ) ;
292+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
293+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
294+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
295+ ) ;
296+
297+ it . effect ( "lends the server when the event stream is unavailable" , ( ) =>
298+ Effect . gen ( function * ( ) {
299+ const reads : Array < string > = [ ] ;
300+ const settling = yield * settle ( { reads, events : Effect . fail ( "no event stream" ) } ) . pipe (
301+ Effect . forkChild ,
302+ ) ;
303+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
304+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
305+ assert . deepStrictEqual ( reads , [ "read" ] ) ;
306+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
307+ ) ;
308+
309+ const releasing = ( ) => {
310+ let released = false ;
311+ const stream = Stream . never . pipe (
312+ Stream . ensuring (
313+ Effect . sync ( ( ) => {
314+ released = true ;
315+ } ) ,
316+ ) ,
317+ ) ;
318+ return { stream, released : ( ) => released } ;
319+ } ;
320+
321+ it . effect ( "releases the event subscription when the read fails" , ( ) =>
322+ Effect . gen ( function * ( ) {
323+ const { stream, released } = releasing ( ) ;
324+ const settling = yield * settle ( {
325+ reads : [ ] ,
326+ read : Effect . fail ( "read failed" ) ,
327+ events : Effect . succeed ( stream ) ,
328+ } ) . pipe ( Effect . forkChild ) ;
329+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
330+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
331+ // The child fiber is interrupted with the parent's scope; let that land.
332+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
333+ assert . isTrue ( released ( ) ) ;
334+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
335+ ) ;
336+
337+ it . effect ( "releases the event subscription when the read outlives the deadline" , ( ) =>
338+ Effect . gen ( function * ( ) {
339+ const { stream, released } = releasing ( ) ;
340+ const settling = yield * settle ( {
341+ reads : [ ] ,
342+ read : Effect . never ,
343+ events : Effect . succeed ( stream ) ,
344+ } ) . pipe ( Effect . forkChild ) ;
345+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
346+ assert . isUndefined ( settling . pollUnsafe ( ) ) ;
347+ yield * TestClock . adjust ( "5 seconds" ) ;
348+ assert . strictEqual ( settling . pollUnsafe ( ) ?. _tag , "Success" ) ;
349+ // The child fiber is interrupted with the parent's scope; let that land.
350+ for ( let i = 0 ; i < 20 ; i ++ ) yield * Effect . yieldNow ;
351+ assert . isTrue ( released ( ) ) ;
352+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
353+ ) ;
354+ } ) ;
355+
168356describe ( "OpenCode2Server passwords" , ( ) => {
169357 it . effect ( "generates a distinct 256-bit password per server" , ( ) =>
170358 Effect . gen ( function * ( ) {
@@ -206,26 +394,80 @@ server.listen(0, "127.0.0.1", () => {
206394});
207395` ;
208396
397+ // Serves the endpoints `make` reads for a spawned server: /api/info, the
398+ // /api/event subscription, and the /api/model catalog read. The catalog event
399+ // goes out after a pause and is logged just before it does, so a log that
400+ // already holds it when `withConnection` returns proves the lend waited for the
401+ // event rather than returning on the read alone.
402+ const CATALOG_SERVER = `import { createServer } from "node:http";
403+ import { appendFileSync } from "node:fs";
404+ const directory = process.env.T3_TEST_DIRECTORY;
405+ const log = (line) => appendFileSync(process.env.T3_TEST_LOG, line + "\\n");
406+ const expected = "Basic " + Buffer.from("opencode:" + process.env.OPENCODE_PASSWORD).toString("base64");
407+ const server = createServer((request, response) => {
408+ if (request.headers.authorization !== expected) {
409+ response.writeHead(401, { "content-type": "application/json", "www-authenticate": "Basic" });
410+ response.end(${ JSON . stringify ( UNAUTHORIZED_BODY ) } );
411+ return;
412+ }
413+ const path = new URL(request.url, "http://127.0.0.1").pathname;
414+ if (path === "/api/info") {
415+ response.writeHead(200, { "content-type": "application/json" });
416+ response.end(JSON.stringify({ version: "2.0.18", pid: process.pid, urls: [], paths: { tmp: "/tmp" } }));
417+ return;
418+ }
419+ if (path === "/api/event") {
420+ log("event-request");
421+ response.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
422+ response.flushHeaders();
423+ setTimeout(() => {
424+ log("event-sent");
425+ response.write("data: " + JSON.stringify({
426+ id: "evt_catalog", created: 1, type: "model.updated", data: {}, location: { directory },
427+ }) + "\\n\\n");
428+ }, 250);
429+ return;
430+ }
431+ if (path === "/api/model") {
432+ log("model");
433+ response.writeHead(200, { "content-type": "application/json" });
434+ response.end(JSON.stringify({ location: { directory }, data: [] }));
435+ return;
436+ }
437+ response.writeHead(404);
438+ response.end();
439+ });
440+ server.listen(0, "127.0.0.1", () => {
441+ process.stdout.write("server listening on http://127.0.0.1:" + server.address().port + "\\n");
442+ });
443+ ` ;
444+
445+ const writeFakeBinary = ( script : string ) =>
446+ Effect . gen ( function * ( ) {
447+ const fs = yield * FileSystem . FileSystem ;
448+ const path = yield * Path . Path ;
449+ const platform = yield * HostProcessPlatform ;
450+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-opencode2-fake-" } ) ;
451+ const isWindows = platform === "win32" ;
452+ const binaryPath = path . join ( directory , isWindows ? "opencode.cmd" : "opencode" ) ;
453+ const scriptPath = path . join ( directory , "opencode.mjs" ) ;
454+ yield * fs . writeFileString ( scriptPath , script ) ;
455+ yield * fs . writeFileString (
456+ binaryPath ,
457+ isWindows
458+ ? `@echo off\r\n"${ yield * HostProcessExecutablePath } " "${ scriptPath } " %*\r\n`
459+ : `#!/bin/sh\nexec "${ yield * HostProcessExecutablePath } " "${ scriptPath } " "$@"\n` ,
460+ ) ;
461+ if ( ! isWindows ) yield * fs . chmod ( binaryPath , 0o755 ) ;
462+ return { directory, binaryPath } ;
463+ } ) ;
464+
209465describe ( "OpenCode2Server spawned server" , ( ) => {
210466 it . live (
211467 "is ready once /api/info accepts the generated password" ,
212468 ( ) =>
213469 Effect . gen ( function * ( ) {
214- const fs = yield * FileSystem . FileSystem ;
215- const path = yield * Path . Path ;
216- const platform = yield * HostProcessPlatform ;
217- const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-opencode2-fake-" } ) ;
218- const isWindows = platform === "win32" ;
219- const binaryPath = path . join ( directory , isWindows ? "opencode.cmd" : "opencode" ) ;
220- const scriptPath = path . join ( directory , "opencode.mjs" ) ;
221- yield * fs . writeFileString ( scriptPath , FAKE_SERVER ) ;
222- yield * fs . writeFileString (
223- binaryPath ,
224- isWindows
225- ? `@echo off\r\n"${ yield * HostProcessExecutablePath } " "${ scriptPath } " %*\r\n`
226- : `#!/bin/sh\nexec "${ yield * HostProcessExecutablePath } " "${ scriptPath } " "$@"\n` ,
227- ) ;
228- if ( ! isWindows ) yield * fs . chmod ( binaryPath , 0o755 ) ;
470+ const { directory, binaryPath } = yield * writeFakeBinary ( FAKE_SERVER ) ;
229471
230472 const server = yield * OpenCode2Server . make ( {
231473 binaryPath,
@@ -250,4 +492,44 @@ describe("OpenCode2Server spawned server", () => {
250492 ) ,
251493 15_000 ,
252494 ) ;
495+
496+ it . live (
497+ "is lent after the directory's catalog event" ,
498+ ( ) =>
499+ Effect . gen ( function * ( ) {
500+ const fs = yield * FileSystem . FileSystem ;
501+ const path = yield * Path . Path ;
502+ const { directory, binaryPath } = yield * writeFakeBinary ( CATALOG_SERVER ) ;
503+ const logPath = path . join ( directory , "requests.log" ) ;
504+
505+ const server = yield * OpenCode2Server . make ( {
506+ binaryPath,
507+ serverUrl : "" ,
508+ serverPassword : "" ,
509+ directory,
510+ environment : {
511+ ...( yield * HostProcessEnvironment ) ,
512+ OPENCODE_SERVER_PASSWORD : "ambient" ,
513+ T3_TEST_DIRECTORY : directory ,
514+ T3_TEST_LOG : logPath ,
515+ } ,
516+ } ) ;
517+ const connection = yield * server . withConnection ( ( value ) => Effect . succeed ( value ) ) ;
518+ assert . strictEqual ( connection . version , "2.0.18" ) ;
519+ assert . strictEqual ( connection . external , false ) ;
520+ const lines = ( yield * fs . readFileString ( logPath ) ) . trim ( ) . split ( "\n" ) ;
521+ // Subscribed before the read, and still waiting when the event went out.
522+ assert . isTrue ( lines . indexOf ( "event-request" ) < lines . indexOf ( "model" ) ) ;
523+ assert . include ( lines , "event-sent" ) ;
524+ } ) . pipe (
525+ Effect . scoped ,
526+ Effect . provide (
527+ Layer . mergeAll (
528+ OpenCode2Client . layer ,
529+ OpenCodeRuntime . OpenCodeRuntimeLive . pipe ( Layer . provide ( OpenCodeServerLedger . layerTest ) ) ,
530+ ) . pipe ( Layer . provideMerge ( Layer . mergeAll ( NodeServices . layer , FetchHttpClient . layer ) ) ) ,
531+ ) ,
532+ ) ,
533+ 15_000 ,
534+ ) ;
253535} ) ;
0 commit comments