oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "path/filepath"
8 "regexp"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/rastrillo/rastrillo/blobs"
14
15 "amadan.net/oficina/mapa/internal/mapa"
16 "amadan.net/oficina/notion.git/notiontest"
17)
18
19// The sweep (level-up build note 85, review round 8, M3): every screen
20// that shows a date, a time or a count, seeded so each shows one, read
21// in German and in en-US. A date, time or count written the en-IE way
22// on either is a place that did not use the reader's locale.
23
24const months = "January|February|March|April|May|June|July|August|" +
25 "September|October|November|December"
26
27// english is what only an English (en-IE) writing of a date has: a
28// day before a month name with no dot after it, an English weekday,
29// a short English pill, English week-strip letters, or a count
30// grouped with a comma.
31var english = regexp.MustCompile(`\b\d{1,2} (` + months + `|Jan|Feb|` +
32 `Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b|\b(Monday|Tuesday|Wednesday|` +
33 `Thursday|Friday|Saturday|Sunday)\b|\b(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ` +
34 `\d|<abbr aria-hidden="true">[TW]</abbr>|1,234|\b1234\b`)
35
36// notAmerican is what en-US never writes: a day before its month, a
37// weekday with no comma after it, a 24-hour afternoon, an ungrouped
38// count.
39var notAmerican = regexp.MustCompile(`\b\d{1,2} (` + months + `|Jan|` +
40 `Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b|\b(Monday|Tuesday|` +
41 `Wednesday|Thursday|Friday|Saturday|Sunday) \d|\b(1[3-9]|2[0-3]):` +
42 `[0-5]\d\b|\b1234\b`)
43
44// sweepWorld is Ana's: an old page with an image taken out of it, a
45// team page linked and mentioned from her daily notes, edited, grown by
46// 1,234 blocks and restored.
47func sweepWorld(t *testing.T) (*rig, map[string]string) {
48 rig := newRig(t)
49 moving := rig.clocked(t)
50 moving.now = time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC)
51 archive := rig.createPage(t, Ana, "Archive", "Old page.")
52 // Its Markdown screen lists the image as "Earlier image, taken out
53 // on <date>".
54 photo := rig.upload(t, Ana, pngOf(t, 1))
55 rig.editPage(t, Ana, archive, "Old page.\n\n"+photo.markdown("Photo"))
56 rig.editPage(t, Ana, archive, "Old page.")
57 moving.now = time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC)
58 acme := rig.createPage(t, Ana, "Acme", "Customer.")
59 first := revisionOf(t, rig, acme)
60 rig.post(t, Ana, "/d/2026-11-10", url.Values{"revision": {"0"},
61 "body": {"Met [[Acme]] about the pilot. Next [[2026-11-20]]."}})
62 rig.post(t, Ana, "/d/2026-11-11", url.Values{"revision": {"0"},
63 "body": {"Acme called again."}})
64 moving.now = time.Date(2026, 11, 11, 21, 9, 0, 0, time.UTC)
65 lines := []string{"Customer."}
66 for range 1234 {
67 lines = append(lines, "A line.")
68 }
69 rig.editPage(t, Ana, acme, strings.Join(lines, "\n\n"))
70 moving.now = time.Date(2026, 11, 12, 21, 9, 0, 0, time.UTC)
71 rig.post(t, Ana, acme+"/history/"+first, nil)
72 moving.now = time.Date(2026, 11, 12, 22, 0, 0, 0, time.UTC)
73 // The daily note pinned, so the sidebar lists it, and its own
74 // history and delete screens, which name it by its date.
75 island := between(rig.page(t, Ana, "/d/2026-11-10"),
76 `id="mapa-editor">`, "</script>")
77 day := "/p/" + between(island, `"page":"`, `"`)
78 rig.post(t, Ana, day+"/pin", url.Values{"pin": {"1"}})
79 return rig, map[string]string{"archive": archive, "acme": acme,
80 "first": first, "day": day,
81 "dayRevision": between(island, `"base":"`, `"`)}
82}
83
84// sweepGet is a GET in language, with headers.
85func (rig *rig) sweepGet(t *testing.T, target, language string,
86 headers ...string) string {
87 t.Helper()
88 request := httptest.NewRequest(http.MethodGet, target, nil)
89 request.Header.Set("Accept-Language", language)
90 for at := 0; at+1 < len(headers); at += 2 {
91 request.Header.Set(headers[at], headers[at+1])
92 }
93 recorder := httptest.NewRecorder()
94 rig.built.As(Ana).ServeHTTP(recorder, request)
95 if recorder.Code != http.StatusOK {
96 t.Fatalf("GET %s: %d", target, recorder.Code)
97 }
98 page := recorder.Body.String()
99 // Settings names every format in its own way, on purpose.
100 before, _, _ := strings.Cut(page, "<select")
101 return before
102}
103
104func TestEveryScreenWritesDatesInTheReadersLocale(t *testing.T) {
105 rig, at := sweepWorld(t)
106 acme := at["acme"]
107 screens := []string{"/", "/d/2026-11-10", "/d/2026-11-11?month=2026-10",
108 "/d/2026-11-17", acme, acme + "/history",
109 acme + "/history/" + at["first"], at["archive"],
110 at["archive"] + "/edit", "/pages",
111 at["day"] + "/history", at["day"] + "/history/" + at["dayRevision"],
112 at["day"] + "/delete",
113 "/search?q=2026-11", "/search?q=pilot", "/settings?done=zone"}
114 for language, wrong := range map[string]*regexp.Regexp{
115 "de": english, "en-US": notAmerican} {
116 for _, screen := range screens {
117 page := rig.sweepGet(t, screen, language)
118 if found := wrong.FindString(page); found != "" {
119 t.Errorf("%s in %s writes %q", screen, language, found)
120 }
121 }
122 panel := rig.sweepGet(t, acme+"/history?panel=1", language,
123 "Sec-Fetch-Dest", "empty")
124 if found := wrong.FindString(panel); found != "" {
125 t.Errorf("the History fragment in %s writes %q", language, found)
126 }
127 }
128}
129
130// TestTheSweepSeesEveryKindOfDate keeps the sweep honest: in en-IE the
131// same screens hold every pattern it looks for, so a screen that
132// stopped showing one would not pass quietly.
133func TestTheSweepSeesEveryKindOfDate(t *testing.T) {
134 rig, at := sweepWorld(t)
135 acme := at["acme"]
136 wants := map[string][]string{
137 "/d/2026-11-10": {"Tuesday 10 November<span mapa-title-faint>",
138 "<title>Tuesday 10 November 2026", "Friday, 20 November",
139 "Tue, 10 Nov", "<abbr aria-hidden=\"true\">T</abbr>"},
140 acme: {"Tuesday 10 November 2026", "Wednesday 11 November 2026",
141 // editedWhen counts from the real clock, which is past 12
142 // November 2026 for good.
143 "Edited by you on 12 November"},
144 acme + "/history": {"Added 1,234 blocks",
145 "Restored the version from 10 November 2026 at 08:00",
146 ">21:09</time>"},
147 acme + "/history/" + at["first"]: {"12 November 2026 at 21:09"},
148 "/pages": {">1 September 2026</time>"},
149 at["archive"] + "/edit": {
150 "Earlier image, taken out on 1 September 2026"},
151 "/search?q=2026-11": {"Tuesday 10 November 2026"},
152 "/search?q=pilot": {"Tuesday 10 November 2026"},
153 "/settings?done=zone": {
154 "Today is now Thursday 12 November 2026"},
155 at["day"] + "/delete": {"Tuesday 10 November 2026"},
156 at["day"] + "/history/" + at["dayRevision"]: {
157 "Tuesday 10 November 2026"},
158 "/": {"<span mapa-side-label>Tuesday 10 November 2026"},
159 }
160 for screen, texts := range wants {
161 wantIn(t, screen, "en-IE", rig.sweepGet(t, screen, "en-IE"),
162 texts...)
163 }
164}
165
166// TestImageRefusalsWriteNumbersInTheReadersLocale: an upload's refusal
167// is a sentence the editor shows, so its numbers are the reader's too.
168func TestImageRefusalsWriteNumbersInTheReadersLocale(t *testing.T) {
169 rig := newRigWith(t, func(config *mapa.Config, _ string) {
170 config.Images.QuotaPerson = 2*(64<<10+28) + 10
171 })
172 rig.upload(t, Ana, pngOf(t, 1))
173 rig.upload(t, Ana, pngOf(t, 2))
174 refused := func(language string, raw []byte) string {
175 body, kind := multipartOf(t, nil, "a.png", raw)
176 request := httptest.NewRequest(http.MethodPost, "/images", body)
177 request.Header.Set("Content-Type", kind)
178 request.Header.Set("Accept", "application/json")
179 request.Header.Set("Origin", testOrigin)
180 request.Header.Set("Accept-Language", language)
181 recorder := httptest.NewRecorder()
182 rig.built.As(Ana).ServeHTTP(recorder, request)
183 return recorder.Body.String()
184 }
185 for language, wants := range map[string][]string{
186 "en-IE": {"10,000 pixels", "131,138 bytes"},
187 "de": {"10.000 pixels", "131.138 bytes"},
188 } {
189 sizes := refused(language, pngHeader(10001, 1))
190 quota := refused(language, pngOf(t, 3))
191 if !strings.Contains(sizes, wants[0]) {
192 t.Errorf("the size refusal in %s: %s", language, sizes)
193 }
194 if !strings.Contains(quota, wants[1]) {
195 t.Errorf("the allowance refusal in %s: %s", language, quota)
196 }
197 }
198}
199
200// TestTheImportWritesDatesAndCountsInTheReadersLocale is the sweep over
201// the Notion import's screens: the report's counts and the allowances
202// on the import page, and "created there" and "edited there" on an
203// imported page and its History, in German and en-US, and each seen in
204// en-IE so the check proves something.
205func TestTheImportWritesDatesAndCountsInTheReadersLocale(t *testing.T) {
206 ws := notiontest.NewWorkspace("sweep")
207 root := ws.Page("", notiontest.Text("Imported"))
208 // 1,234 backticks, each made literal: the report counts them.
209 ws.Add(root, notiontest.Para(notiontest.Text(strings.Repeat("`",
210 1234))))
211 rig, _ := newImportRigWith(t, ws, func(config *mapa.Config,
212 dir string) {
213 images, err := blobs.Dir(filepath.Join(dir, "images"))
214 if err != nil {
215 t.Fatal(err)
216 }
217 config.Images.Objects = images
218 config.Images.QuotaTeam = 1234 << 20
219 })
220 rig.importAll(t)
221 found := importedHref.FindStringSubmatch(rig.sweepGet(t, "/",
222 "en-IE"))
223 if found == nil {
224 t.Fatal("the imported page is not in the sidebar")
225 }
226 page := found[1]
227 wants := map[string][]string{
228 "/import/notion": {"literally</dt><dd>1,234</dd>",
229 "of 1,234 MB used"},
230 page: {"From Notion, created there 1 September 2026"},
231 page + "/history": {"edited there 1 September 2026"},
232 }
233 for screen, texts := range wants {
234 wantIn(t, screen, "en-IE", rig.sweepGet(t, screen, "en-IE"),
235 texts...)
236 for language, wrong := range map[string]*regexp.Regexp{
237 "de": english, "en-US": notAmerican} {
238 if found := wrong.FindString(rig.sweepGet(t, screen,
239 language)); found != "" {
240 t.Errorf("%s in %s writes %q", screen, language, found)
241 }
242 }
243 }
244}
245
246// importedHref is the sidebar's link to the page titled Imported.
247var importedHref = regexp.MustCompile(`(?s)href="(/p/[0-9a-z]+)"[^>]*>` +
248 `(?:<svg.*?</svg>)?<span mapa-side-label>Imported</span>`)
249