| 1 | package review |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "the-valley/flow/internal/git" |
| 12 | ) |
| 13 | |
| 14 | func 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(-1) + "\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(-1)[: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 diffs as text but that isn't UTF-8, or holds a NUL byte |
| 47 | // past where git looks for one, reaches the reviewer CLI named with its |
| 48 | // size, its content left out, and the prompt is text. A textconv the clone |
| 49 | // configures for it is not run. |
| 50 | func TestReviewDiffSendsText(t *testing.T) { |
| 51 | dir := t.TempDir() |
| 52 | run := func(args ...string) { |
| 53 | cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"}, args...)...) |
| 54 | cmd.Dir = dir |
| 55 | if out, err := cmd.CombinedOutput(); err != nil { |
| 56 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 57 | } |
| 58 | } |
| 59 | write := func(name, content string) { |
| 60 | if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | } |
| 64 | run("init", "-q", "-b", "main") |
| 65 | run("config", "diff.sealed.textconv", "false") |
| 66 | write(".gitattributes", "*.enc diff=sealed\n") |
| 67 | write("main.go", "package old\n") |
| 68 | run("add", "-A") |
| 69 | run("commit", "-qm", "base") |
| 70 | run("checkout", "-qb", "work") |
| 71 | write("main.go", "package main\n") |
| 72 | write("key.enc", "\xff\xfe\x80sealed\n\x9a\x01\n") // no NUL byte, so git diffs it as text |
| 73 | write("dump.txt", strings.Repeat("x", 9000)+"\x00\n") // git looks for NUL in the first 8000 bytes only |
| 74 | run("add", "-A") |
| 75 | run("commit", "-qm", "change") |
| 76 | |
| 77 | bin := t.TempDir() |
| 78 | stdin := filepath.Join(bin, "stdin") |
| 79 | if err := os.WriteFile(filepath.Join(bin, "claude"), []byte("#!/bin/sh\ncat > '"+stdin+"'\necho '{\"findings\":[],\"summary\":\"clean\"}'\n"), 0o755); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | t.Setenv("PATH", bin+":"+os.Getenv("PATH")) |
| 83 | |
| 84 | if _, err := ReviewDiff(dir, ReviewConfig{Backend: "claude"}, "main"); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | data, err := os.ReadFile(stdin) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | prompt := string(data) |
| 92 | if !utf8.ValidString(prompt) || strings.IndexByte(prompt, 0) >= 0 { |
| 93 | t.Fatalf("the reviewer got what isn't text:\n%q", prompt) |
| 94 | } |
| 95 | for _, want := range []string{"+++ b/key.enc\n" + git.NotText(13) + "\n", "+++ b/dump.txt\n" + git.NotText(9002) + "\n", "+package main"} { |
| 96 | if !strings.Contains(prompt, want) { |
| 97 | t.Errorf("the reviewer's prompt lacks %q:\n%s", want, prompt) |
| 98 | } |
| 99 | } |
| 100 | } |