oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "fmt"
8 "strings"
9 "testing"
10 "time"
11
12 "github.com/chromedp/chromedp"
13 "github.com/chromedp/chromedp/kb"
14
15 "amadan.net/oficina/mapa/internal/mapa"
16)
17
18// Paul's ruling of 2026-11-16 on the sidebar (build note 80), in real
19// Chromium, scripts off and on, open and in the rail: no note of your
20// own in Recently edited, in the rail's tooltips or in the Menu sheet,
21// and exactly one row the person can see with the full highlight.
22
23// litShown is the label of every element with aria-current="page" that
24// is on screen, the sidebar's and the bar's alike.
25const litShown = `Array.prototype.filter.call(
26 document.querySelectorAll('[aria-current="page"]'),
27 function (el) { return el.checkVisibility(); }
28).map(function (el) {
29 var label = el.querySelector("[mapa-side-label]") || el;
30 return label.textContent.trim();
31}).join(" | ")`
32
33// sidebarText is all the sidebar and the bar's nav hold, shown or not:
34// the rail's tooltips are its labels.
35const sidebarText = `[document.querySelector("[mapa-side]"),
36 document.querySelector("[estilo-bar-nav]")].map(function (el) {
37 return el ? el.textContent : "";
38}).join("\n")`
39
40// ownNotes are Ana's: a team page she wrote, a pinned team page, a
41// scratch page, a pinned scratch page and today's note.
42type ownNotes struct {
43 team, pinned, scratch, pinnedNote, day, theirs string
44}
45
46func writeOwnNotes(t *testing.T, store *mapa.Store) ownNotes {
47 t.Helper()
48 ctx := context.Background()
49 // Ana's viewer is read once her first write has made her a member.
50 viewer := func() mapa.Viewer {
51 found, err := store.Viewer(ctx, Ana, nil)
52 if err != nil {
53 t.Fatal(err)
54 }
55 return found
56 }
57 page := func(made mapa.Page, err error) string {
58 t.Helper()
59 if err != nil {
60 t.Fatal(err)
61 }
62 return mapa.PagePath(made)
63 }
64 pin := func(path string, made mapa.Page) {
65 if err := store.SetPin(ctx, viewer(), made.ID, true); err != nil {
66 t.Fatalf("pinning %s: %v", path, err)
67 }
68 }
69 notes := ownNotes{}
70 team, err := store.CreateTeamPage(ctx, Ana, "Quarterly plan", "Text.")
71 notes.team = page(team, err)
72 pinned, err := store.CreateTeamPage(ctx, Ana, "Pinned plan", "Text.")
73 notes.pinned = page(pinned, err)
74 pin(notes.pinned, pinned)
75 scratch, err := store.CreateScratch(ctx, Ana, secretLine+"\n\nMore.")
76 notes.scratch = page(scratch, err)
77 note, err := store.CreateScratch(ctx, Ana, "Pinned note")
78 notes.pinnedNote = page(note, err)
79 pin(notes.pinnedNote, note)
80 theirs, err := store.CreateTeamPage(ctx, Bo, "Bo's page", "Text.")
81 notes.theirs = page(theirs, err)
82 notes.day = "/d/" + time.Now().UTC().Format(time.DateOnly)
83 if _, _, err := store.SaveDay(ctx, viewer(), notes.day[3:], 0, 0,
84 "Daily words."); err != nil {
85 t.Fatal(err)
86 }
87 return notes
88}
89
90func TestTheSidebarHasOneHighlightAndNoPrivateLines(t *testing.T) {
91 rig, store := browserRig(t, 0)
92 notes := writeOwnNotes(t, store)
93 pinnedNote := "Pinned note, only you see this"
94 cases := []struct{ path, open, rail string }{
95 {notes.scratch, "Scratch", "Scratch"},
96 {"/", "Today", "Today"},
97 {notes.day, "Today", "Today"},
98 {"/d/2025-01-06", "Today", "Today"},
99 {notes.team, "Quarterly plan", "All pages"},
100 {notes.pinned, "Pinned plan", "All pages"},
101 {notes.theirs, "All pages", "All pages"},
102 {notes.pinnedNote, pinnedNote, "Scratch"},
103 {"/pages", "All pages", "All pages"},
104 }
105 for _, live := range []bool{false, true} {
106 for _, side := range sideCases {
107 for _, each := range cases {
108 name := fmt.Sprintf("%s at %dpx, %s (cookie %q), scripts %v",
109 each.path, side.width, side.show, side.cookie, live)
110 drive(t, rig, scripts(live), sideCookie(rig, side.cookie),
111 chromedp.EmulateViewport(side.width, 877),
112 chromedp.Navigate(rig.Origin+each.path),
113 chromedp.WaitVisible("[mapa-side]"))
114 want := each.open
115 if side.show == "rail" {
116 want = each.rail
117 }
118 if got := read(t, rig, chromedp.Evaluate[string](
119 litShown)); got != want {
120 t.Errorf("%s: lit %q, want %q", name, got, want)
121 }
122 held := read(t, rig, chromedp.Evaluate[string](sidebarText))
123 if strings.Contains(held, secretLine) {
124 t.Errorf("%s: a scratch page's first line is in the "+
125 "sidebar", name)
126 }
127 }
128 }
129 }
130}
131
132// Below 800px the places are rows in estilo's Menu sheet: no note of
133// your own is among them, and the page's place is the one lit.
134func TestTheMenuSheetHoldsNoPrivateLines(t *testing.T) {
135 rig, store := browserRig(t, 0)
136 notes := writeOwnNotes(t, store)
137 for _, live := range []bool{false, true} {
138 drive(t, rig, scripts(live), sideCookie(rig, ""),
139 chromedp.EmulateViewport(390, 844),
140 chromedp.Navigate(rig.Origin+notes.team),
141 chromedp.WaitVisible("main h1"))
142 held := read(t, rig, chromedp.Evaluate[string](sidebarText))
143 lit := read(t, rig, chromedp.Evaluate[string](
144 `Array.prototype.map.call(document.querySelectorAll(
145 '[estilo-bar-nav] [aria-current="page"]'),
146 function (el) { return el.textContent.trim(); }).join(" | ")`))
147 if strings.Contains(held, secretLine) || lit != "All pages" {
148 t.Errorf("scripts %v: the Menu sheet lights %q; private line "+
149 "shown: %v", live, lit, strings.Contains(held, secretLine))
150 }
151 }
152}
153
154// ⌘\ folds the sidebar in place, with script: the highlight goes from
155// the page's row to its place, and back.
156func TestFoldingInPlaceMovesTheHighlight(t *testing.T) {
157 rig, store := browserRig(t, 0)
158 notes := writeOwnNotes(t, store)
159 drive(t, rig, scripts(true), sideCookie(rig, "open"),
160 chromedp.EmulateViewport(1400, 877),
161 chromedp.Navigate(rig.Origin+notes.team),
162 chromedp.WaitVisible("[mapa-side]"))
163 for _, want := range []string{"Quarterly plan", "All pages",
164 "Quarterly plan"} {
165 if got := read(t, rig, chromedp.Evaluate[string](
166 litShown)); got != want {
167 t.Fatalf("lit %q, want %q", got, want)
168 }
169 drive(t, rig, chromedp.KeyEvent(`\`, mod()),
170 chromedp.Sleep(50*time.Millisecond))
171 }
172 drive(t, rig, chromedp.KeyEvent(kb.Escape))
173}
174