package side import ( "bytes" "html/template" "net/http" "net/http/httptest" "os/exec" "strings" "testing" "amadan.net/oficina/estilo.git/internal/tokencheck" ) const glyph = template.HTML(``) func render(t *testing.T, s Side) string { t.Helper() tmpl, err := template.ParseFS(Templates(), "templates/*.html") if err != nil { t.Fatal(err) } var b bytes.Buffer if err := tmpl.ExecuteTemplate(&b, "estilo-side", s); err != nil { t.Fatal(err) } return b.String() } func sample() Side { return Side{ Label: "Mapa", Places: []Item{ {Href: "/", Label: "Home", Icon: glyph, Current: true}, {Href: "/search", Label: "Search", Icon: glyph, Key: "⌘K"}, }, Sections: []Section{ {Heading: "Pinned", Items: []Item{ {Href: "/p/a", Label: "Alpha", Icon: glyph, Current: true, Locked: ", only you see this"}, }}, {Heading: "Recently edited", Items: []Item{ {Href: "/p/a", Label: "Alpha", Icon: glyph, Current: true}, {Href: "/p/b", Label: "Beta", Icon: glyph}, }}, {Heading: "Empty"}, }, Foot: template.HTML(`` + string(glyph) + `New`), Fold: &Fold{Action: "/view/sidebar", Return: "/p/a", Hide: "Hide sidebar", Show: "Show sidebar", Key: `⌘\`, Cookie: "mapa-sidebar"}, } } // Exactly one full highlight: the first page row for the page, and its // place drawn twice (unlit open, lit and hidden for the rail). func TestLightLeavesOneHighlight(t *testing.T) { s := sample() s.Light() if s.Places[0].Current || !s.Places[0].Holds { t.Errorf("place holding the page: Current %v Holds %v, want false true", s.Places[0].Current, s.Places[0].Holds) } if s.Places[1].Holds { t.Error("a place not holding the page became Holds") } if !s.Sections[0].Items[0].Current || s.Sections[1].Items[0].Current { t.Error("the first page row must keep the highlight and the second lose it") } html := render(t, s) if n := strings.Count(html, `aria-current="page"`); n != 2 { t.Errorf("%d rows carry aria-current, want 2 (the page row and the rail's hidden place)", n) } for _, want := range []string{ ``, ``, } { if !strings.Contains(html, want) { t.Errorf("missing %s", want) } } } // With no page row lit, the place keeps the highlight and is drawn once. func TestLightKeepsThePlaceWithoutAPageRow(t *testing.T) { s := sample() s.Sections = nil s.Light() if !s.Places[0].Current || s.Places[0].Holds { t.Error("the place must keep the highlight when no page row has it") } if strings.Contains(render(t, s), "estilo-side-holds") { t.Error("a place is drawn twice with no page row lit") } } func TestRendersRowsHeadsFootAndFold(t *testing.T) { s := sample() s.Light() html := render(t, s) for _, want := range []string{ `