gunk-dev/the-valley

fixtures.go

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