package mapatest
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"amadan.net/oficina/mapa/internal/mapa"
)
// Paul's ruling of 2026-11-16 on the sidebar (build note 80), as the
// server draws it, so as the screen is with scripts off: Recently
// edited is team pages only, the Scratch screen lists scratch pages,
// a pinned note keeps a lock, and exactly one row has the full
// highlight. The browser half is sidebar_browser_test.go.
const secretLine = "Zanzibar secret first line"
// sideNav is the sidebar's markup on a page.
func sideNav(html string) string {
return between(html, "")
}
// withSide is a GET carrying the mapa-sidebar cookie, "" for none.
func (rig *rig) withSide(t *testing.T, who, path, state string) string {
t.Helper()
request := httptest.NewRequest(http.MethodGet, path, nil)
if state != "" {
request.AddCookie(&http.Cookie{Name: "mapa-sidebar", Value: state})
}
recorder := httptest.NewRecorder()
rig.built.As(who).ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("GET %s: %d", path, recorder.Code)
}
return recorder.Body.String()
}
// lit is the label of the one sidebar row with aria-current that the
// open sidebar (rail false) or the rail shows, failing unless there is
// exactly one. CSS decides what is shown: the open sidebar hides a
// place's lit copy (mapa-side-holds="rail"); the rail hides the page
// rows (mapa-side-page) and a place's unlit copy.
func lit(t *testing.T, side string, rail bool) string {
t.Helper()
var labels []string
for _, row := range strings.Split(side, "")
hidden := strings.Contains(tag, `mapa-side-holds="rail"`)
if rail {
hidden = strings.Contains(tag, "mapa-side-page") ||
strings.Contains(tag, `mapa-side-holds="open"`)
}
if !hidden && strings.Contains(tag, `aria-current="page"`) {
labels = append(labels, between(row, "",
"<"))
}
}
if len(labels) != 1 {
t.Fatalf("rail %v: %d rows have the full highlight %v:\n%s", rail,
len(labels), labels, side)
}
return labels[0]
}
// unstyled is how many rows carry aria-current with no CSS at all,
// where only the hidden attribute hides a row (review low 4).
func unstyled(side string) int {
count := 0
for _, row := range strings.Split(side, "")
if strings.Contains(tag, `aria-current="page"`) &&
!strings.Contains(tag, " hidden") {
count++
}
}
return count
}
// writeNotes gives Ana a team page, a scratch page and today's note,
// each written by her, and answers their paths.
func writeNotes(t *testing.T, rig *rig) (team, scratch, day string) {
t.Helper()
team = rig.createPage(t, Ana, "Quarterly plan", "Team text.")
made := rig.post(t, Ana, "/scratch",
url.Values{"body": {secretLine + "\n\nMore."}})
scratch = made.Header().Get("Location")
day = "/d/" + time.Now().UTC().Format(time.DateOnly)
if saved := rig.post(t, Ana, day, url.Values{
"body": {"Daily words."}}); saved.Code != http.StatusSeeOther {
t.Fatalf("writing today: %d", saved.Code)
}
return team, scratch, day
}
func TestRecentlyEditedListsTeamPagesOnly(t *testing.T) {
rig := newRig(t)
team, scratch, day := writeNotes(t, rig)
for _, path := range []string{"/pages", team, scratch, day, "/"} {
for _, state := range []string{"", "open", "rail"} {
html := rig.withSide(t, Ana, path, state)
side := sideNav(html)
recent := between(side, "Recently edited", "mapa-side-foot")
if !strings.Contains(recent, "Quarterly plan") {
t.Fatalf("%s: the team page is not in Recently edited",
path)
}
if strings.Contains(side, secretLine) ||
strings.Contains(barNav(html), secretLine) {
t.Fatalf("%s (%q): a scratch page's first line is in the "+
"sidebar or the Menu sheet", path, state)
}
if strings.Contains(recent, `href="`+scratch+`"`) ||
strings.Contains(recent, `href="`+day+`"`) {
t.Fatalf("%s: a note of Ana's own is in Recently edited:"+
"\n%s", path, recent)
}
}
}
}
func TestTheScratchScreenListsYourScratchPages(t *testing.T) {
rig := newRig(t)
rig.post(t, Ana, "/scratch", url.Values{"body": {"Older note"}})
rig.post(t, Ana, "/scratch", url.Values{"body": {"Newer note"}})
html := rig.page(t, Ana, "/scratch")
list := between(html, `aria-label="Your scratch pages"`, "")
if !strings.Contains(html, "Only you see these") ||
strings.Index(list, "Newer note") < 0 ||
strings.Index(list, "Newer note") > strings.Index(list,
"Older note") {
t.Fatalf("the Scratch screen:\n%s", list)
}
}
func TestAPinnedNoteStaysWithALock(t *testing.T) {
rig := newRig(t)
_, scratch, _ := writeNotes(t, rig)
rig.post(t, Ana, scratch+"/pin", url.Values{"pin": {"1"}})
pinned := between(sideNav(rig.page(t, Ana, "/pages")), "Pinned",
"Recently edited")
if !strings.Contains(pinned, secretLine) ||
!strings.Contains(pinned, "mapa-side-lock") ||
!strings.Contains(pinned, "only you see this") {
t.Fatalf("the pinned scratch page:\n%s", pinned)
}
}
// One full highlight for each kind of page, open and in the rail.
func TestOneFullHighlightForEachPage(t *testing.T) {
rig := newRig(t)
team, scratch, day := writeNotes(t, rig)
pinned := rig.createPage(t, Ana, "Pinned plan", "Pinned.")
rig.post(t, Ana, pinned+"/pin", url.Values{"pin": {"1"}})
theirs := rig.createPage(t, Bo, "Bo's page", "Not Ana's.")
pinnedNote := rig.post(t, Ana, "/scratch",
url.Values{"body": {"Pinned note"}}).Header().Get("Location")
rig.post(t, Ana, pinnedNote+"/pin", url.Values{"pin": {"1"}})
cases := []struct{ path, open, rail string }{
{scratch, "Scratch", "Scratch"},
{"/", "Today", "Today"},
{day, "Today", "Today"},
{"/d/2025-01-06", "Today", "Today"},
{team, "Quarterly plan", "All pages"},
{pinned, "Pinned plan", "All pages"},
{theirs, "All pages", "All pages"},
{pinnedNote, "Pinned note", "Scratch"},
}
for _, each := range cases {
html := rig.page(t, Ana, each.path)
for rail, want := range map[bool]string{false: each.open,
true: each.rail} {
if got := lit(t, sideNav(html), rail); got != want {
t.Errorf("%s (rail %v): lit %q, want %q", each.path, rail,
got, want)
}
}
if got := unstyled(sideNav(html)); got != 1 {
t.Errorf("%s: with no CSS %d rows are lit", each.path, got)
}
// The Menu sheet has places only: the page's place is lit.
if got := strings.Count(barNav(html),
`aria-current="page"`); got != 1 {
t.Errorf("%s: the Menu sheet lights %d rows", each.path, got)
}
}
}
// Recently edited still holds six team pages after more private writes
// than the window of events it first reads (review M1): the team filter
// is in the query, so private writes cannot push team pages out of it.
func TestRecentlyEditedOutlastsManyPrivateWrites(t *testing.T) {
rig := newRig(t)
for number := 1; number <= 7; number++ {
rig.createPage(t, Ana, fmt.Sprintf("Team page %d", number), "Text.")
}
for number := 0; number < 210; number++ {
rig.post(t, Ana, "/scratch",
url.Values{"body": {fmt.Sprintf("Note %d", number)}})
}
recent := between(sideNav(rig.page(t, Ana, "/pages")),
"Recently edited", "mapa-side-foot")
if got := strings.Count(recent, "mapa-side-page"); got != 6 {
t.Fatalf("Recently edited holds %d rows:\n%s", got, recent)
}
for number := 2; number <= 7; number++ {
if !strings.Contains(recent, fmt.Sprintf("Team page %d<", number)) {
t.Fatalf("Team page %d is not in Recently edited", number)
}
}
}
// recentRows is the titles in Ana's Recently edited, in order.
func recentRows(t *testing.T, rig *rig) []string {
t.Helper()
recent := between(sideNav(rig.page(t, Ana, "/pages")),
"Recently edited", "mapa-side-foot")
var titles []string
for _, row := range strings.Split(recent, "")[1:] {
title, _, _ := strings.Cut(row, "<")
titles = append(titles, title)
}
return titles
}
// Six team pages, then 900 edits to one: the other five are still
// there (review round 6 L2). A window of the newest events, capped,
// held only the one page.
func TestRecentlyEditedOutlastsManyEditsToOnePage(t *testing.T) {
rig := newRig(t)
ctx := context.Background()
var heavy mapa.Page
for number := 1; number <= 6; number++ {
page, err := rig.built.Store.CreateTeamPage(ctx, Ana,
fmt.Sprintf("Team page %d", number), "Text.")
if err != nil {
t.Fatal(err)
}
heavy = page
}
revision := heavy.Revision
for number := range 900 {
if _, err := rig.built.Store.Commit(ctx, Ana, heavy.ID, revision,
fmt.Sprintf("Edit %d.", number)); err != nil {
t.Fatal(err)
}
err := rig.db.G.Raw("SELECT revision FROM pages WHERE id = ?",
heavy.ID).Scan(&revision).Error
if err != nil {
t.Fatal(err)
}
}
got := strings.Join(recentRows(t, rig), ", ")
if got != "Team page 6, Team page 5, Team page 4, Team page 3, "+
"Team page 2, Team page 1" {
t.Fatalf("Recently edited: %s", got)
}
}
// Deleted team pages among the recent writes leave six live rows, and
// no deleted title (review round 6 L3).
func TestRecentlyEditedLeavesOutDeletedPages(t *testing.T) {
rig := newRig(t)
ctx := context.Background()
for number := 1; number <= 9; number++ {
page, err := rig.built.Store.CreateTeamPage(ctx, Ana,
fmt.Sprintf("Team page %d", number), "Text.")
if err != nil {
t.Fatal(err)
}
if number%3 == 0 {
if err := rig.built.Store.Delete(ctx, Ana, page.ID); err != nil {
t.Fatal(err)
}
}
}
got := strings.Join(recentRows(t, rig), ", ")
if got != "Team page 8, Team page 7, Team page 5, Team page 4, "+
"Team page 2, Team page 1" {
t.Fatalf("Recently edited: %s", got)
}
}