Git designed for AI agents.
package classify
import (
"strings"
"testing"
"github.com/feder-cr/git-ai/proto/go/gitalypb"
)
// Spec: path rules are checked in order GENERATED, TESTS, DOCS, CONFIG; first match wins.
func TestSpecPathRulePrecedence(t *testing.T) {
code := []byte("package a\n")
cases := []struct {
path string
want gitalypb.ReviewFile_ChangeClass
}{
{"pkg/x.pb.go", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"pkg/x_pb2.py", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"web/app.min.js", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"pkg/x_generated.go", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"testdata/x.pb.go", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"pkg/x_test.go", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"pkg/test_mod.py", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"pkg/mod_test.py", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"web/a.spec.ts", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"web/a.test.jsx", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"web/__tests__/a.js", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"pkg/test/helper.go", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"pkg/tests/data.txt", gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"doc/x.go", gitalypb.ReviewFile_CHANGE_CLASS_DOCS},
{"docs/x.toml", gitalypb.ReviewFile_CHANGE_CLASS_DOCS},
{"README.md", gitalypb.ReviewFile_CHANGE_CLASS_DOCS},
{"pkg/x.rst", gitalypb.ReviewFile_CHANGE_CLASS_DOCS},
{"config/app.yml", gitalypb.ReviewFile_CHANGE_CLASS_CONFIG},
{".gitignore", gitalypb.ReviewFile_CHANGE_CLASS_CONFIG},
{"pkg/x.ini", gitalypb.ReviewFile_CHANGE_CLASS_CONFIG},
}
for _, tc := range cases {
t.Run(tc.path, func(t *testing.T) {
got, reason := File(tc.path, code, code)
if got != tc.want {
t.Fatalf("File(%q) class = %v, want %v", tc.path, got, tc.want)
}
if reason == "" {
t.Errorf("File(%q) returned empty reason", tc.path)
}
})
}
}
// Spec: first 20 lines containing "Code generated" + "DO NOT EDIT" or "@generated" → GENERATED.
// PLAN5 "WP44 RULE" 3 (binding): the marker counts only in the OLD version, or in
// the new version of an added file; adding a marker to an existing file is not GENERATED.
func TestSpecGeneratedMarkerWindow(t *testing.T) {
marker := "// Code generated by tool. DO NOT EDIT.\npackage a\n"
filler := strings.Repeat("// filler\n", 20)
cases := []struct {
name string
path string
old string
new string
want gitalypb.ReviewFile_ChangeClass
}{
{"marker only in old version", "pkg/a.go", marker, "package a\n", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
// The marker is ignored, so adding only the header comment is COMMENT_ONLY,
// and adding it together with a logic change is CODE.
{"marker only in new version", "pkg/a.go", "package a\n", marker, gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"marker added with a logic change", "pkg/a.go", "package a\nvar x = 1\n", marker + "var x = 2\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"@generated in first lines", "pkg/a.go", "// @generated\npackage a\n", "// @generated\npackage a\n", gitalypb.ReviewFile_CHANGE_CLASS_GENERATED},
{"marker on line 21 is ignored", "pkg/a.go",
filler + marker + "var x = 1\n",
filler + marker + "var x = 2\n",
gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"Code generated without DO NOT EDIT is not a marker", "pkg/a.go",
"// Code generated by hand.\npackage a\nvar x = 1\n",
"// Code generated by hand.\npackage a\nvar x = 2\n",
gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File(tc.path, []byte(tc.old), []byte(tc.new))
if got != tc.want {
t.Fatalf("File class = %v, want %v (reason %q)", got, tc.want, reason)
}
})
}
}
// Spec: COMMENT_ONLY when token streams with comments and whitespace removed are identical; strings are not comments.
func TestSpecGoCommentOnlyAndStrings(t *testing.T) {
cases := []struct {
name string
old string
new string
want gitalypb.ReviewFile_ChangeClass
}{
{"comment text changed",
"// hello\npackage a\n\nvar x = 1 // trailing\n",
"// goodbye world\npackage a\n\nvar x = 1 /* changed */\n",
gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"whitespace only", "package a\nvar x=1\n", "package a\n\nvar x = 1\n", gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"comment marker inside string changed", `var s = "// a"` + "\n", `var s = "// b"` + "\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"URL inside string changed", `var u = "http://a.example"` + "\n", `var u = "http://b.example"` + "\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"comment marker inside raw string changed", "var r = `// x`\n", "var r = `// y`\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File("pkg/a.go", []byte(tc.old), []byte(tc.new))
if got != tc.want {
t.Fatalf("File class = %v, want %v (reason %q)", got, tc.want, reason)
}
})
}
}
// Spec: Python docstrings count as code; '#' inside strings is not a comment.
func TestSpecPythonDocstringsAndHashInString(t *testing.T) {
cases := []struct {
name string
path string
old string
new string
want gitalypb.ReviewFile_ChangeClass
}{
{"comment changed", "pkg/mod.py", "# one\nx = 1\n", "# two\nx = 1 # three\n", gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"docstring changed", "pkg/mod.py",
"def f():\n \"\"\"Doc one.\"\"\"\n return 1\n",
"def f():\n \"\"\"Doc two.\"\"\"\n return 1\n",
gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"hash inside string changed", "pkg/mod.py", "s = \"a # b\"\n", "s = \"a # c\"\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
// PLAN5 "WP44 RULE" 1 (binding): shell and Ruby are never COMMENT_ONLY.
{"shell comment changed", "run.sh", "# a\necho hi\n", "# b\necho hi\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"ruby comment changed", "src/a.rb", "# a\nx = 1\n", "# b\nx = 1\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File(tc.path, []byte(tc.old), []byte(tc.new))
if got != tc.want {
t.Fatalf("File class = %v, want %v (reason %q)", got, tc.want, reason)
}
})
}
}
// Spec: Java/Rust lexers handle block comments and nested comments. PLAN5 "WP44
// RULE" 1 (binding): C and JavaScript changes are always CODE.
func TestSpecOtherLanguages(t *testing.T) {
cases := []struct {
name string
path string
old string
new string
want gitalypb.ReviewFile_ChangeClass
}{
{"C block comment changed", "src/a.c", "/* a */ int x;\n", "/* b */ int x;\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"C char literal changed", "src/a.c", "char c = '*';\n", "char c = '+';\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"Java block comment changed", "src/Main.java", "class A { /* x */ }\n", "class A { /* y */ }\n", gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"Rust nested block comment changed", "src/lib.rs", "/* a /* b */ c */\nfn f() {}\n", "/* z */\nfn f() {}\n", gitalypb.ReviewFile_CHANGE_CLASS_COMMENT_ONLY},
{"JS template literal changed", "web/a.js", "const s = `a // b`;\n", "const s = `a // c`;\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"JS line comment with URL string kept", "web/a.js", "// a\nconst u = \"http://x\";\n", "// b\nconst u = \"http://x\";\n", gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File(tc.path, []byte(tc.old), []byte(tc.new))
if got != tc.want {
t.Fatalf("File class = %v, want %v (reason %q)", got, tc.want, reason)
}
})
}
}
// Spec: added or deleted files (nil side) are never COMMENT_ONLY; path rules still apply; unknown language → CODE.
func TestSpecAddedDeletedAndUnknownLanguage(t *testing.T) {
cases := []struct {
name string
path string
old []byte
new []byte
want gitalypb.ReviewFile_ChangeClass
}{
{"added code file", "pkg/new.go", nil, []byte("package a\n"), gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"deleted code file", "pkg/old.go", []byte("package a\n"), nil, gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
{"added test file", "pkg/x_test.go", nil, []byte("package a\n"), gitalypb.ReviewFile_CHANGE_CLASS_TESTS},
{"deleted doc file", "docs/x.md", []byte("text\n"), nil, gitalypb.ReviewFile_CHANGE_CLASS_DOCS},
{"unknown language with comment-only change", "pkg/a.lua", []byte("-- a\nx=1\n"), []byte("-- b\nx=1\n"), gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File(tc.path, tc.old, tc.new)
if got != tc.want {
t.Fatalf("File class = %v, want %v (reason %q)", got, tc.want, reason)
}
})
}
}
// Spec: any lexer uncertainty (EOF inside string/comment) → CODE, even when whitespace-only.
func TestSpecLexerUncertaintyIsCode(t *testing.T) {
cases := []struct {
name string
path string
old string
new string
}{
{"EOF inside block comment", "pkg/a.go", "var x = 1 /* open", "var x = 1 /* open"},
{"EOF inside string", "pkg/a.go", "var s = \"abc", "var s = \"abc"},
{"EOF inside python triple-quoted string", "pkg/mod.py", "x = \"\"\"abc", "x = \"\"\"abc"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, reason := File(tc.path, []byte(tc.old), []byte(tc.new))
if got != gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED {
t.Fatalf("File class = %v, want CODE (reason %q)", got, reason)
}
})
}
}
// Spec: binary content → CODE unless a path rule matches.
func TestSpecBinaryContent(t *testing.T) {
binA := []byte{0x00, 0x01, 0xff, 0xfe}
binB := []byte{0x00, 0x01, 0xfe, 0xff}
if got, reason := File("pkg/blob.go", binA, binB); got != gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED {
t.Errorf("binary change in .go file class = %v, want CODE (reason %q)", got, reason)
}
if got, reason := File("pkg/blob.go", binA, binA); got != gitalypb.ReviewFile_CHANGE_CLASS_CODE_UNSPECIFIED {
t.Errorf("identical binary content class = %v, want CODE (reason %q)", got, reason)
}
if got, reason := File("assets/logo.md", binA, binB); got != gitalypb.ReviewFile_CHANGE_CLASS_DOCS {
t.Errorf("binary change under path rule .md class = %v, want DOCS (reason %q)", got, reason)
}
}
// Spec: every result carries a reason; the decision is deterministic and nothing panics on edge inputs.
func TestSpecReasonNonEmptyAndDeterministic(t *testing.T) {
inputs := []struct {
path string
old []byte
new []byte
}{
{"pkg/a.go", []byte("// a\npackage a\n"), []byte("// b\npackage a\n")},
{"pkg/a.go", []byte("package a\n"), []byte("package b\n")},
{"pkg/a.go", []byte{}, []byte{}},
{"pkg/a.go", nil, nil},
{"pkg/a.go", []byte("\"unterminated"), []byte("\"unterminated")},
{"x.unknownext", []byte("abc"), []byte("abd")},
{"", nil, []byte("x")},
}
for _, in := range inputs {
c1, r1 := File(in.path, in.old, in.new)
c2, r2 := File(in.path, in.old, in.new)
if c1 != c2 || r1 != r2 {
t.Errorf("File(%q) not deterministic: (%v,%q) vs (%v,%q)", in.path, c1, r1, c2, r2)
}
if r1 == "" {
t.Errorf("File(%q) returned empty reason for class %v", in.path, c1)
}
}
}