package web
import (
"strings"
"testing"
)
// On a phone /me closes to one summary row per section (settings.js
// folds the <details>). The page must draw every section as a details
// with a summary that can say what it is set to, give the matrix the
// column words a stacked phone layout needs, and say "no address" once.
func TestMeSectionsFoldToSummaryRows(t *testing.T) {
me := meView{Name: "jan", Admin: true, PasswordAvailable: true,
Email: accountEmailView{Available: true},
Notify: &prefsView{
Email: accountEmailView{Available: true},
Rows: []prefRow{{Family: "mention", Label: "someone mentions me", Timing: "immediate",
Cells: []prefCell{{Name: "mention.inapp", Column: "inapp", ColLabel: "in app", On: true},
{Name: "mention.email", Column: "email", ColLabel: "email"}}},
{Family: "reply", Label: "a reply", Timing: "immediate",
Cells: []prefCell{{Name: "reply.email", Column: "email", ColLabel: "email"}}}},
},
}
var b strings.Builder
if err := noHooks.template(mePageTmpl).ExecuteTemplate(&b, "content",
pageData{Title: "jan", User: "jan", Me: &me}); err != nil {
t.Fatal(err)
}
got := b.String()
for _, id := range []string{"email", "password", "access", "sessions", "notifications"} {
i := strings.Index(got, `<section id="`+id+`"`)
if i < 0 {
t.Errorf("FAIL: no %s section", id)
continue
}
// Up to the next section's fold: notifications holds a nested
// push section, so its own </section> comes early.
sec := got[i:]
if own := strings.Index(sec, `<details class="me-sec"`); own >= 0 {
if next := strings.Index(sec[own+1:], `<details class="me-sec"`); next >= 0 {
sec = sec[:own+1+next]
}
}
if !strings.Contains(sec, `<details class="me-sec" open data-hook="me-sec"><summary class="me-sum">`) {
t.Errorf("FAIL: section %s is not an open details with a summary row", id)
}
if !strings.Contains(sec, "</details>") {
t.Errorf("FAIL: section %s's details is not closed inside it", id)
}
}
if n := strings.Count(got, `data-hook="me-sec-val"`); n < 3 {
t.Errorf("FAIL: %d summary values, want one each for email, access and sessions", n)
}
if !strings.Contains(got, `class="pm-col"`) {
t.Error("FAIL: matrix cells carry no column word for the phone layout")
}
if n := strings.Count(got, `data-hook="email-unset-once"`); n != 1 {
t.Errorf("FAIL: the no-address note appears %d times, want once", n)
}
if strings.Count(got, "<details") != strings.Count(got, "</details>") {
t.Error("FAIL: details tags are unbalanced")
}
// A confirmed address: no once-note.
me.Notify.Email.Confirmed, me.Email.Confirmed = "[email protected]", "[email protected]"
var c strings.Builder
if err := noHooks.template(mePageTmpl).ExecuteTemplate(&c, "content",
pageData{Title: "jan", User: "jan", Me: &me}); err != nil {
t.Fatal(err)
}
if strings.Contains(c.String(), `data-hook="email-unset-once"`) {
t.Error("FAIL: the no-address note shows with an address confirmed")
}
if !t.Failed() {
t.Log("ok")
}
}