Git designed for AI agents.
package bench
import (
"fmt"
"strings"
"testing"
"github.com/feder-cr/git-ai/tools/graft-agents/protocol"
)
func TestNotSentReason(t *testing.T) {
big := strings.Repeat("x", 3000)
for _, tc := range []struct {
path string
side string
generated bool
want string
}{
{"a.go", "package a\n", false, ""},
{"a.bin", "a\x00b", false, NotSentBinary},
{"big.go", strings.Repeat("a\n", 600), false, NotSentLarge},
{"web/package-lock.json", "{}", false, NotSentLockfile},
{"go.sum", "x\n", false, NotSentLockfile},
{"gen.go", "// Code generated by protoc-gen-go. DO NOT EDIT.\npackage gen\n", false, NotSentGenerated},
{"x.ts", "// @generated\n", false, NotSentGenerated},
{"y.py", "x = 1\n", true, NotSentGenerated},
{"app.min.js", "a\n", false, NotSentMinified},
{"bundle.js", big + "\n", false, NotSentMinified},
{"code.js", strings.Repeat("let a = 1;\n", 400), false, ""},
} {
maxBytes := int64(protocol.DefaultMaxFileBytes)
if tc.path == "big.go" {
maxBytes = 100
}
got := notSentReason(tc.path, [][]byte{[]byte(tc.side)}, maxBytes, tc.generated)
if got != tc.want {
t.Errorf("%s: got %q, want %q", tc.path, got, tc.want)
}
}
}
// TestSampleSeparatesRustFromJava mirrors the mined corpus: few Rust and Python cases, many of the
// others. Rust and Python are taken whole and the rest is split evenly over TS/JS, Go and Java.
func TestSampleSeparatesRustFromJava(t *testing.T) {
var cases []Case
add := func(n int, family, path string) {
for i := 0; i < n; i++ {
cases = append(cases, Case{ID: fmt.Sprintf("%s-%s-%d", family, path, i), Family: family, Path: path})
}
}
add(983, FamilyTSJS, "a.ts")
add(96, FamilyPython, "a.py")
add(457, FamilyGo, "a.go")
add(22, FamilyRustJava, "a.rs")
add(1625, FamilyRustJava, "A.java")
counts := map[string]int{}
for _, c := range Sample(cases, 600, 1) {
counts[LanguageOf(c.Path)]++
}
if counts["rust"] != 22 || counts["python"] != 96 {
t.Fatalf("counts %v: Rust and Python must be taken whole", counts)
}
rest := 600 - 22 - 96
for _, l := range []string{"typescript", "go", "java"} {
if counts[l] < rest/3 || counts[l] > rest/3+1 {
t.Fatalf("counts %v: the rest must be split evenly (%d each)", counts, rest/3)
}
}
}
func TestReportByLanguageAndKind(t *testing.T) {
rep := NewReport("test", []Result{
{Family: FamilyRustJava, Language: "rust", Kind: protocol.KindContent, Outcome: OutcomeCorrect},
{Family: FamilyRustJava, Language: "java", Kind: protocol.KindContent, Outcome: OutcomeWrong},
{Family: FamilyRustJava, Language: "java", Kind: protocol.KindModifyDelete, Outcome: OutcomeAbstained},
})
if rep.Languages["java"].Cases != 2 || rep.Kinds[protocol.KindContent].ErrorRate() != 0.5 || rep.Kinds[protocol.KindModifyDelete].Abstained != 1 {
t.Fatalf("languages %+v, kinds %+v", rep.Languages, rep.Kinds)
}
var b strings.Builder
if err := rep.WriteMarkdown(&b); err != nil {
t.Fatal(err)
}
for _, want := range []string{"| language | cases", "| rust (low n) | 1 |", "| kind | cases", "| content (low n) | 2 | 1 (0) | 1 |", "| modify-delete (low n) | 1 |", "By language: java 0 of 1 accepted, rust 0 of 1 accepted."} {
if !strings.Contains(b.String(), want) {
t.Fatalf("markdown lacks %q:\n%s", want, b.String())
}
}
}
func TestSampleIsStratifiedAndStable(t *testing.T) {
var cases []Case
for i := 0; i < 30; i++ {
family := []string{FamilyGo, FamilyPython, FamilyTSJS}[i%3]
cases = append(cases, Case{ID: string(rune('a'+i)) + family, Family: family})
}
a, b := Sample(cases, 9, 7), Sample(cases, 9, 7)
counts := map[string]int{}
for i := range a {
if a[i].ID != b[i].ID {
t.Fatalf("the sample is not stable: %v vs %v", a, b)
}
counts[a[i].Family]++
}
if counts[FamilyGo] != 3 || counts[FamilyPython] != 3 || counts[FamilyTSJS] != 3 {
t.Fatalf("the sample is not stratified: %v", counts)
}
if c := Sample(cases, 9, 8); c[0].ID == a[0].ID && c[1].ID == a[1].ID && c[2].ID == a[2].ID {
t.Fatalf("another seed gave the same sample")
}
if len(Sample(cases, 0, 1)) != 30 {
t.Fatal("n <= 0 must take every case")
}
}
func TestReportCounts(t *testing.T) {
rep := NewReport("test", []Result{
{Family: FamilyGo, Outcome: OutcomeCorrect, Exact: true},
{Family: FamilyGo, Outcome: OutcomeWrong, BlankLinesOnly: true, SyntaxUnchecked: true},
{Family: FamilyPython, Outcome: OutcomeRejected},
{Family: FamilyPython, Outcome: OutcomeNotSent, Reason: NotSentGenerated},
})
if rep.Total.BlankLinesOnly != 1 || rep.Families[FamilyGo].SyntaxUnchecked != 1 {
t.Fatalf("counts %+v", rep.Families)
}
if rep.Total.Cases != 4 || rep.Total.Accepted() != 2 || rep.Total.ErrorRate() != 0.5 {
t.Fatalf("totals %+v", rep.Total)
}
if rep.Families[FamilyGo].Correct != 1 || rep.NotSent[NotSentGenerated] != 1 {
t.Fatalf("families %+v, not sent %+v", rep.Families, rep.NotSent)
}
var b strings.Builder
if err := rep.WriteMarkdown(&b); err != nil {
t.Fatal(err)
}
if !strings.Contains(b.String(), "| total | 4 | 1 (1) | 1 | 1 | 1 |") || !strings.Contains(b.String(), "blank lines only (counted as wrong): 1 (go 1, python 0)") ||
!strings.Contains(b.String(), "does not parse with the grammar (a dialect such as Flow, or code the grammar does not cover): 1 of 2.") {
t.Fatalf("markdown:\n%s", b.String())
}
}
// TestCommittedCorpusLoads checks the corpus file of the benchmark: permissive licenses, no
// duplicate, a pinned commit for every repository.
func TestCommittedCorpusLoads(t *testing.T) {
c, err := LoadCorpus("corpus.json")
if err != nil {
t.Fatal(err)
}
for _, r := range c.Repos {
if len(r.Commit) != 40 {
t.Errorf("%s: commit %q is not a full object ID", r.Name, r.Commit)
}
}
}
func TestBlankLinesOnly(t *testing.T) {
human := []byte("a\n\nb\n c\n")
for result, want := range map[string]bool{
"a\nb\n c\n": true, // a blank line less
"a\n\n\nb\n\n c\n": true, // blank lines more
"a\nb\nc\n": false, // indentation counts
"a\nb\n d\n": false,
} {
if got := blankLinesOnly([]byte(result), human); got != want {
t.Errorf("blankLinesOnly(%q) = %v, want %v", result, got, want)
}
}
}
// TestReportCountsTheAnswersTheAgreeGateHolds: an accepted answer with a Hold is counted apart; what
// would land through verify is the rest, and its wrong rate is the one the gate is judged by.
func TestReportCountsTheAnswersTheAgreeGateHolds(t *testing.T) {
rep := NewReport("agree", []Result{
{ID: "1", Family: "go", Outcome: OutcomeCorrect},
{ID: "2", Family: "go", Outcome: OutcomeWrong},
{ID: "3", Family: "go", Outcome: OutcomeWrong, Hold: "the second model disagreed"},
{ID: "4", Family: "go", Outcome: OutcomeCorrect, Hold: "the second model disagreed"},
{ID: "5", Family: "go", Outcome: OutcomeRejected, Hold: "never held: not accepted"},
})
if got := rep.Total; got.Held != 2 || got.HeldWrong != 1 || got.Accepted() != 4 {
t.Fatalf("total = %+v", got)
}
var b strings.Builder
if err := rep.WriteMarkdown(&b); err != nil {
t.Fatal(err)
}
want := "agree_with gate: 4 accepted answers, 2 held back as proposals (the second model did not agree; 1 of them wrong), 2 would land through verify (1 wrong, 50.0%)."
if !strings.Contains(b.String(), want) {
t.Fatalf("report lacks the gate line:\n%s", b.String())
}
}