69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"view/model"
|
|
)
|
|
|
|
func TestParseCommitPatches(t *testing.T) {
|
|
raw := `diff --git a/main.go b/main.go
|
|
index 111..222 100644
|
|
--- a/main.go
|
|
+++ b/main.go
|
|
@@ -1,2 +1,3 @@
|
|
package main
|
|
+// feature
|
|
`
|
|
got := parseCommitPatches(raw)
|
|
p, ok := got["main.go"]
|
|
if !ok || p.Binary || p.Text == "" {
|
|
t.Fatalf("patch=%#v", got)
|
|
}
|
|
if !strings.Contains(p.Text, "+// feature") {
|
|
t.Fatalf("missing added line: %q", p.Text)
|
|
}
|
|
}
|
|
|
|
func TestParseCommitPatchesBinaryAndDelete(t *testing.T) {
|
|
raw := `diff --git a/logo.png b/logo.png
|
|
index 111..222 100644
|
|
Binary files a/logo.png and b/logo.png differ
|
|
diff --git a/gone.go b/gone.go
|
|
deleted file mode 100644
|
|
index 111..000
|
|
--- a/gone.go
|
|
+++ /dev/null
|
|
@@ -1 +0,0 @@
|
|
-package gone
|
|
`
|
|
got := parseCommitPatches(raw)
|
|
if !got["logo.png"].Binary {
|
|
t.Fatalf("binary not marked: %#v", got["logo.png"])
|
|
}
|
|
if got["gone.go"].Text == "" || got["gone.go"].Binary {
|
|
t.Fatalf("deleted file patch=%#v", got["gone.go"])
|
|
}
|
|
}
|
|
|
|
func TestFormatCommitDiffForAI(t *testing.T) {
|
|
d := model.GitCommitDetail{
|
|
GitCommit: model.GitCommit{Hash: "abc123", Author: "Ann", Email: "a@b.c", Date: "2026-08-19", Message: "add feature", Added: 2, Deleted: 1},
|
|
Files: []model.GitFileChange{
|
|
{Path: "main.go", Status: "modified", Added: 2, Deleted: 1, Patch: "+// feature\n-old"},
|
|
{Path: "logo.png", Status: "modified", Binary: true},
|
|
},
|
|
}
|
|
text := FormatCommitDiffForAI(d, 0)
|
|
if !strings.Contains(text, "add feature") || !strings.Contains(text, "main.go") || !strings.Contains(text, "+// feature") {
|
|
t.Fatalf("unexpected prompt:\n%s", text)
|
|
}
|
|
if !strings.Contains(text, "[binary]") {
|
|
t.Fatal("binary file should be listed")
|
|
}
|
|
tiny := FormatCommitDiffForAI(d, 80)
|
|
if !strings.Contains(tiny, "因过长未纳入") && !strings.Contains(tiny, "add feature") {
|
|
t.Fatalf("truncated prompt:\n%s", tiny)
|
|
}
|
|
}
|