| new file mode 100644 |
| index 0000000..fd1f112 |
| --- /dev/null |
| +++ b/internal/mapatest/saving_test.go |
| @@ -0,0 +1,396 @@ |
| +package mapatest |
| + |
| +import ( |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "net/url" |
| + "strconv" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Gate L9, drafts, rebase and commits (level-up §6.5, §7): the server's |
| +// half. The editor's half (the caret, the table, the three tries) is |
| +// editor/'s, in Node and the browser. |
| + |
| +// postJSON is a POST whose answer the script reads. |
| +func (rig *rig) postJSON(who, target string, |
| + form url.Values) (int, map[string]any) { |
| + request := httptest.NewRequest(http.MethodPost, target, |
| + strings.NewReader(form.Encode())) |
| + request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + request.Header.Set("Origin", testOrigin) |
| + request.Header.Set("Accept", "application/json") |
| + recorder := httptest.NewRecorder() |
| + rig.built.As(who).ServeHTTP(recorder, request) |
| + reply := map[string]any{} |
| + _ = json.Unmarshal(recorder.Body.Bytes(), &reply) |
| + return recorder.Code, reply |
| +} |
| + |
| +// editorData is the island a page carries (level-up §6.8). |
| +type editorData struct { |
| + Source string `json:"source"` |
| + Base string `json:"base"` |
| + Draft *struct { |
| + Version string `json:"version"` |
| + Base string `json:"base"` |
| + } `json:"draft"` |
| + Links map[string]struct { |
| + Kind, Href, Title string |
| + } `json:"links"` |
| +} |
| + |
| +// islandOf reads the island out of a page, or fails. |
| +func islandOf(t *testing.T, page string) editorData { |
| + t.Helper() |
| + raw := between(page, `<script type="application/json" id="mapa-editor">`, |
| + "</script>") |
| + var data editorData |
| + if err := json.Unmarshal([]byte(raw), &data); err != nil { |
| + t.Fatalf("the island does not parse: %v\n%s", err, raw) |
| + } |
| + return data |
| +} |
| + |
| +// committed is a page's committed text, as Bo's island holds it (Bo |
| +// has no draft in these tests). |
| +func committed(t *testing.T, rig *rig, path string) string { |
| + t.Helper() |
| + return islandOf(t, rig.page(t, Bo, path)).Source |
| +} |
| + |
| +// draftRow is the person's one draft's base and version, as stored. |
| +func (rig *rig) draftRow(t *testing.T) (base, version int64) { |
| + t.Helper() |
| + row := rig.db.G.Raw("SELECT base_revision, version FROM drafts").Row() |
| + if err := row.Scan(&base, &version); err != nil { |
| + t.Fatal(err) |
| + } |
| + return base, version |
| +} |
| + |
| +const fourBlocks = "One.\n\nTwo.\n\nThree.\n\nFour." |
| + |
| +func mergeOf(t *testing.T, reply map[string]any) map[string]any { |
| + t.Helper() |
| + merge, ok := reply["merge"].(map[string]any) |
| + if !ok { |
| + t.Fatalf("no merge in %v", reply) |
| + } |
| + return merge |
| +} |
| + |
| +func TestL9ACleanRebaseMovesTheBaseAndTheCommitIsSaved(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| + first := revisionOf(t, rig, acme) |
| + _, reply := rig.autosave(t, Ana, acme, first, "0", |
| + "One.\n\nTwo, Ana.\n\nThree.\n\nFour.") |
| + if reply["revision"] != first || reply["merge"] != nil { |
| + t.Fatalf("an autosave over the current revision: %v", reply) |
| + } |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"One.\n\nTwo.\n\nThree, Bo.\n\n\n\nFour."}}) |
| + second := revisionOf(t, rig, acme) |
| + _, reply = rig.autosave(t, Ana, acme, first, |
| + reply["version"].(string), "One.\n\nTwo, Ana!\n\nThree.\n\nFour.") |
| + merge := mergeOf(t, reply) |
| + want := "One.\n\nTwo, Ana!\n\nThree, Bo.\n\n\n\nFour." |
| + if reply["revision"] != second || merge["against"] != second || |
| + merge["body"] != want { |
| + t.Fatalf("the reply's merge: %v", reply) |
| + } |
| + code, rebased := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| + "rebase": {second}, "body": {want}, |
| + "version": {reply["version"].(string)}}) |
| + if code != http.StatusOK || rebased["merge"] != nil { |
| + t.Fatalf("the rebase: %d %v", code, rebased) |
| + } |
| + if base, _ := rig.draftRow(t); idText(base) != second { |
| + t.Fatal("the rebase did not move the draft's base") |
| + } |
| + saved := rig.post(t, Ana, acme+"/draft/save", |
| + url.Values{"version": {rebased["version"].(string)}}) |
| + if saved.Header().Get("Location") != acme { |
| + t.Fatalf("the commit after a rebase went to %q, not Saved", |
| + saved.Header().Get("Location")) |
| + } |
| + if got := committed(t, rig, acme); got != want { |
| + t.Fatalf("the commit did not keep M's bytes: %q", got) |
| + } |
| +} |
| + |
| +func TestL9ARebaseIsRefusedAndMovesNothing(t *testing.T) { |
| + rig := newRig(t) |
| + moving := rig.clocked(t) |
| + acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| + other := rig.createPage(t, Ana, "Other", "Elsewhere.") |
| + old := revisionOf(t, rig, acme) |
| + if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| + "rebase": {old}, "body": {"x"}}); code != |
| + http.StatusUnprocessableEntity { |
| + t.Fatalf("a rebase with no draft: %d", code) |
| + } |
| + moving.now = moving.now.Add(91 * 24 * time.Hour) |
| + rig.post(t, Bo, acme, url.Values{"revision": {old}, |
| + "body": {"One, Bo."}}) |
| + current := revisionOf(t, rig, acme) |
| + _, reply := rig.autosave(t, Ana, acme, current, "0", "Mine.") |
| + version := reply["version"].(string) |
| + for name, form := range map[string]url.Values{ |
| + "a stale version": {"rebase": {current}, "version": {"1"}}, |
| + "another page's revision": {"rebase": {revisionOf(t, rig, other)}, |
| + "version": {version}}, |
| + "a revision past the window": {"rebase": {old}, |
| + "version": {version}}, |
| + } { |
| + form.Set("body", "Moved?") |
| + code, _ := rig.postJSON(Ana, acme+"/draft", form) |
| + base, stored := rig.draftRow(t) |
| + if code < 400 || idText(base) != current || |
| + idText(stored) != version { |
| + t.Errorf("%s: %d, and the draft moved", name, code) |
| + } |
| + } |
| + if !strings.Contains(rig.page(t, Ana, acme), "Mine.") { |
| + t.Fatal("a refused rebase wrote the draft's text") |
| + } |
| +} |
| + |
| +func TestL9AConflictCarriesItsHunksAndResolveAnswersJSON(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", fourBlocks) |
| + first := revisionOf(t, rig, acme) |
| + _, reply := rig.autosave(t, Ana, acme, first, "0", |
| + "One.\n\nTwo, Ana.\n\nThree.\n\nFour.") |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"One.\n\nTwo, Bo.\n\nThree.\n\nFour, Bo."}}) |
| + second := revisionOf(t, rig, acme) |
| + _, reply = rig.autosave(t, Ana, acme, first, |
| + reply["version"].(string), "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree."+ |
| + "\n\nFour.") |
| + merge := mergeOf(t, reply) |
| + hunks, _ := merge["hunks"].([]any) |
| + if merge["conflict"] != true || merge["against"] != second || |
| + len(hunks) != 1 { |
| + t.Fatalf("the conflict reply: %v", merge) |
| + } |
| + hunk := hunks[0].(map[string]any) |
| + if hunk["at"] != 2.0 || hunk["count"] != 1.0 || |
| + hunk["base"] != "Two." || hunk["mine"] != "Two, Ana." || |
| + hunk["theirs"] != "Two, Bo." { |
| + t.Fatalf("the hunk: %v", hunk) |
| + } |
| + events := rig.count(t, "events") |
| + code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{ |
| + "current": {second}, "version": {reply["version"].(string)}, |
| + "choice-0": {"mine"}}) |
| + want := "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree.\n\nFour, Bo." |
| + if code != http.StatusOK || resolved["outcome"] != "saved" || |
| + resolved["body"] != want || resolved["revision"] == second { |
| + t.Fatalf("Resolve's JSON: %d %v", code, resolved) |
| + } |
| + if rig.count(t, "events") != events+1 || rig.count(t, "drafts") != 0 || |
| + committed(t, rig, acme) != want { |
| + t.Fatal("Resolve did not commit the choice exactly once") |
| + } |
| +} |
| + |
| +func TestL9AResolveFromAStaleVersionCommitsNothing(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", "One.") |
| + first := revisionOf(t, rig, acme) |
| + _, reply := rig.autosave(t, Ana, acme, first, "0", "One, Ana.") |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"One, Bo."}}) |
| + second := revisionOf(t, rig, acme) |
| + rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| + "One, Ana, again.") |
| + code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{ |
| + "current": {second}, "version": {reply["version"].(string)}, |
| + "choice-0": {"mine"}}) |
| + if code != http.StatusOK || resolved["outcome"] != "conflicted" || |
| + resolved["revision"] != second || resolved["body"] != "One, Bo." { |
| + t.Fatalf("a stale Resolve: %d %v", code, resolved) |
| + } |
| + if revisionOf(t, rig, acme) != second { |
| + t.Fatal("a stale Resolve committed") |
| + } |
| +} |
| + |
| +func TestL9CommitOneNeedsTheCurrentVersion(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", "One.") |
| + first := revisionOf(t, rig, acme) |
| + _, tabA := rig.autosave(t, Ana, acme, first, "0", "One, tab A.") |
| + offered := tabA["version"].(string) |
| + // Another tab's draft is offered to this one; it never typed. |
| + _, tabB := rig.autosave(t, Ana, acme, first, offered, "One, tab B.") |
| + events := rig.count(t, "events") |
| + code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{"commit": {"1"}, |
| + "version": {offered}, "body": {"One, closing tab A."}}) |
| + if code != http.StatusConflict || rig.count(t, "events") != events || |
| + revisionOf(t, rig, acme) != first { |
| + t.Fatalf("commit=1 with a stale version: %d", code) |
| + } |
| + if _, stored := rig.draftRow(t); idText(stored) != tabB["version"] { |
| + t.Fatal("commit=1 with a stale version wrote the draft") |
| + } |
| + code, saved := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| + "commit": {"1"}, "version": {tabB["version"].(string)}, |
| + "body": {"One, closing tab B."}}) |
| + if code != http.StatusOK || saved["saved"] != true || |
| + committed(t, rig, acme) != "One, closing tab B." || |
| + rig.count(t, "drafts") != 0 { |
| + t.Fatalf("commit=1 from the current version: %d %v", code, saved) |
| + } |
| +} |
| + |
| +// Past the keepalive cap the closing tab sends no text: the draft as |
| +// last autosaved is committed. |
| +func TestL9CommitOneWithoutABodyCommitsTheStoredDraft(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", "One.") |
| + _, reply := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", |
| + "One, autosaved.") |
| + if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| + "commit": {"1"}}); code != http.StatusConflict { |
| + t.Fatalf("commit=1 with no version and no text: %d", code) |
| + } |
| + code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{ |
| + "commit": {"1"}, "version": {reply["version"].(string)}}) |
| + if code != http.StatusOK || |
| + committed(t, rig, acme) != "One, autosaved." { |
| + t.Fatalf("commit=1 with no text: %d", code) |
| + } |
| +} |
| + |
| +func TestL9ASaveThatChangesNothingWritesNothing(t *testing.T) { |
| + rig := newRig(t) |
| + acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") |
| + first := revisionOf(t, rig, acme) |
| + day := "/d/2026-11-20" |
| + rig.post(t, Ana, day, url.Values{"body": {"Written."}}) |
| + revisions, events := rig.count(t, "revisions"), rig.count(t, "events") |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"One.\n\nTwo.\n\n"}}) |
| + rig.post(t, Ana, day, url.Values{"body": {"Written."}, |
| + "revision": {revisionAs(t, rig, Ana, day)}}) |
| + if rig.count(t, "revisions") != revisions || |
| + rig.count(t, "events") != events || revisionOf(t, rig, acme) != first { |
| + t.Fatal("a save of the same text wrote a revision") |
| + } |
| + // Merged, to exactly the current body: Ana's draft changed nothing |
| + // that Bo's revision had not changed too. |
| + _, reply := rig.autosave(t, Ana, acme, first, "0", "One.\n\nTwo, both.") |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"One.\n\nTwo, both."}}) |
| + revisions, events = rig.count(t, "revisions"), rig.count(t, "events") |
| + saved := rig.post(t, Ana, acme+"/draft/save", |
| + url.Values{"version": {reply["version"].(string)}}) |
| + if saved.Code != http.StatusSeeOther || |
| + rig.count(t, "revisions") != revisions || |
| + rig.count(t, "events") != events || rig.count(t, "drafts") != 0 { |
| + t.Fatal("a merge equal to the current body wrote, or kept the draft") |
| + } |
| +} |
| + |
| +func TestL9MakeTeamPageRefusesAStaleRevision(t *testing.T) { |
| + rig := newRig(t) |
| + day := "/d/2026-11-12" |
| + rig.post(t, Ana, day, url.Values{"body": {"Mine.\n\nShare this."}}) |
| + note := between(rig.page(t, Ana, day), `href="/p/`, `/share"`) |
| + form := rig.page(t, Ana, "/p/"+note+"/share") |
| + rig.post(t, Ana, day, url.Values{"body": {"New first.\n\nMine." + |
| + "\n\nShare this."}, "revision": {revisionAs(t, rig, Ana, day)}}) |
| + pages, events := rig.count(t, "pages"), rig.count(t, "events") |
| + stale := rig.post(t, Ana, "/p/"+note+"/share", url.Values{ |
| + "title": {"Shared"}, "block": {"1"}, |
| + "revision": {formRevision(form)}}) |
| + if stale.Code != http.StatusUnprocessableEntity || |
| + !strings.Contains(stale.Body.String(), "Choose them again") { |
| + t.Fatalf("a stale Make team page: %d", stale.Code) |
| + } |
| + if rig.count(t, "pages") != pages || rig.count(t, "events") != events { |
| + t.Fatal("a stale Make team page wrote something") |
| + } |
| +} |
| + |
| +func TestL9TheFirstScratchCommitIsSingleFlight(t *testing.T) { |
| + rig := newRig(t) |
| + token := "tab-token-0123456789" |
| + if code, reply := rig.postJSON(Ana, "/scratch", url.Values{ |
| + "first": {"tab-empty-0123456789"}, "body": {" \n"}}); code != |
| + http.StatusUnprocessableEntity || reply["empty"] != true || |
| + rig.count(t, "pages") != 0 { |
| + t.Fatalf("an empty first autosave: %d %v", code, reply) |
| + } |
| + var group sync.WaitGroup |
| + replies := make([]map[string]any, 2) |
| + for at := range replies { |
| + group.Add(1) |
| + go func() { |
| + defer group.Done() |
| + _, replies[at] = rig.postJSON(Ana, "/scratch", url.Values{ |
| + "first": {token}, "body": {"First words."}}) |
| + }() |
| + } |
| + group.Wait() |
| + if rig.count(t, "pages") != 1 || replies[0]["page"] == nil || |
| + replies[0]["page"] != replies[1]["page"] { |
| + t.Fatalf("two quick autosaves: %d pages, %v", rig.count(t, "pages"), |
| + replies) |
| + } |
| + code, later := rig.postJSON(Ana, "/scratch", url.Values{ |
| + "first": {token}, "body": {"First words, and more."}, |
| + "version": {eitherVersion(replies[0]["version"], |
| + replies[1]["version"])}}) |
| + if code != http.StatusOK || later["version"] == nil || |
| + rig.count(t, "pages") != 1 || rig.count(t, "drafts") != 1 { |
| + t.Fatalf("a later autosave is not a draft on the page: %d %v", code, |
| + later) |
| + } |
| +} |
| + |
| +// eitherVersion is whichever reply carried a version: the one that became a |
| +// draft on the page the other made. |
| +func eitherVersion(values ...any) string { |
| + for _, value := range values { |
| + if text, ok := value.(string); ok { |
| + return text |
| + } |
| + } |
| + return "0" |
| +} |
| + |
| +func TestL9NoBaseAndGoneReplies(t *testing.T) { |
| + rig := newRig(t) |
| + moving := rig.clocked(t) |
| + acme := rig.createPage(t, Ana, "Acme", "One.") |
| + first := revisionOf(t, rig, acme) |
| + moving.now = moving.now.Add(91 * 24 * time.Hour) |
| + _, reply := rig.autosave(t, Ana, acme, first, "0", "Mine.") |
| + rig.post(t, Bo, acme, url.Values{"revision": {first}, |
| + "body": {"Bo's."}}) |
| + _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| + "Mine, still.") |
| + if mergeOf(t, reply)["noBase"] != true { |
| + t.Fatalf("an aged base: %v", reply) |
| + } |
| + rig.post(t, Bo, acme+"/delete", nil) |
| + _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string), |
| + "Mine, after.") |
| + if mergeOf(t, reply)["gone"] != true || reply["revision"] != nil { |
| + t.Fatalf("a deleted page: %v", reply) |
| + } |
| + _, day := rig.autosave(t, Ana, "/d/2026-11-30", "0", "0", "A day.") |
| + if day["revision"] != nil || day["merge"] != nil || day["version"] == "" { |
| + t.Fatalf("an unwritten day's autosave: %v", day) |
| + } |
| +} |
| + |
| +func idText(value int64) string { return strconv.FormatInt(value, 10) } |