package mapatest import ( "net/http" "net/http/httptest" "net/url" "path/filepath" "regexp" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/oficina/mapa/internal/mapa" "amadan.net/oficina/notion.git/notiontest" ) // The sweep (level-up build note 85, review round 8, M3): every screen // that shows a date, a time or a count, seeded so each shows one, read // in German and in en-US. A date, time or count written the en-IE way // on either is a place that did not use the reader's locale. const months = "January|February|March|April|May|June|July|August|" + "September|October|November|December" // english is what only an English (en-IE) writing of a date has: a // day before a month name with no dot after it, an English weekday, // a short English pill, English week-strip letters, or a count // grouped with a comma. var english = regexp.MustCompile(`\b\d{1,2} (` + months + `|Jan|Feb|` + `Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b|\b(Monday|Tuesday|Wednesday|` + `Thursday|Friday|Saturday|Sunday)\b|\b(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ` + `\d||1,234|\b1234\b`) // notAmerican is what en-US never writes: a day before its month, a // weekday with no comma after it, a 24-hour afternoon, an ungrouped // count. var notAmerican = regexp.MustCompile(`\b\d{1,2} (` + months + `|Jan|` + `Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b|\b(Monday|Tuesday|` + `Wednesday|Thursday|Friday|Saturday|Sunday) \d|\b(1[3-9]|2[0-3]):` + `[0-5]\d\b|\b1234\b`) // sweepWorld is Ana's: an old page with an image taken out of it, a // team page linked and mentioned from her daily notes, edited, grown by // 1,234 blocks and restored. func sweepWorld(t *testing.T) (*rig, map[string]string) { rig := newRig(t) moving := rig.clocked(t) moving.now = time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC) archive := rig.createPage(t, Ana, "Archive", "Old page.") // Its Markdown screen lists the image as "Earlier image, taken out // on ". photo := rig.upload(t, Ana, pngOf(t, 1)) rig.editPage(t, Ana, archive, "Old page.\n\n"+photo.markdown("Photo")) rig.editPage(t, Ana, archive, "Old page.") moving.now = time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC) acme := rig.createPage(t, Ana, "Acme", "Customer.") first := revisionOf(t, rig, acme) rig.post(t, Ana, "/d/2026-11-10", url.Values{"revision": {"0"}, "body": {"Met [[Acme]] about the pilot. Next [[2026-11-20]]."}}) rig.post(t, Ana, "/d/2026-11-11", url.Values{"revision": {"0"}, "body": {"Acme called again."}}) moving.now = time.Date(2026, 11, 11, 21, 9, 0, 0, time.UTC) lines := []string{"Customer."} for range 1234 { lines = append(lines, "A line.") } rig.editPage(t, Ana, acme, strings.Join(lines, "\n\n")) moving.now = time.Date(2026, 11, 12, 21, 9, 0, 0, time.UTC) rig.post(t, Ana, acme+"/history/"+first, nil) moving.now = time.Date(2026, 11, 12, 22, 0, 0, 0, time.UTC) // The daily note pinned, so the sidebar lists it, and its own // history and delete screens, which name it by its date. island := between(rig.page(t, Ana, "/d/2026-11-10"), `id="mapa-editor">`, "") day := "/p/" + between(island, `"page":"`, `"`) rig.post(t, Ana, day+"/pin", url.Values{"pin": {"1"}}) return rig, map[string]string{"archive": archive, "acme": acme, "first": first, "day": day, "dayRevision": between(island, `"base":"`, `"`)} } // sweepGet is a GET in language, with headers. func (rig *rig) sweepGet(t *testing.T, target, language string, headers ...string) string { t.Helper() request := httptest.NewRequest(http.MethodGet, target, nil) request.Header.Set("Accept-Language", language) for at := 0; at+1 < len(headers); at += 2 { request.Header.Set(headers[at], headers[at+1]) } recorder := httptest.NewRecorder() rig.built.As(Ana).ServeHTTP(recorder, request) if recorder.Code != http.StatusOK { t.Fatalf("GET %s: %d", target, recorder.Code) } page := recorder.Body.String() // Settings names every format in its own way, on purpose. before, _, _ := strings.Cut(page, "", "Tuesday 10 November 2026", "Friday, 20 November", "Tue, 10 Nov", "<abbr aria-hidden=\"true\">T</abbr>"}, acme: {"Tuesday 10 November 2026", "Wednesday 11 November 2026", // editedWhen counts from the real clock, which is past 12 // November 2026 for good. "Edited by you on 12 November"}, acme + "/history": {"Added 1,234 blocks", "Restored the version from 10 November 2026 at 08:00", ">21:09</time>"}, acme + "/history/" + at["first"]: {"12 November 2026 at 21:09"}, "/pages": {">1 September 2026</time>"}, at["archive"] + "/edit": { "Earlier image, taken out on 1 September 2026"}, "/search?q=2026-11": {"Tuesday 10 November 2026"}, "/search?q=pilot": {"Tuesday 10 November 2026"}, "/settings?done=zone": { "Today is now Thursday 12 November 2026"}, at["day"] + "/delete": {"Tuesday 10 November 2026"}, at["day"] + "/history/" + at["dayRevision"]: { "Tuesday 10 November 2026"}, "/": {"<span mapa-side-label>Tuesday 10 November 2026"}, } for screen, texts := range wants { wantIn(t, screen, "en-IE", rig.sweepGet(t, screen, "en-IE"), texts...) } } // TestImageRefusalsWriteNumbersInTheReadersLocale: an upload's refusal // is a sentence the editor shows, so its numbers are the reader's too. func TestImageRefusalsWriteNumbersInTheReadersLocale(t *testing.T) { rig := newRigWith(t, func(config *mapa.Config, _ string) { config.Images.QuotaPerson = 2*(64<<10+28) + 10 }) rig.upload(t, Ana, pngOf(t, 1)) rig.upload(t, Ana, pngOf(t, 2)) refused := func(language string, raw []byte) string { body, kind := multipartOf(t, nil, "a.png", raw) request := httptest.NewRequest(http.MethodPost, "/images", body) request.Header.Set("Content-Type", kind) request.Header.Set("Accept", "application/json") request.Header.Set("Origin", testOrigin) request.Header.Set("Accept-Language", language) recorder := httptest.NewRecorder() rig.built.As(Ana).ServeHTTP(recorder, request) return recorder.Body.String() } for language, wants := range map[string][]string{ "en-IE": {"10,000 pixels", "131,138 bytes"}, "de": {"10.000 pixels", "131.138 bytes"}, } { sizes := refused(language, pngHeader(10001, 1)) quota := refused(language, pngOf(t, 3)) if !strings.Contains(sizes, wants[0]) { t.Errorf("the size refusal in %s: %s", language, sizes) } if !strings.Contains(quota, wants[1]) { t.Errorf("the allowance refusal in %s: %s", language, quota) } } } // TestTheImportWritesDatesAndCountsInTheReadersLocale is the sweep over // the Notion import's screens: the report's counts and the allowances // on the import page, and "created there" and "edited there" on an // imported page and its History, in German and en-US, and each seen in // en-IE so the check proves something. func TestTheImportWritesDatesAndCountsInTheReadersLocale(t *testing.T) { ws := notiontest.NewWorkspace("sweep") root := ws.Page("", notiontest.Text("Imported")) // 1,234 backticks, each made literal: the report counts them. ws.Add(root, notiontest.Para(notiontest.Text(strings.Repeat("`", 1234)))) rig, _ := newImportRigWith(t, ws, func(config *mapa.Config, dir string) { images, err := blobs.Dir(filepath.Join(dir, "images")) if err != nil { t.Fatal(err) } config.Images.Objects = images config.Images.QuotaTeam = 1234 << 20 }) rig.importAll(t) found := importedHref.FindStringSubmatch(rig.sweepGet(t, "/", "en-IE")) if found == nil { t.Fatal("the imported page is not in the sidebar") } page := found[1] wants := map[string][]string{ "/import/notion": {"literally</dt><dd>1,234</dd>", "of 1,234 MB used"}, page: {"From Notion, created there 1 September 2026"}, page + "/history": {"edited there 1 September 2026"}, } for screen, texts := range wants { wantIn(t, screen, "en-IE", rig.sweepGet(t, screen, "en-IE"), texts...) for language, wrong := range map[string]*regexp.Regexp{ "de": english, "en-US": notAmerican} { if found := wrong.FindString(rig.sweepGet(t, screen, language)); found != "" { t.Errorf("%s in %s writes %q", screen, language, found) } } } } // importedHref is the sidebar's link to the page titled Imported. var importedHref = regexp.MustCompile(`(?s)href="(/p/[0-9a-z]+)"[^>]*>` + `(?:<svg.*?</svg>)?<span mapa-side-label>Imported</span>`)