//go:build browser package mapatest import ( "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // Fable's review of the date picker (3a7ee5f; level-up build note 83, // §14 round 7): M1 a date after a ! goes in after a space, never as an // embed, and where the Markdown would read it differently it is refused // with a note; M3 a selection is kept; L1 the field trims as ⌘K does; L2 // today is the viewer's zone's; L3 ← → move the field's caret once it // holds text. // insertMenu runs Insert → label's command, by its data-mapa-command. func insertMenu(command string) chromedp.Action[chromedp.Void] { return chromedp.Steps(chromedp.Click(`#mapa-menu-insert > summary`), chromedp.Click(`#mapa-menu-insert button[data-mapa-command="`+ command+`"]`)) } func TestADateAfterABangIsNeverAnEmbed(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, "2026-11-30") drive(t, rig, chromedp.KeyEvent("Look!"), insertMenu("insert-date-pick"), chromedp.WaitVisible(picker), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Look! [[2026-11-16]]" { t.Fatalf("the picker after a ! wrote %q", got) } drive(t, rig, chromedp.KeyEvent(" Wow!"), insertMenu("insert-date")) if got := typed(t, rig); got != "Look! [[2026-11-16]] Wow! "+ "[[2026-11-16]]" { t.Fatalf("Today's date after a ! wrote %q", got) } chips := read(t, rig, chromedp.Evaluate[int]( `document.querySelectorAll("article [mapa-chip]").length`)) if chips != 2 { t.Fatalf("%d chips on the page, not 2", chips) } } func TestADateTheMarkdownWouldMisreadIsRefused(t *testing.T) { rig, store := browserRig(t, 0) drive(t, rig, onNewDocument(mondayClock)) teamPage(t, rig, store, "`` foo ` bar ``") drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { var view = mapaEditor.view, Selection = view.state.selection.constructor; view.focus(); view.dispatch(view.state.tr.setSelection( Selection.create(view.state.doc, 2))); })()`), insertMenu("insert-date-pick"), chromedp.WaitVisible(`[mapa-datepick-note]`)) said := text(t, rig, `[mapa-datepick-note]`) if said != "A date can’t go here: the Markdown around it would read "+ "differently, such as a stray ` or * pairing up. Move the caret "+ "and try again." { t.Fatalf("the refusal says %q", said) } if read(t, rig, chromedp.Evaluate[bool]( `!!document.querySelector("[mapa-datepick]")`)) { t.Fatal("the picker opened where no date can go") } if got := typed(t, rig); got != "`` foo ` bar ``" { t.Fatalf("the refusal wrote %q", got) } } func TestADateOverASelectionKeepsTheWords(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, "2026-12-01") drive(t, rig, chromedp.KeyEvent("Call Ana today"), selectWords("Ana"), insertMenu("insert-date-pick"), chromedp.WaitVisible(picker), chromedp.KeyEvent(kb.ArrowRight), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Call Ana[[2026-11-17]] today" { t.Fatalf("Date… over a selection wrote %q", got) } drive(t, rig, selectWords("Call"), insertMenu("insert-date")) if got := typed(t, rig); got != "Call[[2026-11-16]] Ana[[2026-11-17]] "+ "today" { t.Fatalf("Today's date over a selection wrote %q", got) } } func TestThePickersFieldTrimsAsCmdKDoesAndKeepsItsCaret(t *testing.T) { rig, _ := browserRig(t, 0) slashDate(t, rig, "2026-12-02", "Then ") // U+FEFF is not a space to Go's TrimSpace, so ⌘K reads no date here. drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function (field) { field.value = "\ufefffriday"; field.dispatchEvent(new Event("input")); })(document.querySelector("[mapa-datepick-field]"))`)) if got := read(t, rig, chromedp.Evaluate[string](pickerDay)); got != "2026-11-16" { t.Fatalf("U+FEFF friday chose %s", got) } drive(t, rig, retype("next friday"), chromedp.KeyEvent(kb.ArrowLeft), chromedp.KeyEvent(kb.ArrowLeft)) caret := read(t, rig, chromedp.Evaluate[int]( `document.querySelector("[mapa-datepick-field]").selectionStart`)) if day := read(t, rig, chromedp.Evaluate[string](pickerDay)); caret != 9 || day != "2026-11-20" { t.Fatalf("← in the field: caret %d, day %s", caret, day) } drive(t, rig, chromedp.KeyEvent(kb.ArrowDown), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "Then [[2026-11-27]]" { t.Fatalf("the field's day wrote %q", got) } } func TestTodayIsTheViewersZones(t *testing.T) { rig, _ := browserRig(t, 0, harness.WithCoarsePointer()) drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, "2026-12-03") // Noon UTC on the 16th is two in the morning of the 17th at UTC+14. drive(t, rig, chromedp.Evaluate[chromedp.Void]( `document.body.setAttribute("data-zone", "Pacific/Kiritimati")`), caretAt(0), insertMenu("insert-date")) if got := typed(t, rig); got != "[[2026-11-17]]" { t.Fatalf("Today's date at UTC+14 wrote %q", got) } }