gunk-dev/the-valley

pre_review_test.go

sourcecfedff357a41main's published tip history
3.1 KiB
1package cmd
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestSeveritiesAtOrAbove(t *testing.T) {
11 tests := []struct {
12 level string
13 included []string
14 excluded []string
15 }{
16 {
17 level: "critical",
18 included: []string{"critical"},
19 excluded: []string{"high", "medium", "low"},
20 },
21 {
22 level: "high",
23 included: []string{"high", "critical"},
24 excluded: []string{"medium", "low"},
25 },
26 {
27 level: "medium",
28 included: []string{"medium", "high", "critical"},
29 excluded: []string{"low"},
30 },
31 {
32 level: "low",
33 included: []string{"low", "medium", "high", "critical"},
34 excluded: []string{},
35 },
36 {
37 level: "unknown",
38 included: []string{"critical"},
39 excluded: []string{"high", "medium", "low"},
40 },
41 }
42
43 for _, tt := range tests {
44 t.Run(tt.level, func(t *testing.T) {
45 result := severitiesAtOrAbove(tt.level)
46 for _, s := range tt.included {
47 if !result[s] {
48 t.Errorf("expected %q to be included for level %q", s, tt.level)
49 }
50 }
51 for _, s := range tt.excluded {
52 if result[s] {
53 t.Errorf("expected %q to be excluded for level %q", s, tt.level)
54 }
55 }
56 })
57 }
58}
59
60// TestPreReviewDiffsAgainstOriginsDefaultBranch runs _pre-review in a clone
61// whose local main lags origin's, with a stub reviewer that saves its
62// prompt: the reviewer gets the commit the work adds on origin's main, not
63// the commit that landed since the local main.
64func TestPreReviewDiffsAgainstOriginsDefaultBranch(t *testing.T) {
65 home, repo, bin := t.TempDir(), t.TempDir(), t.TempDir()
66 t.Setenv("HOME", home)
67 t.Setenv("KLAUS_BACKEND", "claude")
68 t.Setenv(runIDEnv, "")
69 t.Setenv(runEventsEnv, "")
70 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
71 write := func(path, content string) {
72 t.Helper()
73 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
74 t.Fatal(err)
75 }
76 if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
77 t.Fatal(err)
78 }
79 }
80 write(filepath.Join(home, ".klaus", "config.json"), `{"backends": {"claude": {"review_backend": "claude"}}, "pre_review": {"prose": {"enabled": false}}}`)
81 prompt := filepath.Join(bin, "prompt")
82 write(filepath.Join(bin, "claude"), "#!/bin/sh\ncat > '"+prompt+"'\necho '{\"findings\": [], \"summary\": \"clean\"}'\n")
83
84 commit := func(name string) string {
85 write(filepath.Join(repo, name), name+"\n")
86 snapshotGit(t, repo, "add", name)
87 snapshotGit(t, repo, "commit", "-qm", name)
88 return snapshotGit(t, repo, "rev-parse", "HEAD")
89 }
90 snapshotGit(t, repo, "init", "-q")
91 base := commit("base.txt")
92 landed := commit("landed.txt")
93 snapshotGit(t, repo, "update-ref", "refs/remotes/origin/main", landed)
94 snapshotGit(t, repo, "checkout", "-qb", "work", "origin/main")
95 snapshotGit(t, repo, "update-ref", "refs/heads/main", base)
96 commit("work.txt")
97
98 rootCmd.SetArgs([]string{"_pre-review", "--dir", repo})
99 if err := rootCmd.Execute(); err != nil {
100 t.Fatal(err)
101 }
102 data, err := os.ReadFile(prompt)
103 if err != nil {
104 t.Fatal(err)
105 }
106 if !strings.Contains(string(data), "+++ b/work.txt") || strings.Contains(string(data), "landed.txt") {
107 t.Errorf("the reviewer did not get the work's own diff:\n%s", data)
108 }
109}