58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package pierre
|
|
|
|
import (
|
|
"github.com/google/go-cmp/cmp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplitDiffIntoChunks_SmallDiff(t *testing.T) {
|
|
diff := "diff --git a/file1.txt b/file1.txt\n+added line\n"
|
|
max := 1000
|
|
chunks := splitDiffIntoChunks([]byte(diff), max)
|
|
if got, want := len(chunks), 1; got != want {
|
|
t.Fatalf("expected %d chunk, got %d", want, got)
|
|
}
|
|
if diff != chunks[0] {
|
|
t.Fatalf("chunk content changed: %s", diff)
|
|
}
|
|
}
|
|
|
|
func TestSplitDiffIntoChunks_MultipleFiles(t *testing.T) {
|
|
diff := "diff --git a/file1.txt b/file1.txt\n+added line 1\n" +
|
|
"diff --git a/file2.txt b/file2.txt\n+added line 2\n"
|
|
max := 50 // each file diff is less than this, but total exceeds
|
|
chunks := splitDiffIntoChunks([]byte(diff), max)
|
|
if got, want := len(chunks), 2; got != want {
|
|
t.Fatalf("expected %d chunks, got %d", want, got)
|
|
}
|
|
// Ensure each chunk starts with the proper file header
|
|
if !strings.HasPrefix(chunks[0], "diff --git a/file1.txt") {
|
|
t.Fatalf("first chunk does not contain file1 header: %s", chunks[0])
|
|
}
|
|
if !strings.HasPrefix(chunks[1], "diff --git a/file2.txt") {
|
|
t.Fatalf("second chunk does not contain file2 header: %s", chunks[1])
|
|
}
|
|
}
|
|
|
|
func TestSplitDiffIntoChunks_LargeSingleFile(t *testing.T) {
|
|
// Create a diff that exceeds maxSize by repeating a line
|
|
line := "+very long added line that will be repeated many times to exceed the chunk size\n"
|
|
diff := "diff --git a/large.txt b/large.txt\n" + strings.Repeat(line, 200)
|
|
max := 500 // small limit
|
|
chunks := splitDiffIntoChunks([]byte(diff), max)
|
|
// Ensure no chunk exceeds max size
|
|
for i, c := range chunks {
|
|
if len(c) > max {
|
|
t.Fatalf("chunk %d exceeds max size: %d > %d", i, len(c), max)
|
|
}
|
|
}
|
|
// Recombine chunks and compare to original (ignoring possible split boundaries)
|
|
recombined := strings.Join(chunks, "")
|
|
if diff != recombined {
|
|
if d := cmp.Diff(diff, recombined); d != "" {
|
|
t.Fatalf("recombined diff differs:\n%s", d)
|
|
}
|
|
}
|
|
}
|