gunk-dev/the-valley

project_test.go

source026c37612e61main's published tip history
13.0 KiB
1package export_test
2
3import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "slices"
8 "strings"
9 "testing"
10
11 "the-valley/webui/export"
12 "the-valley/webui/export/exporttest"
13)
14
15// Nothing the anonymous reader may not see reaches export.json: not the
16// hidden project's knots or changes, not the plan's operator sections,
17// not drafts, branches, older versions, repository URLs or request refs,
18// not a knot's storage path, and not one principal's own name.
19func TestBuildLeavesOutWhatIsNotTheReaders(t *testing.T) {
20 f := exporttest.New(t)
21 for _, leak := range []string{
22 `"path"`, "outcomes/", "waiting_on_you", `"operator":`, "changes_error",
23 "The site deploys the app", "The site serves the docs", "Hidden.", "site-operator",
24 "lmnoplmnop", "deploy: a hidden change",
25 "zyxwvutsrq", "wip: a draft", "app-agent-draft", "scratch",
26 "plan: list milestones", "4c4c4c4c", "app-agent-session",
27 "git@", "valley-host", "integration-requests", `"record"`, `"seq"`, `"restack"`,
28 "app-operator", "app-agent", "drive-by",
29 } {
30 if bytes.Contains(f.ExportJSON, []byte(leak)) {
31 t.Errorf("export.json holds %q", leak)
32 }
33 }
34 if len(f.Export.Projects) != 1 || f.Export.Projects[0].Name != "app" {
35 t.Fatalf("exported %v, want app alone", f.Export.Projects)
36 }
37}
38
39func knotOf(t *testing.T, e *export.Export, id string) export.Knot {
40 t.Helper()
41 for _, k := range e.Projects[0].Knots {
42 if k.ID == id {
43 return k
44 }
45 }
46 t.Fatalf("the export holds no knot %s", id)
47 return export.Knot{}
48}
49
50// A reference to the hidden project's knot keeps only the reference. It
51// still holds its knot up: a knot it blocks stays blocked, and is never
52// shown as free to start.
53func TestBuildHidesForeignEdges(t *testing.T) {
54 f := exporttest.New(t)
55 k := knotOf(t, f.Export, "oc-a000003")
56 if len(k.Edges) != 1 {
57 t.Fatalf("edges %v", k.Edges)
58 }
59 e := k.Edges[0]
60 if e.State != export.EdgeHidden || e.Status != nil || e.Title != nil || e.Why != nil || e.Ref != "site:oc-b000001" {
61 t.Errorf("the hidden edge is %+v", e)
62 }
63 if slices.Contains(k.WaitingOn, "site:oc-b000001") || !slices.Contains(k.WaitingOn, "oc-a000002") {
64 t.Errorf("waiting_on is %v; it keeps the local blocker and drops the hidden one", k.WaitingOn)
65 }
66 if k.State != "blocked" || k.Ready {
67 t.Errorf("the knot is %s, ready %v; it stays blocked", k.State, k.Ready)
68 }
69 if !slices.Equal(k.BlockedBy, []string{"oc-a000002", "site:oc-b000001"}) {
70 t.Errorf("blocked_by is %v; it is the knot's own field, as written", k.BlockedBy)
71 }
72 if target := knotOf(t, f.Export, "bd-a000004").Edges; len(target) != 1 || target[0].State != export.EdgeHidden || target[0].Field != "target" {
73 t.Errorf("the target edge is %+v", target)
74 }
75}
76
77// A reference to another exported project's knot keeps what the plan
78// says of it.
79func TestBuildKeepsEdgesBetweenExportedProjects(t *testing.T) {
80 f := exporttest.New(t)
81 in := f.Inputs
82 in.Visibility = map[string]export.Visibility{"app": export.Public, "site": export.Public}
83 in.SourceTips = map[string]string{"app": in.SourceTips["app"], "site": strings.Repeat("e", 40)}
84 in.Details = append(in.Details,
85 detail("site", "8c2e4a6b0d1f3e5a7c9b1d3f5e7a9c0b2d4f6e81", "oc-b000001"),
86 detail("site", "8c2e4a6b0d1f3e5a7c9b1d3f5e7a9c0b2d4f6e81", "oc-b000002"))
87 e, err := export.Build(in)
88 if err != nil {
89 t.Fatal(err)
90 }
91 edge := knotOf(t, e, "oc-a000003").Edges[0]
92 if edge.State != export.EdgeResolved || edge.Title == nil || *edge.Title != "The site deploys the app" {
93 t.Errorf("the edge is %+v", edge)
94 }
95 if !slices.Contains(knotOf(t, e, "oc-a000003").WaitingOn, "site:oc-b000001") {
96 t.Error("waiting_on dropped an exported project's blocker")
97 }
98}
99
100func detail(project, tip, id string) []byte {
101 b, _ := json.Marshal(map[string]any{"schema": "valley-knot/v1", "project": project, "tip": tip,
102 "knot": map[string]any{"id": id, "body": "", "comments": []any{}}})
103 return b
104}
105
106// In flight is the plan's claimed knots, oldest claim first, and then the
107// knots explicitly in progress that nobody claims.
108func TestBuildInFlight(t *testing.T) {
109 f := exporttest.New(t)
110 if got := f.Export.Projects[0].InFlight; !slices.Equal(got, []string{"oc-a000002", "oc-a000006"}) {
111 t.Errorf("in_flight is %v", got)
112 }
113}
114
115// Principal names pass through the display mapping, and a name it does
116// not map shows as the generic role. An edited comment's editor is a
117// principal too, and a comment never edited names no editor.
118func TestBuildMapsPrincipals(t *testing.T) {
119 f := exporttest.New(t)
120 claimed := knotOf(t, f.Export, "oc-a000002")
121 if claimed.CreatedBy != "operator" || claimed.Claim.By != "agent" || claimed.Comments[1].Author != "agent" {
122 t.Errorf("the claimed knot shows %q, %q, %q", claimed.CreatedBy, claimed.Claim.By, claimed.Comments[1].Author)
123 }
124 if c := claimed.Comments[1]; c.Edited != "2026-10-25T10:30:00Z" || c.EditedBy != "agent" {
125 t.Errorf("the edited comment shows edited %q by %q", c.Edited, c.EditedBy)
126 }
127 if c := claimed.Comments[0]; c.Edited != "" || c.EditedBy != "" {
128 t.Errorf("a comment never edited shows edited %q by %q", c.Edited, c.EditedBy)
129 }
130 bug := knotOf(t, f.Export, "bd-a000004")
131 if bug.CreatedBy != export.Unmapped || bug.Comments[0].Author != export.Unmapped {
132 t.Errorf("an unmapped principal shows as %q and %q", bug.CreatedBy, bug.Comments[0].Author)
133 }
134 c := f.Export.Projects[0].Changes[0]
135 if c.Owner != "agent" || c.Version.Uploader != "agent" {
136 t.Errorf("the change shows %q and %q", c.Owner, c.Version.Uploader)
137 }
138}
139
140// A change keeps its active version alone, with its request and outcome,
141// and includes only changes the export carries.
142func TestBuildKeepsTheActiveVersion(t *testing.T) {
143 f := exporttest.New(t)
144 changes := f.Export.Projects[0].Changes
145 if len(changes) != 1 {
146 t.Fatalf("changes %v", changes)
147 }
148 v := changes[0].Version
149 if v.N != 2 || v.Request == nil || v.Outcome == nil || v.Outcome.Decision != "stale" {
150 t.Errorf("the version is %+v", v)
151 }
152 if len(changes[0].Includes) != 0 {
153 t.Errorf("includes %v names a draft", changes[0].Includes)
154 }
155}
156
157// A field the valley adds is not exported until the projection names it.
158func TestBuildReadsNamedFieldsAlone(t *testing.T) {
159 f := exporttest.New(t)
160 in := f.Inputs
161 in.Plan = bytes.Replace(in.Plan, []byte(`"id": "oc-a000001",`), []byte(`"id": "oc-a000001", "secret_field": "do not export",`), 1)
162 e, err := export.Build(in)
163 if err != nil {
164 t.Fatal(err)
165 }
166 data, err := e.Encode()
167 if err != nil {
168 t.Fatal(err)
169 }
170 if bytes.Contains(data, []byte("do not export")) {
171 t.Error("an unnamed field reached the export")
172 }
173}
174
175// What the projection refuses, so that a partial or inconsistent export
176// is never published.
177func TestBuildRefuses(t *testing.T) {
178 f := exporttest.New(t)
179 cases := []struct {
180 name string
181 change func(in *export.Inputs)
182 want string
183 }{
184 {"a detail read at another graph tip", func(in *export.Inputs) {
185 in.Details[0] = bytes.Replace(in.Details[0], []byte("3f6a0c1d"), []byte("3f6a0c1e"), 1)
186 }, "different graph tips"},
187 {"a knot without its detail", func(in *export.Inputs) { in.Details = in.Details[1:] }, "has no detail"},
188 {"a detail twice", func(in *export.Inputs) { in.Details = append(in.Details, in.Details[0]) }, "twice"},
189 {"a public project the plan could not read", func(in *export.Inputs) {
190 in.Plan = bytes.Replace(in.Plan, []byte(`"project": "app",
191 "error": null`), []byte(`"project": "app",
192 "error": "unreadable"`), 1)
193 }, "could not read public project app"},
194 {"a public project the pending listing could not read", func(in *export.Inputs) {
195 in.Pending = bytes.Replace(in.Pending, []byte(`"url": "git@valley-host:app.git",
196 "error": null`), []byte(`"url": "git@valley-host:app.git",
197 "error": "unreadable"`), 1)
198 }, "pending listing could not read public project app"},
199 {"a public project the plan does not list", func(in *export.Inputs) {
200 in.Visibility = map[string]export.Visibility{"app": export.Public, "other": export.Public}
201 }, "not in the plan"},
202 {"a visibility v1 does not export", func(in *export.Inputs) {
203 in.Visibility = map[string]export.Visibility{"app": "private"}
204 }, "public projects alone"},
205 {"a public project with no source tip", func(in *export.Inputs) { in.SourceTips = nil }, "no source tip"},
206 {"a display name with a control character", func(in *export.Inputs) {
207 in.Principals = export.Principals{"app-agent": "agent\x1b"}
208 }, "display mapping"},
209 {"a plan of another schema", func(in *export.Inputs) {
210 in.Plan = bytes.Replace(in.Plan, []byte("valley-plan/v1"), []byte("valley-plan/v2"), 1)
211 }, "schema"},
212 {"knot text with a control character", func(in *export.Inputs) {
213 for i, d := range in.Details {
214 in.Details[i] = bytes.Replace(d, []byte(`"body": "`), []byte(`"body": "\u0007`), 1)
215 }
216 }, "control character"},
217 }
218 for _, c := range cases {
219 t.Run(c.name, func(t *testing.T) {
220 in := f.Inputs
221 in.Details = slices.Clone(in.Details)
222 c.change(&in)
223 _, err := export.Build(in)
224 if err == nil || !strings.Contains(err.Error(), c.want) {
225 t.Fatalf("Build gave %v, want an error saying %q", err, c.want)
226 }
227 })
228 }
229 in := f.Inputs
230 in.Details = slices.Clone(in.Details)
231 in.Details[0] = bytes.Replace(in.Details[0], []byte("3f6a0c1d"), []byte("3f6a0c1e"), 1)
232 if _, err := export.Build(in); !errors.Is(err, export.ErrInconsistent) {
233 t.Errorf("a detail at another tip gave %v, want ErrInconsistent", err)
234 }
235}
236
237// With no public project, the export is empty and valid.
238func TestBuildExportsNothingWhenNothingIsPublic(t *testing.T) {
239 f := exporttest.New(t)
240 in := f.Inputs
241 in.Visibility = nil
242 e, err := export.Build(in)
243 if err != nil {
244 t.Fatal(err)
245 }
246 if len(e.Projects) != 0 {
247 t.Errorf("exported %v", e.Projects)
248 }
249}
250
251// export.json is read strictly: a field the schema does not name, or a
252// project name that is not safe as a file name, fails it.
253func TestDecodeIsStrict(t *testing.T) {
254 f := exporttest.New(t)
255 if _, err := export.Decode(f.ExportJSON); err != nil {
256 t.Fatal(err)
257 }
258 for name, data := range map[string][]byte{
259 "an unknown field": bytes.Replace(f.ExportJSON, []byte(`"collected"`), []byte(`"extra":1,"collected"`), 1),
260 "an unsafe name": bytes.Replace(f.ExportJSON, []byte(`"project":"app"`), []byte(`"project":"../app"`), 1),
261 "a second document": append(append([]byte{}, f.ExportJSON...), f.ExportJSON...),
262 "another schema": bytes.Replace(f.ExportJSON, []byte(export.Schema), []byte("valley-web-export/v0"), 1),
263 } {
264 if _, err := export.Decode(data); err == nil {
265 t.Errorf("Decode took %s", name)
266 }
267 }
268}
269
270// Nothing but white space may follow export.json's document: not a
271// closing bracket or brace, which ends a document without starting
272// another, and not a malformed suffix. A receiver refuses such an
273// export.json as bad-export even when the manifest declares its bytes.
274func TestDecodeRefusesTrailingInput(t *testing.T) {
275 f := exporttest.New(t)
276 for _, suffix := range []string{"]", "}", "]garbage", "}}", "],", "garbage", ",", "{", `"`, "nul", "\x00", "{}", "[]", "1"} {
277 data := append(append([]byte{}, f.ExportJSON...), suffix...)
278 if _, err := export.Decode(data); err == nil {
279 t.Errorf("Decode took export.json followed by %q", suffix)
280 }
281 m := *f.Manifest
282 m.File = export.BodyOf(export.ExportFile, data)
283 if _, err := m.CheckExport(data); export.CodeOf(err) != export.CodeBadExport {
284 t.Errorf("CheckExport gave %v for export.json followed by %q, want bad-export", err, suffix)
285 }
286 }
287 data := append(append([]byte{}, f.ExportJSON...), " \t\r\n\n"...)
288 if _, err := export.Decode(data); err != nil {
289 t.Errorf("Decode refused export.json followed by white space: %v", err)
290 }
291}
292
293// The content digest ignores display-time fields and nothing else.
294func TestDigest(t *testing.T) {
295 f := exporttest.New(t)
296 base, err := f.Export.Digest()
297 if err != nil {
298 t.Fatal(err)
299 }
300 reread := func() *export.Export {
301 e, err := export.Decode(f.ExportJSON)
302 if err != nil {
303 t.Fatal(err)
304 }
305 return e
306 }
307 same := map[string]func(e *export.Export){
308 "collected": func(e *export.Export) { e.Collected = "2026-10-26T00:00:00Z" },
309 "a claim's age": func(e *export.Export) {
310 for i := range e.Projects[0].Knots {
311 if c := e.Projects[0].Knots[i].Claim; c != nil {
312 c.AgeSeconds, c.Stale = 999999, true
313 }
314 }
315 },
316 }
317 for name, change := range same {
318 e := reread()
319 change(e)
320 if got, _ := e.Digest(); got != base {
321 t.Errorf("changing %s changed the digest", name)
322 }
323 }
324 differs := map[string]func(e *export.Export){
325 "a title": func(e *export.Export) { e.Projects[0].Knots[0].Title += "!" },
326 "a source tip": func(e *export.Export) { e.Projects[0].SourceTip = strings.Repeat("a", 40) },
327 "a claim's at": func(e *export.Export) { e.Projects[0].Knots[1].Claim.At = "2026-10-25T11:00:00Z" },
328 "a comment": func(e *export.Export) { e.Projects[0].Knots[1].Comments[0].Body += "." },
329 }
330 for name, change := range differs {
331 e := reread()
332 change(e)
333 if got, _ := e.Digest(); got == base {
334 t.Errorf("changing %s left the digest as it was", name)
335 }
336 }
337 // Digest copies what it clears: the export it was given is unchanged.
338 if f.Export.Collected == "" || knotOf(t, f.Export, "oc-a000002").Claim.AgeSeconds == 0 {
339 t.Error("Digest changed the export it read")
340 }
341}