| 1587 | } |
| 1588 | |
| 1589 | func TestEditFiles_FileResults(t *testing.T) { |
| 1590 | t.Parallel() |
| 1591 | |
| 1592 | tmpdir := os.TempDir() |
| 1593 | logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug) |
| 1594 | |
| 1595 | t.Run("DiffRequestedSingleFile", func(t *testing.T) { |
| 1596 | t.Parallel() |
| 1597 | |
| 1598 | fs := afero.NewMemMapFs() |
| 1599 | api := agentfiles.NewAPI(logger, fs, nil) |
| 1600 | path := filepath.Join(tmpdir, "diff-single") |
| 1601 | require.NoError(t, afero.WriteFile(fs, path, []byte("hello world\n"), 0o644)) |
| 1602 | |
| 1603 | resp := runEditFiles(t, api, workspacesdk.FileEditRequest{ |
| 1604 | IncludeDiff: true, |
| 1605 | Files: []workspacesdk.FileEdits{ |
| 1606 | { |
| 1607 | Path: path, |
| 1608 | Edits: []workspacesdk.FileEdit{ |
| 1609 | {Search: "hello", Replace: "HELLO"}, |
| 1610 | }, |
| 1611 | }, |
| 1612 | }, |
| 1613 | }) |
| 1614 | require.Len(t, resp.Files, 1) |
| 1615 | require.Equal(t, path, resp.Files[0].Path) |
| 1616 | // udiff.Unified emits "--- <path>\n+++ <path>\n@@ ...". |
| 1617 | require.Contains(t, resp.Files[0].Diff, "--- "+path+"\n") |
| 1618 | require.Contains(t, resp.Files[0].Diff, "+++ "+path+"\n") |
| 1619 | require.Contains(t, resp.Files[0].Diff, "-hello world") |
| 1620 | require.Contains(t, resp.Files[0].Diff, "+HELLO world") |
| 1621 | }) |
| 1622 | |
| 1623 | t.Run("DiffRequestedNoOpEdit", func(t *testing.T) { |
| 1624 | t.Parallel() |
| 1625 | |
| 1626 | fs := afero.NewMemMapFs() |
| 1627 | api := agentfiles.NewAPI(logger, fs, nil) |
| 1628 | path := filepath.Join(tmpdir, "diff-noop") |
| 1629 | require.NoError(t, afero.WriteFile(fs, path, []byte("same\n"), 0o644)) |
| 1630 | |
| 1631 | resp := runEditFiles(t, api, workspacesdk.FileEditRequest{ |
| 1632 | IncludeDiff: true, |
| 1633 | Files: []workspacesdk.FileEdits{ |
| 1634 | { |
| 1635 | Path: path, |
| 1636 | Edits: []workspacesdk.FileEdit{ |
| 1637 | // Replace with identical text (no-op). |
| 1638 | {Search: "same", Replace: "same"}, |
| 1639 | }, |
| 1640 | }, |
| 1641 | }, |
| 1642 | }) |
| 1643 | require.Len(t, resp.Files, 1) |
| 1644 | require.Equal(t, path, resp.Files[0].Path) |
| 1645 | require.Empty(t, resp.Files[0].Diff, "no-op edit produces empty diff") |
| 1646 | }) |