package chrome import ( "html/template" "net/http/httptest" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // testSections is the row artifacts declares, which is the shape that motivated // the type: three sections with a path of their own, and a first one standing // for the service root plus the listing rows reached from it. func testSections() []Section { return []Section{ {Name: "channels", Href: "/", Paths: []string{"/"}, Prefixes: []string{"/~"}}, {Name: "images", Href: "/images", Paths: []string{"/images"}}, {Name: "cache", Href: "/cache", Paths: []string{"/cache"}}, {Name: "mirrors", Href: "/mirrors", Paths: []string{"/mirrors"}}, } } // renderSections executes a layout invoking the section partial, the way a // service's own layout does. func renderSections(t *testing.T, v any) string { t.Helper() tpl := MustAttach(template.New("layout")) tpl, err := tpl.Parse(`{{template "srht-sections" .}}`) require.NoError(t, err) var b strings.Builder require.NoError(t, tpl.Execute(&b, v)) return b.String() } // activeNames is the tabs a path lights up, which for a well-formed row is // never more than one. func activeNames(tabs []SectionTab) []string { var names []string for _, tab := range tabs { if tab.Active { names = append(names, tab.Name) } } return names } func TestSectionRowRendersUpstreamMarkup(t *testing.T) { svc := NewService(testConf(), "diff.sr.ht") svc.Sections = testSections() page := svc.Page(httptest.NewRequest("GET", "/cache", nil), "t", "alice") out := renderSections(t, page) // The classes are the whole reason the row needs no stylesheet of its own: // both come from core.sr.ht's nav.scss through every service's base import. assert.Contains(t, out, `