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 "strings"
7 "testing"
8
9 "amadan.net/rastrillo/rastrillo/harness"
10 "github.com/chromedp/chromedp"
11 "github.com/chromedp/chromedp/kb"
12)
13
14// The date picker (Paul, 2026-11-16; level-up build note 83): / → Date
15// and Insert → Date… open a month at the caret with today chosen; the
16// keys, a typed date, a click and a tap insert the day's chip; Esc
17// inserts nothing; axe finds nothing serious. The browser's clock is
18// Monday 16 November 2026.
19
20// mondayClock moves the page's clock to Monday 16 November 2026, noon
21// UTC, keeping it running. "Today" is read in the page's data-zone,
22// Europe/Dublin here, where it is noon too, whatever the machine's zone.
23const mondayClock = `(function () {
24 var Real = Date;
25 var shift = Real.UTC(2026, 10, 16, 12, 0, 0) - Real.now();
26 class Monday extends Real {
27 constructor (...parts) {
28 if (parts.length === 0) super(Real.now() + shift); else super(...parts);
29 }
30 static now () { return Real.now() + shift; }
31 }
32 window.Date = Monday;
33})()`
34
35const picker = `[mapa-datepick]`
36
37// pickerDay is the picker's chosen day, as its field names it.
38const pickerDay = `(function (field) {
39 return field.getAttribute("aria-activedescendant").slice(-10);
40})(document.querySelector("[mapa-datepick-field]"))`
41
42// retype selects what the picker's field holds and types text over it.
43func retype(text string) chromedp.Action[chromedp.Void] {
44 return chromedp.Steps(chromedp.Evaluate[chromedp.Void](
45 `document.querySelector("[mapa-datepick-field]").select()`),
46 chromedp.KeyEvent(text))
47}
48
49// slashDate types text then "/date" on an unwritten day and chooses
50// Date from the / list, so the picker opens.
51func slashDate(t *testing.T, rig *harness.Rig, date, text string) {
52 t.Helper()
53 drive(t, rig, onNewDocument(mondayClock))
54 emptyDay(t, rig, date)
55 drive(t, rig, chromedp.KeyEvent(text+"/date"))
56 listShows(t, rig, "Datechoose a day")
57 drive(t, rig, enter(), chromedp.WaitVisible(picker))
58}
59
60func TestSlashDateInsertsTheDayTheKeysChoose(t *testing.T) {
61 rig, _ := browserRig(t, 0)
62 slashDate(t, rig, "2026-11-25", "Call Ana on ")
63 if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got !=
64 "2026-11-16" {
65 t.Fatalf("the picker opened on %s, not today", got)
66 }
67 if !read(t, rig, chromedp.Evaluate[bool](
68 `document.activeElement.matches("[mapa-datepick-field]")`)) {
69 t.Fatal("the picker's field does not have focus")
70 }
71 drive(t, rig, chromedp.KeyEvent(kb.ArrowRight),
72 chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowDown),
73 chromedp.KeyEvent(kb.ArrowUp), chromedp.KeyEvent(kb.ArrowRight),
74 chromedp.KeyEvent(kb.ArrowRight))
75 if got := text(t, rig, `[mapa-datepick-said]`); got !=
76 "Friday, 20 November" {
77 t.Fatalf("the picker says %q", got)
78 }
79 drive(t, rig, enter(), chromedp.WaitNotPresent(picker))
80 if got := typed(t, rig); got != "Call Ana on [[2026-11-20]]" {
81 t.Fatalf("the keys wrote %q", got)
82 }
83 if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) {
84 t.Fatal("focus did not come back to the text")
85 }
86 // Page Down is a month on, Page Up a month back.
87 drive(t, rig, chromedp.KeyEvent(" /date"))
88 listShows(t, rig, "Datechoose a day")
89 drive(t, rig, enter(), chromedp.WaitVisible(picker),
90 chromedp.KeyEvent(kb.PageDown), chromedp.KeyEvent(kb.PageDown),
91 chromedp.KeyEvent(kb.PageUp), enter(),
92 chromedp.WaitNotPresent(picker))
93 if got := typed(t, rig); got != "Call Ana on [[2026-11-20]] "+
94 "[[2026-12-16]]" {
95 t.Fatalf("Page Down wrote %q", got)
96 }
97}
98
99func TestATypedDateMovesThePicker(t *testing.T) {
100 rig, _ := browserRig(t, 0)
101 slashDate(t, rig, "2026-11-26", "Ship it ")
102 drive(t, rig, chromedp.KeyEvent("next friday"))
103 if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got !=
104 "2026-11-20" {
105 t.Fatalf("next friday chose %s", got)
106 }
107 drive(t, rig, chromedp.KeyEvent(kb.ArrowDown))
108 drive(t, rig, retype("3 march"))
109 if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got !=
110 "2026-03-03" {
111 t.Fatalf("3 march chose %s", got)
112 }
113 // What is not a date says so, and Enter inserts nothing.
114 drive(t, rig, chromedp.KeyEvent("x"), enter())
115 if said := text(t, rig, `[mapa-datepick-said]`); !strings.Contains(said,
116 "isn’t a date") {
117 t.Fatalf("the picker says %q", said)
118 }
119 drive(t, rig, retype("next friday"), enter(),
120 chromedp.WaitNotPresent(picker))
121 if got := typed(t, rig); got != "Ship it [[2026-11-20]]" {
122 t.Fatalf("a typed date wrote %q", got)
123 }
124}
125
126func TestEscClosesThePickerAndInsertsNothing(t *testing.T) {
127 rig, _ := browserRig(t, 0)
128 slashDate(t, rig, "2026-11-27", "Nothing here ")
129 drive(t, rig, chromedp.KeyEvent(kb.ArrowRight),
130 chromedp.KeyEvent(kb.Escape), chromedp.WaitNotPresent(picker))
131 // The Markdown is trimmed: the space typed before / is there.
132 if got := typed(t, rig); got != "Nothing here" {
133 t.Fatalf("Esc wrote %q", got)
134 }
135 if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) {
136 t.Fatal("focus did not come back to the text")
137 }
138 drive(t, rig, chromedp.KeyEvent("still"))
139 if got := typed(t, rig); got != "Nothing here still" {
140 t.Fatalf("typing after Esc wrote %q", got)
141 }
142}
143
144func TestInsertDateOpensThePickerBesideTodaysDate(t *testing.T) {
145 rig, _ := browserRig(t, 0)
146 drive(t, rig, onNewDocument(mondayClock))
147 emptyDay(t, rig, "2026-11-28")
148 drive(t, rig, chromedp.KeyEvent("Due "),
149 chromedp.Click(`#mapa-menu-insert > summary`))
150 rows := read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call(
151 document.querySelectorAll("#mapa-menu-insert [estilo-menu-label]"),
152 function (label) { return label.textContent; }).slice(-3).join(" | ")`))
153 if rows != "Today's date | Date… | Image…" {
154 t.Fatalf("Insert ends %q", rows)
155 }
156 drive(t, rig, chromedp.Click(
157 `#mapa-menu-insert button[data-mapa-command="insert-date-pick"]`),
158 chromedp.WaitVisible(picker))
159 if found := axeSerious(t, rig); found != "" {
160 t.Fatalf("axe, with the picker open:\n%s", found)
161 }
162 // By mouse: the next month, then a day.
163 drive(t, rig, chromedp.Click(picker+` button[aria-label="Next month"]`),
164 chromedp.Click(picker+` td[data-date="2026-12-18"]`),
165 chromedp.WaitNotPresent(picker))
166 if got := typed(t, rig); got != "Due [[2026-12-18]]" {
167 t.Fatalf("a click wrote %q", got)
168 }
169}
170
171func TestThePickerWorksOnAPhone(t *testing.T) {
172 rig, _ := browserRig(t, 0, harness.WithCoarsePointer())
173 drive(t, rig, onNewDocument(mondayClock), scripts(true),
174 chromedp.EmulateViewport(390, 844),
175 chromedp.Navigate(rig.Origin+"/d/2026-11-29"),
176 chromedp.WaitVisible(mounted), caretAt(0),
177 chromedp.WaitVisible(`[mapa-accessory]`), chromedp.KeyEvent("On "),
178 chromedp.Click(`[mapa-accessory] [aria-label="Blocks"]`),
179 chromedp.KeyEvent("da"))
180 listShows(t, rig, "Datechoose a day")
181 drive(t, rig, chromedp.Click(`[mapa-combo]:not([hidden]) li:first-child`),
182 chromedp.WaitVisible(picker))
183 fits := read(t, rig, chromedp.Evaluate[string](`(function () {
184 var box = document.querySelector("[mapa-datepick]").getBoundingClientRect();
185 var day = document.querySelector("[mapa-datepick] td[data-date]")
186 .getBoundingClientRect();
187 return [box.left >= 0 && box.right <= innerWidth && box.top >= 0 &&
188 box.bottom <= innerHeight, day.height >= 32,
189 document.activeElement.matches("[mapa-datepick]")].join();
190})()`))
191 if fits != "true,true,true" {
192 t.Fatalf("on a phone, on screen, 32px days, keyboard down: %s", fits)
193 }
194 drive(t, rig, chromedp.Click(picker+` td[data-date="2026-11-20"]`),
195 chromedp.WaitNotPresent(picker))
196 if got := typed(t, rig); got != "On [[2026-11-20]]" {
197 t.Fatalf("a tap wrote %q", got)
198 }
199}
200