package mapatest import ( "crypto/sha256" "fmt" "net/http" "net/http/httptest" "net/url" "regexp" "strings" "testing" "time" ) // Level-up ยง4.5, History: the summaries from each revision's event verb, // "Earlier history has aged out", the fragment rule, scope (L6, gate 1) // and nothing written by a GET (L7). // summaries is each version's one line in a page's history, newest // first, as the viewer reads it. func (rig *rig) summaries(t *testing.T, who, path string) []string { t.Helper() panel := rig.page(t, who, path+"/history") var lines []string for _, part := range strings.Split(panel, "")[1:] { line, _, _ := strings.Cut(part, "") lines = append(lines, line) } return lines } func expectSummaries(t *testing.T, got []string, want ...string) { t.Helper() if strings.Join(got, " | ") != strings.Join(want, " | ") { t.Fatalf("summaries:\n got %q\n want %q", got, want) } } func TestHistorySummarisesEachVerb(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) step := func() { moving.now = moving.now.Add(time.Hour) } acme := rig.createPage(t, Ana, "Acme", "Intro.\n\n## Contacts\n\n"+ "Ana, budget.") first := revisionOf(t, rig, acme) notes := rig.createPage(t, Ana, "Notes", "See [[Acme]].") edit := func(who, body string) { step() rig.post(t, who, acme, url.Values{"revision": {revisionOf(t, rig, acme)}, "body": {body}}) } edit(Bo, "Intro.\n\n## Contacts\n\nAna, budget. Weekly call.") edit(Bo, "Intro.\n\n## Contacts\n\nAna, budget. Weekly call.\n\nTom.") edit(Bo, "## Contacts\n\nAna, budget. Weekly call.\n\nTom.") step() rig.post(t, Bo, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) step() rig.post(t, Ana, acme+"/links", nil) step() rig.post(t, Ana, acme+"/blocks", url.Values{"block": {"1"}}) step() rig.post(t, Ana, acme+"/history/"+first, nil) expectSummaries(t, rig.summaries(t, Ana, acme), "Restored the version from 12 November 2026 at 09:00", "Made a block linkable", "Renamed from Acme", "Removed a block", "Added a block", "Edited Contacts", "Created this page") expectSummaries(t, rig.summaries(t, Ana, notes), "Updated links", "Created this page") panel := rig.page(t, Ana, acme+"/history") if !strings.Contains(panel, "bo") || !strings.Contains(panel, "You") || !strings.Contains(panel, "

Today

") { t.Fatal("authors or days are not shown as the viewer reads them") } rig.post(t, Ana, notes+"/delete", nil) back := rig.post(t, Ana, strings.Replace(notes, "/p/", "/deleted/", 1), nil).Header().Get("Location") back, _, _ = strings.Cut(back, "?") expectSummaries(t, rig.summaries(t, Ana, back), "Brought back after it was deleted") } // Past the window, a rename names its new title, a restore cannot name // its source, and the list says the rest has aged out. Nothing is ever // diffed against a revision the window has dropped. func TestHistoryAgesOut(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) acme := rig.createPage(t, Ana, "Acme", "Body A.") first := revisionOf(t, rig, acme) moving.now = moving.now.Add(time.Hour) rig.post(t, Ana, acme, url.Values{"revision": {first}, "body": {"Body B."}}) if strings.Contains(rig.page(t, Ana, acme+"/history"), "aged out") { t.Fatal("a page inside the window says its history aged out") } moving.now = moving.now.Add(80 * 24 * time.Hour) rig.post(t, Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) moving.now = moving.now.Add(5 * 24 * time.Hour) rig.post(t, Ana, acme+"/history/"+first, nil) moving.now = moving.now.Add(6 * 24 * time.Hour) expectSummaries(t, rig.summaries(t, Ana, acme), "Restored an earlier version", "Renamed to Acme Ltd") panel := rig.page(t, Ana, acme+"/history") if !strings.Contains(panel, "Earlier history has aged out.") { t.Fatal("the list does not say earlier history aged out") } oldest := lastBetween(panel, acme+"/history/", `"`) version := rig.page(t, Ana, acme+"/history/"+oldest) if !strings.Contains(version, "The version before it has aged out") || strings.Contains(version, "") { t.Fatal("a version was diffed against one past the window") } } func TestHistoryShowsOlderInPages(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) acme := rig.createPage(t, Ana, "Acme", "0") for count := 1; count <= 31; count++ { moving.now = moving.now.Add(time.Minute) rig.post(t, Ana, acme, url.Values{"revision": {revisionOf(t, rig, acme)}, "body": {fmt.Sprint(count)}}) } if got := len(rig.summaries(t, Ana, acme)); got != 30 { t.Fatalf("the first screen summarises %d versions", got) } panel := rig.page(t, Ana, acme+"/history") older := between(panel, `href="`+acme+`/history?older=`, `"`) if older != "30" { t.Fatalf("Show older pages on to %q", older) } rest := rig.page(t, Ana, acme+"/history?older=30") if strings.Count(rest, "") != 2 || !strings.Contains(rest, "Created this page") { t.Fatal("Show older does not list the rest") } } // fetchAs is a GET with request headers, as a browser sends them. func (rig *rig) fetchAs(who, target string, headers map[string]string) *httptest.ResponseRecorder { request := httptest.NewRequest(http.MethodGet, target, nil) for name, value := range headers { request.Header.Set(name, value) } recorder := httptest.NewRecorder() rig.built.As(who).ServeHTTP(recorder, request) return recorder } // Docs' rule: only a fetch (Sec-Fetch-Dest: empty) gets the fragment; a // document request, or one with no Sec-Fetch-* at all, the full page. func TestThePanelFragmentFollowsSecFetchDest(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Body.") revision := revisionOf(t, rig, acme) for _, path := range []string{acme + "/history?panel=1", acme + "/history/" + revision + "?panel=1"} { for _, check := range []struct { headers map[string]string fragment bool }{ {map[string]string{"Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors"}, true}, {map[string]string{"Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate"}, false}, {nil, false}, } { got := rig.fetchAs(Ana, path, check.headers) body := got.Body.String() isFragment := strings.HasPrefix(body, "