| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/chromedp/cdproto/cdp" |
| 11 | "github.com/chromedp/cdproto/emulation" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | |
| 14 | "amadan.net/oficina/mapa/internal/mapa" |
| 15 | ) |
| 16 | |
| 17 | // Dates in the reader's locale in a real Chromium (level-up build note |
| 18 | // 85): what the browser's own Accept-Language asks for is what the page |
| 19 | // writes, scripts off and on; the picker's month, columns, first day |
| 20 | // and typed numbers follow it; a chip typed after mount is labelled as |
| 21 | // the server labels it; and mounting moves nothing in other locales. |
| 22 | |
| 23 | // browserLanguage makes the browser itself ask for language. |
| 24 | func browserLanguage(language string) chromedp.Action[chromedp.Void] { |
| 25 | return chromedp.Func(func(ctx context.Context, |
| 26 | target *chromedp.Target) error { |
| 27 | agent, err := chromedp.Evaluate[string]( |
| 28 | `navigator.userAgent`)(ctx, target) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | _, err = cdp.Call(ctx, target, emulation.SetUserAgentOverride, |
| 33 | emulation.SetUserAgentOverrideParams{UserAgent: agent, |
| 34 | AcceptLanguage: language}) |
| 35 | return err |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | func TestTheBrowsersLanguageWritesTheDayScriptsOffAndOn(t *testing.T) { |
| 40 | rig, _ := browserRigAsking(t, 0, "") |
| 41 | for language, want := range map[string][2]string{ |
| 42 | "de-DE": {"Dienstag, 17. November", "Montag"}, |
| 43 | "en-US": {"Tuesday, November 17", "Sunday"}, |
| 44 | } { |
| 45 | for _, live := range []bool{false, true} { |
| 46 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), |
| 47 | browserLanguage(language), scripts(live), |
| 48 | chromedp.Navigate(rig.Origin+"/d/2026-11-17"), |
| 49 | chromedp.WaitVisible("h1[mapa-title]")) |
| 50 | title := text(t, rig, "h1[mapa-title]") |
| 51 | first := read(t, rig, chromedp.Evaluate[string]( |
| 52 | `document.querySelector("[mapa-month-grid] th").abbr`)) |
| 53 | if !strings.HasPrefix(title, want[0]) || first != want[1] { |
| 54 | t.Errorf("%s, scripts %v: the title %q, the week from %q", |
| 55 | language, live, title, first) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestThePickerIsInTheReadersLocale(t *testing.T) { |
| 62 | for language, want := range map[string]struct { |
| 63 | head, first, title, said, typed, typedDate string |
| 64 | }{ |
| 65 | "en-US": {"Sunday S", "2026-11-01", "November 2026", |
| 66 | "Monday, November 16, today", "12/11", "2026-12-11"}, |
| 67 | "de": {"Montag M", "2026-10-26", "November 2026", |
| 68 | "Montag, 16. November, today", "12.11.", "2026-11-12"}, |
| 69 | "fr": {"lundi L", "2026-10-26", "novembre 2026", |
| 70 | "lundi 16 novembre, today", "12/11", "2026-11-12"}, |
| 71 | } { |
| 72 | rig, _ := browserRigAsking(t, 0, language) |
| 73 | slashDate(t, rig, "2026-11-25", "On ") |
| 74 | got := read(t, rig, chromedp.Evaluate[string](`(function (root) { |
| 75 | var head = root.querySelector("th"); |
| 76 | return [head.abbr + " " + head.textContent, |
| 77 | root.querySelector("td[data-date]").dataset.date, |
| 78 | root.querySelector("[mapa-datepick-head] strong").textContent, |
| 79 | root.querySelector("[mapa-datepick-said]").textContent].join(" | "); |
| 80 | })(document.querySelector("[mapa-datepick]"))`)) |
| 81 | if wanted := strings.Join([]string{want.head, want.first, |
| 82 | want.title, want.said}, " | "); got != wanted { |
| 83 | t.Errorf("%s: the picker reads %q, want %q", language, got, |
| 84 | wanted) |
| 85 | } |
| 86 | drive(t, rig, chromedp.KeyEvent(want.typed), enter(), |
| 87 | chromedp.WaitNotPresent(picker)) |
| 88 | if got := typed(t, rig); got != "On [["+want.typedDate+"]]" { |
| 89 | t.Errorf("%s: %s wrote %q", language, want.typed, got) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // A chip typed after mount is labelled by the editor from the island's |
| 95 | // patterns, as the server labels it after a reload. |
| 96 | func TestATypedChipIsLabelledAsTheServerLabelsIt(t *testing.T) { |
| 97 | rig, _ := browserRigAsking(t, 0, "de") |
| 98 | drive(t, rig, onNewDocument(mondayClock)) |
| 99 | emptyDay(t, rig, "2026-11-24") |
| 100 | drive(t, rig, chromedp.KeyEvent("Am [[2025-01-01")) |
| 101 | listShows(t, rig, "Mittwoch, 1. Januar 2025") |
| 102 | drive(t, rig, enter()) |
| 103 | chip := `article [mapa-chip="date"]` |
| 104 | before := text(t, rig, chip) |
| 105 | drive(t, rig, chromedp.KeyEvent("s", mod())) |
| 106 | // Committed: the base moves off 0 once the first save lands. |
| 107 | read(t, rig, chromedp.Poll[bool](`mapaEditor.saver.version === "0" && |
| 108 | mapaEditor.saver.base !== "0"`)) |
| 109 | drive(t, rig, chromedp.Reload(), chromedp.WaitVisible(mounted)) |
| 110 | if after := text(t, rig, chip); before != "Mittwoch, 1. Januar 2025" || |
| 111 | after != before { |
| 112 | t.Fatalf("typed %q, served %q", before, after) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // L5 in other locales: the server's labels are the editor's, so the |
| 117 | // mount moves nothing in German or in American English either. |
| 118 | func TestL5NoShiftInOtherLocales(t *testing.T) { |
| 119 | for _, language := range []string{"de", "en-US"} { |
| 120 | rig, store := browserRigAsking(t, 0, language) |
| 121 | page, err := store.CreateTeamPage(context.Background(), Bo, |
| 122 | "Pilot", shiftBody+"\n\nSince [[2025-01-01|then]] and "+ |
| 123 | "[[2025-01-01]].") |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | drive(t, rig, onNewDocument(shiftRecorder)) |
| 128 | for _, width := range []int64{1440, 390} { |
| 129 | noShift(t, rig, mapa.PagePath(page), width, false, false) |
| 130 | } |
| 131 | noShift(t, rig, "/d/2026-11-02", 1000, true, false) |
| 132 | } |
| 133 | } |
| 134 | |