| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // Gate L9, drafts, rebase and commits (level-up §6.5, §7): the server's |
| 16 | // half. The editor's half (the caret, the table, the three tries) is |
| 17 | // editor/'s, in Node and the browser. |
| 18 | |
| 19 | // postJSON is a POST whose answer the script reads. |
| 20 | func (rig *rig) postJSON(who, target string, |
| 21 | form url.Values) (int, map[string]any) { |
| 22 | form = counted(target, form) |
| 23 | request := httptest.NewRequest(http.MethodPost, target, |
| 24 | strings.NewReader(form.Encode())) |
| 25 | request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 26 | request.Header.Set("Origin", testOrigin) |
| 27 | request.Header.Set("Accept", "application/json") |
| 28 | recorder := httptest.NewRecorder() |
| 29 | rig.built.As(who).ServeHTTP(recorder, request) |
| 30 | reply := map[string]any{} |
| 31 | _ = json.Unmarshal(recorder.Body.Bytes(), &reply) |
| 32 | return recorder.Code, reply |
| 33 | } |
| 34 | |
| 35 | // editorData is the island a page carries (level-up §6.8). |
| 36 | type editorData struct { |
| 37 | Source string `json:"source"` |
| 38 | Base string `json:"base"` |
| 39 | Draft *struct { |
| 40 | Version string `json:"version"` |
| 41 | Base string `json:"base"` |
| 42 | } `json:"draft"` |
| 43 | Links map[string]struct { |
| 44 | Kind, Href, Title string |
| 45 | } `json:"links"` |
| 46 | } |
| 47 | |
| 48 | // islandOf reads the island out of a page, or fails. |
| 49 | func islandOf(t *testing.T, page string) editorData { |
| 50 | t.Helper() |
| 51 | raw := between(page, `<script type="application/json" id="mapa-editor">`, |
| 52 | "</script>") |
| 53 | var data editorData |
| 54 | if err := json.Unmarshal([]byte(raw), &data); err != nil { |
| 55 | t.Fatalf("the island does not parse: %v\n%s", err, raw) |
| 56 | } |
| 57 | return data |
| 58 | } |
| 59 | |
| 60 | // committed is a page's committed text, as Bo's island holds it (Bo |
| 61 | // has no draft in these tests). |
| 62 | func committed(t *testing.T, rig *rig, path string) string { |
| 63 | t.Helper() |
| 64 | return islandOf(t, rig.page(t, Bo, path)).Source |
| 65 | } |
| 66 | |
| 67 | // draftRow is the person's one draft's base and version, as stored. |
| 68 | func (rig *rig) draftRow(t *testing.T) (base, version int64) { |
| 69 | t.Helper() |
| 70 | row := rig.db.G.Raw("SELECT base_revision, version FROM drafts").Row() |
| 71 | if err := row.Scan(&base, &version); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | return base, version |
| 75 | } |
| 76 | |
| 77 | const fourBlocks = "One.\n\nTwo.\n\nThree.\n\nFour." |
| 78 | |
| 79 | func mergeOf(t *testing.T, reply map[string]any) map[string]any { |
| 80 | t.Helper() |
| 81 | merge, ok := reply["merge"].(map[string]any) |
| 82 | if !ok { |
| 83 | t.Fatalf("no merge in %v", reply) |
| 84 | } |
| 85 | return merge |
| 86 | } |
| 87 | |
| 88 | func TestL9ACleanRebaseMovesTheBaseAndTheCommitIsSaved(t *testing.T) { |
| 89 | rig := newRig(t) |
| 90 | acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| 91 | first := revisionOf(t, rig, acme) |
| 92 | _, reply := rig.autosave(t, Ana, acme, first, "0", |
| 93 | "One.\n\nTwo, Ana.\n\nThree.\n\nFour.") |
| 94 | if reply["revision"] != first || reply["merge"] != nil { |
| 95 | t.Fatalf("an autosave over the current revision: %v", reply) |
| 96 | } |
| 97 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 98 | "body": {"One.\n\nTwo.\n\nThree, Bo.\n\n\n\nFour."}}) |
| 99 | second := revisionOf(t, rig, acme) |
| 100 | _, reply = rig.autosave(t, Ana, acme, first, |
| 101 | reply["version"].(string), "One.\n\nTwo, Ana!\n\nThree.\n\nFour.") |
| 102 | merge := mergeOf(t, reply) |
| 103 | want := "One.\n\nTwo, Ana!\n\nThree, Bo.\n\n\n\nFour." |
| 104 | if reply["revision"] != second || merge["against"] != second || |
| 105 | merge["body"] != want { |
| 106 | t.Fatalf("the reply's merge: %v", reply) |
| 107 | } |
| 108 | code, rebased := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| 109 | "rebase": {second}, "body": {want}, |
| 110 | "version": {reply["version"].(string)}}) |
| 111 | if code != http.StatusOK || rebased["merge"] != nil { |
| 112 | t.Fatalf("the rebase: %d %v", code, rebased) |
| 113 | } |
| 114 | if base, _ := rig.draftRow(t); idText(base) != second { |
| 115 | t.Fatal("the rebase did not move the draft's base") |
| 116 | } |
| 117 | saved := rig.post(t, Ana, acme+"/draft/save", |
| 118 | url.Values{"version": {rebased["version"].(string)}}) |
| 119 | if saved.Header().Get("Location") != acme { |
| 120 | t.Fatalf("the commit after a rebase went to %q, not Saved", |
| 121 | saved.Header().Get("Location")) |
| 122 | } |
| 123 | if got := committed(t, rig, acme); got != want { |
| 124 | t.Fatalf("the commit did not keep M's bytes: %q", got) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestL9ARebaseIsRefusedAndMovesNothing(t *testing.T) { |
| 129 | rig := newRig(t) |
| 130 | moving := rig.clocked(t) |
| 131 | acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| 132 | other := rig.createPage(t, Ana, "Other", "Elsewhere.") |
| 133 | old := revisionOf(t, rig, acme) |
| 134 | if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| 135 | "rebase": {old}, "body": {"x"}}); code != |
| 136 | http.StatusUnprocessableEntity { |
| 137 | t.Fatalf("a rebase with no draft: %d", code) |
| 138 | } |
| 139 | moving.now = moving.now.Add(91 * 24 * time.Hour) |
| 140 | rig.post(t, Bo, acme, url.Values{"revision": {old}, |
| 141 | "body": {"One, Bo."}}) |
| 142 | current := revisionOf(t, rig, acme) |
| 143 | _, reply := rig.autosave(t, Ana, acme, current, "0", "Mine.") |
| 144 | version := reply["version"].(string) |
| 145 | for name, form := range map[string]url.Values{ |
| 146 | "a stale version": {"rebase": {current}, "version": {"1"}}, |
| 147 | "another page's revision": {"rebase": {revisionOf(t, rig, other)}, |
| 148 | "version": {version}}, |
| 149 | "a revision past the window": {"rebase": {old}, |
| 150 | "version": {version}}, |
| 151 | } { |
| 152 | form.Set("body", "Moved?") |
| 153 | code, _ := rig.postJSON(Ana, acme+"/draft", form) |
| 154 | base, stored := rig.draftRow(t) |
| 155 | if code < 400 || idText(base) != current || |
| 156 | idText(stored) != version { |
| 157 | t.Errorf("%s: %d, and the draft moved", name, code) |
| 158 | } |
| 159 | } |
| 160 | if !strings.Contains(rig.page(t, Ana, acme), "Mine.") { |
| 161 | t.Fatal("a refused rebase wrote the draft's text") |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestL9AConflictCarriesItsHunksAndResolveAnswersJSON(t *testing.T) { |
| 166 | rig := newRig(t) |
| 167 | acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| 168 | first := revisionOf(t, rig, acme) |
| 169 | _, reply := rig.autosave(t, Ana, acme, first, "0", |
| 170 | "One.\n\nTwo, Ana.\n\nThree.\n\nFour.") |
| 171 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 172 | "body": {"One.\n\nTwo, Bo.\n\nThree.\n\nFour, Bo."}}) |
| 173 | second := revisionOf(t, rig, acme) |
| 174 | _, reply = rig.autosave(t, Ana, acme, first, |
| 175 | reply["version"].(string), "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree."+ |
| 176 | "\n\nFour.") |
| 177 | merge := mergeOf(t, reply) |
| 178 | hunks, _ := merge["hunks"].([]any) |
| 179 | if merge["conflict"] != true || merge["against"] != second || |
| 180 | len(hunks) != 1 { |
| 181 | t.Fatalf("the conflict reply: %v", merge) |
| 182 | } |
| 183 | hunk := hunks[0].(map[string]any) |
| 184 | if hunk["at"] != 2.0 || hunk["count"] != 1.0 || |
| 185 | hunk["base"] != "Two." || hunk["mine"] != "Two, Ana." || |
| 186 | hunk["theirs"] != "Two, Bo." { |
| 187 | t.Fatalf("the hunk: %v", hunk) |
| 188 | } |
| 189 | events := rig.count(t, "events") |
| 190 | code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{ |
| 191 | "current": {second}, "version": {reply["version"].(string)}, |
| 192 | "choice-0": {"mine"}}) |
| 193 | want := "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree.\n\nFour, Bo." |
| 194 | if code != http.StatusOK || resolved["outcome"] != "saved" || |
| 195 | resolved["body"] != want || resolved["revision"] == second { |
| 196 | t.Fatalf("Resolve's JSON: %d %v", code, resolved) |
| 197 | } |
| 198 | if rig.count(t, "events") != events+1 || rig.count(t, "drafts") != 0 || |
| 199 | committed(t, rig, acme) != want { |
| 200 | t.Fatal("Resolve did not commit the choice exactly once") |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func TestL9AResolveFromAStaleVersionCommitsNothing(t *testing.T) { |
| 205 | rig := newRig(t) |
| 206 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 207 | first := revisionOf(t, rig, acme) |
| 208 | _, reply := rig.autosave(t, Ana, acme, first, "0", "One, Ana.") |
| 209 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 210 | "body": {"One, Bo."}}) |
| 211 | second := revisionOf(t, rig, acme) |
| 212 | rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| 213 | "One, Ana, again.") |
| 214 | code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{ |
| 215 | "current": {second}, "version": {reply["version"].(string)}, |
| 216 | "choice-0": {"mine"}}) |
| 217 | if code != http.StatusOK || resolved["outcome"] != "conflicted" || |
| 218 | resolved["revision"] != second || resolved["body"] != "One, Bo." { |
| 219 | t.Fatalf("a stale Resolve: %d %v", code, resolved) |
| 220 | } |
| 221 | if revisionOf(t, rig, acme) != second { |
| 222 | t.Fatal("a stale Resolve committed") |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func TestL9CommitOneNeedsTheCurrentVersion(t *testing.T) { |
| 227 | rig := newRig(t) |
| 228 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 229 | first := revisionOf(t, rig, acme) |
| 230 | _, tabA := rig.autosave(t, Ana, acme, first, "0", "One, tab A.") |
| 231 | offered := tabA["version"].(string) |
| 232 | // Another tab's draft is offered to this one; it never typed. |
| 233 | _, tabB := rig.autosave(t, Ana, acme, first, offered, "One, tab B.") |
| 234 | events := rig.count(t, "events") |
| 235 | code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{"commit": {"1"}, |
| 236 | "version": {offered}, "body": {"One, closing tab A."}}) |
| 237 | if code != http.StatusConflict || rig.count(t, "events") != events || |
| 238 | revisionOf(t, rig, acme) != first { |
| 239 | t.Fatalf("commit=1 with a stale version: %d", code) |
| 240 | } |
| 241 | if _, stored := rig.draftRow(t); idText(stored) != tabB["version"] { |
| 242 | t.Fatal("commit=1 with a stale version wrote the draft") |
| 243 | } |
| 244 | code, saved := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| 245 | "commit": {"1"}, "version": {tabB["version"].(string)}, |
| 246 | "body": {"One, closing tab B."}}) |
| 247 | if code != http.StatusOK || saved["saved"] != true || |
| 248 | committed(t, rig, acme) != "One, closing tab B." || |
| 249 | rig.count(t, "drafts") != 0 { |
| 250 | t.Fatalf("commit=1 from the current version: %d %v", code, saved) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Past the keepalive cap the closing tab sends no text: the draft as |
| 255 | // last autosaved is committed. |
| 256 | func TestL9CommitOneWithoutABodyCommitsTheStoredDraft(t *testing.T) { |
| 257 | rig := newRig(t) |
| 258 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 259 | _, reply := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", |
| 260 | "One, autosaved.") |
| 261 | if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| 262 | "commit": {"1"}}); code != http.StatusConflict { |
| 263 | t.Fatalf("commit=1 with no version and no text: %d", code) |
| 264 | } |
| 265 | code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| 266 | "commit": {"1"}, "version": {reply["version"].(string)}}) |
| 267 | if code != http.StatusOK || |
| 268 | committed(t, rig, acme) != "One, autosaved." { |
| 269 | t.Fatalf("commit=1 with no text: %d", code) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | func TestL9ASaveThatChangesNothingWritesNothing(t *testing.T) { |
| 274 | rig := newRig(t) |
| 275 | acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") |
| 276 | first := revisionOf(t, rig, acme) |
| 277 | day := "/d/2026-11-20" |
| 278 | rig.post(t, Ana, day, url.Values{"body": {"Written."}}) |
| 279 | revisions, events := rig.count(t, "revisions"), rig.count(t, "events") |
| 280 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 281 | "body": {"One.\n\nTwo.\n\n"}}) |
| 282 | rig.post(t, Ana, day, url.Values{"body": {"Written."}, |
| 283 | "revision": {revisionAs(t, rig, Ana, day)}}) |
| 284 | if rig.count(t, "revisions") != revisions || |
| 285 | rig.count(t, "events") != events || revisionOf(t, rig, acme) != first { |
| 286 | t.Fatal("a save of the same text wrote a revision") |
| 287 | } |
| 288 | // Merged, to exactly the current body: Ana's draft changed nothing |
| 289 | // that Bo's revision had not changed too. |
| 290 | _, reply := rig.autosave(t, Ana, acme, first, "0", "One.\n\nTwo, both.") |
| 291 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 292 | "body": {"One.\n\nTwo, both."}}) |
| 293 | revisions, events = rig.count(t, "revisions"), rig.count(t, "events") |
| 294 | saved := rig.post(t, Ana, acme+"/draft/save", |
| 295 | url.Values{"version": {reply["version"].(string)}}) |
| 296 | if saved.Code != http.StatusSeeOther || |
| 297 | rig.count(t, "revisions") != revisions || |
| 298 | rig.count(t, "events") != events || rig.count(t, "drafts") != 0 { |
| 299 | t.Fatal("a merge equal to the current body wrote, or kept the draft") |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | func TestL9MakeTeamPageRefusesAStaleRevision(t *testing.T) { |
| 304 | rig := newRig(t) |
| 305 | day := "/d/2026-11-12" |
| 306 | rig.post(t, Ana, day, url.Values{"body": {"Mine.\n\nShare this."}}) |
| 307 | note := between(rig.page(t, Ana, day), `href="/p/`, `/share"`) |
| 308 | form := rig.page(t, Ana, "/p/"+note+"/share") |
| 309 | rig.post(t, Ana, day, url.Values{"body": {"New first.\n\nMine." + |
| 310 | "\n\nShare this."}, "revision": {revisionAs(t, rig, Ana, day)}}) |
| 311 | pages, events := rig.count(t, "pages"), rig.count(t, "events") |
| 312 | stale := rig.post(t, Ana, "/p/"+note+"/share", url.Values{ |
| 313 | "title": {"Shared"}, "block": {"1"}, |
| 314 | "revision": {formRevision(form)}}) |
| 315 | if stale.Code != http.StatusUnprocessableEntity || |
| 316 | !strings.Contains(stale.Body.String(), "Choose them again") { |
| 317 | t.Fatalf("a stale Make team page: %d", stale.Code) |
| 318 | } |
| 319 | if rig.count(t, "pages") != pages || rig.count(t, "events") != events { |
| 320 | t.Fatal("a stale Make team page wrote something") |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | func TestL9TheFirstScratchCommitIsSingleFlight(t *testing.T) { |
| 325 | rig := newRig(t) |
| 326 | token := "tab-token-0123456789" |
| 327 | if code, reply := rig.postJSON(Ana, "/scratch", url.Values{ |
| 328 | "first": {"tab-empty-0123456789"}, "body": {" \n"}}); code != |
| 329 | http.StatusUnprocessableEntity || reply["empty"] != true || |
| 330 | rig.count(t, "pages") != 0 { |
| 331 | t.Fatalf("an empty first autosave: %d %v", code, reply) |
| 332 | } |
| 333 | var group sync.WaitGroup |
| 334 | replies := make([]map[string]any, 2) |
| 335 | for at := range replies { |
| 336 | group.Add(1) |
| 337 | go func() { |
| 338 | defer group.Done() |
| 339 | _, replies[at] = rig.postJSON(Ana, "/scratch", url.Values{ |
| 340 | "first": {token}, "body": {"First words."}}) |
| 341 | }() |
| 342 | } |
| 343 | group.Wait() |
| 344 | if rig.count(t, "pages") != 1 || replies[0]["page"] == nil || |
| 345 | replies[0]["page"] != replies[1]["page"] { |
| 346 | t.Fatalf("two quick autosaves: %d pages, %v", rig.count(t, "pages"), |
| 347 | replies) |
| 348 | } |
| 349 | code, later := rig.postJSON(Ana, "/scratch", url.Values{ |
| 350 | "first": {token}, "body": {"First words, and more."}, |
| 351 | "version": {eitherVersion(replies[0]["version"], |
| 352 | replies[1]["version"])}}) |
| 353 | if code != http.StatusOK || later["version"] == nil || |
| 354 | rig.count(t, "pages") != 1 || rig.count(t, "drafts") != 1 { |
| 355 | t.Fatalf("a later autosave is not a draft on the page: %d %v", code, |
| 356 | later) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // eitherVersion is whichever reply carried a version: the one that became a |
| 361 | // draft on the page the other made. |
| 362 | func eitherVersion(values ...any) string { |
| 363 | for _, value := range values { |
| 364 | if text, ok := value.(string); ok { |
| 365 | return text |
| 366 | } |
| 367 | } |
| 368 | return "0" |
| 369 | } |
| 370 | |
| 371 | func TestL9NoBaseAndGoneReplies(t *testing.T) { |
| 372 | rig := newRig(t) |
| 373 | moving := rig.clocked(t) |
| 374 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 375 | first := revisionOf(t, rig, acme) |
| 376 | moving.now = moving.now.Add(91 * 24 * time.Hour) |
| 377 | _, reply := rig.autosave(t, Ana, acme, first, "0", "Mine.") |
| 378 | rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| 379 | "body": {"Bo's."}}) |
| 380 | _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| 381 | "Mine, still.") |
| 382 | if mergeOf(t, reply)["noBase"] != true { |
| 383 | t.Fatalf("an aged base: %v", reply) |
| 384 | } |
| 385 | rig.post(t, Bo, acme+"/delete", nil) |
| 386 | _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| 387 | "Mine, after.") |
| 388 | if mergeOf(t, reply)["gone"] != true || reply["revision"] != nil { |
| 389 | t.Fatalf("a deleted page: %v", reply) |
| 390 | } |
| 391 | _, day := rig.autosave(t, Ana, "/d/2026-11-30", "0", "0", "A day.") |
| 392 | if day["revision"] != nil || day["merge"] != nil || day["version"] == "" { |
| 393 | t.Fatalf("an unwritten day's autosave: %v", day) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | func idText(value int64) string { return strconv.FormatInt(value, 10) } |
| 398 | |