Files
pipelines/internal/filediff/git_baseline_test.go
T
alan 70391b17c2 Stage diffs: clean Git files take their baseline from the index blob
On a large Git workspace the 32 MiB retained-text budget was spent
alphabetically on files that never change, so small late edits were
recorded with no_patch. The stage-start snapshot now keeps text only for
files dirty or untracked at start; a clean tracked file keeps its hash and
stage-0 index blob id. When it changes or is deleted, the after capture
reads that blob in one `git cat-file --batch` and uses it only if its
SHA-256 matches the start read, so stage commits/resets do not move the
baseline and filters or assume-unchanged never yield a wrong patch. The
after capture shares unchanged files' representation instead of spending
budget on them again.

FileChange gains no_patch_reason (binary, file_size, budget, lines,
evidence_size, git_status, baseline). Non-Git workspaces keep their
behaviour.
2026-11-04 17:08:01 +00:00

247 lines
9.2 KiB
Go

package filediff
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func fileByPath(t *testing.T, changes *protocol.StageFileChanges, name string) protocol.FileChange {
t.Helper()
for _, f := range changes.Files {
if f.Path == name {
return f
}
}
t.Fatalf("%s not listed: %+v", name, changes)
return protocol.FileChange{}
}
// TestGitCleanFilesDoNotSpendBudget is the large-repository regression: clean
// tracked files that exhaust the text budget alphabetically must not cost a
// small, late edited file its patch. Their start content comes from Git.
func TestGitCleanFilesDoNotSpendBudget(t *testing.T) {
restore := SetSnapshotBudgetForTesting(64)
defer restore()
dir, git := gitRepo(t)
if err := os.MkdirAll(filepath.Join(dir, "tests/server"), 0o700); err != nil {
t.Fatal(err)
}
for i := 0; i < 20; i++ {
put(t, dir, fmt.Sprintf("a-%02d.txt", i), strings.Repeat("filler\n", 4))
}
put(t, dir, "tests/server/test_file_intake_start.py", "def test_one():\n pass\n")
git("add", ".")
git("commit", "-qm", "init")
ctx := context.Background()
before := Capture(ctx, dir)
put(t, dir, "tests/server/test_file_intake_start.py", "def test_one():\n assert True\n")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "complete" || len(changes.Files) != 1 {
t.Fatalf("capture = %+v, want complete with one file", changes)
}
f := changes.Files[0]
if f.NoPatch || f.Truncated || f.NoPatchReason != "" || !strings.Contains(f.Patch, "- pass") || !strings.Contains(f.Patch, "+ assert True") {
t.Fatalf("late edit lost its patch: %+v", f)
}
for name, entry := range before.files {
if entry.data != nil {
t.Fatalf("clean tracked %s retained content", name)
}
}
}
// TestGitStageCommitsMidVisit proves the baseline is the content recorded at
// visit start, not the HEAD the stage moved by committing.
func TestGitStageCommitsMidVisit(t *testing.T) {
dir, git := gitRepo(t)
put(t, dir, "contracts.py", "one\ntwo\n")
put(t, dir, "keep.txt", "keep\n")
git("add", ".")
git("commit", "-qm", "init")
ctx := context.Background()
before := Capture(ctx, dir)
put(t, dir, "contracts.py", "one\ntwo\nthree\n")
put(t, dir, "added.py", "new\n")
git("add", ".")
git("commit", "-qm", "stage commit")
put(t, dir, "contracts.py", "one\nTWO\nthree\n")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "complete" || len(changes.Files) != 2 {
t.Fatalf("capture = %+v", changes)
}
c := fileByPath(t, changes, "contracts.py")
if !strings.Contains(c.Patch, "-two") || !strings.Contains(c.Patch, "+TWO") || !strings.Contains(c.Patch, "+three") {
t.Fatalf("patch not against stage-start content: %q", c.Patch)
}
if a := fileByPath(t, changes, "added.py"); a.Kind != "added" || !strings.Contains(a.Patch, "+new") {
t.Fatalf("committed new file: %+v", a)
}
}
// TestGitDirtyAtStartEditedFurther proves pre-existing dirty content, not
// HEAD, is the baseline, and that only such files retain start content.
func TestGitDirtyAtStartEditedFurther(t *testing.T) {
dir, git := gitRepo(t)
put(t, dir, "dirty.txt", "committed\n")
put(t, dir, "clean.txt", "clean\n")
git("add", ".")
git("commit", "-qm", "init")
put(t, dir, "dirty.txt", "dirty before stage\n")
put(t, dir, "untracked.txt", "untracked before\n")
ctx := context.Background()
before := Capture(ctx, dir)
if before.files["dirty.txt"].data == nil || before.files["untracked.txt"].data == nil || before.files["clean.txt"].data != nil {
t.Fatalf("retained content: dirty=%q untracked=%q clean=%q", before.files["dirty.txt"].data, before.files["untracked.txt"].data, before.files["clean.txt"].data)
}
put(t, dir, "dirty.txt", "stage edit\n")
put(t, dir, "untracked.txt", "untracked after\n")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "complete" || len(changes.Files) != 2 {
t.Fatalf("capture = %+v", changes)
}
d := fileByPath(t, changes, "dirty.txt")
if !strings.Contains(d.Patch, "-dirty before stage") || strings.Contains(d.Patch, "committed") {
t.Fatalf("dirty baseline wrong: %q", d.Patch)
}
if u := fileByPath(t, changes, "untracked.txt"); !strings.Contains(u.Patch, "-untracked before") {
t.Fatalf("untracked baseline wrong: %q", u.Patch)
}
}
// TestGitAddDeleteRename covers added, deleted (worktree and git rm) and
// renamed files whose start content is recovered from Git.
func TestGitAddDeleteRename(t *testing.T) {
dir, git := gitRepo(t)
put(t, dir, "gone.txt", "gone line\n")
put(t, dir, "removed.txt", "removed line\n")
put(t, dir, "old.txt", "moved line\n")
git("add", ".")
git("commit", "-qm", "init")
ctx := context.Background()
before := Capture(ctx, dir)
if err := os.Remove(filepath.Join(dir, "gone.txt")); err != nil {
t.Fatal(err)
}
git("rm", "-q", "removed.txt")
git("mv", "old.txt", "new.txt")
put(t, dir, "fresh.txt", "fresh line\n")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "complete" || len(changes.Files) != 5 {
t.Fatalf("capture = %+v", changes)
}
for name, want := range map[string]string{"gone.txt": "-gone line", "removed.txt": "-removed line", "old.txt": "-moved line"} {
if f := fileByPath(t, changes, name); f.Kind != "deleted" || !strings.Contains(f.Patch, want) || !strings.Contains(f.Patch, "/dev/null") {
t.Fatalf("deleted %s: %+v", name, f)
}
}
for name, want := range map[string]string{"new.txt": "+moved line", "fresh.txt": "+fresh line"} {
if f := fileByPath(t, changes, name); f.Kind != "added" || !strings.Contains(f.Patch, want) {
t.Fatalf("added %s: %+v", name, f)
}
}
}
// TestGitOversizedFileHasReason proves only a file over the per-file cap loses
// its patch, with a reason, while a small edit beside it keeps its patch.
func TestGitOversizedFileHasReason(t *testing.T) {
dir, git := gitRepo(t)
put(t, dir, "big.txt", strings.Repeat("x\n", maxFileBytes/2+1))
put(t, dir, "small.txt", "a\n")
put(t, dir, "image.bin", "\x00one")
git("add", ".")
git("commit", "-qm", "init")
ctx := context.Background()
before := Capture(ctx, dir)
put(t, dir, "big.txt", strings.Repeat("y\n", maxFileBytes/2+1))
put(t, dir, "small.txt", "b\n")
put(t, dir, "image.bin", "\x00two")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "partial" || len(changes.Files) != 3 {
t.Fatalf("capture = %+v", changes)
}
if b := fileByPath(t, changes, "big.txt"); !b.NoPatch || b.Patch != "" || b.NoPatchReason != reasonFileSize {
t.Fatalf("oversized: %+v", b)
}
if s := fileByPath(t, changes, "small.txt"); !strings.Contains(s.Patch, "+b") || s.NoPatchReason != "" {
t.Fatalf("small: %+v", s)
}
if i := fileByPath(t, changes, "image.bin"); !i.Binary || i.NoPatch || i.NoPatchReason != reasonBinary {
t.Fatalf("binary: %+v", i)
}
}
// TestGitUnverifiableBaselineHasNoPatch proves a file Git reports clean whose
// index content differs from disk (assume-unchanged, filters) never yields a
// patch against the wrong baseline.
func TestGitUnverifiableBaselineHasNoPatch(t *testing.T) {
dir, git := gitRepo(t)
put(t, dir, "hidden.txt", "index\n")
git("add", ".")
git("commit", "-qm", "init")
git("update-index", "--assume-unchanged", "hidden.txt")
put(t, dir, "hidden.txt", "disk before\n")
ctx := context.Background()
before := Capture(ctx, dir)
put(t, dir, "hidden.txt", "disk after\n")
changes := Compare(before, Capture(ctx, dir, before))
if len(changes.Files) != 1 {
t.Fatalf("capture = %+v", changes)
}
if f := changes.Files[0]; !f.NoPatch || f.Patch != "" || f.NoPatchReason != reasonBaseline || changes.Status != "partial" {
t.Fatalf("unverifiable baseline: %+v (%s)", f, changes.Status)
}
}
// TestGitLargeRepositoryCaptureTime measures a ~6,500-file, ~43 MB committed
// repository (the shape that exhausted the budget) and checks the late edit
// keeps its patch. Run with -v to see the timing; -short skips it.
func TestGitLargeRepositoryCaptureTime(t *testing.T) {
if testing.Short() {
t.Skip("large fixture")
}
dir, git := gitRepo(t)
line := strings.Repeat("abcdefghij", 7) + "\n" // 71 bytes
body := strings.Repeat(line, 93) // ~6.6 KB per file
for d := 0; d < 65; d++ {
sub := filepath.Join(dir, "src", fmt.Sprintf("pkg%02d", d))
if err := os.MkdirAll(sub, 0o700); err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
put(t, sub, fmt.Sprintf("m%03d.py", i), body)
}
}
if err := os.MkdirAll(filepath.Join(dir, "tests/server"), 0o700); err != nil {
t.Fatal(err)
}
put(t, dir, "tests/server/test_file_intake_start.py", "def test_one():\n pass\n")
git("add", ".")
git("commit", "-qm", "init")
ctx := context.Background()
start := time.Now()
before := Capture(ctx, dir)
beforeTime := time.Since(start)
put(t, dir, "tests/server/test_file_intake_start.py", "def test_one():\n assert True\n")
put(t, filepath.Join(dir, "src/pkg64"), "m099.py", body+"tail\n")
start = time.Now()
after := Capture(ctx, dir, before)
afterTime := time.Since(start)
changes := Compare(before, after)
t.Logf("6,501 files / ~43 MB: before %v, after %v", beforeTime, afterTime)
if changes.Status != "complete" || len(changes.Files) != 2 {
t.Fatalf("capture = %+v", changes)
}
for _, f := range changes.Files {
if f.Patch == "" || f.NoPatch {
t.Fatalf("no patch for %s: %+v", f.Path, f)
}
}
}