//go:build browser package mapatest import ( "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // The date picker (Paul, 2026-11-16; level-up build note 83): / → Date // and Insert → Date… open a month at the caret with today chosen; the // keys, a typed date, a click and a tap insert the day's chip; Esc // inserts nothing; axe finds nothing serious. The browser's clock is // Monday 16 November 2026. // mondayClock moves the page's clock to Monday 16 November 2026, noon // UTC, keeping it running. "Today" is read in the page's data-zone, // Europe/Dublin here, where it is noon too, whatever the machine's zone. const mondayClock = `(function () { var Real = Date; var shift = Real.UTC(2026, 10, 16, 12, 0, 0) - Real.now(); class Monday extends Real { constructor (...parts) { if (parts.length === 0) super(Real.now() + shift); else super(...parts); } static now () { return Real.now() + shift; } } window.Date = Monday; })()` const picker = `[mapa-datepick]` // pickerDay is the picker's chosen day, as its field names it. const pickerDay = `(function (field) { return field.getAttribute("aria-activedescendant").slice(-10); })(document.querySelector("[mapa-datepick-field]"))` // retype selects what the picker's field holds and types text over it. func retype(text string) chromedp.Action[chromedp.Void] { return chromedp.Steps(chromedp.Evaluate[chromedp.Void]( `document.querySelector("[mapa-datepick-field]").select()`), chromedp.KeyEvent(text)) } // slashDate types text then "/date" on an unwritten day and chooses // Date from the / list, so the picker opens. func slashDate(t *testing.T, rig *harness.Rig, date, text string) { t.Helper() drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, date) drive(t, rig, chromedp.KeyEvent(text+"/date")) listShows(t, rig, "Datechoose a day") drive(t, rig, enter(), chromedp.WaitVisible(picker)) } func TestSlashDateInsertsTheDayTheKeysChoose(t *testing.T) { rig, _ := browserRig(t, 0) slashDate(t, rig, "2026-11-25", "Call Ana on ") if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got != "2026-11-16" { t.Fatalf("the picker opened on %s, not today", got) } if !read(t, rig, chromedp.Evaluate[bool]( `document.activeElement.matches("[mapa-datepick-field]")`)) { t.Fatal("the picker's field does not have focus") } drive(t, rig, chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowDown), chromedp.KeyEvent(kb.ArrowUp), chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.ArrowRight)) if got := text(t, rig, `[mapa-datepick-said]`); got != "Friday, 20 November" { t.Fatalf("the picker says %q", got) } drive(t, rig, enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Call Ana on [[2026-11-20]]" { t.Fatalf("the keys wrote %q", got) } if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { t.Fatal("focus did not come back to the text") } // Page Down is a month on, Page Up a month back. drive(t, rig, chromedp.KeyEvent(" /date")) listShows(t, rig, "Datechoose a day") drive(t, rig, enter(), chromedp.WaitVisible(picker), chromedp.KeyEvent(kb.PageDown), chromedp.KeyEvent(kb.PageDown), chromedp.KeyEvent(kb.PageUp), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Call Ana on [[2026-11-20]] "+ "[[2026-12-16]]" { t.Fatalf("Page Down wrote %q", got) } } func TestATypedDateMovesThePicker(t *testing.T) { rig, _ := browserRig(t, 0) slashDate(t, rig, "2026-11-26", "Ship it ") drive(t, rig, chromedp.KeyEvent("next friday")) if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got != "2026-11-20" { t.Fatalf("next friday chose %s", got) } drive(t, rig, chromedp.KeyEvent(kb.ArrowDown)) drive(t, rig, retype("3 march")) if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got != "2026-03-03" { t.Fatalf("3 march chose %s", got) } // What is not a date says so, and Enter inserts nothing. drive(t, rig, chromedp.KeyEvent("x"), enter()) if said := text(t, rig, `[mapa-datepick-said]`); !strings.Contains(said, "isn’t a date") { t.Fatalf("the picker says %q", said) } drive(t, rig, retype("next friday"), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Ship it [[2026-11-20]]" { t.Fatalf("a typed date wrote %q", got) } } func TestEscClosesThePickerAndInsertsNothing(t *testing.T) { rig, _ := browserRig(t, 0) slashDate(t, rig, "2026-11-27", "Nothing here ") drive(t, rig, chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.Escape), chromedp.WaitNotPresent(picker)) // The Markdown is trimmed: the space typed before / is there. if got := typed(t, rig); got != "Nothing here" { t.Fatalf("Esc wrote %q", got) } if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { t.Fatal("focus did not come back to the text") } drive(t, rig, chromedp.KeyEvent("still")) if got := typed(t, rig); got != "Nothing here still" { t.Fatalf("typing after Esc wrote %q", got) } } func TestInsertDateOpensThePickerBesideTodaysDate(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, "2026-11-28") drive(t, rig, chromedp.KeyEvent("Due "), chromedp.Click(`#mapa-menu-insert > summary`)) rows := read(t, rig, chromedp.Evaluate[string](`Array.prototype.map.call( document.querySelectorAll("#mapa-menu-insert [estilo-menu-label]"), function (label) { return label.textContent; }).slice(-3).join(" | ")`)) if rows != "Today's date | Date… | Image…" { t.Fatalf("Insert ends %q", rows) } drive(t, rig, chromedp.Click( `#mapa-menu-insert button[data-mapa-command="insert-date-pick"]`), chromedp.WaitVisible(picker)) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, with the picker open:\n%s", found) } // By mouse: the next month, then a day. drive(t, rig, chromedp.Click(picker+` button[aria-label="Next month"]`), chromedp.Click(picker+` td[data-date="2026-12-18"]`), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Due [[2026-12-18]]" { t.Fatalf("a click wrote %q", got) } } func TestThePickerWorksOnAPhone(t *testing.T) { rig, _ := browserRig(t, 0, harness.WithCoarsePointer()) drive(t, rig, onNewDocument(mondayClock), scripts(true), chromedp.EmulateViewport(390, 844), chromedp.Navigate(rig.Origin+"/d/2026-11-29"), chromedp.WaitVisible(mounted), caretAt(0), chromedp.WaitVisible(`[mapa-accessory]`), chromedp.KeyEvent("On "), chromedp.Click(`[mapa-accessory] [aria-label="Blocks"]`), chromedp.KeyEvent("da")) listShows(t, rig, "Datechoose a day") drive(t, rig, chromedp.Click(`[mapa-combo]:not([hidden]) li:first-child`), chromedp.WaitVisible(picker)) fits := read(t, rig, chromedp.Evaluate[string](`(function () { var box = document.querySelector("[mapa-datepick]").getBoundingClientRect(); var day = document.querySelector("[mapa-datepick] td[data-date]") .getBoundingClientRect(); return [box.left >= 0 && box.right <= innerWidth && box.top >= 0 && box.bottom <= innerHeight, day.height >= 32, document.activeElement.matches("[mapa-datepick]")].join(); })()`)) if fits != "true,true,true" { t.Fatalf("on a phone, on screen, 32px days, keyboard down: %s", fits) } drive(t, rig, chromedp.Click(picker+` td[data-date="2026-11-20"]`), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "On [[2026-11-20]]" { t.Fatalf("a tap wrote %q", got) } }