//go:build browser package mapatest import ( "context" "strings" "testing" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // Dates in the reader's locale in a real Chromium (level-up build note // 85): what the browser's own Accept-Language asks for is what the page // writes, scripts off and on; the picker's month, columns, first day // and typed numbers follow it; a chip typed after mount is labelled as // the server labels it; and mounting moves nothing in other locales. // browserLanguage makes the browser itself ask for language. func browserLanguage(language string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { agent, err := chromedp.Evaluate[string]( `navigator.userAgent`)(ctx, target) if err != nil { return err } _, err = cdp.Call(ctx, target, emulation.SetUserAgentOverride, emulation.SetUserAgentOverrideParams{UserAgent: agent, AcceptLanguage: language}) return err }) } func TestTheBrowsersLanguageWritesTheDayScriptsOffAndOn(t *testing.T) { rig, _ := browserRigAsking(t, 0, "") for language, want := range map[string][2]string{ "de-DE": {"Dienstag, 17. November", "Montag"}, "en-US": {"Tuesday, November 17", "Sunday"}, } { for _, live := range []bool{false, true} { drive(t, rig, chromedp.Navigate(rig.Origin+"/"), browserLanguage(language), scripts(live), chromedp.Navigate(rig.Origin+"/d/2026-11-17"), chromedp.WaitVisible("h1[mapa-title]")) title := text(t, rig, "h1[mapa-title]") first := read(t, rig, chromedp.Evaluate[string]( `document.querySelector("[mapa-month-grid] th").abbr`)) if !strings.HasPrefix(title, want[0]) || first != want[1] { t.Errorf("%s, scripts %v: the title %q, the week from %q", language, live, title, first) } } } } func TestThePickerIsInTheReadersLocale(t *testing.T) { for language, want := range map[string]struct { head, first, title, said, typed, typedDate string }{ "en-US": {"Sunday S", "2026-11-01", "November 2026", "Monday, November 16, today", "12/11", "2026-12-11"}, "de": {"Montag M", "2026-10-26", "November 2026", "Montag, 16. November, today", "12.11.", "2026-11-12"}, "fr": {"lundi L", "2026-10-26", "novembre 2026", "lundi 16 novembre, today", "12/11", "2026-11-12"}, } { rig, _ := browserRigAsking(t, 0, language) slashDate(t, rig, "2026-11-25", "On ") got := read(t, rig, chromedp.Evaluate[string](`(function (root) { var head = root.querySelector("th"); return [head.abbr + " " + head.textContent, root.querySelector("td[data-date]").dataset.date, root.querySelector("[mapa-datepick-head] strong").textContent, root.querySelector("[mapa-datepick-said]").textContent].join(" | "); })(document.querySelector("[mapa-datepick]"))`)) if wanted := strings.Join([]string{want.head, want.first, want.title, want.said}, " | "); got != wanted { t.Errorf("%s: the picker reads %q, want %q", language, got, wanted) } drive(t, rig, chromedp.KeyEvent(want.typed), enter(), chromedp.WaitNotPresent(picker)) if got := typed(t, rig); got != "On [["+want.typedDate+"]]" { t.Errorf("%s: %s wrote %q", language, want.typed, got) } } } // A chip typed after mount is labelled by the editor from the island's // patterns, as the server labels it after a reload. func TestATypedChipIsLabelledAsTheServerLabelsIt(t *testing.T) { rig, _ := browserRigAsking(t, 0, "de") drive(t, rig, onNewDocument(mondayClock)) emptyDay(t, rig, "2026-11-24") drive(t, rig, chromedp.KeyEvent("Am [[2025-01-01")) listShows(t, rig, "Mittwoch, 1. Januar 2025") drive(t, rig, enter()) chip := `article [mapa-chip="date"]` before := text(t, rig, chip) drive(t, rig, chromedp.KeyEvent("s", mod())) // Committed: the base moves off 0 once the first save lands. read(t, rig, chromedp.Poll[bool](`mapaEditor.saver.version === "0" && mapaEditor.saver.base !== "0"`)) drive(t, rig, chromedp.Reload(), chromedp.WaitVisible(mounted)) if after := text(t, rig, chip); before != "Mittwoch, 1. Januar 2025" || after != before { t.Fatalf("typed %q, served %q", before, after) } } // L5 in other locales: the server's labels are the editor's, so the // mount moves nothing in German or in American English either. func TestL5NoShiftInOtherLocales(t *testing.T) { for _, language := range []string{"de", "en-US"} { rig, store := browserRigAsking(t, 0, language) page, err := store.CreateTeamPage(context.Background(), Bo, "Pilot", shiftBody+"\n\nSince [[2025-01-01|then]] and "+ "[[2025-01-01]].") if err != nil { t.Fatal(err) } drive(t, rig, onNewDocument(shiftRecorder)) for _, width := range []int64{1440, 390} { noShift(t, rig, mapa.PagePath(page), width, false, false) } noShift(t, rig, "/d/2026-11-02", 1000, true, false) } }