@@ -181,6 +181,89 @@ func TestWriteFileToolOverwritePreservesExistingEncoding(t *testing.T) {
181181 }
182182}
183183
184+ func TestWriteFileToolExplicitEncodingIntent (t * testing.T ) {
185+ t .Setenv ("ZERO_FORMAT_ON_WRITE" , "" )
186+ // Explicit intent wins independently; omitted intent retains automatic
187+ // restoration. Every case also exercises a second tracked overwrite.
188+ for _ , tt := range []struct {
189+ name , existing , content , bom , endings , want string
190+ }{
191+ {"default both" , "\ufeff old\r \n " , "new\n " , "" , "" , "\ufeff new\r \n " },
192+ {"remove BOM retain CRLF" , "\ufeff old\r \n " , "new\n " , "remove" , "" , "new\r \n " },
193+ {"LF retain BOM" , "\ufeff old\r \n " , "new\n " , "" , "lf" , "\ufeff new\n " },
194+ {"LF without BOM" , "old\r \n " , "new\n " , "" , "lf" , "new\n " },
195+ {"remove and LF" , "\ufeff old\r \n " , "\ufeff new\r \n " , "remove" , "lf" , "new\n " },
196+ {"add BOM retain LF" , "old\n " , "new\n " , "add" , "" , "\ufeff new\n " },
197+ {"CRLF retain BOM" , "\ufeff old\n " , "new\n " , "" , "crlf" , "\ufeff new\r \n " },
198+ {"CRLF without BOM" , "old\n " , "new\n " , "" , "crlf" , "new\r \n " },
199+ {"add and CRLF" , "old\n " , "new\n " , "add" , "crlf" , "\ufeff new\r \n " },
200+ {"empty remove BOM" , "\ufeff old\r \n " , "" , "remove" , "" , "" },
201+ {"empty default" , "\ufeff old\r \n " , "" , "" , "" , "\ufeff " },
202+ } {
203+ t .Run (tt .name , func (t * testing.T ) {
204+ root := t .TempDir ()
205+ path := filepath .Join (root , "example.txt" )
206+ if err := os .WriteFile (path , []byte (tt .existing ), 0o644 ); err != nil {
207+ t .Fatal (err )
208+ }
209+ path , err := filepath .EvalSymlinks (path )
210+ if err != nil {
211+ t .Fatal (err )
212+ }
213+ options := RunOptions {FileTracker : NewFileTracker ()}
214+ read := NewScopedReadFileTool (root , nil ).(optionsAwareTool ).RunWithOptions (context .Background (), map [string ]any {"path" : path }, options )
215+ if read .Status != StatusOK {
216+ t .Fatal (read .Output )
217+ }
218+ args := map [string ]any {"path" : path , "content" : tt .content , "overwrite" : true }
219+ if tt .bom != "" {
220+ args ["bom" ] = tt .bom
221+ }
222+ if tt .endings != "" {
223+ args ["line_endings" ] = tt .endings
224+ }
225+ for i := 0 ; i < 2 ; i ++ {
226+ result := NewScopedWriteFileTool (root , nil ).(optionsAwareTool ).RunWithOptions (context .Background (), args , options )
227+ if result .Status != StatusOK {
228+ t .Fatal (result .Output )
229+ }
230+ got , err := os .ReadFile (path )
231+ if err != nil {
232+ t .Fatal (err )
233+ }
234+ if string (got ) != tt .want {
235+ t .Fatalf ("written bytes = %q, want %q" , got , tt .want )
236+ }
237+ if ! options .FileTracker .SeenWhole (path ) {
238+ t .Fatal ("encoding intent discarded whole-file observation" )
239+ }
240+ }
241+ })
242+ }
243+ }
244+
245+ func TestWriteFileToolRejectsInvalidEncodingIntent (t * testing.T ) {
246+ for _ , key := range []string {"bom" , "line_endings" } {
247+ for _ , value := range []any {"invalid" , true } {
248+ root := t .TempDir ()
249+ path := filepath .Join (root , "example.txt" )
250+ if err := os .WriteFile (path , []byte ("original" ), 0o644 ); err != nil {
251+ t .Fatal (err )
252+ }
253+ result := NewScopedWriteFileTool (root , nil ).Run (context .Background (), map [string ]any {
254+ "path" : path , "content" : "replacement" , "overwrite" : true , key : value ,
255+ })
256+ if result .Status != StatusError {
257+ t .Fatalf ("accepted %s=%v" , key , value )
258+ }
259+ got , err := os .ReadFile (path )
260+ if err != nil || string (got ) != "original" {
261+ t .Fatalf ("invalid intent changed target: %q, %v" , got , err )
262+ }
263+ }
264+ }
265+ }
266+
184267func TestWriteFileToolEncodingPreservationKeepsWholeFileObservation (t * testing.T ) {
185268 t .Setenv ("ZERO_FORMAT_ON_WRITE" , "" )
186269 tests := []struct {
@@ -233,6 +316,7 @@ func TestWriteFileToolNewFileRetainsCallerBytes(t *testing.T) {
233316 want := []byte ("\xef \xbb \xbf new\r \n text\n " )
234317 result := NewScopedWriteFileTool (root , nil ).Run (context .Background (), map [string ]any {
235318 "path" : "example.txt" , "content" : string (want ),
319+ "bom" : "remove" , "line_endings" : "lf" ,
236320 })
237321 if result .Status != StatusOK {
238322 t .Fatalf ("write failed: %s" , result .Output )
0 commit comments