Artifact 1430e05b1462a5a6fb118bf244ead3db815c3e30c5b738ca974863ec0e921752:
- File
internal/web/web_test.go
— part of check-in
[3253bba7bb]
at
2026-08-15 16:15:08
on branch trunk
— layout: persistent nav, code-block styling, single h1, a11y (010/100/110/120/140)
Header nav + search form in base; markdown headings demoted one level; pre gets --code-bg; tables scroll in .tw wrappers; th scope attrs. (user: dmestas, size: 3361) [annotate] [blame] [check-ins using]
package web import ( "net/http/httptest" "path/filepath" "strings" "testing" "craftdesign.group/quarry/internal/api" "craftdesign.group/quarry/internal/index" "craftdesign.group/quarry/internal/ops" ) func testRegistry(t *testing.T) *ops.Registry { t.Helper() // An empty (but real) index so entity lookups answer not-found // rather than "no store configured". ix, err := index.Open(filepath.Join(t.TempDir(), "index.db")) if err != nil { t.Fatal(err) } t.Cleanup(func() { ix.Close() }) r := ops.NewRegistry() if err := api.RegisterAll(r, api.Deps{Version: "9.9.9-test", Index: ix}); err != nil { t.Fatal(err) } return r } func TestHomePageRendersThroughVersionOp(t *testing.T) { rec := httptest.NewRecorder() Handler(testRegistry(t)).ServeHTTP(rec, httptest.NewRequest("GET", "/", nil)) if rec.Code != 200 { t.Fatalf("status %d", rec.Code) } body := rec.Body.String() if !strings.Contains(body, "quarry 9.9.9-test") { t.Fatalf("footer missing op-served version:\n%s", body) } if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") { t.Fatalf("content-type %q", ct) } if strings.Contains(body, "<script") { t.Fatal("no-JS baseline violated: script tag on the base page") } if !strings.Contains(body, "prefers-color-scheme") { t.Fatal("dark theme missing") } } func TestUnknownPathIs404(t *testing.T) { rec := httptest.NewRecorder() Handler(testRegistry(t)).ServeHTTP(rec, httptest.NewRequest("GET", "/nope", nil)) if rec.Code != 404 { t.Fatalf("status %d", rec.Code) } } func TestThemed404Page(t *testing.T) { for _, path := range []string{"/nope", "/wiki/NoSuchPage", "/checkin/deadbeefdead", "/ticket/deadbeef"} { rec := httptest.NewRecorder() Handler(testRegistry(t)).ServeHTTP(rec, httptest.NewRequest("GET", path, nil)) if rec.Code != 404 { t.Errorf("%s: status %d", path, rec.Code) continue } body := rec.Body.String() if !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") { t.Errorf("%s: content-type %q — bare default 404?", path, rec.Header().Get("Content-Type")) } if !strings.Contains(body, "quarry 9.9.9-test") { t.Errorf("%s: 404 page missing version footer", path) } if !strings.Contains(body, `href="/timeline"`) { t.Errorf("%s: 404 page has no way back into the site", path) } } } func TestPersistentNavOnEveryPage(t *testing.T) { // Ticket 010: every page carries the site header — nav links and the // search form — not just the homepage. for _, path := range []string{"/", "/timeline", "/wiki", "/ticket", "/search", "/nope"} { rec := httptest.NewRecorder() Handler(testRegistry(t)).ServeHTTP(rec, httptest.NewRequest("GET", path, nil)) body := rec.Body.String() for _, want := range []string{`<nav>`, `href="/timeline"`, `href="/wiki"`, `href="/ticket"`, `action="/search"`} { if !strings.Contains(body, want) { t.Errorf("%s: missing %s in header", path, want) } } } } func TestTablesScrollInWrapper(t *testing.T) { // Ticket 120: wide tables scroll inside .tw, not the page. rec := httptest.NewRecorder() Handler(testRegistry(t)).ServeHTTP(rec, httptest.NewRequest("GET", "/timeline", nil)) body := rec.Body.String() if strings.Contains(body, "<table>") && !strings.Contains(body, `<div class="tw"><table>`) { t.Fatalf("timeline table not wrapped:\n%s", body) } }