gunk-dev/the-valley

branchname_test.go

source601b054bc28emain's published tip history
3.3 KiB
1package cmd
2
3import (
4 "context"
5 "strings"
6 "testing"
7)
8
9func TestDefaultBranchName(t *testing.T) {
10 const id = "20261011-1843-bd92f081"
11 cases := []struct {
12 name, id, issueTitle, prompt, want string
13 }{
14 {
15 name: "heading with repo prefix, cut at a word",
16 prompt: "# site: the operator guest starts with valley-host\n\nBody text.\n",
17 want: "the-operator-guest-starts-with-valley-bd92f081",
18 },
19 {
20 name: "repo prefix is case-insensitive",
21 prompt: "# Site: Fix the boot order",
22 want: "fix-the-boot-order-bd92f081",
23 },
24 {
25 name: "repo name later in the title stays",
26 prompt: "# Teach site: the boot order",
27 want: "teach-site-the-boot-order-bd92f081",
28 },
29 {
30 name: "first heading beats an earlier first line",
31 prompt: "Some context first.\n\n# Add a retry budget\n\n# Second heading\n",
32 want: "add-a-retry-budget-bd92f081",
33 },
34 {
35 name: "only level-1 headings count",
36 prompt: "Fix the flaky watcher test.\n\n## Why\nIt flakes.\n",
37 want: "fix-the-flaky-watcher-test-bd92f081",
38 },
39 {
40 name: "a heading inside a code fence is not the title",
41 prompt: "Run the installer:\n```sh\n# install deps\nmake\n```\n~~~\n# also not this\n~~~\n",
42 want: "run-the-installer-bd92f081",
43 },
44 {
45 name: "a heading after a closed fence counts",
46 prompt: "```\n# comment\n```\n# Real title\n",
47 want: "real-title-bd92f081",
48 },
49 {
50 name: "first non-empty line, CRLF",
51 prompt: "\r\n \r\n Rename the config loader \r\nmore\r\n",
52 want: "rename-the-config-loader-bd92f081",
53 },
54 {
55 name: "#word is not a heading",
56 prompt: "#hashtag sync fix\n",
57 want: "hashtag-sync-fix-bd92f081",
58 },
59 {
60 name: "issue title beats the heading",
61 issueTitle: "site: Dashboard crashes on resize",
62 prompt: "# Something else\n",
63 want: "dashboard-crashes-on-resize-bd92f081",
64 },
65 {
66 name: "issue title without a slug falls through to the heading",
67 issueTitle: "🔥🔥",
68 prompt: "# Put out the fire\n",
69 want: "put-out-the-fire-bd92f081",
70 },
71 {
72 name: "unicode and punctuation collapse to single dashes",
73 prompt: "# Café — naïve résumé: fix \"quotes\" & (parens)!!\n",
74 want: "caf-na-ve-r-sum-fix-quotes-parens-bd92f081",
75 },
76 {
77 name: "cut keeps a word that ends exactly at 40",
78 prompt: "# " + strings.Repeat("a", 40) + " more words",
79 want: strings.Repeat("a", 40) + "-bd92f081",
80 },
81 {
82 name: "a single word over 40 is cut hard",
83 prompt: "# " + strings.Repeat("x", 50),
84 want: strings.Repeat("x", 40) + "-bd92f081",
85 },
86 {
87 name: "no slug anywhere falls back",
88 prompt: "# 日本語のタイトル\n本文\n",
89 want: "agent/" + id,
90 },
91 {
92 name: "repo prefix alone and punctuation fall back",
93 prompt: "site:\n",
94 want: "agent/" + id,
95 },
96 {
97 name: "a name git rejects falls back",
98 id: "20261011-1843-tail.lock",
99 prompt: "# Fix it",
100 want: "agent/20261011-1843-tail.lock",
101 },
102 }
103 for _, tc := range cases {
104 t.Run(tc.name, func(t *testing.T) {
105 runID := tc.id
106 if runID == "" {
107 runID = id
108 }
109 got := defaultBranchName(context.Background(), runID, "site", tc.issueTitle, tc.prompt)
110 if got != tc.want {
111 t.Errorf("defaultBranchName = %q, want %q", got, tc.want)
112 }
113 })
114 }
115}