Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · metadata/number_test.go

Line attribution on main · 213 line(s)

e05f1c48
Keenan Nemetz
13d ago
1 package metadata
2
3 import (
4 "testing"
5 "time"
6
d37d301f
Keenan Nemetz
17h ago
7 "rodent.software/vole/core/git"
e05f1c48
Keenan Nemetz
13d ago
8 )
9
10 const (
11 numIssueA = "01M5JR2JK29KKMAA1T9MDNTPBP"
12 numIssueB = "01M5N1PRC4GJPZNEEG8PR8J4D6"
13 numIssueC = "01M5Z58NQARS4QCR9K4EWFNKNC"
14 numRequestA = "01M5XCHRQFT0FTM0CJYRYWVD3V"
15 numRequestB = "01M5XCHYYYWCWZPFDWC8RS3X6X"
16 )
17
18 // One sequence covers both kinds, and the order is the order the commits landed.
19 func TestRankAdditionsNumbersBothKinds(t *testing.T) {
20 t.Parallel()
21 n := RankAdditions([][]string{
22 {"issues/" + numIssueA + "/issue.md"},
b492b557
Keenan Nemetz
3d ago
23 {"changes/" + numRequestA + "/change.md"},
e05f1c48
Keenan Nemetz
13d ago
24 {"issues/" + numIssueB + "/issue.md"},
25 })
26 for want, id := range map[int]string{1: numIssueA, 2: numRequestA, 3: numIssueB} {
27 got, _, ok := n.At(want)
28 if !ok || got != id {
29 t.Fatalf("#%d = %q (ok=%v), want %q", want, got, ok, id)
30 }
31 }
32 if kind := n.Kind(numRequestA); kind != KindRequest {
33 t.Fatalf("kind = %q, want %q", kind, KindRequest)
34 }
35 if kind := n.Kind(numIssueA); kind != KindIssue {
36 t.Fatalf("kind = %q, want %q", kind, KindIssue)
37 }
38 if n.Len() != 3 {
39 t.Fatalf("len = %d, want 3", n.Len())
40 }
41 }
42
43 // A bulk import is one commit holding several entities. Their ULIDs order them,
44 // because a ULID starts with its timestamp, so the result does not depend on the
45 // order git happens to print paths in.
46 func TestRankAdditionsOrdersABatchByULID(t *testing.T) {
47 t.Parallel()
48 batch := []string{
b492b557
Keenan Nemetz
3d ago
49 "changes/" + numRequestB + "/change.md",
e05f1c48
Keenan Nemetz
13d ago
50 "issues/" + numIssueC + "/issue.md",
51 "issues/" + numIssueA + "/issue.md",
52 }
53 n := RankAdditions([][]string{batch})
54 want := []string{numIssueA, numRequestB, numIssueC} // 01M5JR… < 01M5XC… < 01M5Z5…
55 for i, id := range want {
56 if got, _, ok := n.At(i + 1); !ok || got != id {
57 t.Fatalf("#%d = %q (ok=%v), want %q", i+1, got, ok, id)
58 }
59 }
60 }
61
62 // Only the file that creates the entity takes a number. A comment, an approval, or
63 // an edit in the same commit must not consume one or renumber anything.
64 func TestRankAdditionsIgnoresNonCreationPaths(t *testing.T) {
65 t.Parallel()
66 n := RankAdditions([][]string{
67 {"issues/" + numIssueA + "/issue.md"},
68 {
69 "issues/" + numIssueA + "/comments/" + numIssueB + ".md",
b492b557
Keenan Nemetz
3d ago
70 "changes/" + numRequestA + "/approvals/keenan.md",
e05f1c48
Keenan Nemetz
13d ago
71 "runs/" + numIssueC + "/1/record.md",
72 },
73 {"issues/" + numIssueA + "/issue.md"}, // an edit is not an addition, but a bug would look like one
74 })
75 if n.Len() != 1 {
76 t.Fatalf("len = %d, want 1", n.Len())
77 }
78 if got, _ := n.Of(numIssueA); got != 1 {
79 t.Fatalf("#%d for the issue, want 1", got)
80 }
81 if _, ok := n.Of(numIssueB); ok {
82 t.Fatal("a comment id was numbered")
83 }
84 }
85
86 // A path that is deleted and re-added keeps its first number: `#1` must not come
87 // to mean a different issue because someone restored a file.
88 func TestRankAdditionsKeepsTheFirstNumberForAReAddedPath(t *testing.T) {
89 t.Parallel()
90 n := RankAdditions([][]string{
91 {"issues/" + numIssueA + "/issue.md"},
92 {"issues/" + numIssueB + "/issue.md"},
93 {"issues/" + numIssueA + "/issue.md"},
94 })
95 if got, _ := n.Of(numIssueA); got != 1 {
96 t.Fatalf("#%d for the re-added issue, want 1", got)
97 }
98 if got, _ := n.Of(numIssueB); got != 2 {
99 t.Fatalf("#%d for the later issue, want 2", got)
100 }
101 }
102
103 func TestParseNumber(t *testing.T) {
104 t.Parallel()
105 for _, ok := range []string{"1", "42", "999999999", "#42", " #42 "} {
106 if n, good := ParseNumber(ok); !good || n < 1 {
107 t.Fatalf("ParseNumber(%q) = %d, %v", ok, n, good)
108 }
109 }
110 // A 26-character ULID is never read as an alias, even in the impossible case
111 // of it being all digits: the length alone decides.
112 for _, bad := range []string{"0", "", "007x", "1234567890", "01M5JR2JK29KKMAA1T9MDNTPBP", "-1"} {
113 if _, good := ParseNumber(bad); good {
114 t.Fatalf("ParseNumber(%q) was accepted", bad)
115 }
116 }
117 }
118
119 // The numbers come from history: an edit does not move them, and a deleted entity
120 // keeps the number no one else may take.
121 func TestLoadNumbersFollowsHistory(t *testing.T) {
122 t.Parallel()
123 root := t.TempDir()
124 if err := git.InitBare(root, "main"); err != nil {
125 t.Fatal(err)
126 }
127 g := git.Open(root)
128 tree, err := g.MkTree(nil)
129 if err != nil {
130 t.Fatal(err)
131 }
132 seed, err := g.CommitTree(tree, "", "seed")
133 if err != nil {
134 t.Fatal(err)
135 }
136 if err := g.UpdateRef(Ref, seed); err != nil {
137 t.Fatal(err)
138 }
139
140 now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
141 first, err := NewIssue("First", "", "keenan", nil, nil, now)
142 if err != nil {
143 t.Fatal(err)
144 }
145 if err := CreateIssue(g, first, "keenan"); err != nil {
146 t.Fatal(err)
147 }
148 req, err := NewRequest("Land it", "", "keenan", "main", seed, now.Add(time.Minute))
149 if err != nil {
150 t.Fatal(err)
151 }
152 if err := CreateRequest(g, req, "keenan"); err != nil {
153 t.Fatal(err)
154 }
155 second, err := NewIssue("Second", "", "keenan", nil, nil, now.Add(2*time.Minute))
156 if err != nil {
157 t.Fatal(err)
158 }
159 if err := CreateIssue(g, second, "keenan"); err != nil {
160 t.Fatal(err)
161 }
162
163 nums, err := LoadNumbers(g, Ref)
164 if err != nil {
165 t.Fatal(err)
166 }
167 for want, id := range map[int]string{1: first.ID, 2: req.ID, 3: second.ID} {
168 if got, _, ok := nums.At(want); !ok || got != id {
169 t.Fatalf("#%d = %q (ok=%v), want %q", want, got, ok, id)
170 }
171 }
172
173 // Closing the first issue is an edit, so the numbering must not move.
174 if _, err := SetIssueState(g, first.ID, "closed", "keenan"); err != nil {
175 t.Fatal(err)
176 }
177 nums, err = LoadNumbers(g, Ref)
178 if err != nil {
179 t.Fatal(err)
180 }
181 if got, _ := nums.Of(first.ID); got != 1 {
182 t.Fatalf("#%d after closing, want 1", got)
183 }
184
185 // Deleting the middle entity leaves its number spent.
186 if _, err := git.CommitFiles(g, Ref, func(files map[string][]byte) error {
b492b557
Keenan Nemetz
3d ago
187 delete(files, "changes/"+req.ID+"/change.md")
e05f1c48
Keenan Nemetz
13d ago
188 return nil
189 }, "delete a request", "keenan"); err != nil {
190 t.Fatal(err)
191 }
192 nums, err = LoadNumbers(g, Ref)
193 if err != nil {
194 t.Fatal(err)
195 }
196 if got, _, ok := nums.At(2); !ok || got != req.ID {
197 t.Fatalf("#2 = %q (ok=%v) after deletion, want %q", got, ok, req.ID)
198 }
199 third, err := NewIssue("Third", "", "keenan", nil, nil, now.Add(3*time.Minute))
200 if err != nil {
201 t.Fatal(err)
202 }
203 if err := CreateIssue(g, third, "keenan"); err != nil {
204 t.Fatal(err)
205 }
206 nums, err = LoadNumbers(g, Ref)
207 if err != nil {
208 t.Fatal(err)
209 }
210 if got, _ := nums.Of(third.ID); got != 4 {
211 t.Fatalf("#%d for the issue created after a deletion, want 4", got)
212 }
213 }