package layout import ( "bytes" "html/template" "strings" "testing" "testing/fstest" ) func testPagesFS() fstest.MapFS { return fstest.MapFS{ "pages/dashboard.html.tmpl": &fstest.MapFile{Data: []byte( `{{define "content"}}
hello {{.Title}}
{{end}}` + `{{define "fragment"}}fragment {{.Title}}
{{end}}`, )}, } } func testFuncs() template.FuncMap { return template.FuncMap{"buildInfo": func() string { return "test-build" }} } func TestNewRendererAndPage(t *testing.T) { r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs()) if err != nil { t.Fatalf("NewRenderer: %v", err) } var buf bytes.Buffer data := PageHeader{ Title: "Dashboard", AppName: "example", Brand: "EXAMPLE", BrandInitial: "e", BasePath: "", NavItems: []NavItem{{ID: "dashboard", Label: "Dashboard", Href: "/"}}, ActiveNav: "dashboard", } if err := r.Page(&buf, "dashboard", data); err != nil { t.Fatalf("Page: %v", err) } out := buf.String() for _, want := range []string{ "hello Dashboard
", `class="nav-link active"`, "test-build", "fragment Dashboard
"; got != want { t.Errorf("ExecuteNamed rendered %q, want %q", got, want) } } func TestPageUnknown(t *testing.T) { r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs()) if err != nil { t.Fatalf("NewRenderer: %v", err) } if err := r.Page(&bytes.Buffer{}, "nope", PageHeader{}); err == nil { t.Fatal("expected error for unknown page") } } // TestThemeBlocksInSync guards the hand-kept duplication in // static/style.css: every theme declares the same --yw-* properties, and // the "system" media-query block is identical to "light". func TestThemeBlocksInSync(t *testing.T) { css, err := staticFS.ReadFile("static/style.css") if err != nil { t.Fatal(err) } block := func(selector string) string { s := string(css) i := strings.Index(s, selector+" {") if i < 0 { t.Fatalf("style.css: no block %q", selector) } s = s[i+len(selector)+2:] return s[:strings.Index(s, "}")] } decls := func(b string) (names, full []string) { for _, line := range strings.Split(b, "\n") { line = strings.TrimSpace(line) if line == "" { continue } full = append(full, line) names = append(names, strings.SplitN(line, ":", 2)[0]) } return names, full } darkNames, _ := decls(block(`:root[data-theme="system"]`)) lightNames, light := decls(block(`:root[data-theme="light"]`)) _, system := decls(block(` :root[data-theme="system"]`)) if strings.Join(darkNames, ",") != strings.Join(lightNames, ",") { t.Errorf("dark and light declare different properties:\ndark: %v\nlight: %v", darkNames, lightNames) } if strings.Join(light, "\n") != strings.Join(system, "\n") { t.Errorf("light and prefers-color-scheme: light blocks differ:\nlight: %v\nsystem: %v", light, system) } } // TestLogoutIsPostForm pins the navbar's logout control to a POST form: // a GET link would let any foreign page (an