| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/db" |
| 16 | |
| 17 | "amadan.net/oficina/mapa/internal/mapa" |
| 18 | ) |
| 19 | |
| 20 | // Dates, times and numbers in the reader's locale (level-up build note |
| 21 | // 85): rastrillo's middleware reads Accept-Language among the locales |
| 22 | // Mapa offers, the team's MAPA_LOCALE when none matches. Every page |
| 23 | // here is the server's HTML, which is the page with scripts off. |
| 24 | |
| 25 | // getIn is a GET whose browser asks for language. |
| 26 | func (rig *rig) getIn(t *testing.T, who, target, language string) string { |
| 27 | t.Helper() |
| 28 | recorder := httptest.NewRecorder() |
| 29 | request := httptest.NewRequest(http.MethodGet, target, nil) |
| 30 | if language != "" { |
| 31 | request.Header.Set("Accept-Language", language) |
| 32 | } |
| 33 | rig.built.As(who).ServeHTTP(recorder, request) |
| 34 | if recorder.Code != http.StatusOK { |
| 35 | t.Fatalf("GET %s in %s: %d", target, language, recorder.Code) |
| 36 | } |
| 37 | return recorder.Body.String() |
| 38 | } |
| 39 | |
| 40 | // wantIn fails unless page holds every one of wants. |
| 41 | func wantIn(t *testing.T, what, language, page string, wants ...string) { |
| 42 | t.Helper() |
| 43 | for _, want := range wants { |
| 44 | if !strings.Contains(page, want) { |
| 45 | t.Errorf("%s in %s lacks %q", what, language, want) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestADaysTitleIsInTheReadersLocale(t *testing.T) { |
| 51 | rig := newRig(t) |
| 52 | for language, wants := range map[string][]string{ |
| 53 | "en-IE": {"Tuesday 17 November<span mapa-title-faint> 2026", |
| 54 | "Previous day, Monday 16 November 2026"}, |
| 55 | "en-US": {"Tuesday, November 17<span mapa-title-faint>, 2026", |
| 56 | "Previous day, Monday, November 16, 2026"}, |
| 57 | "es": {"martes, 17 de noviembre<span mapa-title-faint> de 2026"}, |
| 58 | "de-DE,de;q=0.9": { |
| 59 | "Dienstag, 17. November<span mapa-title-faint> 2026", |
| 60 | "Previous day, Montag, 16. November 2026"}, |
| 61 | "fr": {"mardi 17 novembre<span mapa-title-faint> 2026"}, |
| 62 | } { |
| 63 | day := rig.getIn(t, Ana, "/d/2026-11-17", language) |
| 64 | wantIn(t, "the day's title", language, day, wants...) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestDateChipsAreInTheReadersLocale(t *testing.T) { |
| 69 | rig := newRig(t) |
| 70 | plan := rig.createPage(t, Ana, "Plan", "Since [[2025-01-01]].") |
| 71 | for language, label := range map[string]string{ |
| 72 | "en-IE": "Wednesday 1 January 2025", |
| 73 | "en-US": "Wednesday, January 1, 2025", |
| 74 | "de": "Mittwoch, 1. Januar 2025", |
| 75 | "fr": "mercredi 1 janvier 2025", |
| 76 | } { |
| 77 | page := rig.getIn(t, Ana, plan, language) |
| 78 | wantIn(t, "the chip", language, page, "</svg>"+label+"</a>", |
| 79 | `aria-label="`+label+`, your note"`) |
| 80 | island := between(page, `id="mapa-editor">`, "</script>") |
| 81 | var data struct { |
| 82 | Links map[string]struct{ Title string } |
| 83 | Dates struct{ Code string } |
| 84 | } |
| 85 | if err := json.Unmarshal([]byte(island), &data); err != nil { |
| 86 | t.Fatalf("%s: the island: %v", language, err) |
| 87 | } |
| 88 | if data.Links["2025-01-01"].Title != label || |
| 89 | data.Dates.Code != language { |
| 90 | t.Errorf("%s: the island says %+v", language, data) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // The week strip, the month and their columns start on the locale's |
| 96 | // first day: Monday in en-IE, Sunday in en-US. |
| 97 | func TestTheWeekStartsWhereTheReadersLocaleStartsIt(t *testing.T) { |
| 98 | rig := newRig(t) |
| 99 | for language, wants := range map[string][]string{ |
| 100 | "en-IE": {"<th abbr=\"Monday\">M</th><th abbr=\"Tuesday\">T</th>", |
| 101 | "<strong>November 2026</strong>"}, |
| 102 | "en-US": {"<th abbr=\"Sunday\">S</th><th abbr=\"Monday\">M</th>", |
| 103 | "<strong>November 2026</strong>"}, |
| 104 | "de": {"<th abbr=\"Montag\">M</th><th abbr=\"Dienstag\">D</th>"}, |
| 105 | "fr": {"<th abbr=\"lundi\">L</th>", |
| 106 | "<strong>novembre 2026</strong>"}, |
| 107 | } { |
| 108 | day := rig.getIn(t, Ana, "/d/2026-11-20", language) |
| 109 | wantIn(t, "the month", language, day, wants...) |
| 110 | } |
| 111 | for language, want := range map[string][2]string{ |
| 112 | "en-IE": {"2026-11-16", "2026-10-26"}, |
| 113 | "en-US": {"2026-11-15", "2026-11-01"}, |
| 114 | "de": {"2026-11-16", "2026-10-26"}, |
| 115 | } { |
| 116 | day := rig.getIn(t, Ana, "/d/2026-11-20", language) |
| 117 | week := between(between(day, "<ol mapa-week-days>", "</ol>"), |
| 118 | `href="/d/`, `"`) |
| 119 | month := between(between(day, "<table mapa-month-grid>", |
| 120 | "</table>"), `href="/d/`, `"`) |
| 121 | if week != want[0] || month != want[1] { |
| 122 | t.Errorf("%s: the week starts %s, the month's grid %s; want %v", |
| 123 | language, week, month, want) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestHistoryTimesAndCountsAreInTheReadersLocale(t *testing.T) { |
| 129 | rig := newRig(t) |
| 130 | moving := rig.clocked(t) |
| 131 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 132 | moving.now = moving.now.Add(12*time.Hour + 9*time.Minute) |
| 133 | paragraphs := []string{"One."} |
| 134 | for number := range 1234 { |
| 135 | paragraphs = append(paragraphs, "Line "+itoa(number)+".") |
| 136 | } |
| 137 | rig.editPage(t, Ana, acme, strings.Join(paragraphs, "\n\n")) |
| 138 | moving.now = moving.now.AddDate(0, 0, 4) |
| 139 | for language, wants := range map[string][]string{ |
| 140 | "en-IE": {"Thursday, 12 November", ">21:09</time>", ">09:00</time>", |
| 141 | "Added 1,234 blocks", `title="12 November 2026 at 21:09"`}, |
| 142 | "en-US": {"Thursday, November 12", ">9:09 PM</time>", |
| 143 | ">9:00 AM</time>", "Added 1,234 blocks"}, |
| 144 | "de": {"Donnerstag, 12. November", ">21:09</time>", |
| 145 | "Added 1.234 blocks", `title="12. November 2026 um 21:09"`}, |
| 146 | "fr": {"jeudi 12 novembre", "Added 1\u202f234 blocks"}, |
| 147 | } { |
| 148 | history := rig.getIn(t, Ana, acme+"/history", language) |
| 149 | wantIn(t, "History", language, history, wants...) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // editPage saves body over the page's current revision through the |
| 154 | // Markdown screen's form. |
| 155 | func (rig *rig) editPage(t *testing.T, who, page, body string) { |
| 156 | t.Helper() |
| 157 | form := rig.page(t, who, page+"/edit") |
| 158 | revision := between(form, `name="revision" value="`, `"`) |
| 159 | saved := rig.post(t, who, page, url.Values{"body": {body}, |
| 160 | "revision": {revision}}) |
| 161 | if saved.Code != http.StatusSeeOther { |
| 162 | t.Fatalf("saving %s: %d\n%s", page, saved.Code, saved.Body.String()) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func itoa(number int) string { |
| 167 | digits := "" |
| 168 | for { |
| 169 | digits = string(rune('0'+number%10)) + digits |
| 170 | if number /= 10; number == 0 { |
| 171 | return digits |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // ⌘K's date row: the words stay English, numbers follow the locale's |
| 177 | // order, and the row is labelled as a chip is. |
| 178 | func TestTypedDatesReadNumbersInTheReadersOrder(t *testing.T) { |
| 179 | rig := newRig(t) |
| 180 | rig.clocked(t) |
| 181 | for language, want := range map[string][3]string{ |
| 182 | "en-IE": {"12/11", "2026-11-12", "Thursday, 12 November"}, |
| 183 | "en-US": {"12/11", "2026-12-11", "Friday, December 11"}, |
| 184 | "de": {"12.11.", "2026-11-12", "Donnerstag, 12. November"}, |
| 185 | } { |
| 186 | answer := rig.getIn(t, Ana, "/search/suggest?q="+ |
| 187 | url.QueryEscape(want[0]), language) |
| 188 | if !strings.Contains(answer, `"date":"`+want[1]+`"`) || |
| 189 | !strings.Contains(answer, `"label":"`+want[2]+`"`) || |
| 190 | !strings.Contains(answer, `"numeric":true`) { |
| 191 | t.Errorf("%s in %s: %s", want[0], language, answer) |
| 192 | } |
| 193 | } |
| 194 | // ⌘K reads numbers only with the locale's own separator: "1.5" in |
| 195 | // English and "12/11" in German are searches (review round 8, M1). |
| 196 | for language, query := range map[string]string{"en-IE": "1.5", |
| 197 | "en-US": "1.5", "de": "12/11"} { |
| 198 | answer := rig.getIn(t, Ana, "/search/suggest?q="+ |
| 199 | url.QueryEscape(query), language) |
| 200 | if strings.Contains(answer, `"date":`) { |
| 201 | t.Errorf("%s in %s is a date: %s", query, language, answer) |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // What is stored is canonical: the same Markdown, ISO dates and all, |
| 207 | // whatever locale wrote it or reads it. |
| 208 | func TestTheStoredMarkdownIsTheSameInEveryLocale(t *testing.T) { |
| 209 | rig := newRig(t) |
| 210 | const body = "Call on [[2026-11-20]], not 12/11.\n\n- [[2025-01-01]]" |
| 211 | for _, language := range []string{"en-US", "de", "fr"} { |
| 212 | page := rig.createPageIn(t, Ana, "Plan "+language, body, language) |
| 213 | var sources []string |
| 214 | for _, reader := range []string{"en-IE", "en-US", "de", "fr"} { |
| 215 | form := rig.getIn(t, Ana, page+"/edit", reader) |
| 216 | sources = append(sources, between(form, "<textarea", "</textarea>")) |
| 217 | wantIn(t, "the Markdown screen", reader, form, |
| 218 | "Call on [[2026-11-20]], not 12/11.") |
| 219 | } |
| 220 | for _, source := range sources[1:] { |
| 221 | if source != sources[0] { |
| 222 | t.Errorf("written in %s, the Markdown reads differently by "+ |
| 223 | "locale:\n%s\n%s", language, sources[0], source) |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // createPageIn is createPage from a browser asking for language. |
| 230 | func (rig *rig) createPageIn(t *testing.T, who, title, body, |
| 231 | language string) string { |
| 232 | t.Helper() |
| 233 | request := httptest.NewRequest(http.MethodPost, "/pages", |
| 234 | strings.NewReader(url.Values{"title": {title}, |
| 235 | "body": {body}}.Encode())) |
| 236 | request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 237 | request.Header.Set("Origin", testOrigin) |
| 238 | request.Header.Set("Accept-Language", language) |
| 239 | recorder := httptest.NewRecorder() |
| 240 | rig.built.As(who).ServeHTTP(recorder, request) |
| 241 | if recorder.Code != http.StatusSeeOther { |
| 242 | t.Fatalf("creating %q in %s: %d", title, language, recorder.Code) |
| 243 | } |
| 244 | return recorder.Header().Get("Location") |
| 245 | } |
| 246 | |
| 247 | // Accept-Language picks among the offered locales, a region falling to |
| 248 | // its language; anything else is the team's own. |
| 249 | func TestTheLocaleIsNegotiatedWithTheTeamsAsFallback(t *testing.T) { |
| 250 | rig := newRig(t) |
| 251 | for language, want := range map[string]string{ |
| 252 | "": "Tuesday 17 November<", |
| 253 | "de-AT": "Dienstag, 17. November<", |
| 254 | "en-AU,en;q=0.8": "Tuesday 17 November<", |
| 255 | "es-MX": "martes, 17 de noviembre<", |
| 256 | "ja,pt-BR;q=0.9": "Tuesday 17 November<", |
| 257 | "ja,en-US;q=0.5": "Tuesday, November 17<", |
| 258 | "fr-CA,fr;q=0.9,": "mardi 17 novembre<", |
| 259 | } { |
| 260 | day := rig.getIn(t, Ana, "/d/2026-11-17", language) |
| 261 | wantIn(t, "the day's title", language, day, want) |
| 262 | } |
| 263 | american := teamRig(t, "en-US") |
| 264 | wantIn(t, "an en-US team's day", "", american.getIn(t, Ana, |
| 265 | "/d/2026-11-17", ""), "Tuesday, November 17<", |
| 266 | "<th abbr=\"Sunday\">S</th>") |
| 267 | } |
| 268 | |
| 269 | // teamRig is newRig with MAPA_LOCALE set to team. |
| 270 | func teamRig(t *testing.T, team string) *rig { |
| 271 | t.Helper() |
| 272 | env := testEnv(Ana + "," + Bo) |
| 273 | env["MAPA_LOCALE"] = team |
| 274 | config, err := mapa.LoadConfig(func(key string) string { |
| 275 | return env[key] |
| 276 | }) |
| 277 | if err != nil { |
| 278 | t.Fatal(err) |
| 279 | } |
| 280 | dir := t.TempDir() |
| 281 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 282 | database, err := db.Open(filepath.Join(dir, "mapa.db"), logger) |
| 283 | if err != nil { |
| 284 | t.Fatal(err) |
| 285 | } |
| 286 | t.Cleanup(func() { database.Close() }) |
| 287 | built, err := mapa.AppForTest(database, config, logger, Ana) |
| 288 | if err != nil { |
| 289 | t.Fatal(err) |
| 290 | } |
| 291 | return &rig{built: built, db: database, dir: dir} |
| 292 | } |
| 293 | |