| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // Level-up §4.5, History: the summaries from each revision's event verb, |
| 16 | // "Earlier history has aged out", the fragment rule, scope (L6, gate 1) |
| 17 | // and nothing written by a GET (L7). |
| 18 | |
| 19 | // summaries is each version's one line in a page's history, newest |
| 20 | // first, as the viewer reads it. |
| 21 | func (rig *rig) summaries(t *testing.T, who, path string) []string { |
| 22 | t.Helper() |
| 23 | panel := rig.page(t, who, path+"/history") |
| 24 | var lines []string |
| 25 | for _, part := range strings.Split(panel, "<small mapa-ver-summary>")[1:] { |
| 26 | line, _, _ := strings.Cut(part, "</small>") |
| 27 | lines = append(lines, line) |
| 28 | } |
| 29 | return lines |
| 30 | } |
| 31 | |
| 32 | func expectSummaries(t *testing.T, got []string, want ...string) { |
| 33 | t.Helper() |
| 34 | if strings.Join(got, " | ") != strings.Join(want, " | ") { |
| 35 | t.Fatalf("summaries:\n got %q\n want %q", got, want) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestHistorySummarisesEachVerb(t *testing.T) { |
| 40 | rig := newRig(t) |
| 41 | moving := rig.clocked(t) |
| 42 | step := func() { moving.now = moving.now.Add(time.Hour) } |
| 43 | acme := rig.createPage(t, Ana, "Acme", "Intro.\n\n## Contacts\n\n"+ |
| 44 | "Ana, budget.") |
| 45 | first := revisionOf(t, rig, acme) |
| 46 | notes := rig.createPage(t, Ana, "Notes", "See [[Acme]].") |
| 47 | edit := func(who, body string) { |
| 48 | step() |
| 49 | rig.post(t, who, acme, url.Values{"revision": {revisionOf(t, rig, |
| 50 | acme)}, "body": {body}}) |
| 51 | } |
| 52 | edit(Bo, "Intro.\n\n## Contacts\n\nAna, budget. Weekly call.") |
| 53 | edit(Bo, "Intro.\n\n## Contacts\n\nAna, budget. Weekly call.\n\nTom.") |
| 54 | edit(Bo, "## Contacts\n\nAna, budget. Weekly call.\n\nTom.") |
| 55 | step() |
| 56 | rig.post(t, Bo, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) |
| 57 | step() |
| 58 | rig.post(t, Ana, acme+"/links", nil) |
| 59 | step() |
| 60 | rig.post(t, Ana, acme+"/blocks", url.Values{"block": {"1"}}) |
| 61 | step() |
| 62 | rig.post(t, Ana, acme+"/history/"+first, nil) |
| 63 | expectSummaries(t, rig.summaries(t, Ana, acme), |
| 64 | "Restored the version from 12 November 2026 at 09:00", |
| 65 | "Made a block linkable", "Renamed from Acme", "Removed a block", |
| 66 | "Added a block", "Edited Contacts", "Created this page") |
| 67 | expectSummaries(t, rig.summaries(t, Ana, notes), "Updated links", |
| 68 | "Created this page") |
| 69 | panel := rig.page(t, Ana, acme+"/history") |
| 70 | if !strings.Contains(panel, "<b>bo</b>") || |
| 71 | !strings.Contains(panel, "<b>You</b>") || |
| 72 | !strings.Contains(panel, "<h3>Today</h3>") { |
| 73 | t.Fatal("authors or days are not shown as the viewer reads them") |
| 74 | } |
| 75 | rig.post(t, Ana, notes+"/delete", nil) |
| 76 | back := rig.post(t, Ana, strings.Replace(notes, "/p/", "/deleted/", 1), |
| 77 | nil).Header().Get("Location") |
| 78 | back, _, _ = strings.Cut(back, "?") |
| 79 | expectSummaries(t, rig.summaries(t, Ana, back), |
| 80 | "Brought back after it was deleted") |
| 81 | } |
| 82 | |
| 83 | // Past the window, a rename names its new title, a restore cannot name |
| 84 | // its source, and the list says the rest has aged out. Nothing is ever |
| 85 | // diffed against a revision the window has dropped. |
| 86 | func TestHistoryAgesOut(t *testing.T) { |
| 87 | rig := newRig(t) |
| 88 | moving := rig.clocked(t) |
| 89 | acme := rig.createPage(t, Ana, "Acme", "Body A.") |
| 90 | first := revisionOf(t, rig, acme) |
| 91 | moving.now = moving.now.Add(time.Hour) |
| 92 | rig.post(t, Ana, acme, url.Values{"revision": {first}, |
| 93 | "body": {"Body B."}}) |
| 94 | if strings.Contains(rig.page(t, Ana, acme+"/history"), "aged out") { |
| 95 | t.Fatal("a page inside the window says its history aged out") |
| 96 | } |
| 97 | moving.now = moving.now.Add(80 * 24 * time.Hour) |
| 98 | rig.post(t, Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) |
| 99 | moving.now = moving.now.Add(5 * 24 * time.Hour) |
| 100 | rig.post(t, Ana, acme+"/history/"+first, nil) |
| 101 | moving.now = moving.now.Add(6 * 24 * time.Hour) |
| 102 | expectSummaries(t, rig.summaries(t, Ana, acme), |
| 103 | "Restored an earlier version", "Renamed to Acme Ltd") |
| 104 | panel := rig.page(t, Ana, acme+"/history") |
| 105 | if !strings.Contains(panel, "Earlier history has aged out.") { |
| 106 | t.Fatal("the list does not say earlier history aged out") |
| 107 | } |
| 108 | oldest := lastBetween(panel, acme+"/history/", `"`) |
| 109 | version := rig.page(t, Ana, acme+"/history/"+oldest) |
| 110 | if !strings.Contains(version, "The version before it has aged out") || |
| 111 | strings.Contains(version, "<del>") { |
| 112 | t.Fatal("a version was diffed against one past the window") |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestHistoryShowsOlderInPages(t *testing.T) { |
| 117 | rig := newRig(t) |
| 118 | moving := rig.clocked(t) |
| 119 | acme := rig.createPage(t, Ana, "Acme", "0") |
| 120 | for count := 1; count <= 31; count++ { |
| 121 | moving.now = moving.now.Add(time.Minute) |
| 122 | rig.post(t, Ana, acme, url.Values{"revision": {revisionOf(t, rig, |
| 123 | acme)}, "body": {fmt.Sprint(count)}}) |
| 124 | } |
| 125 | if got := len(rig.summaries(t, Ana, acme)); got != 30 { |
| 126 | t.Fatalf("the first screen summarises %d versions", got) |
| 127 | } |
| 128 | panel := rig.page(t, Ana, acme+"/history") |
| 129 | older := between(panel, `href="`+acme+`/history?older=`, `"`) |
| 130 | if older != "30" { |
| 131 | t.Fatalf("Show older pages on to %q", older) |
| 132 | } |
| 133 | rest := rig.page(t, Ana, acme+"/history?older=30") |
| 134 | if strings.Count(rest, "<small mapa-ver-summary>") != 2 || |
| 135 | !strings.Contains(rest, "Created this page") { |
| 136 | t.Fatal("Show older does not list the rest") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // fetchAs is a GET with request headers, as a browser sends them. |
| 141 | func (rig *rig) fetchAs(who, target string, |
| 142 | headers map[string]string) *httptest.ResponseRecorder { |
| 143 | request := httptest.NewRequest(http.MethodGet, target, nil) |
| 144 | for name, value := range headers { |
| 145 | request.Header.Set(name, value) |
| 146 | } |
| 147 | recorder := httptest.NewRecorder() |
| 148 | rig.built.As(who).ServeHTTP(recorder, request) |
| 149 | return recorder |
| 150 | } |
| 151 | |
| 152 | // Docs' rule: only a fetch (Sec-Fetch-Dest: empty) gets the fragment; a |
| 153 | // document request, or one with no Sec-Fetch-* at all, the full page. |
| 154 | func TestThePanelFragmentFollowsSecFetchDest(t *testing.T) { |
| 155 | rig := newRig(t) |
| 156 | acme := rig.createPage(t, Ana, "Acme", "Body.") |
| 157 | revision := revisionOf(t, rig, acme) |
| 158 | for _, path := range []string{acme + "/history?panel=1", |
| 159 | acme + "/history/" + revision + "?panel=1"} { |
| 160 | for _, check := range []struct { |
| 161 | headers map[string]string |
| 162 | fragment bool |
| 163 | }{ |
| 164 | {map[string]string{"Sec-Fetch-Dest": "empty", |
| 165 | "Sec-Fetch-Mode": "cors"}, true}, |
| 166 | {map[string]string{"Sec-Fetch-Dest": "document", |
| 167 | "Sec-Fetch-Mode": "navigate"}, false}, |
| 168 | {nil, false}, |
| 169 | } { |
| 170 | got := rig.fetchAs(Ana, path, check.headers) |
| 171 | body := got.Body.String() |
| 172 | isFragment := strings.HasPrefix(body, "<aside mapa-history") |
| 173 | full := strings.HasPrefix(body, "<!doctype html>") && |
| 174 | strings.Contains(body, "<aside mapa-history") |
| 175 | if got.Code != http.StatusOK || isFragment != check.fragment || |
| 176 | (!check.fragment && !full) { |
| 177 | t.Errorf("%s with %v: %d, fragment %v", path, |
| 178 | check.headers, got.Code, isFragment) |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Gate 1 for history: a private page's history and its versions are a |
| 185 | // 404 to anyone else, and a team page's history reads the same whether |
| 186 | // or not Ana's private notes link to it (L6, "history summaries"). |
| 187 | func TestHistoryKeepsScope(t *testing.T) { |
| 188 | rig := newRig(t) |
| 189 | mine := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}). |
| 190 | Header().Get("Location") |
| 191 | revision := revisionAs(t, rig, Ana, mine) |
| 192 | for _, path := range []string{mine + "/history", |
| 193 | mine + "/history?panel=1", mine + "/history/" + revision} { |
| 194 | got := rig.fetchAs(Bo, path, map[string]string{ |
| 195 | "Sec-Fetch-Dest": "empty"}) |
| 196 | if got.Code != http.StatusNotFound { |
| 197 | t.Errorf("Bo's GET %s: %d", path, got.Code) |
| 198 | } |
| 199 | } |
| 200 | quiet, acme := gate1World(t, false) |
| 201 | busy, _ := gate1World(t, true) |
| 202 | revisions := regexp.MustCompile(`/history/[0-9]+`) |
| 203 | for _, path := range []string{acme + "/history", |
| 204 | acme + "/history?panel=1"} { |
| 205 | without := steadyRevisions(revisions.ReplaceAllString( |
| 206 | quiet.page(t, Bo, path), "")) |
| 207 | with := steadyRevisions(revisions.ReplaceAllString( |
| 208 | busy.page(t, Bo, path), "")) |
| 209 | if without != with { |
| 210 | t.Errorf("Bo's %s differs:\n%s", path, |
| 211 | firstDifference(without, with)) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // L7: every new GET leaves every table as it was. A dump of the rows is |
| 217 | // compared, not the file, which the WAL and checkpoints move. |
| 218 | func TestHistoryGetsWriteNothing(t *testing.T) { |
| 219 | rig := newRig(t) |
| 220 | acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") |
| 221 | first := revisionOf(t, rig, acme) |
| 222 | rig.post(t, Ana, acme, url.Values{"revision": {first}, |
| 223 | "body": {"One.\n\nTwo, again."}}) |
| 224 | before := dumpAll(t, rig) |
| 225 | for _, who := range []string{Bo, Ana} { |
| 226 | for _, path := range []string{acme + "/history", |
| 227 | acme + "/history?panel=1", acme + "/history?older=30", |
| 228 | acme + "/history/" + first, acme + "/history/" + first + |
| 229 | "?panel=1"} { |
| 230 | for _, dest := range []string{"empty", "document", ""} { |
| 231 | got := rig.fetchAs(who, path, map[string]string{ |
| 232 | "Sec-Fetch-Dest": dest}) |
| 233 | if got.Code != http.StatusOK { |
| 234 | t.Fatalf("%s GET %s: %d", who, path, got.Code) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | if dumpAll(t, rig) != before { |
| 240 | t.Fatal("a history GET wrote to the database") |
| 241 | } |
| 242 | rig.post(t, Ana, acme+"/history/"+first, nil) |
| 243 | if dumpAll(t, rig) == before { |
| 244 | t.Fatal("the dump is vacuous: a restore did not change it") |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // dumpAll hashes every row of every table. |
| 249 | func dumpAll(t *testing.T, rig *rig) string { |
| 250 | t.Helper() |
| 251 | var tables []string |
| 252 | if err := rig.db.G.Raw("SELECT name FROM sqlite_master WHERE " + |
| 253 | "type = 'table' ORDER BY name").Scan(&tables).Error; err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | hash := sha256.New() |
| 257 | for _, table := range tables { |
| 258 | var rows []map[string]any |
| 259 | err := rig.db.G.Raw(`SELECT * FROM "` + table + `" ORDER BY rowid`). |
| 260 | Scan(&rows).Error |
| 261 | if err != nil { |
| 262 | t.Fatal(err) |
| 263 | } |
| 264 | fmt.Fprintf(hash, "%s %v\n", table, rows) |
| 265 | } |
| 266 | return fmt.Sprintf("%x", hash.Sum(nil)) |
| 267 | } |
| 268 | |