gunk-dev/the-valley

fixtures.go

source03252b3b26acmain's published tip history
9.0 KiB
1package main
2
3import (
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.
11func 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: "operator"}
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.
26func 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.
35func 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.
43func Fixtures() []Case {
44 milestone := mk("outcome", "Slice one lands")
45 question := mk("question", "Where does the graph live", func(k *knot.Knot) {
46 k.Status, k.Owner = "answered", "operator"
47 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
48 })
49 bug := mk("bug", "The hook reads too much", func(k *knot.Knot) { k.Parent = milestone.ID })
50 idea := mk("idea", "Knots as cords", func(k *knot.Knot) {
51 k.Status, k.GraduatedInto, k.Parent = "graduated", question.ID, milestone.ID
52 })
53 work := mk("outcome", "Write the hook case", func(k *knot.Knot) {
54 k.Status, k.Parent, k.Owner = "in-progress", milestone.ID, "alice"
55 k.BlockedBy = []string{bug.ID, "app:oc-0123456"}
56 k.ClaimedBy, k.ClaimedAt = "20261015-0058-feb0a926", "2026-10-15T00:58:00Z"
57 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr", "https://github.com/example/site/pull/12"}
58 k.Target, k.Source = "site", "2026-10-15, the plan-graph design"
59 k.Body = "Some \"quoted\" text, and a line with <angle> brackets & more.\n\n- a list\n"
60 })
61 // A milestone waiting on its own work item is no cycle.
62 waiting := mk("outcome", "Slice one ships", func(k *knot.Knot) { k.BlockedBy = []string{work.ID} })
63 work2 := mk("outcome", "Write the hook case")
64 work2.Slug = "write-the-hook-case-2"
65 work2.ID = knot.DeriveID(work2.TypeOf(), work2.Slug)
66
67 valid := []Case{
68 {Name: "every-shape", Accept: true, Files: with(files(milestone, question, bug, idea, work, waiting, work2), work.ID, "alice", "Claimed for the run.\n")},
69 {Name: "one-knot", Accept: true, Files: files(mk("bug", "A lone bug"))},
70 // A graduated idea moves to work taken on, or to a question to
71 // settle.
72 {Name: "graduated-into-an-outcome", Accept: true, Files: files(milestone, mk("idea", "Slices", func(k *knot.Knot) {
73 k.Status, k.GraduatedInto = "graduated", milestone.ID
74 }))},
75 {Name: "graduated-into-a-question", Accept: true, Files: files(question, mk("idea", "One ref", func(k *knot.Knot) {
76 k.Status, k.GraduatedInto = "graduated", question.ID
77 }))},
78 }
79 for _, t := range knot.Types {
80 for _, s := range t.Statuses() {
81 k := mk(t.Name, "A knot that is "+s, func(k *knot.Knot) { k.Status = s })
82 fs := files(k)
83 if s == "graduated" {
84 k.GraduatedInto = question.ID
85 fs = files(k, question)
86 }
87 valid = append(valid, Case{Name: "status-" + t.Name + "-" + s, Accept: true, Files: fs})
88 }
89 }
90
91 // Each rejected graph breaks one shared rule and nothing else.
92 reject := func(name string, rule knot.Rule, fs map[string]string) Case {
93 return Case{Name: name, Rule: rule, Files: fs}
94 }
95 edit := func(k *knot.Knot, f func(string) string) map[string]string {
96 return map[string]string{k.Path(): f(string(knot.Render(k)))}
97 }
98 set := func(k *knot.Knot, f func(*knot.Knot)) *knot.Knot {
99 c := k.Clone()
100 f(c)
101 return c
102 }
103 a, b := mk("outcome", "Alpha"), mk("outcome", "Beta")
104 moved := mk("bug", "A bug filed as an outcome")
105 misfiled := map[string]string{"outcomes/" + moved.ID + "-" + moved.Slug + ".md": string(knot.Render(moved))}
106 twin := files(a)
107 twin["outcomes/"+a.ID+"-alpha-twin.md"] = string(knot.Render(a))
108 asked := mk("question", "Asked")
109 other := mk("idea", "Another idea")
110 cases := []Case{
111 reject("unknown-type", knot.RuleType, map[string]string{a.Path(): string(knot.Render(set(a, func(k *knot.Knot) { k.Type = "task" })))}),
112 reject("type-decision", knot.RuleType, map[string]string{asked.Path(): string(knot.Render(set(asked, func(k *knot.Knot) { k.Type = "decision" })))}),
113 reject("unknown-status", knot.RuleStatus, files(set(a, func(k *knot.Knot) { k.Status = "blocked" }))),
114 reject("status-of-another-type", knot.RuleStatus, files(set(mk("bug", "Bug"), func(k *knot.Knot) { k.Status = "done" }))),
115 reject("id-not-hex", knot.RuleIDShape, edit(a, func(s string) string { return strings.Replace(s, "id: "+a.ID, "id: oc-xyz", 1) })),
116 reject("id-of-another-type", knot.RuleIDShape, edit(a, func(s string) string { return strings.Replace(s, "id: oc-", "id: bd-", 1) })),
117 reject("id-prefix-dcr", knot.RuleIDShape, edit(asked, func(s string) string { return strings.Replace(s, "id: qn-", "id: dcr-", 1) })),
118 reject("id-not-derived", knot.RuleIDDerived, files(set(a, func(k *knot.Knot) { k.ID = "oc-0000000" }))),
119 reject("filename-disagrees", knot.RuleFilename, map[string]string{a.Path(): string(knot.Render(set(a, func(k *knot.Knot) { k.ID = b.ID })))}),
120 reject("knot-in-another-directory", knot.RuleDirectory, misfiled),
121 reject("duplicate-id", knot.RuleUniqueID, twin),
122 reject("unknown-field", knot.RuleFieldOnType, edit(a, func(s string) string {
123 return strings.Replace(s, "blocked_by: []\n", "blocked_by: []\npriority: \"high\"\n", 1)
124 })),
125 reject("blocked-by-on-an-idea", knot.RuleFieldOnType, edit(mk("idea", "Idea"), func(s string) string {
126 return strings.Replace(s, "created_by: \"operator\"\n", "created_by: \"operator\"\nblocked_by: []\n", 1)
127 })),
128 reject("changes-on-an-idea", knot.RuleFieldOnType, files(set(mk("idea", "Idea"), func(k *knot.Knot) {
129 k.Changes = []string{"valley:the-valley/kkkkllllmmmmnnnnooooppppqqqqrrrr"}
130 }))),
131 reject("claim-on-a-question", knot.RuleFieldOnType, edit(asked, func(s string) string {
132 return strings.Replace(s, "created_by: \"operator\"\n", "created_by: \"operator\"\nclaimed_by: \"run\"\nclaimed_at: \"2026-10-15T00:58:00Z\"\n", 1)
133 })),
134 reject("blocked-by-on-a-question", knot.RuleFieldOnType, edit(asked, func(s string) string {
135 return strings.Replace(s, "created_by: \"operator\"\n", "created_by: \"operator\"\nblocked_by: []\n", 1)
136 })),
137 reject("creator-not-a-name", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.CreatedBy = "the operator" }))),
138 reject("owner-not-a-name", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Owner = "-dash" }))),
139 reject("blocker-not-a-reference", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.BlockedBy = []string{"app/oc-0123456"} }))),
140 reject("change-not-a-link", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Changes = []string{"https://example.com/pull/1"} }))),
141 reject("claim-time-not-a-time", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.ClaimedBy, k.ClaimedAt = "run", "yesterday" }))),
142 reject("created-not-a-date", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Created = "2026-10-15T00:00" }))),
143 reject("target-not-a-project", knot.RuleFieldShape, files(set(a, func(k *knot.Knot) { k.Target = "two words" }))),
144 reject("outcome-without-blocked-by", knot.RuleRequired, edit(a, func(s string) string { return strings.Replace(s, "blocked_by: []\n", "", 1) })),
145 reject("empty-title", knot.RuleRequired, files(set(a, func(k *knot.Knot) { k.Title = "" }))),
146 reject("no-status", knot.RuleRequired, edit(a, func(s string) string { return strings.Replace(s, "status: open\n", "", 1) })),
147 reject("parent-not-in-the-graph", knot.RuleEdgeResolves, files(set(a, func(k *knot.Knot) { k.Parent = b.ID }))),
148 reject("blocker-not-in-the-graph", knot.RuleEdgeResolves, files(set(a, func(k *knot.Knot) { k.BlockedBy = []string{b.ID} }))),
149 reject("graduated-into-no-knot", knot.RuleEdgeResolves, files(set(idea, func(k *knot.Knot) { k.Parent = "" }))),
150 reject("graduated-into-a-bug", knot.RuleFieldShape, files(set(idea, func(k *knot.Knot) { k.Parent, k.GraduatedInto = "", bug.ID }), set(bug, func(k *knot.Knot) { k.Parent = "" }))),
151 reject("graduated-into-an-idea", knot.RuleFieldShape, files(set(idea, func(k *knot.Knot) { k.Parent, k.GraduatedInto = "", other.ID }), other)),
152 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 = "" }))),
153 reject("graduated-without-destination", knot.RuleGraduated, files(mk("idea", "Gone", func(k *knot.Knot) { k.Status = "graduated" }))),
154 reject("destination-while-raw", knot.RuleGraduated, files(mk("idea", "Raw", func(k *knot.Knot) { k.GraduatedInto = question.ID }), question)),
155 }
156 return append(valid, cases...)
157}