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")
}
}
// Every branch of every folded section closes its own <details>, and
// each summary value follows the state it reports.
func TestMeSummaryValuesFollowState(t *testing.T) {
render := func(t *testing.T, me meView, ap *appearView) string {
t.Helper()
var b strings.Builder
if err := noHooks.template(mePageTmpl).ExecuteTemplate(&b, "content",
pageData{Title: "jan", User: "jan", Me: &me, Appear: ap}); err != nil {
t.Fatal(err)
}
return b.String()
}
vals := func(page string) map[string]string {
out := map[string]string{}
for _, part := range strings.Split(page, `<section id="`)[1:] {
id := part[:strings.Index(part, `"`)]
const hook = `data-hook="me-sec-val">`
if k := strings.Index(part, hook); k >= 0 && k < strings.Index(part+"</summary>", "</summary>") {
v := part[k+len(hook):]
out[id] = v[:strings.Index(v, "</span>")]
}
}
return out
}
full := meView{Name: "jan", PasswordAvailable: true, Notify: &prefsView{},
Email: accountEmailView{Available: true, Confirmed: "[email protected]"},
Access: []meAccessRow{{Repo: "quarry", Role: "Reader"}},
Push: meSyncView{Available: true, Held: true},
Agents: meAgentsView{Show: true, Rows: []agentView{{Name: "a1"}, {Name: "a2"}}},
Sessions: []sessionRow{{}, {}, {}},
}
ap := &appearView{Site: []themeOption{{ID: "t1", Name: "Theme One"}}, SiteID: "t1"}
page := render(t, full, ap)
if o, cl := strings.Count(page, "<details"), strings.Count(page, "</details>"); o != cl {
t.Errorf("FAIL: %d <details> opened, %d closed with every section drawn", o, cl)
}
on := vals(page)
// Values that carry data: the address, the counts, the theme's name.
for id, want := range map[string]string{"email": "[email protected]", "access": "1", "agents": "2",
"sessions": "3", "appearance": "Theme One"} {
if !strings.Contains(on[id], want) {
t.Errorf("FAIL: %s summary %q does not carry %q", id, on[id], want)
}
}
// The same sections in their other state must say something else.
other := full
other.Email = accountEmailView{Available: true}
other.Admin, other.Access = true, nil
other.Push.Held = false
other.Sessions = []sessionRow{{}}
off := vals(render(t, other, nil))
for _, id := range []string{"email", "access", "push", "sessions"} {
if off[id] == "" || off[id] == on[id] {
t.Errorf("FAIL: %s summary does not change with its state (%q then %q)", id, on[id], off[id])
}
}
// Nothing optional available: still balanced, and no value drawn
// for a section that is not there.
bare := meView{Name: "jan", Notify: &prefsView{}, Sessions: []sessionRow{{}}}
page = render(t, bare, nil)
if o, cl := strings.Count(page, "<details"), strings.Count(page, "</details>"); o != cl {
t.Errorf("FAIL: %d <details> opened, %d closed with nothing optional", o, cl)
}
for _, id := range []string{"email", "push", "agents", "appearance"} {
if v, ok := vals(page)[id]; ok && v != "" {
t.Errorf("FAIL: %s shows %q with nothing to report", id, v)
}
}
if !t.Failed() {
t.Log("ok")
}
}