| 1 | package knot |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha1" |
| 5 | "encoding/hex" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "path" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // tree is a graph's files in memory, each named by the SHA-1 of its |
| 15 | // content, as git would name it. |
| 16 | type tree map[string]string |
| 17 | |
| 18 | func oid(content string) string { |
| 19 | sum := sha1.Sum([]byte(content)) |
| 20 | return hex.EncodeToString(sum[:]) |
| 21 | } |
| 22 | |
| 23 | func (t tree) entries() []Entry { |
| 24 | var es []Entry |
| 25 | dirs := map[string]bool{} |
| 26 | for p, content := range t { |
| 27 | es = append(es, Entry{Path: p, Mode: "100644", OID: oid(content)}) |
| 28 | for d := path.Dir(p); d != "."; d = path.Dir(d) { |
| 29 | dirs[d] = true |
| 30 | } |
| 31 | } |
| 32 | for d := range dirs { |
| 33 | es = append(es, Entry{Path: d, Mode: "40000"}) |
| 34 | } |
| 35 | sort.Slice(es, func(i, j int) bool { return es[i].Path < es[j].Path }) |
| 36 | return es |
| 37 | } |
| 38 | |
| 39 | func (t tree) index(tb testing.TB) *Index { |
| 40 | tb.Helper() |
| 41 | idx, err := Layout(t.entries()) |
| 42 | if err != nil { |
| 43 | tb.Fatalf("layout: %v", err) |
| 44 | } |
| 45 | return idx |
| 46 | } |
| 47 | |
| 48 | func (t tree) reader() Reader { |
| 49 | byOID := map[string]string{} |
| 50 | for _, content := range t { |
| 51 | byOID[oid(content)] = content |
| 52 | } |
| 53 | return func(id string, max int) ([]byte, error) { |
| 54 | content, ok := byOID[id] |
| 55 | if !ok { |
| 56 | return nil, errors.New("names an object the push did not bring") |
| 57 | } |
| 58 | if len(content) > max { |
| 59 | return nil, fmt.Errorf("is %d bytes, more than %d", len(content), max) |
| 60 | } |
| 61 | return []byte(content), nil |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func (t tree) with(knots ...*Knot) tree { |
| 66 | out := tree{} |
| 67 | for p, c := range t { |
| 68 | out[p] = c |
| 69 | } |
| 70 | for _, k := range knots { |
| 71 | out[k.Path()] = string(Render(k)) |
| 72 | } |
| 73 | return out |
| 74 | } |
| 75 | |
| 76 | func newKnot(t, title string) *Knot { |
| 77 | ty, _ := TypeNamed(t) |
| 78 | slug := Slug(title) |
| 79 | k := &Knot{Type: t, ID: DeriveID(ty, slug), Slug: slug, Status: ty.Initial(), Title: title, |
| 80 | Created: "2026-10-15", CreatedBy: "operator"} |
| 81 | if t == "outcome" || t == "bug" { |
| 82 | k.BlockedBy = []string{} |
| 83 | } |
| 84 | return k |
| 85 | } |
| 86 | |
| 87 | // ruleOf is the rule err refuses under, or "" for no refusal. |
| 88 | func ruleOf(err error) Rule { |
| 89 | var e *Error |
| 90 | if errors.As(err, &e) { |
| 91 | return e.Rule |
| 92 | } |
| 93 | if err != nil { |
| 94 | return "unruled: " + Rule(err.Error()) |
| 95 | } |
| 96 | return "" |
| 97 | } |
| 98 | |
| 99 | func push(old, new tree, pusher string) error { |
| 100 | p := Push{New: new.indexOrNil(), Read: new.mergedReader(old), Pusher: pusher} |
| 101 | if p.New == nil { |
| 102 | _, err := Layout(new.entries()) |
| 103 | return err |
| 104 | } |
| 105 | if old != nil { |
| 106 | p.Old = old.indexOrNil() |
| 107 | } |
| 108 | return p.Check() |
| 109 | } |
| 110 | |
| 111 | func (t tree) indexOrNil() *Index { |
| 112 | idx, err := Layout(t.entries()) |
| 113 | if err != nil { |
| 114 | return nil |
| 115 | } |
| 116 | return idx |
| 117 | } |
| 118 | |
| 119 | // mergedReader reads objects of both trees, as a repository holding both |
| 120 | // would. |
| 121 | func (t tree) mergedReader(old tree) Reader { |
| 122 | all := tree{} |
| 123 | for p, c := range old { |
| 124 | all["old/"+p] = c |
| 125 | } |
| 126 | for p, c := range t { |
| 127 | all[p] = c |
| 128 | } |
| 129 | return all.reader() |
| 130 | } |
| 131 | |
| 132 | func TestTheCanonicalFormReadsBackExactly(t *testing.T) { |
| 133 | k := newKnot("outcome", "Ship the \"plan\" graph <now> & then") |
| 134 | k.Status, k.Parent, k.Owner, k.Source = "in-progress", "oc-1234567", "alice", "2026-10-15, a design" |
| 135 | k.BlockedBy = []string{"oc-89abcde", "bd-0123456", "app:oc-7654321"} |
| 136 | k.ClaimedBy, k.ClaimedAt = "run-1", "2026-10-15T00:58:00Z" |
| 137 | k.Changes = []string{"https://github.com/a/b/pull/3", "valley:x/kkkkllllmmmmnnnnooooppppqqqqrrrr"} |
| 138 | k.Target = "app:bd-0123456" |
| 139 | k.Body = "A body.\n\n\twith a tab, and ünïcode.\n" |
| 140 | data := Render(k) |
| 141 | want := `--- |
| 142 | type: outcome |
| 143 | id: ` + k.ID + ` |
| 144 | status: in-progress |
| 145 | title: "Ship the \"plan\" graph <now> & then" |
| 146 | created: 2026-10-15 |
| 147 | created_by: "operator" |
| 148 | source: "2026-10-15, a design" |
| 149 | parent: oc-1234567 |
| 150 | owner: "alice" |
| 151 | blocked_by: ["app:oc-7654321", bd-0123456, oc-89abcde] |
| 152 | claimed_by: "run-1" |
| 153 | claimed_at: "2026-10-15T00:58:00Z" |
| 154 | changes: ["https://github.com/a/b/pull/3", "valley:x/kkkkllllmmmmnnnnooooppppqqqqrrrr"] |
| 155 | target: "app:bd-0123456" |
| 156 | --- |
| 157 | |
| 158 | A body. |
| 159 | |
| 160 | with a tab, and ünïcode. |
| 161 | ` |
| 162 | if string(data) != want { |
| 163 | t.Fatalf("Render =\n%s\nwant\n%s", data, want) |
| 164 | } |
| 165 | got, err := ParseKnot(k.Path(), k.Slug, data) |
| 166 | if err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | if string(Render(got)) != want { |
| 170 | t.Errorf("a parsed knot renders as\n%s", Render(got)) |
| 171 | } |
| 172 | |
| 173 | // Each of these says the same thing in YAML, or nearly, and each is |
| 174 | // refused, so there is one way to write the knot. |
| 175 | canonical := string(data) |
| 176 | for _, c := range []struct{ what, from, to string }{ |
| 177 | {"keys out of order", "status: in-progress\ntitle: ", "title: \"x\"\nstatus: in-progress\ntitle: "}, |
| 178 | {"a string written bare", `created_by: "operator"`, "created_by: operator"}, |
| 179 | {"a token written as a string", "status: in-progress", `status: "in-progress"`}, |
| 180 | {"a list out of order", `["app:oc-7654321", bd-0123456, oc-89abcde]`, `[oc-89abcde, bd-0123456, "app:oc-7654321"]`}, |
| 181 | {"an entry twice", `["app:oc-7654321", bd-0123456, oc-89abcde]`, `["app:oc-7654321", bd-0123456, bd-0123456, oc-89abcde]`}, |
| 182 | {"a local id quoted", `, bd-0123456,`, `, "bd-0123456",`}, |
| 183 | {"a list without spaces", `["app:oc-7654321", bd-0123456, oc-89abcde]`, `["app:oc-7654321",bd-0123456,oc-89abcde]`}, |
| 184 | {"a block list", "changes: [\"https://github.com/a/b/pull/3\", \"valley:x/kkkkllllmmmmnnnnooooppppqqqqrrrr\"]", "changes:\n - \"https://github.com/a/b/pull/3\""}, |
| 185 | {"an escape where a character would do", `"Ship the`, `"` + "\\" + `u0053hip the`}, |
| 186 | {"two spaces after a key", "owner: ", "owner: "}, |
| 187 | {"a space at the end of a line", "status: in-progress\n", "status: in-progress \n"}, |
| 188 | {"a YAML comment", "status: in-progress\n", "status: in-progress\n# a comment\n"}, |
| 189 | {"no blank line before the body", "---\n\nA body.", "---\nA body."}, |
| 190 | {"two blank lines before the body", "---\n\nA body.", "---\n\n\nA body."}, |
| 191 | {"a blank line at the end", "ünïcode.\n", "ünïcode.\n\n"}, |
| 192 | {"no newline at the end", "ünïcode.\n", "ünïcode."}, |
| 193 | {"a key named twice", "owner: \"alice\"\n", "owner: \"alice\"\nowner: \"alice\"\n"}, |
| 194 | {"an empty optional key", "owner: \"alice\"\n", "owner: \"\"\n"}, |
| 195 | {"an empty optional list", "target:", "changes: []\ntarget:"}, |
| 196 | } { |
| 197 | variant := strings.Replace(canonical, c.from, c.to, 1) |
| 198 | if variant == canonical { |
| 199 | t.Fatalf("%s: the edit changed nothing", c.what) |
| 200 | } |
| 201 | _, err := ParseKnot(k.Path(), k.Slug, []byte(variant)) |
| 202 | if r := ruleOf(err); r != RuleCanonical && r != RuleFieldShape { |
| 203 | t.Errorf("%s: rule %q, want canonical: %v", c.what, r, err) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestTextHoldsNoControlCharacters(t *testing.T) { |
| 209 | k := newKnot("bug", "A bug") |
| 210 | for _, bad := range []string{"\x1b[31m", "\r", "\x00", "\u0085", "\x7f", "", "\xff"} { |
| 211 | k.Body = "text " + bad + " more\n" |
| 212 | if _, err := ParseKnot(k.Path(), k.Slug, Render(k)); ruleOf(err) != RuleText { |
| 213 | t.Errorf("a body holding %q: %v, want a refusal under %s", bad, err, RuleText) |
| 214 | } |
| 215 | } |
| 216 | for _, good := range []string{"\t", "", "日本"} { |
| 217 | k.Body = "text " + good + " more\n" |
| 218 | if _, err := ParseKnot(k.Path(), k.Slug, Render(k)); err != nil { |
| 219 | t.Errorf("a body holding %q: %v", good, err) |
| 220 | } |
| 221 | } |
| 222 | // A title is a JSON string, which can write a control character as |
| 223 | // an escape: the file holds none, and the title it reads as does. |
| 224 | for _, title := range []string{"two\nlines", "a \x1b[2J clear", "a \u0007 bell", "a\ttab"} { |
| 225 | k.Body, k.Title = "", title |
| 226 | if _, err := ParseKnot(k.Path(), k.Slug, Render(k)); ruleOf(err) != RuleText { |
| 227 | t.Errorf("the title %q: %v", title, err) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestEdgesFormNoCycle(t *testing.T) { |
| 233 | a, b, c := newKnot("outcome", "A"), newKnot("outcome", "B"), newKnot("outcome", "C") |
| 234 | acyclic := tree{}.with(a, b, c) |
| 235 | if err := Validate(acyclic.index(t), acyclic.reader()); err != nil { |
| 236 | t.Fatal(err) |
| 237 | } |
| 238 | link := func(f func(a, b, c *Knot)) tree { |
| 239 | a, b, c := a.Clone(), b.Clone(), c.Clone() |
| 240 | f(a, b, c) |
| 241 | return tree{}.with(a, b, c) |
| 242 | } |
| 243 | for _, x := range []struct { |
| 244 | what string |
| 245 | graph tree |
| 246 | rule Rule |
| 247 | }{ |
| 248 | {"a parent loop", link(func(a, b, c *Knot) { a.Parent, b.Parent, c.Parent = b.ID, c.ID, a.ID }), RuleCycle}, |
| 249 | {"a knot its own parent", link(func(a, b, c *Knot) { a.Parent = a.ID }), RuleCycle}, |
| 250 | {"a blocker loop", link(func(a, b, c *Knot) { |
| 251 | a.BlockedBy, b.BlockedBy = []string{b.ID}, []string{c.ID, "x:oc-1234567"} |
| 252 | c.BlockedBy = []string{a.ID} |
| 253 | }), RuleCycle}, |
| 254 | {"a knot blocking itself", link(func(a, b, c *Knot) { b.BlockedBy = []string{b.ID} }), RuleCycle}, |
| 255 | // A milestone waiting on its own work item is no cycle: the two |
| 256 | // kinds of edge are checked apart. |
| 257 | {"a parent waiting on its child", link(func(a, b, c *Knot) { b.Parent = a.ID; a.BlockedBy = []string{b.ID} }), ""}, |
| 258 | {"a diamond", link(func(a, b, c *Knot) { a.BlockedBy = []string{b.ID, c.ID}; b.BlockedBy = []string{c.ID} }), ""}, |
| 259 | } { |
| 260 | err := Validate(x.graph.index(t), x.graph.reader()) |
| 261 | if ruleOf(err) != x.rule { |
| 262 | t.Errorf("%s: %v, want rule %q", x.what, err, x.rule) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // A push is checked from what it changes, and that finds a cycle the |
| 267 | // push closes even when the knot it changes is not on the loop's |
| 268 | // first edge. |
| 269 | before := link(func(a, b, c *Knot) { a.BlockedBy = []string{b.ID}; b.BlockedBy = []string{c.ID} }) |
| 270 | after := link(func(a, b, c *Knot) { |
| 271 | a.BlockedBy = []string{b.ID} |
| 272 | b.BlockedBy = []string{c.ID} |
| 273 | c.BlockedBy = []string{a.ID} |
| 274 | }) |
| 275 | if err := push(before, after, "operator"); ruleOf(err) != RuleCycle { |
| 276 | t.Errorf("a push closing a loop: %v", err) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestWhatAPushMayChange(t *testing.T) { |
| 281 | a, b := newKnot("outcome", "A"), newKnot("idea", "B") |
| 282 | comment := CommentsDirectory + "/" + a.ID + "/20261015T005800Z-0123456789abcdef.md" |
| 283 | before := tree{}.with(a, b) |
| 284 | before[comment] = string(RenderComment(&Comment{Author: "operator", Body: "Noted.\n"})) |
| 285 | if err := push(nil, before, "operator"); err != nil { |
| 286 | t.Fatalf("the create: %v", err) |
| 287 | } |
| 288 | if err := push(nil, before, "alice"); ruleOf(err) != RuleProvenance { |
| 289 | t.Errorf("a create naming another creator: %v", err) |
| 290 | } |
| 291 | change := func(f func(t tree)) tree { |
| 292 | next := tree{} |
| 293 | for p, c := range before { |
| 294 | next[p] = c |
| 295 | } |
| 296 | f(next) |
| 297 | return next |
| 298 | } |
| 299 | edited := func(f func(k *Knot)) func(t tree) { |
| 300 | return func(t tree) { |
| 301 | k := a.Clone() |
| 302 | f(k) |
| 303 | delete(t, a.Path()) |
| 304 | t[k.Path()] = string(Render(k)) |
| 305 | } |
| 306 | } |
| 307 | for _, x := range []struct { |
| 308 | what string |
| 309 | after tree |
| 310 | pusher string |
| 311 | rule Rule |
| 312 | }{ |
| 313 | {"a status change by anyone", change(edited(func(k *Knot) { k.Status = "done" })), "alice", ""}, |
| 314 | {"a new knot by its creator", change(func(t tree) { k := newKnot("bug", "New"); k.CreatedBy = "alice"; t[k.Path()] = string(Render(k)) }), "alice", ""}, |
| 315 | {"a new knot naming another creator", change(func(t tree) { k := newKnot("bug", "New"); t[k.Path()] = string(Render(k)) }), "alice", RuleProvenance}, |
| 316 | {"a comment by its author", change(func(t tree) { |
| 317 | t[CommentsDirectory+"/"+b.ID+"/20261016T000000Z-0123456789abcdef.md"] = string(RenderComment(&Comment{Author: "alice", Body: "Yes.\n"})) |
| 318 | }), "alice", ""}, |
| 319 | {"a comment naming another author", change(func(t tree) { |
| 320 | t[CommentsDirectory+"/"+b.ID+"/20261016T000000Z-0123456789abcdef.md"] = string(RenderComment(&Comment{Author: "operator", Body: "Yes.\n"})) |
| 321 | }), "alice", RuleProvenance}, |
| 322 | {"a comment stamped with no time", change(func(t tree) { |
| 323 | t[CommentsDirectory+"/"+b.ID+"/20261315T000000Z-0123456789abcdef.md"] = string(RenderComment(&Comment{Author: "operator", Body: "Yes.\n"})) |
| 324 | }), "operator", RuleLayout}, |
| 325 | {"a comment on no knot", change(func(t tree) { |
| 326 | t[CommentsDirectory+"/oc-0000000/20261016T000000Z-0123456789abcdef.md"] = string(RenderComment(&Comment{Author: "operator", Body: "Yes.\n"})) |
| 327 | }), "operator", RuleEdgeResolves}, |
| 328 | {"a changed comment", change(func(t tree) { t[comment] = string(RenderComment(&Comment{Author: "operator", Body: "Unsaid.\n"})) }), "operator", RuleTransition}, |
| 329 | {"a deleted comment", change(func(t tree) { delete(t, comment) }), "operator", RuleTransition}, |
| 330 | {"a deleted knot", change(func(t tree) { delete(t, b.Path()) }), "operator", RuleTransition}, |
| 331 | {"a moved knot", change(func(t tree) { |
| 332 | k := b.Clone() |
| 333 | k.Slug = "b-2" |
| 334 | k.ID = DeriveID(k.TypeOf(), k.Slug) |
| 335 | delete(t, b.Path()) |
| 336 | t[k.Path()] = string(Render(k)) |
| 337 | }), "operator", RuleTransition}, |
| 338 | {"a changed created_by", change(edited(func(k *Knot) { k.CreatedBy = "alice" })), "alice", RuleTransition}, |
| 339 | {"a changed created date", change(edited(func(k *Knot) { k.Created = "2026-10-16" })), "operator", RuleTransition}, |
| 340 | {"a changed type", change(func(t tree) { |
| 341 | k := a.Clone() |
| 342 | k.Type, k.BlockedBy = "bug", []string{} |
| 343 | t[a.Path()] = string(Render(k)) |
| 344 | }), "operator", RuleIDShape}, |
| 345 | {"a changed id", change(func(t tree) { k := a.Clone(); k.ID = "oc-0000000"; t[a.Path()] = string(Render(k)) }), "operator", RuleFilename}, |
| 346 | {"an edge to no knot", change(edited(func(k *Knot) { k.BlockedBy = []string{"oc-0000000"} })), "operator", RuleEdgeResolves}, |
| 347 | {"a parent that is an idea", change(edited(func(k *Knot) { k.Parent = b.ID })), "operator", RuleParentOutcome}, |
| 348 | {"a claim with no time", change(edited(func(k *Knot) { k.ClaimedBy = "run" })), "operator", RuleClaim}, |
| 349 | } { |
| 350 | if err := push(before, x.after, x.pusher); ruleOf(err) != x.rule { |
| 351 | t.Errorf("%s: %v, want rule %q", x.what, err, x.rule) |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // A listing is read as git writes a tree: every directory is an entry of |
| 357 | // its own, in the mode 40000 exactly. A tree written by hand can name a |
| 358 | // file ideas/<id>-<slug>.md at its root, which lists no ideas directory, |
| 359 | // or a directory in the mode 040000, which git reads as a tree and never |
| 360 | // writes. |
| 361 | func TestALayoutListsEveryDirectoryAsGitWritesIt(t *testing.T) { |
| 362 | k := newKnot("idea", "Listed") |
| 363 | knotFile := Entry{Path: k.Path(), Mode: "100644", OID: oid("knot")} |
| 364 | commentDir := Entry{Path: CommentsDirectory + "/" + k.ID, Mode: "40000"} |
| 365 | comment := Entry{Path: commentDir.Path + "/20261015T000000Z-0123456789abcdef.md", Mode: "100644", OID: oid("comment")} |
| 366 | ideas, comments := Entry{Path: "ideas", Mode: "40000"}, Entry{Path: CommentsDirectory, Mode: "40000"} |
| 367 | for _, x := range []struct { |
| 368 | what string |
| 369 | es []Entry |
| 370 | rule Rule |
| 371 | }{ |
| 372 | {"every directory listed", []Entry{ideas, knotFile, comments, commentDir, comment}, ""}, |
| 373 | {"a knot without its directory", []Entry{knotFile}, RuleLayout}, |
| 374 | {"a comment without its knot's directory", []Entry{ideas, knotFile, comments, comment}, RuleLayout}, |
| 375 | {"a comment without comments/", []Entry{ideas, knotFile, commentDir, comment}, RuleLayout}, |
| 376 | {"a directory in the mode 040000", []Entry{{Path: "ideas", Mode: "040000"}, knotFile}, RuleLayout}, |
| 377 | } { |
| 378 | if _, err := Layout(x.es); ruleOf(err) != x.rule { |
| 379 | t.Errorf("%s: %v, want rule %q", x.what, err, x.rule) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Each limit, at its edge and one past it. |
| 385 | func TestTheLimitsHoldAtTheirEdges(t *testing.T) { |
| 386 | sized := func(n int) *Knot { |
| 387 | k := newKnot("idea", "Sized") |
| 388 | k.Body = "x\n" |
| 389 | k.Body = strings.Repeat("x", n-len(Render(k))+1) + "\n" |
| 390 | return k |
| 391 | } |
| 392 | for _, x := range []struct { |
| 393 | what string |
| 394 | k *Knot |
| 395 | rule Rule |
| 396 | }{ |
| 397 | {"a knot of the most bytes", sized(MaxKnotSize), ""}, |
| 398 | {"a knot one byte larger", sized(MaxKnotSize + 1), RuleLimit}, |
| 399 | {"a title of the most bytes", func() *Knot { k := newKnot("idea", "T"); k.Title = strings.Repeat("t", MaxTitle); return k }(), ""}, |
| 400 | {"a title one byte longer", func() *Knot { k := newKnot("idea", "T"); k.Title = strings.Repeat("t", MaxTitle+1); return k }(), RuleLimit}, |
| 401 | {"a name of the most bytes", func() *Knot { k := newKnot("idea", "T"); k.Owner = strings.Repeat("o", MaxName); return k }(), ""}, |
| 402 | {"a name one byte longer", func() *Knot { k := newKnot("idea", "T"); k.Owner = strings.Repeat("o", MaxName+1); return k }(), RuleLimit}, |
| 403 | {"a list of the most entries", func() *Knot { k := newKnot("bug", "T"); k.BlockedBy = refs(MaxListItems); return k }(), ""}, |
| 404 | {"a list one entry longer", func() *Knot { k := newKnot("bug", "T"); k.BlockedBy = refs(MaxListItems + 1); return k }(), RuleLimit}, |
| 405 | } { |
| 406 | if len(Render(x.k)) > MaxKnotSize+1 { |
| 407 | t.Fatalf("%s: the knot is %d bytes", x.what, len(Render(x.k))) |
| 408 | } |
| 409 | _, err := ParseKnot(x.k.Path(), x.k.Slug, Render(x.k)) |
| 410 | if ruleOf(err) != x.rule { |
| 411 | t.Errorf("%s: %v, want rule %q", x.what, err, x.rule) |
| 412 | } |
| 413 | } |
| 414 | if n := len(Render(sized(MaxKnotSize))); n != MaxKnotSize { |
| 415 | t.Fatalf("the largest knot is %d bytes", n) |
| 416 | } |
| 417 | |
| 418 | c := &Comment{Author: "operator", Body: "x\n"} |
| 419 | c.Body = strings.Repeat("x", MaxCommentSize-len(RenderComment(c))+1) + "\n" |
| 420 | if _, err := ParseComment("c", RenderComment(c)); err != nil || len(RenderComment(c)) != MaxCommentSize { |
| 421 | t.Errorf("a comment of the most bytes: %v", err) |
| 422 | } |
| 423 | c.Body = "x" + c.Body |
| 424 | if _, err := ParseComment("c", RenderComment(c)); ruleOf(err) != RuleLimit { |
| 425 | t.Errorf("a comment one byte larger: %v", err) |
| 426 | } |
| 427 | |
| 428 | // The counts are read from the listing alone. |
| 429 | listing := func(knots, comments int) []Entry { |
| 430 | es := []Entry{{Path: "ideas", Mode: "40000"}, {Path: "comments", Mode: "40000"}} |
| 431 | var first string |
| 432 | for i := range knots { |
| 433 | slug := fmt.Sprintf("k%d", i) |
| 434 | id := DeriveID(Types[2], slug) |
| 435 | first = id |
| 436 | es = append(es, Entry{Path: "ideas/" + id + "-" + slug + ".md", Mode: "100644", OID: id}) |
| 437 | } |
| 438 | es = append(es, Entry{Path: "comments/" + first, Mode: "40000"}) |
| 439 | for i := range comments { |
| 440 | es = append(es, Entry{Path: fmt.Sprintf("comments/%s/20261015T000000Z-%016x.md", first, i), Mode: "100644", OID: "c"}) |
| 441 | } |
| 442 | return es |
| 443 | } |
| 444 | for _, x := range []struct { |
| 445 | knots, comments int |
| 446 | rule Rule |
| 447 | }{{MaxKnots, MaxComments, ""}, {MaxKnots + 1, 1, RuleLimit}, {1, MaxComments + 1, RuleLimit}} { |
| 448 | if _, err := Layout(listing(x.knots, x.comments)); ruleOf(err) != x.rule { |
| 449 | t.Errorf("%d knots and %d comments: %v, want rule %q", x.knots, x.comments, err, x.rule) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // A slug of the most bytes, and one byte longer. |
| 454 | for _, x := range []struct { |
| 455 | n int |
| 456 | rule Rule |
| 457 | }{{MaxSlug, ""}, {MaxSlug + 1, RuleLimit}} { |
| 458 | slug := strings.Repeat("s", x.n) |
| 459 | id := DeriveID(Types[2], slug) |
| 460 | _, err := Layout([]Entry{{Path: "ideas", Mode: "40000"}, {Path: "ideas/" + id + "-" + slug + ".md", Mode: "100644"}}) |
| 461 | if r := ruleOf(err); (x.rule == "") != (r == "") { |
| 462 | t.Errorf("a slug of %d bytes: %v", x.n, err) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // The paths one push may add or change. |
| 467 | before := tree{}.with(newKnot("outcome", "Base")) |
| 468 | grow := func(n int) tree { |
| 469 | after := tree{}.with(newKnot("outcome", "Base")) |
| 470 | for i := range n { |
| 471 | after = after.with(newKnot("idea", fmt.Sprintf("Idea %d", i))) |
| 472 | } |
| 473 | return after |
| 474 | } |
| 475 | if err := push(before, grow(MaxChangedPaths), "operator"); err != nil { |
| 476 | t.Errorf("a push of %d files: %v", MaxChangedPaths, err) |
| 477 | } |
| 478 | if err := push(before, grow(MaxChangedPaths+1), "operator"); ruleOf(err) != RuleLimit { |
| 479 | t.Errorf("a push of %d files: %v", MaxChangedPaths+1, err) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func refs(n int) []string { |
| 484 | var list []string |
| 485 | for i := range n { |
| 486 | list = append(list, fmt.Sprintf("app:oc-%07x", i)) |
| 487 | } |
| 488 | return list |
| 489 | } |
| 490 | |
| 491 | func TestANewIDTakesASuffixOnACollision(t *testing.T) { |
| 492 | ty, _ := TypeNamed("outcome") |
| 493 | taken := map[string]bool{} |
| 494 | var slugs []string |
| 495 | for range 3 { |
| 496 | id, slug := NewID(ty, "Ship it", func(id string) bool { return taken[id] }) |
| 497 | taken[id] = true |
| 498 | slugs = append(slugs, slug) |
| 499 | if id != DeriveID(ty, slug) { |
| 500 | t.Errorf("%s is not derived from %s", id, slug) |
| 501 | } |
| 502 | } |
| 503 | if strings.Join(slugs, " ") != "ship-it ship-it-2 ship-it-3" { |
| 504 | t.Errorf("slugs = %v", slugs) |
| 505 | } |
| 506 | long := strings.Repeat("word ", 20) |
| 507 | _, slug := NewID(ty, long, func(id string) bool { return id == DeriveID(ty, Slug(long)) }) |
| 508 | if len(slug) > MaxSlug || !strings.HasSuffix(slug, "-2") || !slugShape.MatchString(slug) { |
| 509 | t.Errorf("a long title's suffixed slug is %q", slug) |
| 510 | } |
| 511 | if s := Slug("¿Qué? — 日本"); s != "qu" { |
| 512 | t.Errorf("Slug = %q", s) |
| 513 | } |
| 514 | if s := Slug("日本"); s != "knot" { |
| 515 | t.Errorf("Slug of a title with no ASCII letter = %q", s) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func TestACommitNamesItsOperationOnce(t *testing.T) { |
| 520 | tree := strings.Repeat("a", 40) |
| 521 | head := "tree " + tree + "\nparent " + strings.Repeat("b", 40) + "\nauthor p <p> 1 +0000\ncommitter operator <p@x> 1 +0000\n\n" |
| 522 | c, err := ParseCommit([]byte(head+Message("knot new oc-1234567: x", "0123456789abcdef")), 20) |
| 523 | if err != nil || c.Op != "0123456789abcdef" || c.Committer != "operator" || len(c.Parents) != 1 { |
| 524 | t.Fatalf("ParseCommit = %+v, %v", c, err) |
| 525 | } |
| 526 | for _, bad := range []string{ |
| 527 | "knot new\n", |
| 528 | "knot new\n\nValley-Op: 0123\n", |
| 529 | "knot new\n\nValley-Op: 0123456789abcdef\nmore\n", |
| 530 | "Valley-Op: fedcba9876543210\n\nValley-Op: 0123456789abcdef\n", |
| 531 | } { |
| 532 | if _, err := ParseCommit([]byte(head+bad), 20); err == nil { |
| 533 | t.Errorf("a message %q was read", bad) |
| 534 | } |
| 535 | } |
| 536 | if _, err := ParseCommit([]byte("tree "+tree+"\ncommitter a <a> 1 +0000\nparent "+strings.Repeat("b", 40)+"\n\n"+Message("x", "0123456789abcdef")), 20); err == nil { |
| 537 | t.Error("a parent after the other headers was read") |
| 538 | } |
| 539 | } |