gunk-dev/the-valley

project_test.go

source96b47ed3e847main's published tip history
11.5 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.
117func TestBuildMapsPrincipals(t *testing.T) {
118 f := exporttest.New(t)
119 claimed := knotOf(t, f.Export, "oc-a000002")
120 if claimed.CreatedBy != "operator" || claimed.Claim.By != "agent" || claimed.Comments[1].Author != "agent" {
121 t.Errorf("the claimed knot shows %q, %q, %q", claimed.CreatedBy, claimed.Claim.By, claimed.Comments[1].Author)
122 }
123 bug := knotOf(t, f.Export, "bd-a000004")
124 if bug.CreatedBy != export.Unmapped || bug.Comments[0].Author != export.Unmapped {
125 t.Errorf("an unmapped principal shows as %q and %q", bug.CreatedBy, bug.Comments[0].Author)
126 }
127 c := f.Export.Projects[0].Changes[0]
128 if c.Owner != "agent" || c.Version.Uploader != "agent" {
129 t.Errorf("the change shows %q and %q", c.Owner, c.Version.Uploader)
130 }
131}
132
133// A change keeps its active version alone, with its request and outcome,
134// and includes only changes the export carries.
135func TestBuildKeepsTheActiveVersion(t *testing.T) {
136 f := exporttest.New(t)
137 changes := f.Export.Projects[0].Changes
138 if len(changes) != 1 {
139 t.Fatalf("changes %v", changes)
140 }
141 v := changes[0].Version
142 if v.N != 2 || v.Request == nil || v.Outcome == nil || v.Outcome.Decision != "stale" {
143 t.Errorf("the version is %+v", v)
144 }
145 if len(changes[0].Includes) != 0 {
146 t.Errorf("includes %v names a draft", changes[0].Includes)
147 }
148}
149
150// A field the valley adds is not exported until the projection names it.
151func TestBuildReadsNamedFieldsAlone(t *testing.T) {
152 f := exporttest.New(t)
153 in := f.Inputs
154 in.Plan = bytes.Replace(in.Plan, []byte(`"id": "oc-a000001",`), []byte(`"id": "oc-a000001", "secret_field": "do not export",`), 1)
155 e, err := export.Build(in)
156 if err != nil {
157 t.Fatal(err)
158 }
159 data, err := e.Encode()
160 if err != nil {
161 t.Fatal(err)
162 }
163 if bytes.Contains(data, []byte("do not export")) {
164 t.Error("an unnamed field reached the export")
165 }
166}
167
168// What the projection refuses, so that a partial or inconsistent export
169// is never published.
170func TestBuildRefuses(t *testing.T) {
171 f := exporttest.New(t)
172 cases := []struct {
173 name string
174 change func(in *export.Inputs)
175 want string
176 }{
177 {"a detail read at another graph tip", func(in *export.Inputs) {
178 in.Details[0] = bytes.Replace(in.Details[0], []byte("3f6a0c1d"), []byte("3f6a0c1e"), 1)
179 }, "different graph tips"},
180 {"a knot without its detail", func(in *export.Inputs) { in.Details = in.Details[1:] }, "has no detail"},
181 {"a detail twice", func(in *export.Inputs) { in.Details = append(in.Details, in.Details[0]) }, "twice"},
182 {"a public project the plan could not read", func(in *export.Inputs) {
183 in.Plan = bytes.Replace(in.Plan, []byte(`"project": "app",
184 "error": null`), []byte(`"project": "app",
185 "error": "unreadable"`), 1)
186 }, "could not read public project app"},
187 {"a public project the pending listing could not read", func(in *export.Inputs) {
188 in.Pending = bytes.Replace(in.Pending, []byte(`"url": "git@valley-host:app.git",
189 "error": null`), []byte(`"url": "git@valley-host:app.git",
190 "error": "unreadable"`), 1)
191 }, "pending listing could not read public project app"},
192 {"a public project the plan does not list", func(in *export.Inputs) {
193 in.Visibility = map[string]export.Visibility{"app": export.Public, "other": export.Public}
194 }, "not in the plan"},
195 {"a visibility v1 does not export", func(in *export.Inputs) {
196 in.Visibility = map[string]export.Visibility{"app": "private"}
197 }, "public projects alone"},
198 {"a public project with no source tip", func(in *export.Inputs) { in.SourceTips = nil }, "no source tip"},
199 {"a display name with a control character", func(in *export.Inputs) {
200 in.Principals = export.Principals{"app-agent": "agent\x1b"}
201 }, "display mapping"},
202 {"a plan of another schema", func(in *export.Inputs) {
203 in.Plan = bytes.Replace(in.Plan, []byte("valley-plan/v1"), []byte("valley-plan/v2"), 1)
204 }, "schema"},
205 {"knot text with a control character", func(in *export.Inputs) {
206 for i, d := range in.Details {
207 in.Details[i] = bytes.Replace(d, []byte(`"body": "`), []byte(`"body": "\u0007`), 1)
208 }
209 }, "control character"},
210 }
211 for _, c := range cases {
212 t.Run(c.name, func(t *testing.T) {
213 in := f.Inputs
214 in.Details = slices.Clone(in.Details)
215 c.change(&in)
216 _, err := export.Build(in)
217 if err == nil || !strings.Contains(err.Error(), c.want) {
218 t.Fatalf("Build gave %v, want an error saying %q", err, c.want)
219 }
220 })
221 }
222 in := f.Inputs
223 in.Details = slices.Clone(in.Details)
224 in.Details[0] = bytes.Replace(in.Details[0], []byte("3f6a0c1d"), []byte("3f6a0c1e"), 1)
225 if _, err := export.Build(in); !errors.Is(err, export.ErrInconsistent) {
226 t.Errorf("a detail at another tip gave %v, want ErrInconsistent", err)
227 }
228}
229
230// With no public project, the export is empty and valid.
231func TestBuildExportsNothingWhenNothingIsPublic(t *testing.T) {
232 f := exporttest.New(t)
233 in := f.Inputs
234 in.Visibility = nil
235 e, err := export.Build(in)
236 if err != nil {
237 t.Fatal(err)
238 }
239 if len(e.Projects) != 0 {
240 t.Errorf("exported %v", e.Projects)
241 }
242}
243
244// export.json is read strictly: a field the schema does not name, or a
245// project name that is not safe as a file name, fails it.
246func TestDecodeIsStrict(t *testing.T) {
247 f := exporttest.New(t)
248 if _, err := export.Decode(f.ExportJSON); err != nil {
249 t.Fatal(err)
250 }
251 for name, data := range map[string][]byte{
252 "an unknown field": bytes.Replace(f.ExportJSON, []byte(`"collected"`), []byte(`"extra":1,"collected"`), 1),
253 "an unsafe name": bytes.Replace(f.ExportJSON, []byte(`"project":"app"`), []byte(`"project":"../app"`), 1),
254 "a second document": append(append([]byte{}, f.ExportJSON...), f.ExportJSON...),
255 "another schema": bytes.Replace(f.ExportJSON, []byte(export.Schema), []byte("valley-web-export/v0"), 1),
256 } {
257 if _, err := export.Decode(data); err == nil {
258 t.Errorf("Decode took %s", name)
259 }
260 }
261}
262
263// The content digest ignores display-time fields and nothing else.
264func TestDigest(t *testing.T) {
265 f := exporttest.New(t)
266 base, err := f.Export.Digest()
267 if err != nil {
268 t.Fatal(err)
269 }
270 reread := func() *export.Export {
271 e, err := export.Decode(f.ExportJSON)
272 if err != nil {
273 t.Fatal(err)
274 }
275 return e
276 }
277 same := map[string]func(e *export.Export){
278 "collected": func(e *export.Export) { e.Collected = "2026-10-26T00:00:00Z" },
279 "a claim's age": func(e *export.Export) {
280 for i := range e.Projects[0].Knots {
281 if c := e.Projects[0].Knots[i].Claim; c != nil {
282 c.AgeSeconds, c.Stale = 999999, true
283 }
284 }
285 },
286 }
287 for name, change := range same {
288 e := reread()
289 change(e)
290 if got, _ := e.Digest(); got != base {
291 t.Errorf("changing %s changed the digest", name)
292 }
293 }
294 differs := map[string]func(e *export.Export){
295 "a title": func(e *export.Export) { e.Projects[0].Knots[0].Title += "!" },
296 "a source tip": func(e *export.Export) { e.Projects[0].SourceTip = strings.Repeat("a", 40) },
297 "a claim's at": func(e *export.Export) { e.Projects[0].Knots[1].Claim.At = "2026-10-25T11:00:00Z" },
298 "a comment": func(e *export.Export) { e.Projects[0].Knots[1].Comments[0].Body += "." },
299 }
300 for name, change := range differs {
301 e := reread()
302 change(e)
303 if got, _ := e.Digest(); got == base {
304 t.Errorf("changing %s left the digest as it was", name)
305 }
306 }
307 // Digest copies what it clears: the export it was given is unchanged.
308 if f.Export.Collected == "" || knotOf(t, f.Export, "oc-a000002").Claim.AgeSeconds == 0 {
309 t.Error("Digest changed the export it read")
310 }
311}