@@ -125,18 +125,24 @@ describe("write tool", () => {
125125 await expect ( fs . stat ( filePath ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
126126 } ) ;
127127
128- it ( "rejects a delegated write that leaves stale same-size content" , async ( ) => {
129- const filePath = await createTempPath ( "stale.txt" ) ;
130- await fs . writeFile ( filePath , "stale\n" , "utf-8" ) ;
131- const tool = createWriteTool ( tmpDir , {
132- operations : createRecoverableOperations ( async ( ) => { } ) ,
133- } ) ;
134-
135- await expect (
136- tool . execute ( "call-1" , { path : filePath , content : "fresh\n" } , undefined ) ,
137- ) . rejects . toThrow ( "Write verification failed" ) ;
138- await expect ( fs . readFile ( filePath , "utf-8" ) ) . resolves . toBe ( "stale\n" ) ;
139- } ) ;
128+ it . each ( [
129+ { name : "text" , original : Buffer . from ( "stale\n" ) , content : "fresh\n" } ,
130+ { name : "invalid UTF-8" , original : Buffer . from ( [ 0xf0 , 0x90 , 0x80 ] ) , content : "\uFFFD" } ,
131+ ] ) (
132+ "rejects a delegated write that leaves stale same-size bytes: $name" ,
133+ async ( { original, content } ) => {
134+ const filePath = await createTempPath ( "stale.txt" ) ;
135+ await fs . writeFile ( filePath , original ) ;
136+ const tool = createWriteTool ( tmpDir , {
137+ operations : createRecoverableOperations ( async ( ) => { } ) ,
138+ } ) ;
139+
140+ await expect ( tool . execute ( "call-1" , { path : filePath , content } , undefined ) ) . rejects . toThrow (
141+ "Write verification failed" ,
142+ ) ;
143+ await expect ( fs . readFile ( filePath ) ) . resolves . toEqual ( original ) ;
144+ } ,
145+ ) ;
140146
141147 it ( "rejects a delegated write that leaves a non-file target" , async ( ) => {
142148 const filePath = await createTempPath ( "directory" ) ;
@@ -150,19 +156,38 @@ describe("write tool", () => {
150156 ) . rejects . toThrow ( "Write verification failed" ) ;
151157 } ) ;
152158
153- it ( "verifies delegated writes by their persisted UTF-8 bytes" , async ( ) => {
154- const filePath = await createTempPath ( "surrogate.txt" ) ;
155- const content = "unpaired \ud800 surrogate\n" ;
156- const tool = createWriteTool ( tmpDir , {
157- operations : createRecoverableOperations ( ( absolutePath , requestedContent ) =>
159+ it . each ( [ "buffer" , "text" ] as const ) (
160+ "verifies delegated UTF-8 bytes with %s readback" ,
161+ async ( readback ) => {
162+ const filePath = await createTempPath ( "surrogate.txt" ) ;
163+ const content = "unpaired \ud800 surrogate\n" ;
164+ const operations = createRecoverableOperations ( ( absolutePath , requestedContent ) =>
158165 fs . writeFile ( absolutePath , requestedContent , "utf-8" ) ,
159- ) ,
160- } ) ;
166+ ) ;
167+ if ( readback === "text" ) {
168+ operations . readFile = ( absolutePath ) => fs . readFile ( absolutePath , "utf8" ) ;
169+ }
170+ const tool = createWriteTool ( tmpDir , { operations } ) ;
171+
172+ await expect (
173+ tool . execute ( "call-1" , { path : filePath , content } , undefined ) ,
174+ ) . resolves . toMatchObject ( { details : { changed : true , created : true } } ) ;
175+ await expect ( fs . readFile ( filePath ) ) . resolves . toEqual ( Buffer . from ( content , "utf8" ) ) ;
176+ await expect (
177+ tool . execute ( "call-2" , { path : filePath , content } , undefined ) ,
178+ ) . resolves . toMatchObject ( { details : { changed : false } , terminate : true } ) ;
179+ } ,
180+ ) ;
181+
182+ it ( "overwrites invalid UTF-8 bytes that decode to the requested text" , async ( ) => {
183+ const filePath = await createTempPath ( "invalid.txt" ) ;
184+ await fs . writeFile ( filePath , Buffer . from ( [ 0xf0 , 0x90 , 0x80 ] ) ) ;
185+ const tool = createWriteTool ( tmpDir ) ;
161186
162187 await expect (
163- tool . execute ( "call-1" , { path : filePath , content } , undefined ) ,
164- ) . resolves . toMatchObject ( { details : { changed : true , created : true } } ) ;
165- await expect ( fs . readFile ( filePath ) ) . resolves . toEqual ( Buffer . from ( content , "utf8" ) ) ;
188+ tool . execute ( "call-1" , { path : filePath , content : "\uFFFD" } , undefined ) ,
189+ ) . resolves . toMatchObject ( { details : { changed : true , created : false } } ) ;
190+ await expect ( fs . readFile ( filePath ) ) . resolves . toEqual ( Buffer . from ( "\uFFFD" , "utf8" ) ) ;
166191 } ) ;
167192
168193 it ( "writes file URL paths through the shared session path resolver" , async ( ) => {
@@ -190,23 +215,22 @@ describe("write tool", () => {
190215 await expect ( fs . readFile ( asciiPath , "utf-8" ) ) . resolves . toBe ( "ascii\n" ) ;
191216 } ) ;
192217
193- it ( "returns terminal no-op when writing identical content to existing file" , async ( ) => {
194- const filePath = await createTempPath ( "identical.txt" ) ;
195- await fs . writeFile ( filePath , "hello\n" , "utf-8" ) ;
196- const tool = createWriteTool ( tmpDir ) ;
197-
198- const result = await tool . execute (
199- "call-1" ,
200- { path : "identical.txt" , content : "hello\n" } ,
201- undefined ,
202- ) ;
203-
204- const tc0 = expectDefined ( result . content [ 0 ] , "result.content[0] test invariant" ) ;
205- expect ( "text" in tc0 ? tc0 . text : "" ) . toContain ( "No changes made" ) ;
206- expect ( ( result as { terminate ?: boolean } ) . terminate ) . toBe ( true ) ;
207- expect ( result . details ) . toEqual ( { changed : false } ) ;
208- await expect ( fs . readFile ( filePath , "utf-8" ) ) . resolves . toBe ( "hello\n" ) ;
209- } ) ;
218+ it . each ( [ "hello\n" , "café 🦀\r\n日本語 e\u0301\r\n" , "\uFFFD\r\n" ] ) (
219+ "returns terminal no-op for identical UTF-8 content: %j" ,
220+ async ( content ) => {
221+ const filePath = await createTempPath ( "identical.txt" ) ;
222+ await fs . writeFile ( filePath , content , "utf-8" ) ;
223+ const tool = createWriteTool ( tmpDir ) ;
224+
225+ const result = await tool . execute ( "call-1" , { path : "identical.txt" , content } , undefined ) ;
226+
227+ const tc0 = expectDefined ( result . content [ 0 ] , "result.content[0] test invariant" ) ;
228+ expect ( "text" in tc0 ? tc0 . text : "" ) . toContain ( "No changes made" ) ;
229+ expect ( ( result as { terminate ?: boolean } ) . terminate ) . toBe ( true ) ;
230+ expect ( result . details ) . toEqual ( { changed : false } ) ;
231+ await expect ( fs . readFile ( filePath ) ) . resolves . toEqual ( Buffer . from ( content , "utf8" ) ) ;
232+ } ,
233+ ) ;
210234
211235 it ( "reports a created file with its authoritative diff" , async ( ) => {
212236 await createTempPath ( "created.txt" ) ;
0 commit comments