@@ -26,7 +26,7 @@ describe("bounded file byte reads", () => {
2626 } ,
2727 ) ;
2828
29- it . each ( [ undefined , 5 ] ) (
29+ it . each ( [ undefined , 0 , 5 ] ) (
3030 "preserves raw reasons or caller-owned cancellation with expected size %s" ,
3131 async ( expectedSize ) => {
3232 const root = await createTestTempDirectory ( "rea-bounded-cancel-" ) ;
@@ -61,6 +61,48 @@ describe("bounded file byte reads", () => {
6161 } ,
6262 ) ;
6363
64+ it . each ( [
65+ { contents : "" , maxBytes : 0 , expectedSize : undefined , outcome : "EOF" } ,
66+ { contents : "" , maxBytes : 5 , expectedSize : 5 , outcome : "early EOF" } ,
67+ {
68+ contents : "hello" ,
69+ maxBytes : 5 ,
70+ expectedSize : 5 ,
71+ outcome : "expected size" ,
72+ } ,
73+ {
74+ contents : "hello" ,
75+ maxBytes : 4 ,
76+ expectedSize : undefined ,
77+ outcome : "overflow" ,
78+ } ,
79+ ] ) (
80+ "honors cancellation while the $outcome read is in flight" ,
81+ async ( { contents, maxBytes, expectedSize } ) => {
82+ const root = await createTestTempDirectory ( "rea-bounded-cancel-read-" ) ;
83+ const path = join ( root , "input" ) ;
84+ await writeFile ( path , contents ) ;
85+ const handle = await open ( path , "r" ) ;
86+ const controller = new AbortController ( ) ;
87+ const reason = new Error ( "cancelled during read" ) ;
88+ const typed = new Error ( "typed cancellation" , { cause : reason } ) ;
89+ try {
90+ const result = readBoundedFileBytes ( handle , maxBytes , {
91+ signal : controller . signal ,
92+ expectedSize,
93+ cancellationFailure : ( observed ) => {
94+ expect ( observed ) . toBe ( reason ) ;
95+ return typed ;
96+ } ,
97+ } ) ;
98+ controller . abort ( reason ) ;
99+ await expect ( result ) . rejects . toBe ( typed ) ;
100+ } finally {
101+ await handle . close ( ) ;
102+ }
103+ } ,
104+ ) ;
105+
64106 it ( "stops after one overflow byte when a file grows beyond its admitted size" , async ( ) => {
65107 const root = await createTestTempDirectory ( "rea-growing-bytes-" ) ;
66108 const path = join ( root , "input" ) ;
0 commit comments