@@ -23,7 +23,23 @@ import { vi } from "vite-plus/test";
2323
2424vi . mock ( "node:fs/promises" , async ( importOriginal ) => {
2525 const actual = await importOriginal < typeof NodeFSP > ( ) ;
26- return { ...actual , realpath : vi . fn ( actual . realpath ) } ;
26+ return { ...actual , realpath : vi . fn ( actual . realpath ) , open : vi . fn ( actual . open ) } ;
27+ } ) ;
28+
29+ const injectShortReads = Effect . fn ( "injectShortReads" ) ( function * ( target : string ) {
30+ const native = yield * Effect . promise ( ( ) => vi . importActual < typeof NodeFSP > ( "node:fs/promises" ) ) ;
31+ const open = vi . mocked ( NodeFSP . open ) . mockImplementation ( async ( path , flags , mode ) => {
32+ const handle = await native . open ( path , flags , mode ) ;
33+ if ( path === target ) {
34+ const read = handle . read . bind ( handle ) ;
35+ Object . defineProperty ( handle , "read" , {
36+ value : ( buffer : Uint8Array , offset : number , length : number , position : number ) =>
37+ read ( buffer , offset , Math . min ( length , 2 ) , position ) ,
38+ } ) ;
39+ }
40+ return handle ;
41+ } ) ;
42+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => open . mockImplementation ( native . open ) ) ) ;
2743} ) ;
2844
2945const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-" ;
@@ -162,6 +178,57 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
162178 } ) ;
163179
164180 describe ( "readFile" , ( ) => {
181+ it . effect ( "reads UTF-8 files relative to the workspace root" , ( ) =>
182+ Effect . gen ( function * ( ) {
183+ const workspaceFileSystem = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
184+ const cwd = yield * makeTempDir ;
185+ yield * writeTextFile ( cwd , "src/index.ts" , "export const answer = 42;\n" ) ;
186+
187+ const result = yield * workspaceFileSystem . readFile ( {
188+ cwd,
189+ relativePath : "src/index.ts" ,
190+ } ) ;
191+
192+ expect ( result ) . toEqual ( {
193+ relativePath : "src/index.ts" ,
194+ contents : "export const answer = 42;\n" ,
195+ byteLength : 26 ,
196+ truncated : false ,
197+ } ) ;
198+ } ) ,
199+ ) ;
200+
201+ it . effect ( "finishes short reads before decoding UTF-8 text" , ( ) =>
202+ Effect . gen ( function * ( ) {
203+ const files = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
204+ const fs = yield * FileSystem . FileSystem ;
205+ const cwd = yield * makeTempDir ;
206+ const contents = "A café 🐈\n" ;
207+ yield * writeTextFile ( cwd , "short.txt" , contents ) ;
208+ const target = yield * fs . realPath ( `${ cwd } /short.txt` ) ;
209+ yield * injectShortReads ( target ) ;
210+ expect ( yield * files . readFile ( { cwd, relativePath : "short.txt" } ) ) . toEqual ( {
211+ relativePath : "short.txt" ,
212+ contents,
213+ byteLength : Buffer . byteLength ( contents ) ,
214+ truncated : false ,
215+ } ) ;
216+ } ) ,
217+ ) ;
218+
219+ it . effect ( "rejects binary bytes delivered after an initial short read" , ( ) =>
220+ Effect . gen ( function * ( ) {
221+ const files = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
222+ const fs = yield * FileSystem . FileSystem ;
223+ const cwd = yield * makeTempDir ;
224+ yield * writeTextFile ( cwd , "short.txt" , "ab\0cd" ) ;
225+ const target = yield * fs . realPath ( `${ cwd } /short.txt` ) ;
226+ yield * injectShortReads ( target ) ;
227+ const error = yield * files . readFile ( { cwd, relativePath : "short.txt" } ) . pipe ( Effect . flip ) ;
228+ expect ( error ) . toBeInstanceOf ( WorkspaceFileSystem . WorkspaceBinaryFileError ) ;
229+ } ) ,
230+ ) ;
231+
165232 it . effect ( "reads host files outside the workspace root by absolute path" , ( ) =>
166233 Effect . gen ( function * ( ) {
167234 const workspaceFileSystem = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
0 commit comments