11// @effect -diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
22import * as NodeChildProcess from "node:child_process" ;
3+ import * as NodeFSP from "node:fs/promises" ;
4+ import { vi } from "vite-plus/test" ;
35
46import * as NodeServices from "@effect/platform-node/NodeServices" ;
57import { it , describe , expect } from "@effect/vitest" ;
@@ -17,6 +19,27 @@ import * as WorkspacePaths from "./WorkspacePaths.ts";
1719import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
1820import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
1921
22+ vi . mock ( "node:fs/promises" , async ( importOriginal ) => {
23+ const actual = await importOriginal < typeof NodeFSP > ( ) ;
24+ return { ...actual , open : vi . fn ( actual . open ) } ;
25+ } ) ;
26+
27+ const injectShortReads = Effect . fn ( "injectShortReads" ) ( function * ( target : string ) {
28+ const native = yield * Effect . promise ( ( ) => vi . importActual < typeof NodeFSP > ( "node:fs/promises" ) ) ;
29+ const open = vi . mocked ( NodeFSP . open ) . mockImplementation ( async ( path , flags , mode ) => {
30+ const handle = await native . open ( path , flags , mode ) ;
31+ if ( path === target ) {
32+ const read = handle . read . bind ( handle ) ;
33+ Object . defineProperty ( handle , "read" , {
34+ value : ( buffer : Uint8Array , offset : number , length : number , position : number ) =>
35+ read ( buffer , offset , Math . min ( length , 2 ) , position ) ,
36+ } ) ;
37+ }
38+ return handle ;
39+ } ) ;
40+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => open . mockImplementation ( native . open ) ) ) ;
41+ } ) ;
42+
2043const layerProject = WorkspaceFileSystem . layer . pipe (
2144 Layer . provide ( WorkspacePaths . layer ) ,
2245 Layer . provide ( WorkspaceEntries . layer . pipe ( Layer . provide ( WorkspacePaths . layer ) ) ) ,
@@ -78,6 +101,37 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
78101 } ) ,
79102 ) ;
80103
104+ it . effect ( "finishes short reads before decoding UTF-8 text" , ( ) =>
105+ Effect . gen ( function * ( ) {
106+ const files = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
107+ const fs = yield * FileSystem . FileSystem ;
108+ const cwd = yield * makeTempDir ;
109+ const contents = "A café 🐈\n" ;
110+ yield * writeTextFile ( cwd , "short.txt" , contents ) ;
111+ const target = yield * fs . realPath ( `${ cwd } /short.txt` ) ;
112+ yield * injectShortReads ( target ) ;
113+ expect ( yield * files . readFile ( { cwd, relativePath : "short.txt" } ) ) . toEqual ( {
114+ relativePath : "short.txt" ,
115+ contents,
116+ byteLength : Buffer . byteLength ( contents ) ,
117+ truncated : false ,
118+ } ) ;
119+ } ) ,
120+ ) ;
121+
122+ it . effect ( "rejects binary bytes delivered after an initial short read" , ( ) =>
123+ Effect . gen ( function * ( ) {
124+ const files = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
125+ const fs = yield * FileSystem . FileSystem ;
126+ const cwd = yield * makeTempDir ;
127+ yield * writeTextFile ( cwd , "short.txt" , "ab\0cd" ) ;
128+ const target = yield * fs . realPath ( `${ cwd } /short.txt` ) ;
129+ yield * injectShortReads ( target ) ;
130+ const error = yield * files . readFile ( { cwd, relativePath : "short.txt" } ) . pipe ( Effect . flip ) ;
131+ expect ( error ) . toBeInstanceOf ( WorkspaceFileSystem . WorkspaceBinaryFileError ) ;
132+ } ) ,
133+ ) ;
134+
81135 it . effect ( "reads host files outside the workspace root by absolute path" , ( ) =>
82136 Effect . gen ( function * ( ) {
83137 const workspaceFileSystem = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
0 commit comments