| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "reflect" |
| 12 | "regexp" |
| 13 | "slices" |
| 14 | "sort" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | "time" |
| 18 | |
| 19 | "the-valley/note" |
| 20 | "the-valley/web/export" |
| 21 | "the-valley/web/export/exporttest" |
| 22 | ) |
| 23 | |
| 24 | // publishedReader is the request context's key under which |
| 25 | // TestThePublishersOutputServesEachReader names the reader a request is |
| 26 | // read as. |
| 27 | type publishedReader struct{} |
| 28 | |
| 29 | // The real publisher's output (web-published in nix/packages.nix), in the |
| 30 | // directory VALLEY_WEB_PUBLISHED names. Anyone may read app. alice alone |
| 31 | // may read site, and the operator alone docs. notes has no effective |
| 32 | // reader. docs is in the group guides, and the others in tools. The store admits the authorization and the manifest under the |
| 33 | // export key's verifier line, and takes the bodies, as a receiver does. |
| 34 | // |
| 35 | // The export holds app, docs and site, and nothing notes' knots say. |
| 36 | // Three of app's knots are each blocked by an open, an abandoned and a |
| 37 | // missing knot of site, docs or notes. A blocker in an exported project |
| 38 | // keeps every derived field and its edges in the export; one in notes |
| 39 | // leaves only waiting_on_hidden and hidden edges. projectForReader then |
| 40 | // takes out, for each reader, what those fields and edges say of an |
| 41 | // exported project the reader may not see. |
| 42 | // |
| 43 | // The server serves each reader the union of what anyone and they may |
| 44 | // read. For anyone, bob, alice and the operator, no page it renders, a |
| 45 | // knot, a knots page, the projects page, a group's page, a source page, a |
| 46 | // redirect or an error, holds the name, group, alias, former name, |
| 47 | // README, knot titles, comment or ready change of a private project the |
| 48 | // reader may not see. / lands anyone and bob, who may see app alone, on |
| 49 | // its tree, and alice and the operator on the projects page, which lists |
| 50 | // the projects each reader may see. guides answers anyone, bob and alice |
| 51 | // as a group that does not exist. Each project's knots page shows its own |
| 52 | // knots alone, and the site navigation links the projects page alone. |
| 53 | func TestThePublishersOutputServesEachReader(t *testing.T) { |
| 54 | dir := os.Getenv("VALLEY_WEB_PUBLISHED") |
| 55 | if dir == "" { |
| 56 | if os.Getenv("VALLEY_TEST_TOOLS") == "required" { |
| 57 | t.Fatal("VALLEY_WEB_PUBLISHED names no publisher output") |
| 58 | } |
| 59 | t.Skip("VALLEY_WEB_PUBLISHED names no publisher output") |
| 60 | } |
| 61 | ctx := context.Background() |
| 62 | read := func(path string) []byte { |
| 63 | t.Helper() |
| 64 | data, err := os.ReadFile(filepath.Join(dir, path)) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | return data |
| 69 | } |
| 70 | verifiers, err := note.ParseVerifierKeys(read("verifier")) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | store, err := export.OpenStore(exporttest.StorePath(t), func(err error) { t.Errorf("the store reported: %v", err) }) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | |
| 79 | signed := read("published/authorization") |
| 80 | a, err := export.VerifyAuthorization(bytes.NewReader(signed), export.AuthorizationAdmission{ |
| 81 | Verifiers: verifiers, Instance: exporttest.Instance, Destination: exporttest.Destination}) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | if err := store.Authorize(ctx, a, signed); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | for _, c := range []struct { |
| 89 | reader, project string |
| 90 | allowed bool |
| 91 | }{ |
| 92 | {export.Anyone, "app", true}, {export.Anyone, "site", false}, {export.Anyone, "docs", false}, |
| 93 | {"alice", "site", true}, {"alice", "docs", false}, {"operator", "docs", true}, {"operator", "site", false}, |
| 94 | {"bob", "site", false}, {"bob", "docs", false}, |
| 95 | } { |
| 96 | p, _ := a.Project(c.project) |
| 97 | if got := a.Allows(c.reader, c.project, p.Identity); got != c.allowed { |
| 98 | t.Errorf("the authorization lets %s read %s: %v, want %v", c.reader, c.project, got, c.allowed) |
| 99 | } |
| 100 | } |
| 101 | if _, named := a.Project("notes"); named { |
| 102 | t.Errorf("the authorization names notes, which has no effective reader") |
| 103 | } |
| 104 | |
| 105 | // The manifest was signed when the publisher ran; it is admitted a |
| 106 | // second after its signing time, as a receiver admits it then. |
| 107 | signedManifest := read("published/manifest") |
| 108 | text, _, err := note.Open(signedManifest, verifiers) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | unverified, err := export.ParseManifest(text) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | m, err := export.VerifyManifest(bytes.NewReader(signedManifest), export.Admission{ |
| 117 | Verifiers: verifiers, Instance: exporttest.Instance, Destination: exporttest.Destination, |
| 118 | Now: time.Unix(0, unverified.Sequence).Add(time.Second)}) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | st, err := store.Stage(ctx, m) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | inv, err := store.Inventory(st) |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | for _, need := range inv.Needs { |
| 131 | path := filepath.Join("published", "bundles", need.Name+".bundle") |
| 132 | if need.Name == export.ExportFile { |
| 133 | path = filepath.Join("published", export.ExportFile) |
| 134 | } |
| 135 | req := httptest.NewRequest("PUT", export.BodyPath(m.Sequence, need.SHA256), bytes.NewReader(read(path))) |
| 136 | if _, err := export.StageBody(st.Bodies(), export.Body{Name: need.Name, SHA256: need.SHA256, Size: need.Size}, 64<<20, req); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | } |
| 140 | if _, err := store.Activate(ctx, st); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | |
| 144 | data := read(filepath.Join("published", export.ExportFile)) |
| 145 | e, err := export.Decode(data) |
| 146 | if err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | var names []string |
| 150 | projects := map[string]export.Project{} |
| 151 | for _, p := range e.Projects { |
| 152 | names = append(names, p.Name) |
| 153 | projects[p.Name] = p |
| 154 | } |
| 155 | sort.Strings(names) |
| 156 | if !reflect.DeepEqual(names, []string{"app", "docs", "site"}) { |
| 157 | t.Fatalf("the export holds %v, want app, docs and site", names) |
| 158 | } |
| 159 | for _, text := range []string{"notes-readme-text", "notes-knot-text", "notes-abandoned-text", "notes-comment-text"} { |
| 160 | if bytes.Contains(data, []byte(text)) { |
| 161 | t.Errorf("export.json holds %s, which only notes says", text) |
| 162 | } |
| 163 | } |
| 164 | knotOf := func(e *export.Export, project, title string) *export.Knot { |
| 165 | t.Helper() |
| 166 | for i := range e.Projects { |
| 167 | if e.Projects[i].Name != project { |
| 168 | continue |
| 169 | } |
| 170 | for j := range e.Projects[i].Knots { |
| 171 | if k := &e.Projects[i].Knots[j]; k.Title == title { |
| 172 | return k |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | t.Fatalf("%s has no knot %q", project, title) |
| 177 | return nil |
| 178 | } |
| 179 | waits := map[string]string{"site": "Waits on the first private project", "docs": "Waits on the second private project", |
| 180 | "notes": "Waits on the third private project"} |
| 181 | |
| 182 | // What the export says of each blocker app's knots name: everything |
| 183 | // the plan derived of one in site or docs, and of one in notes, which |
| 184 | // is not exported, that the knot waits on something hidden. |
| 185 | for _, project := range []string{"site", "docs"} { |
| 186 | k := knotOf(e, "app", waits[project]) |
| 187 | open := project + ":" + knotOf(e, project, project+"-knot-text").ID |
| 188 | ended := project + ":" + knotOf(e, project, project+"-abandoned-text").ID |
| 189 | missing := project + ":oc-0000000" |
| 190 | for _, c := range []struct { |
| 191 | field string |
| 192 | got, want []string |
| 193 | }{ |
| 194 | {"waiting_on", k.WaitingOn, []string{missing, open}}, {"cleared_by_ending", k.ClearedByEnding, []string{ended}}, |
| 195 | {"dangling", k.Dangling, []string{missing}}, {"unresolved", k.Unresolved, []string{}}, |
| 196 | } { |
| 197 | if !sameSet(c.got, c.want) { |
| 198 | t.Errorf("the export's %s of app's knot blocked by %s is %q, want %q", c.field, project, c.got, c.want) |
| 199 | } |
| 200 | } |
| 201 | if k.WaitingOnHidden { |
| 202 | t.Errorf("the export says app's knot blocked by %s waits on a hidden knot", project) |
| 203 | } |
| 204 | edges := map[string]export.Edge{} |
| 205 | for _, edge := range k.Edges { |
| 206 | edges[edge.Ref] = edge |
| 207 | } |
| 208 | for ref, want := range map[string][3]string{ |
| 209 | open: {export.EdgeResolved, "open", project + "-knot-text"}, ended: {export.EdgeResolved, "abandoned", project + "-abandoned-text"}, |
| 210 | missing: {export.EdgeDangling, "", ""}, |
| 211 | } { |
| 212 | edge, ok := edges[ref] |
| 213 | if !ok || edge.State != want[0] || deref(edge.Status) != want[1] || deref(edge.Title) != want[2] { |
| 214 | t.Errorf("the export's edge to %s is %+v, want %s with status %q and title %q", ref, edge, want[0], want[1], want[2]) |
| 215 | } |
| 216 | } |
| 217 | if len(edges) != 3 { |
| 218 | t.Errorf("app's knot blocked by %s has %d edges, want 3", project, len(edges)) |
| 219 | } |
| 220 | } |
| 221 | unexported := knotOf(e, "app", waits["notes"]) |
| 222 | if !unexported.WaitingOnHidden || len(unexported.WaitingOn)+len(unexported.ClearedByEnding)+len(unexported.Dangling)+len(unexported.Unresolved) > 0 { |
| 223 | t.Errorf("the export says of app's knot blocked by notes more than that it waits on a hidden knot: %+v", unexported) |
| 224 | } |
| 225 | if len(unexported.Edges) != 3 { |
| 226 | t.Errorf("app's knot blocked by notes has %d edges, want 3", len(unexported.Edges)) |
| 227 | } |
| 228 | for _, edge := range unexported.Edges { |
| 229 | if want := (export.Edge{Field: "blocked_by", Ref: edge.Ref, Project: "notes", ID: edge.ID, State: export.EdgeHidden}); !reflect.DeepEqual(edge, want) { |
| 230 | t.Errorf("the export's edge into notes is %+v, want %+v", edge, want) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Each reader's projection keeps what app's knots say of the |
| 235 | // projects that reader may see, and takes out the rest: a blocker in |
| 236 | // another private project leaves waiting_on_hidden and hidden edges |
| 237 | // alone, as one in notes does in the export. The real publisher |
| 238 | // writes no unresolved reference into a project it exports, so a |
| 239 | // second copy of the export is given one into site and one into docs. |
| 240 | shownTo := map[string][]string{ |
| 241 | export.Anyone: {"app"}, "bob": {"app"}, "alice": {"app", "site"}, "operator": {"app", "docs"}, |
| 242 | } |
| 243 | readers := []string{export.Anyone, "bob", "alice", "operator"} |
| 244 | for _, reader := range readers { |
| 245 | shown := map[string]bool{} |
| 246 | for _, p := range shownTo[reader] { |
| 247 | shown[p] = true |
| 248 | } |
| 249 | out, err := projectForReader(ctx, e, shown) |
| 250 | if err != nil { |
| 251 | t.Fatal(err) |
| 252 | } |
| 253 | var got []string |
| 254 | for _, p := range out.Projects { |
| 255 | got = append(got, p.Name) |
| 256 | } |
| 257 | sort.Strings(got) |
| 258 | if !reflect.DeepEqual(got, shownTo[reader]) { |
| 259 | t.Errorf("%s's projection holds %v, want %v", reader, got, shownTo[reader]) |
| 260 | } |
| 261 | for _, project := range []string{"site", "docs", "notes"} { |
| 262 | k, orig := knotOf(out, "app", waits[project]), knotOf(e, "app", waits[project]) |
| 263 | if shown[project] || project == "notes" { |
| 264 | if !reflect.DeepEqual(k, orig) { |
| 265 | t.Errorf("%s's projection changed app's knot blocked by %s:\n%+v\nwant\n%+v", reader, project, k, orig) |
| 266 | } |
| 267 | continue |
| 268 | } |
| 269 | if !k.WaitingOnHidden || len(k.WaitingOn)+len(k.ClearedByEnding)+len(k.Dangling)+len(k.Unresolved) > 0 { |
| 270 | t.Errorf("%s's projection says of app's knot blocked by %s more than that it waits on a hidden knot: %+v", reader, project, k) |
| 271 | } |
| 272 | if len(k.Edges) != len(orig.Edges) { |
| 273 | t.Fatalf("%s's projection has %d edges of app's knot blocked by %s, want %d", reader, len(k.Edges), project, len(orig.Edges)) |
| 274 | } |
| 275 | for i, edge := range k.Edges { |
| 276 | o := orig.Edges[i] |
| 277 | if want := (export.Edge{Field: o.Field, Ref: o.Ref, Project: o.Project, ID: o.ID, State: export.EdgeHidden}); !reflect.DeepEqual(edge, want) { |
| 278 | t.Errorf("%s's projection keeps the edge %+v, want %+v", reader, edge, want) |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | withUnresolved, err := export.Decode(data) |
| 284 | if err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | unresolved := []string{"site:oc-1111111", "docs:oc-1111111"} |
| 288 | knotOf(withUnresolved, "app", waits["site"]).Unresolved = unresolved |
| 289 | for _, reader := range readers { |
| 290 | shown := map[string]bool{} |
| 291 | var want []string |
| 292 | for _, p := range shownTo[reader] { |
| 293 | shown[p] = true |
| 294 | if p != "app" { |
| 295 | want = append(want, p+":oc-1111111") |
| 296 | } |
| 297 | } |
| 298 | out, err := projectForReader(ctx, withUnresolved, shown) |
| 299 | if err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | if got := knotOf(out, "app", waits["site"]).Unresolved; !sameSet(got, want) { |
| 303 | t.Errorf("%s's projection keeps the unresolved references %q, want %q", reader, got, want) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | srv := New(Config{Store: store, Now: time.Now, Log: func(err error) { t.Errorf("the server logged: %v", err) }}) |
| 308 | srv.reader = func(r *http.Request) string { |
| 309 | if name, ok := r.Context().Value(publishedReader{}).(string); ok { |
| 310 | return name |
| 311 | } |
| 312 | return export.Anyone |
| 313 | } |
| 314 | get := func(reader, target string) *httptest.ResponseRecorder { |
| 315 | r := httptest.NewRequest("GET", target, nil) |
| 316 | r = r.WithContext(context.WithValue(r.Context(), publishedReader{}, reader)) |
| 317 | w := httptest.NewRecorder() |
| 318 | srv.ServeHTTP(w, r) |
| 319 | return w |
| 320 | } |
| 321 | |
| 322 | // Every page of a project: its home, source pages, knots page and |
| 323 | // knots. The archive is checked for its status alone, as it is |
| 324 | // compressed. |
| 325 | bases := map[string]string{"app": "/tools/app", "site": "/tools/site", "docs": "/guides/docs", "notes": "/tools/notes"} |
| 326 | pages := func(project string) []string { |
| 327 | p := projects[project] |
| 328 | base := bases[project] |
| 329 | out := []string{base + "/", base + "/knots", base + "/source/tree/" + p.SourceTip + "/", base + "/source/blob/" + p.SourceTip + "/README.md", |
| 330 | base + "/source/log/" + p.SourceTip} |
| 331 | for _, k := range p.Knots { |
| 332 | out = append(out, base+"/knots/"+k.ID) |
| 333 | } |
| 334 | return out |
| 335 | } |
| 336 | archive := func(project string) string { |
| 337 | return bases[project] + "/source/archive/" + projects[project].SourceTip + ".tar.gz" |
| 338 | } |
| 339 | private := map[string]struct{ alias, former string }{"site": {"portal", "homepage"}, "docs": {"handbook", "manual"}} |
| 340 | // What only a private project says: its name, alias, former name, |
| 341 | // README, knot titles, comment and ready change, and docs' group's |
| 342 | // identifier and name. |
| 343 | secrets := func(project string) []string { |
| 344 | out := []string{project, project + "-readme-text", project + "-comment-text", project + "-change-subject"} |
| 345 | if project == "docs" { |
| 346 | out = append(out, "library", "guides") |
| 347 | } |
| 348 | if p, ok := private[project]; ok { |
| 349 | out = append(out, p.alias, p.former) |
| 350 | for _, k := range projects[project].Knots { |
| 351 | out = append(out, k.Title) |
| 352 | } |
| 353 | for _, c := range projects[project].Changes { |
| 354 | out = append(out, c.ChangeID) |
| 355 | } |
| 356 | } else { |
| 357 | out = append(out, project+"-knot-text", project+"-abandoned-text") |
| 358 | } |
| 359 | return out |
| 360 | } |
| 361 | for _, reader := range readers { |
| 362 | shown := map[string]bool{} |
| 363 | for _, p := range shownTo[reader] { |
| 364 | shown[p] = true |
| 365 | } |
| 366 | var hidden []string |
| 367 | answers := map[string]int{"/": http.StatusFound, knotsURL: http.StatusOK, projectsURL: http.StatusOK, |
| 368 | "/tools/": http.StatusOK, "/tools": http.StatusOK, "/nowhere/": http.StatusNotFound, |
| 369 | "/tools/nowhere/": http.StatusNotFound, archive("app"): http.StatusOK} |
| 370 | for _, project := range []string{"app", "site", "docs"} { |
| 371 | want := http.StatusNotFound |
| 372 | if shown[project] { |
| 373 | want = http.StatusOK |
| 374 | } else { |
| 375 | hidden = append(hidden, secrets(project)...) |
| 376 | } |
| 377 | for _, target := range pages(project) { |
| 378 | answers[target] = want |
| 379 | } |
| 380 | if shown[project] { |
| 381 | answers[bases[project]+"/"] = http.StatusFound |
| 382 | } |
| 383 | if p, ok := private[project]; ok { |
| 384 | moved := http.StatusPermanentRedirect |
| 385 | if !shown[project] { |
| 386 | moved = http.StatusNotFound |
| 387 | } |
| 388 | answers["/"+p.alias+"/"] = moved |
| 389 | group, _, _ := strings.Cut(strings.TrimPrefix(bases[project], "/"), "/") |
| 390 | answers["/"+group+"/"+p.former+"/source/tree/"+projects[project].SourceTip+"/"] = moved |
| 391 | } |
| 392 | } |
| 393 | for _, target := range []string{"/guides/", "/guides", "/library/"} { |
| 394 | answers[target] = http.StatusNotFound |
| 395 | if shown["docs"] && target != "/library/" { |
| 396 | answers[target] = http.StatusOK |
| 397 | } |
| 398 | } |
| 399 | hidden = append(hidden, secrets("notes")...) |
| 400 | answers["/tools/notes/"] = http.StatusNotFound |
| 401 | answers["/tools/notes/knots"] = http.StatusNotFound |
| 402 | for target, want := range answers { |
| 403 | w := get(reader, target) |
| 404 | if w.Code != want { |
| 405 | t.Errorf("GET %s as %s answered %d, want %d", target, reader, w.Code, want) |
| 406 | } |
| 407 | if strings.HasSuffix(target, ".tar.gz") { |
| 408 | continue |
| 409 | } |
| 410 | // The layout's navigation is of class site, in its markup and |
| 411 | // its stylesheet, which names no project. |
| 412 | page := w.Header().Get("Location") + "\n" + layoutSite.Replace(w.Body.String()) |
| 413 | for _, secret := range hidden { |
| 414 | if strings.Contains(page, secret) { |
| 415 | t.Errorf("GET %s as %s answered with %q, which only a project %s may not read says", target, reader, secret, reader) |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | for project, title := range waits { |
| 420 | page := get(reader, "/tools/app/knots/"+knotOf(e, "app", title).ID).Body.String() |
| 421 | if names := strings.Contains(page, project+"-knot-text"); names != shown[project] { |
| 422 | t.Errorf("app's knot blocked by %s names its blocker to %s: %v, want %v", project, reader, names, shown[project]) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // / lands on the tree of the one project the reader may see, and |
| 427 | // on the projects page when they may see more. |
| 428 | landing := projectsURL |
| 429 | if len(shownTo[reader]) == 1 { |
| 430 | landing = bases["app"] + "/source/tree/" + projects["app"].SourceTip + "/" |
| 431 | } |
| 432 | if to := get(reader, "/").Header().Get("Location"); to != landing { |
| 433 | t.Errorf("/ as %s redirects to %s, not to %s", reader, to, landing) |
| 434 | } |
| 435 | if nav := section(t, get(reader, knotsURL).Body.String(), `<nav class="site"`, `<span class="fresh">`); nav != `<nav class="site" aria-label="Pages"><a href="/-/projects">projects</a>` { |
| 436 | t.Errorf("the navigation as %s is %s", reader, nav) |
| 437 | } |
| 438 | |
| 439 | // The projects page lists the projects the reader may see, each |
| 440 | // linking its source and its knots, and each group's page those of |
| 441 | // its group. A group with none the reader may see answers as a |
| 442 | // group that does not exist. |
| 443 | row := func(project string) string { |
| 444 | base := bases[project] |
| 445 | return `<li class="listed"><a class="mono" href="` + base + `/source/tree/` + projects[project].SourceTip + `/">` + |
| 446 | strings.TrimPrefix(base, "/") + `</a><a href="` + base + `/knots">knots</a></li>` |
| 447 | } |
| 448 | listed, tools, guides := get(reader, projectsURL).Body.String(), get(reader, "/tools/").Body.String(), get(reader, "/guides/") |
| 449 | if n := strings.Count(listed, `<li class="listed">`); n != len(shownTo[reader]) { |
| 450 | t.Errorf("the projects page lists %d projects to %s, not %d", n, reader, len(shownTo[reader])) |
| 451 | } |
| 452 | for _, project := range []string{"app", "site", "docs"} { |
| 453 | if strings.Contains(listed, row(project)) != shown[project] { |
| 454 | t.Errorf("the projects page lists %s to %s: %v, want %v", project, reader, !shown[project], shown[project]) |
| 455 | } |
| 456 | inGroup := strings.Contains(tools, row(project)) |
| 457 | if project == "docs" { |
| 458 | inGroup = strings.Contains(guides.Body.String(), row(project)) |
| 459 | } |
| 460 | if inGroup != shown[project] { |
| 461 | t.Errorf("%s's group's page lists it to %s: %v, want %v", project, reader, inGroup, shown[project]) |
| 462 | } |
| 463 | } |
| 464 | if missing := get(reader, "/nowhere/"); !shown["docs"] && (fmt.Sprint(guides.Header()) != fmt.Sprint(missing.Header()) || |
| 465 | !bytes.Equal(guides.Body.Bytes(), missing.Body.Bytes())) { |
| 466 | t.Errorf("/guides/ answers %s otherwise than a group that does not exist", reader) |
| 467 | } |
| 468 | |
| 469 | // Each project's knots page links its own knots alone, under its |
| 470 | // project header, whose knots tab is current. Its tree's header |
| 471 | // marks the source tab. |
| 472 | for _, project := range shownTo[reader] { |
| 473 | base, tip := bases[project], projects[project].SourceTip |
| 474 | tabs := func(current string) string { |
| 475 | out := `<nav class="tabs" aria-label="Project">` |
| 476 | for _, tab := range [][2]string{{"source", base + "/source/tree/" + tip + "/"}, {"knots", base + "/knots"}} { |
| 477 | mark := "" |
| 478 | if tab[0] == current { |
| 479 | mark = ` aria-current="page"` |
| 480 | } |
| 481 | out += `<a href="` + tab[1] + `"` + mark + `>` + tab[0] + `</a>` |
| 482 | } |
| 483 | return out + `</nav>` |
| 484 | } |
| 485 | own := get(reader, base+"/knots").Body.String() |
| 486 | if !strings.Contains(own, `<span class="mono">`+strings.TrimPrefix(base, "/")+`</span>`) || !strings.Contains(own, tabs("knots")) { |
| 487 | t.Errorf("%s's knots page as %s has no project header with its knots tab current", project, reader) |
| 488 | } |
| 489 | if tree := get(reader, base+"/source/tree/"+tip+"/").Body.String(); !strings.Contains(tree, tabs("source")) { |
| 490 | t.Errorf("%s's tree as %s has no project header with its source tab current", project, reader) |
| 491 | } |
| 492 | for _, k := range projects[project].Knots { |
| 493 | if !strings.Contains(own, `href="`+base+`/knots/`+k.ID+`"`) { |
| 494 | t.Errorf("%s's knots page as %s does not link its knot %s", project, reader, k.ID) |
| 495 | } |
| 496 | } |
| 497 | for _, link := range regexp.MustCompile(`href="([^"]*/knots/[^"]*)"`).FindAllStringSubmatch(own, -1) { |
| 498 | if !strings.HasPrefix(link[1], base+"/knots/") { |
| 499 | t.Errorf("%s's knots page as %s links %s", project, reader, link[1]) |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // layoutSite takes out of a page the layout's class site. |
| 507 | var layoutSite = strings.NewReplacer(`<nav class="site"`, "", "nav.site", "") |
| 508 | |
| 509 | // sameSet reports whether a and b hold the same strings, in any order. |
| 510 | func sameSet(a, b []string) bool { |
| 511 | a, b = slices.Clone(a), slices.Clone(b) |
| 512 | sort.Strings(a) |
| 513 | sort.Strings(b) |
| 514 | return slices.Equal(a, b) |
| 515 | } |
| 516 | |
| 517 | func deref(s *string) string { |
| 518 | if s == nil { |
| 519 | return "" |
| 520 | } |
| 521 | return *s |
| 522 | } |