| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "the-valley/knot" |
| 7 | ) |
| 8 | |
| 9 | // mk is a knot of type t titled title, as `valley knot new` would write |
| 10 | // it, with each option applied after. |
| 11 | func mk(t, title string, options ...func(*knot.Knot)) *knot.Knot { |
| 12 | ty, _ := knot.TypeNamed(t) |
| 13 | slug := knot.Slug(title) |
| 14 | k := &knot.Knot{Type: t, ID: knot.DeriveID(ty, slug), Slug: slug, Status: ty.Initial(), |
| 15 | Title: title, Created: "2026-10-15", CreatedBy: "patrick"} |
| 16 | if t == "outcome" || t == "bug" { |
| 17 | k.BlockedBy = []string{} |
| 18 | } |
| 19 | for _, o := range options { |
| 20 | o(k) |
| 21 | } |
| 22 | return k |
| 23 | } |
| 24 | |
| 25 | // files writes knots, each at its path. |
| 26 | func files(knots ...*knot.Knot) map[string]string { |
| 27 | out := map[string]string{} |
| 28 | for _, k := range knots { |
| 29 | out[k.Path()] = string(knot.Render(k)) |
| 30 | } |
| 31 | return out |
| 32 | } |
| 33 | |
| 34 | // with is fs and a comment by author on the knot with id. |
| 35 | func with(fs map[string]string, id, author, body string) map[string]string { |
| 36 | fs[knot.CommentsDirectory+"/"+id+"/20261015T005800Z-0123456789abcdef.md"] = string(knot.RenderComment(&knot.Comment{Author: author, Body: body})) |
| 37 | return fs |
| 38 | } |
| 39 | |
| 40 | // Fixtures are the shared fixtures: graphs both validators accept, each |
| 41 | // holding the shapes the live graph uses, and one graph for each way of |
| 42 | // breaking a shared rule. |
| 43 | func Fixtures() []Case { |
| 44 | milestone := mk("outcome", "Slice one lands") |
| 45 | decision := mk("decision", "One ref for the graph", func(k *knot.Knot) { |
| 46 | k.Status, k.Changes = "decided", []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"} |
| 47 | }) |
| 48 | bug := mk("bug", "The hook reads too much", func(k *knot.Knot) { k.Parent = milestone.ID }) |
| 49 | idea := mk("idea", "Knots as cords", func(k *knot.Knot) { |
| 50 | k.Status, k.GraduatedInto, k.Parent = "graduated", decision.ID, milestone.ID |
| 51 | }) |
| 52 | work := mk("outcome", "Write the hook case", func(k *knot.Knot) { |
| 53 | k.Status, k.Parent, k.Owner = "in-progress", milestone.ID, "klaus-env" |
| 54 | k.BlockedBy = []string{bug.ID, "cosmo:oc-0123456"} |
| 55 | k.ClaimedBy, k.ClaimedAt = "20261015-0058-feb0a926", "2026-10-15T00:58:00Z" |
| 56 | k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr", "https://github.com/gunk-dev/klaus/pull/12"} |
| 57 | k.Target, k.Source = "klaus", "2026-10-15, the plan-graph design" |
| 58 | k.Body = "Some \"quoted\" text, and a line with <angle> brackets & more.\n\n- a list\n" |
| 59 | }) |
| 60 | // A milestone waiting on its own work item is no cycle. |
| 61 | waiting := mk("outcome", "Slice one ships", func(k *knot.Knot) { k.BlockedBy = []string{work.ID} }) |
| 62 | work2 := mk("outcome", "Write the hook case") |
| 63 | work2.Slug = "write-the-hook-case-2" |
| 64 | work2.ID = knot.DeriveID(work2.TypeOf(), work2.Slug) |
| 65 | |
| 66 | valid := []Case{ |
| 67 | {Name: "every-shape", Accept: true, Files: with(files(milestone, decision, bug, idea, work, waiting, work2), work.ID, "klaus-env", "Claimed for the run.\n")}, |
| 68 | {Name: "one-knot", Accept: true, Files: files(mk("bug", "A lone bug"))}, |
| 69 | } |
| 70 | for _, t := range knot.Types { |
| 71 | for _, s := range t.Statuses() { |
| 72 | k := mk(t.Name, "A knot that is "+s, func(k *knot.Knot) { k.Status = s }) |
| 73 | fs := files(k) |
| 74 | if s == "graduated" { |
| 75 | k.GraduatedInto = decision.ID |
| 76 | fs = files(k, decision) |
| 77 | } |
| 78 | valid = append(valid, Case{Name: "status-" + t.Name + "-" + s, Accept: true, Files: fs}) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Each rejected graph breaks one shared rule and nothing else. |
| 83 | reject := func(name string, rule knot.Rule, fs map[string]string) Case { |
| 84 | return Case{Name: name, Rule: rule, Files: fs} |
| 85 | } |
| 86 | edit := func(k *knot.Knot, f func(string) string) map[string]string { |
| 87 | return map[string]string{k.Path(): f(string(knot.Render(k)))} |
| 88 | } |
| 89 | set := func(k *knot.Knot, f func(*knot.Knot)) *knot.Knot { |
| 90 | c := k.Clone() |
| 91 | f(c) |
| 92 | return c |
| 93 | } |
| 94 | a, b := mk("outcome", "Alpha"), mk("outcome", "Beta") |
| 95 | moved := mk("bug", "A bug filed as an outcome") |
| 96 | misfiled := map[string]string{"outcomes/" + moved.ID + "-" + moved.Slug + ".md": string(knot.Render(moved))} |
| 97 | twin := files(a) |
| 98 | twin["outcomes/"+a.ID+"-alpha-twin.md"] = string(knot.Render(a)) |
| 99 | undecided := mk("decision", "Undecided") |
| 100 | cases := []Case{ |
| 101 | reject("unknown-type", knot.RuleType, map[string]string{a.Path(): string(knot.Render(set(a, func(k *knot.Knot) { k.Type = "task" })))}), |
| 102 | reject("unknown-status", knot.RuleStatus, files(set(a, func(k *knot.Knot) { k.Status = "blocked" }))), |
| 103 | reject("status-of-another-type", knot.RuleStatus, files(set(mk("bug", "Bug"), func(k *knot.Knot) { k.Status = "done" }))), |
| 104 | reject("id-not-hex", knot.RuleIDShape, edit(a, func(s string) string { return strings.Replace(s, "id: "+a.ID, "id: oc-xyz", 1) })), |
| 105 | reject("id-of-another-type", knot.RuleIDShape, edit(a, func(s string) string { return strings.Replace(s, "id: oc-", "id: bd-", 1) })), |
| 106 | reject("id-not-derived", knot.RuleIDDerived, files(set(a, func(k *knot.Knot) { k.ID = "oc-0000000" }))), |
| 107 | reject("filename-disagrees", knot.RuleFilename, map[string]string{a.Path(): string(knot.Render(set(a, func(k *knot.Knot) { k.ID = b.ID })))}), |
| 108 | reject("knot-in-another-directory", knot.RuleDirectory, misfiled), |
| 109 | reject("duplicate-id", knot.RuleUniqueID, twin), |
| 110 | reject("unknown-field", knot.RuleFieldOnType, edit(a, func(s string) string { |
| 111 | return strings.Replace(s, "blocked_by: []\n", "blocked_by: []\npriority: \"high\"\n", 1) |
| 112 | })), |
| 113 | reject("blocked-by-on-an-idea", knot.RuleFieldOnType, edit(mk("idea", "Idea"), func(s string) string { |
| 114 | return strings.Replace(s, "created_by: \"patrick\"\n", "created_by: \"patrick\"\nblocked_by: []\n", 1) |
| 115 | })), |
| 116 | reject("changes-on-an-idea", knot.RuleFieldOnType, files(set(mk("idea", "Idea"), func(k *knot.Knot) { |
| 117 | k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"} |
| 118 | }))), |
| 119 | reject("claim-on-a-decision", knot.RuleFieldOnType, edit(undecided, func(s string) string { |
| 120 | return strings.Replace(s, "created_by: \"patrick\"\n", "created_by: \"patrick\"\nclaimed_by: \"run\"\nclaimed_at: \"2026-10-15T00:58:00Z\"\n", 1) |
| 121 | })), |
| 122 | reject("creator-not-a-name", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.CreatedBy = "the operator" }))), |
| 123 | reject("owner-not-a-name", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Owner = "-dash" }))), |
| 124 | reject("blocker-not-a-reference", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.BlockedBy = []string{"cosmo/oc-0123456"} }))), |
| 125 | reject("change-not-a-link", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Changes = []string{"https://example.com/pull/1"} }))), |
| 126 | reject("claim-time-not-a-time", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.ClaimedBy, k.ClaimedAt = "run", "yesterday" }))), |
| 127 | reject("created-not-a-date", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Created = "2026-10-15T00:00" }))), |
| 128 | reject("target-not-a-project", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Target = "two words" }))), |
| 129 | reject("outcome-without-blocked-by", knot.RuleRequired, edit(a, func(s string) string { return strings.Replace(s, "blocked_by: []\n", "", 1) })), |
| 130 | reject("empty-title", knot.RuleRequired, files(set(a, func(k *knot.Knot) { k.Title = "" }))), |
| 131 | reject("no-status", knot.RuleRequired, edit(a, func(s string) string { return strings.Replace(s, "status: open\n", "", 1) })), |
| 132 | reject("parent-not-in-the-graph", knot.RuleEdgeResolves, files(set(a, func(k *knot.Knot) { k.Parent = b.ID }))), |
| 133 | reject("blocker-not-in-the-graph", knot.RuleEdgeResolves, files(set(a, func(k *knot.Knot) { k.BlockedBy = []string{b.ID} }))), |
| 134 | reject("graduated-into-no-decision", knot.RuleEdgeResolves, files(set(idea, func(k *knot.Knot) { k.Parent = "" }))), |
| 135 | reject("parent-a-bug", knot.RuleParentOutcome, files(set(a, func(k *knot.Knot) { k.Parent = bug.ID }), set(bug, func(k *knot.Knot) { k.Parent = "" }))), |
| 136 | reject("graduated-without-destination", knot.RuleGraduated, files(mk("idea", "Gone", func(k *knot.Knot) { k.Status = "graduated" }))), |
| 137 | reject("destination-while-raw", knot.RuleGraduated, files(mk("idea", "Raw", func(k *knot.Knot) { k.GraduatedInto = decision.ID }), decision)), |
| 138 | } |
| 139 | return append(valid, cases...) |
| 140 | } |