| 1 | package houserules |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "the-valley/flow/internal/config" |
| 12 | "the-valley/flow/internal/project" |
| 13 | ) |
| 14 | |
| 15 | func runGit(t *testing.T, dir string, args ...string) { |
| 16 | t.Helper() |
| 17 | cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"}, args...)...) |
| 18 | cmd.Dir = dir |
| 19 | if out, err := cmd.CombinedOutput(); err != nil { |
| 20 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // guidanceProject registers a clone whose origin's main holds the house rules |
| 25 | // file; the clone's working tree is then changed to prove it is never read. |
| 26 | func guidanceProject(t *testing.T) (*project.Registry, string) { |
| 27 | t.Helper() |
| 28 | root := t.TempDir() |
| 29 | origin, clone := filepath.Join(root, "origin.git"), filepath.Join(root, "guidance") |
| 30 | runGit(t, root, "init", "--bare", "-b", "main", origin) |
| 31 | runGit(t, root, "clone", origin, clone) |
| 32 | if err := os.MkdirAll(filepath.Join(clone, "guidance"), 0o755); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | file := filepath.Join(clone, "guidance", "house-rules.md") |
| 36 | if err := os.WriteFile(file, []byte("Write what a reader can't infer.\n"), 0o644); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | runGit(t, clone, "add", ".") |
| 40 | runGit(t, clone, "commit", "-m", "rules") |
| 41 | runGit(t, clone, "push", "origin", "HEAD:main") |
| 42 | if err := os.WriteFile(file, []byte("uncommitted taste\n"), 0o644); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | reg := &project.Registry{Projects: map[string]string{}} |
| 46 | if err := reg.Add("guidance", clone); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | return reg, clone |
| 50 | } |
| 51 | |
| 52 | func TestLoadReadsOriginsDefaultBranchWithDigest(t *testing.T) { |
| 53 | reg, _ := guidanceProject(t) |
| 54 | r := Load(context.Background(), &config.HouseRulesConfig{Repo: "guidance", Path: "guidance/house-rules.md"}, reg) |
| 55 | if r.Missing || r.Text != "Write what a reader can't infer." { |
| 56 | t.Fatalf("rules = %+v; want origin's committed text, not the working tree's", r) |
| 57 | } |
| 58 | if len(r.Digest) != 64 || !strings.HasPrefix(r.Source, "guidance:guidance/house-rules.md@") { |
| 59 | t.Errorf("source %q, digest %q", r.Source, r.Digest) |
| 60 | } |
| 61 | if rec := r.Record(); rec == nil || rec.Digest != r.Digest || rec.Source != r.Source { |
| 62 | t.Errorf("record = %+v", rec) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestMissingFileWarnsAndDoesNotBlock(t *testing.T) { |
| 67 | reg, _ := guidanceProject(t) |
| 68 | r := Load(context.Background(), &config.HouseRulesConfig{Repo: "guidance", Path: "nope.md"}, reg) |
| 69 | if !r.Missing || !strings.Contains(r.Warning(), "continuing without them") { |
| 70 | t.Fatalf("rules = %+v", r) |
| 71 | } |
| 72 | if got := r.Append("prompt"); got != "prompt" { |
| 73 | t.Errorf("a missing file changed the prompt: %q", got) |
| 74 | } |
| 75 | if rec := r.Record(); rec == nil || !rec.Missing || rec.Digest != "" { |
| 76 | t.Errorf("record = %+v; want missing", rec) |
| 77 | } |
| 78 | if un := Load(context.Background(), nil, reg); un.Record() != nil || un.Warning() != "" { |
| 79 | t.Errorf("unset rules = %+v", un) |
| 80 | } |
| 81 | if !Load(context.Background(), &config.HouseRulesConfig{Repo: "other", Path: "x"}, reg).Missing { |
| 82 | t.Error("an unregistered project should be missing") |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // TestEveryKindOfRunGetsTheRules appends the rules to each rendered worker |
| 87 | // prompt, with and without an overriding .klaus/prompt.md. |
| 88 | func TestEveryKindOfRunGetsTheRules(t *testing.T) { |
| 89 | custom := t.TempDir() |
| 90 | if err := os.MkdirAll(filepath.Join(custom, ".klaus"), 0o755); err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | if err := os.WriteFile(filepath.Join(custom, ".klaus", "prompt.md"), []byte("Custom for {{.RunID}}.\n"), 0o644); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | r := Rules{Source: "g:r.md@abc", Text: "TASTE-MARKER"} |
| 97 | vars := config.PromptVars{RunID: "r", Branch: "b"} |
| 98 | for name, render := range map[string]func() (string, error){ |
| 99 | "plain": func() (string, error) { return config.RenderPrompt(t.TempDir(), vars) }, |
| 100 | "custom": func() (string, error) { return config.RenderPrompt(custom, vars) }, |
| 101 | "pr": func() (string, error) { |
| 102 | return config.RenderPRFixPrompt(t.TempDir(), config.PromptVars{RunID: "r", PR: "7"}) |
| 103 | }, |
| 104 | "pr-fix-custom": func() (string, error) { |
| 105 | return config.RenderPRFixPrompt(custom, config.PromptVars{RunID: "r", PR: "7"}) |
| 106 | }, |
| 107 | } { |
| 108 | prompt, err := render() |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | got := r.Append(config.WithWorkerRules(prompt)) |
| 113 | if !strings.Contains(got, "TASTE-MARKER") || strings.Index(got, "TASTE-MARKER") < strings.Index(got, config.WorkerRules) { |
| 114 | t.Errorf("%s: house rules missing or ahead of WorkerRules:\n%s", name, got) |
| 115 | } |
| 116 | } |
| 117 | } |