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|[TW]|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, "