@@ -13,7 +13,8 @@ import * as Exit from "effect/Exit";
1313import * as Queue from "effect/Queue" ;
1414import * as Scope from "effect/Scope" ;
1515import * as Stream from "effect/Stream" ;
16- import { HttpClient , HttpClientResponse } from "effect/http" ;
16+ import { HttpClient , HttpClientError , HttpClientRequest , HttpClientResponse } from "effect/http" ;
17+ import * as TestClock from "effect/testing/TestClock" ;
1718import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1819import * as CodexInstallation from "./CodexInstallation.ts" ;
1920
@@ -31,7 +32,8 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
3132 input : {
3233 options ?: Partial < CodexInstallation . CodexInstallationOptions > ;
3334 manifestCurrent ?: Effect . Effect < ModelManifest . ModelManifestData > ;
34- body ?: Stream . Stream < Uint8Array > ;
35+ body ?: Stream . Stream < Uint8Array , HttpClientError . HttpClientError > ;
36+ downloadStatus ?: number ;
3537 baseDir ?: string ;
3638 local ?: { version : string ; appServerFails ?: boolean ; versionFails ?: boolean } ;
3739 platform ?: NodeJS . Platform ;
@@ -106,7 +108,10 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
106108 Effect . sync ( ( ) => {
107109 downloads ++ ;
108110 return Object . defineProperty (
109- HttpClientResponse . fromWeb ( request , new Response ( null ) ) ,
111+ HttpClientResponse . fromWeb (
112+ request ,
113+ new Response ( null , { status : input . downloadStatus ?? 200 } ) ,
114+ ) ,
110115 "stream" ,
111116 {
112117 value : input . body ?? Stream . succeed ( archive ) ,
@@ -366,6 +371,81 @@ it.effect("rejects corrupted downloads without publishing a runtime", () =>
366371 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
367372) ;
368373
374+ it . effect . each ( [ "interrupted body" , "HTTP 404" ] as const ) (
375+ "reports a download failure for %s without blaming disk access" ,
376+ ( failure ) =>
377+ Effect . gen ( function * ( ) {
378+ const request = HttpClientRequest . get ( asset . url ) ;
379+ const response = HttpClientResponse . fromWeb ( request , new Response ( null ) ) ;
380+ const h = yield * makeHarness (
381+ failure === "HTTP 404"
382+ ? { downloadStatus : 404 }
383+ : {
384+ body : Stream . concat (
385+ Stream . succeed ( archive . subarray ( 0 , 10 ) ) ,
386+ Stream . fail (
387+ new HttpClientError . HttpClientError ( {
388+ reason : new HttpClientError . DecodeError ( {
389+ request,
390+ response,
391+ cause : new TypeError ( "terminated" ) ,
392+ } ) ,
393+ } ) ,
394+ ) ,
395+ ) ,
396+ } ,
397+ ) ;
398+ yield * h . installation . start ;
399+ expect ( yield * terminalState ( h . installation ) ) . toMatchObject ( {
400+ phase : "failed" ,
401+ message :
402+ failure === "HTTP 404"
403+ ? "The Codex download server returned HTTP 404. Try again later."
404+ : "The Codex download was interrupted. Check your internet connection, then retry installation." ,
405+ } ) ;
406+ expect ( h . downloads ( ) ) . toBe ( 1 ) ;
407+ expect ( yield * h . fs . readDirectory ( h . installation . managedDirectory ) ) . toEqual ( [ ] ) ;
408+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
409+ ) ;
410+
411+ it . effect (
412+ "preserves the filesystem failure message when installation cannot create its directory" ,
413+ ( ) =>
414+ Effect . gen ( function * ( ) {
415+ const fs = yield * FileSystem . FileSystem ;
416+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-codex-install-test-" } ) ;
417+ const baseDir = `${ directory } /not-a-directory` ;
418+ yield * fs . writeFileString ( baseDir , "existing file" ) ;
419+ const h = yield * makeHarness ( { baseDir } ) ;
420+ yield * h . installation . start ;
421+ expect ( yield * terminalState ( h . installation ) ) . toMatchObject ( {
422+ phase : "failed" ,
423+ message : "Could not install Codex. Check disk space and directory access, then try again." ,
424+ } ) ;
425+ expect ( h . downloads ( ) ) . toBe ( 0 ) ;
426+ expect ( yield * fs . readFileString ( baseDir ) ) . toBe ( "existing file" ) ;
427+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
428+ ) ;
429+
430+ it . effect ( "reports a download timeout and cleans the staging directory" , ( ) =>
431+ Effect . gen ( function * ( ) {
432+ const started = yield * Deferred . make < void > ( ) ;
433+ const stall = Deferred . succeed ( started , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ;
434+ const h = yield * makeHarness ( {
435+ body : Stream . concat ( Stream . succeed ( archive . subarray ( 0 , 10 ) ) , Stream . fromEffect ( stall ) ) ,
436+ } ) ;
437+ yield * h . installation . start ;
438+ yield * Deferred . await ( started ) ;
439+ yield * TestClock . adjust ( "45 minutes" ) ;
440+ expect ( yield * terminalState ( h . installation ) ) . toMatchObject ( {
441+ phase : "failed" ,
442+ message : "The Codex download timed out. Retry installation." ,
443+ } ) ;
444+ expect ( h . downloads ( ) ) . toBe ( 1 ) ;
445+ expect ( yield * h . fs . readDirectory ( h . installation . managedDirectory ) ) . toEqual ( [ ] ) ;
446+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
447+ ) ;
448+
369449it . effect ( "cancels an in-flight download and cleans the staging directory" , ( ) =>
370450 Effect . gen ( function * ( ) {
371451 const downloading = yield * Deferred . make < void > ( ) ;
0 commit comments