gunk-dev/the-valley

prompt_diff_test.go

source68d12192bbd3main's published tip history
3.6 KiB
1package review
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9 "unicode/utf8"
10
11 "the-valley/flow/internal/git"
12)
13
14func TestPromptDiff(t *testing.T) {
15 const truncated = "\n\n[... diff truncated due to size ...]"
16 readme := "diff --git a/README.md b/README.md\nindex 1111111..2222222 100644\n--- a/README.md\n+++ b/README.md\n@@ -1 +1,2 @@\n # widget\n+Deploy with care: déjà vu.\n"
17 code := "diff --git a/main.go b/main.go\nindex 4444444..5555555 100644\n--- a/main.go\n+++ b/main.go\n@@ -1 +1 @@\n-package old\n+package main\n"
18 keyHeader := "diff --git a/key.enc b/key.enc\nnew file mode 100644\nindex 0000000..3333333\n--- /dev/null\n+++ b/key.enc\n"
19 key := keyHeader + "@@ -0,0 +1,2 @@\n+\xff\xfe\x80sealed\n+\x9a\x01\n"
20 badName := "diff --git a/caf\xe9.txt b/caf\xe9.txt\nindex 6666666..7777777 100644\n--- a/caf\xe9.txt\n+++ b/caf\xe9.txt\n@@ -1 +1 @@\n-a\n+b\n"
21 for _, tc := range []struct {
22 name, diff string
23 max int
24 want string
25 }{
26 {"valid text is untouched", readme + code, 1 << 20, readme + code},
27 {"a file that isn't text keeps its header, the others are untouched", readme + key + code, 1 << 20,
28 readme + keyHeader + git.NotText + "\n" + code},
29 {"a path that isn't UTF-8 is marked, its hunks kept", badName, 1 << 20,
30 "diff --git a/caf\uFFFD.txt b/caf\uFFFD.txt\nindex 6666666..7777777 100644\n--- a/caf\uFFFD.txt\n+++ b/caf\uFFFD.txt\n@@ -1 +1 @@\n-a\n+b\n"},
31 {"truncation inside a multi-byte character", "+déjà", 3, "+d" + truncated},
32 {"truncation after sanitizing", key, len(keyHeader) + 4, keyHeader + git.NotText[:4] + truncated},
33 } {
34 t.Run(tc.name, func(t *testing.T) {
35 got := promptDiff(tc.diff, tc.max)
36 if !utf8.ValidString(got) {
37 t.Errorf("promptDiff returned invalid UTF-8: %q", got)
38 }
39 if got != tc.want {
40 t.Errorf("promptDiff:\n got %q\nwant %q", got, tc.want)
41 }
42 })
43 }
44}
45
46// A changed file git treats as text but that isn't UTF-8 reaches the
47// reviewer CLI named, its content left out, and the prompt valid UTF-8.
48func TestReviewDiffSendsValidUTF8(t *testing.T) {
49 dir := t.TempDir()
50 run := func(args ...string) {
51 cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"}, args...)...)
52 cmd.Dir = dir
53 if out, err := cmd.CombinedOutput(); err != nil {
54 t.Fatalf("git %v: %v\n%s", args, err, out)
55 }
56 }
57 write := func(name, content string) {
58 if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
59 t.Fatal(err)
60 }
61 }
62 run("init", "-q", "-b", "main")
63 write("main.go", "package old\n")
64 run("add", "-A")
65 run("commit", "-qm", "base")
66 run("checkout", "-qb", "work")
67 write("main.go", "package main\n")
68 write("key.enc", "\xff\xfe\x80sealed\n\x9a\x01\n") // no NUL byte, so git diffs it as text
69 run("add", "-A")
70 run("commit", "-qm", "change")
71
72 bin := t.TempDir()
73 stdin := filepath.Join(bin, "stdin")
74 if err := os.WriteFile(filepath.Join(bin, "claude"), []byte("#!/bin/sh\ncat > '"+stdin+"'\necho '{\"findings\":[],\"summary\":\"clean\"}'\n"), 0o755); err != nil {
75 t.Fatal(err)
76 }
77 t.Setenv("PATH", bin+":"+os.Getenv("PATH"))
78
79 if _, err := ReviewDiff(dir, ReviewConfig{Backend: "claude"}, "main"); err != nil {
80 t.Fatal(err)
81 }
82 data, err := os.ReadFile(stdin)
83 if err != nil {
84 t.Fatal(err)
85 }
86 prompt := string(data)
87 if !utf8.ValidString(prompt) {
88 t.Fatalf("the reviewer got invalid UTF-8:\n%q", prompt)
89 }
90 for _, want := range []string{"+++ b/key.enc\n" + git.NotText + "\n", "+package main"} {
91 if !strings.Contains(prompt, want) {
92 t.Errorf("the reviewer's prompt lacks %q:\n%s", want, prompt)
93 }
94 }
95}