Gim/internal/core/buffer_edit_fuzz_test.go
Hayden Hargreaves 16d1318c22 feat: start TS impl
This is so vibe coded, but in the interest of time, its a bit
necessary. Plus this is a complex problem that I don't have the mental
bandwidth to invest right now.
2026-04-07 10:23:25 -07:00

63 lines
1.6 KiB
Go

package core
import "testing"
func FuzzComputeBufferEditInvariants(f *testing.F) {
f.Add("abc\ndef", "abc\nxyz")
f.Add("", "x")
f.Add("same", "same")
f.Add("hello", "")
f.Fuzz(func(t *testing.T, oldSource, newSource string) {
edit, ok := computeBufferEdit(oldSource, newSource)
if oldSource == newSource {
if ok {
t.Fatalf("expected no edit when strings are equal")
}
return
}
if !ok {
t.Fatalf("expected edit for differing strings")
}
oldBytes := []byte(oldSource)
newBytes := []byte(newSource)
start := int(edit.StartByte)
oldEnd := int(edit.OldEndByte)
newEnd := int(edit.NewEndByte)
if start < 0 || start > len(oldBytes) || start > len(newBytes) {
t.Fatalf("invalid start byte: %d", start)
}
if oldEnd < start || oldEnd > len(oldBytes) {
t.Fatalf("invalid old end byte: %d", oldEnd)
}
if newEnd < start || newEnd > len(newBytes) {
t.Fatalf("invalid new end byte: %d", newEnd)
}
if string(oldBytes[:start]) != string(newBytes[:start]) {
t.Fatalf("prefix before edit start must match")
}
if string(oldBytes[oldEnd:]) != string(newBytes[newEnd:]) {
t.Fatalf("suffix after edit end must match")
}
sp := byteOffsetToPoint(oldBytes, start)
op := byteOffsetToPoint(oldBytes, oldEnd)
np := byteOffsetToPoint(newBytes, newEnd)
if sp != edit.StartPoint {
t.Fatalf("start point mismatch: got %+v want %+v", edit.StartPoint, sp)
}
if op != edit.OldEndPoint {
t.Fatalf("old end point mismatch: got %+v want %+v", edit.OldEndPoint, op)
}
if np != edit.NewEndPoint {
t.Fatalf("new end point mismatch: got %+v want %+v", edit.NewEndPoint, np)
}
})
}