| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | ) |
| 14 | |
| 15 | // The level-up's layout and scriptless walk (spec §3, §4; Fable's Phase 1 |
| 16 | // review H1 and M5: gates L8 and L11), in real Chromium at desktop |
| 17 | // widths. |
| 18 | |
| 19 | // firstTeamPage is a team page's path, read from All pages. |
| 20 | func firstTeamPage(t *testing.T, rig *harness.Rig) string { |
| 21 | t.Helper() |
| 22 | drive(t, rig, chromedp.Navigate(rig.Origin+"/pages"), |
| 23 | chromedp.WaitVisible(`main a[href^="/p/"]`)) |
| 24 | return read(t, rig, chromedp.AttributeValue(`main a[href^="/p/"]`, |
| 25 | "href")).Value |
| 26 | } |
| 27 | |
| 28 | // columnGeometry is the page column's left, right and width, and the |
| 29 | // Links panel's left (-1 when it is not drawn), in CSS pixels. |
| 30 | const columnGeometry = `(function () { |
| 31 | var body = document.querySelector("article[mapa-body]"); |
| 32 | var panel = document.querySelector("aside[mapa-links]"); |
| 33 | var box = body.getBoundingClientRect(); |
| 34 | var left = -1; |
| 35 | if (panel && getComputedStyle(panel).display !== "none") { |
| 36 | left = panel.getBoundingClientRect().left; |
| 37 | } |
| 38 | return [box.left, box.right, box.width, left].map(Math.round).join(","); |
| 39 | })()` |
| 40 | |
| 41 | // H1: with the panel open, the column keeps its measure (36rem to 42rem |
| 42 | // of text) and clears the panel, at 1280 and 1440, scripts on or off. |
| 43 | func TestTheColumnClearsTheLinksPanel(t *testing.T) { |
| 44 | for _, live := range []bool{false, true} { |
| 45 | rig, _ := browserRig(t, 3) |
| 46 | path := firstTeamPage(t, rig) |
| 47 | for _, width := range []int64{1280, 1440} { |
| 48 | drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 900), |
| 49 | chromedp.Navigate(rig.Origin+path), |
| 50 | chromedp.WaitVisible("article[mapa-body]")) |
| 51 | parts := strings.Split(read(t, rig, |
| 52 | chromedp.Evaluate[string](columnGeometry)), ",") |
| 53 | right, _ := strconv.Atoi(parts[1]) |
| 54 | measure, _ := strconv.Atoi(parts[2]) |
| 55 | panel, _ := strconv.Atoi(parts[3]) |
| 56 | if measure < 576 || measure > 672 { |
| 57 | t.Errorf("%dpx: the column is %dpx wide", width, measure) |
| 58 | } |
| 59 | if panel < 0 || right > panel-16 { |
| 60 | t.Errorf("%dpx: column ends at %d, panel at %d", width, |
| 61 | right, panel) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // sideWidth is the sidebar's width in CSS pixels. |
| 68 | const sideWidth = `Math.round(document.querySelector("[mapa-side]")` + |
| 69 | `.getBoundingClientRect().width)` |
| 70 | |
| 71 | // L8 at desktop width, scripts off: the sidebar folds to the rail and |
| 72 | // back by its POST, the Links panel is drawn, a daily note's pill sits in |
| 73 | // the lead slot and pages its months, the header renames, New team page |
| 74 | // creates, and Make team page shares. |
| 75 | func TestTheScriptlessDesktopWalk(t *testing.T) { |
| 76 | rig, _ := browserRig(t, 3) |
| 77 | path := firstTeamPage(t, rig) |
| 78 | drive(t, rig, scripts(false), chromedp.EmulateViewport(1440, 900), |
| 79 | chromedp.Navigate(rig.Origin+path), |
| 80 | chromedp.WaitVisible("aside[mapa-links]")) |
| 81 | if width := read(t, rig, chromedp.Evaluate[int](sideWidth)); width < 200 { |
| 82 | t.Fatalf("the open sidebar is %dpx", width) |
| 83 | } |
| 84 | drive(t, rig, press(`[mapa-fold-to="rail"]`), |
| 85 | chromedp.WaitVisible(`[mapa-app][data-side="rail"]`)) |
| 86 | if width := read(t, rig, chromedp.Evaluate[int](sideWidth)); width > 60 { |
| 87 | t.Fatalf("the rail is %dpx", width) |
| 88 | } |
| 89 | drive(t, rig, press(`[mapa-fold-to="open"]`), |
| 90 | chromedp.WaitVisible(`[mapa-app][data-side="open"]`)) |
| 91 | walkTheDay(t, rig) |
| 92 | walkRename(t, rig, path) |
| 93 | walkNewAndShare(t, rig) |
| 94 | } |
| 95 | |
| 96 | // walkTheDay: the pill in the lead slot, and the month paged by link. |
| 97 | func walkTheDay(t *testing.T, rig *harness.Rig) { |
| 98 | t.Helper() |
| 99 | drive(t, rig, chromedp.Navigate(rig.Origin+"/d/2026-11-12"), |
| 100 | chromedp.WaitVisible(`[estilo-bar-lead] nav[mapa-day]`), |
| 101 | chromedp.Click(`[estilo-bar-lead] [mapa-day-now]`), |
| 102 | chromedp.WaitVisible(`[estilo-bar-lead] [mapa-month]`), |
| 103 | chromedp.Click(`[estilo-bar-lead] a[aria-label="Next month"]`), |
| 104 | chromedp.WaitVisible(`[estilo-bar-lead] [mapa-month]`)) |
| 105 | title := text(t, rig, `[estilo-bar-lead] [mapa-month-head] strong`) |
| 106 | if title != "December 2026" { |
| 107 | t.Fatalf("the paged month is %q", title) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // walkRename renames in the header: type, then Enter. |
| 112 | func walkRename(t *testing.T, rig *harness.Rig, path string) { |
| 113 | t.Helper() |
| 114 | field := `[estilo-bar-title-input]` |
| 115 | drive(t, rig, chromedp.Navigate(rig.Origin+path), |
| 116 | chromedp.WaitVisible(field), |
| 117 | chromedp.SetValue(field, "Renamed in the header"), |
| 118 | chromedp.Submit(field)) |
| 119 | // The rename answers with a redirect (to Update links, or back with |
| 120 | // done=renamed): wait for it to land before reading the page again. |
| 121 | for range 40 { |
| 122 | here := read(t, rig, chromedp.Location()) |
| 123 | if strings.Contains(here, "/links") || strings.Contains(here, "done=") { |
| 124 | break |
| 125 | } |
| 126 | time.Sleep(50 * time.Millisecond) |
| 127 | } |
| 128 | drive(t, rig, chromedp.Navigate(rig.Origin+path), |
| 129 | chromedp.WaitVisible("h1[mapa-title]")) |
| 130 | if got := text(t, rig, "h1[mapa-title]"); got != "Renamed in the header" { |
| 131 | t.Fatalf("after the header rename the title is %q", got) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // walkNewAndShare creates a team page from the sidebar's popover, then |
| 136 | // shares a scratch page's block as another (Make team page). |
| 137 | func walkNewAndShare(t *testing.T, rig *harness.Rig) { |
| 138 | t.Helper() |
| 139 | drive(t, rig, chromedp.Navigate(rig.Origin+"/pages"), |
| 140 | chromedp.Click(opener), |
| 141 | chromedp.SendKeys(`#mapa-new-title`, "From the sidebar"), |
| 142 | chromedp.Submit(`#mapa-new-title`), |
| 143 | chromedp.WaitVisible("h1[mapa-title]")) |
| 144 | if got := text(t, rig, "h1[mapa-title]"); got != "From the sidebar" { |
| 145 | t.Fatalf("New team page made %q", got) |
| 146 | } |
| 147 | drive(t, rig, chromedp.Navigate(rig.Origin+"/scratch/new"), |
| 148 | chromedp.Click(`[mapa-actions] a[href$="markdown=1"]`), |
| 149 | chromedp.WaitVisible(`main textarea[mapa-editor]`), |
| 150 | chromedp.SendKeys(`main textarea[mapa-editor]`, "Share this."), |
| 151 | press(save), chromedp.WaitVisible("h1[mapa-title]")) |
| 152 | here := strings.TrimPrefix(read(t, rig, chromedp.Location()), rig.Origin) |
| 153 | drive(t, rig, chromedp.Navigate(rig.Origin+here+"/share"), |
| 154 | chromedp.SendKeys(`main input[name="title"]`, "Shared"), |
| 155 | chromedp.Click(`main input[name="block"]`), |
| 156 | chromedp.Submit(`main input[name="title"]`), |
| 157 | chromedp.WaitVisible("h1[mapa-title]")) |
| 158 | if got := text(t, rig, "h1[mapa-title]"); got != "Shared" { |
| 159 | t.Fatalf("Make team page made %q", got) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // heldFor is how long the last navigation took from the click that |
| 164 | // started it (stamped in sessionStorage), in milliseconds. |
| 165 | const heldFor = `Math.round(performance.timeOrigin - ` + |
| 166 | `Number(sessionStorage.getItem("mapa-clicked")))` |
| 167 | |
| 168 | // L11, the busy rule: a POST (Pin, here) holds its spinner for 650ms |
| 169 | // before the page moves on; a view switch (Links) is immediate. |
| 170 | func TestAPostHoldsAndAViewSwitchDoesNot(t *testing.T) { |
| 171 | rig, _ := browserRig(t, 3) |
| 172 | path := firstTeamPage(t, rig) |
| 173 | pin := `form[action$="/pin"] button` |
| 174 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), |
| 175 | chromedp.Navigate(rig.Origin+path), |
| 176 | chromedp.WaitVisible("aside[mapa-links]"), |
| 177 | chromedp.Click(`#mapa-menu-file > summary`)) |
| 178 | read(t, rig, chromedp.Evaluate[any]( |
| 179 | `sessionStorage.setItem("mapa-clicked",`+ |
| 180 | ` String(performance.timeOrigin + performance.now()))`)) |
| 181 | drive(t, rig, chromedp.Click(pin), chromedp.Sleep(100*time.Millisecond)) |
| 182 | if busy := read(t, rig, chromedp.Evaluate[bool]( |
| 183 | `!!document.querySelector('`+pin+`[aria-busy="true"]')`)); !busy { |
| 184 | t.Fatal("Pin shows no spinner") |
| 185 | } |
| 186 | drive(t, rig, chromedp.WaitVisible(`[mapa-side-page]`)) |
| 187 | if held := read(t, rig, chromedp.Evaluate[int](heldFor)); held < 640 { |
| 188 | t.Fatalf("Pin's POST was held for %dms", held) |
| 189 | } |
| 190 | drive(t, rig, chromedp.Click(`[aria-controls="mapa-links"]`), |
| 191 | chromedp.Sleep(50*time.Millisecond)) |
| 192 | if hidden := read(t, rig, chromedp.Evaluate[bool]( |
| 193 | `document.getElementById("mapa-links").hidden`)); !hidden { |
| 194 | t.Fatal("the Links toggle did not switch at once") |
| 195 | } |
| 196 | } |
| 197 | |