| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // Whose drafts a rename carries (level-up build note 78, Fable's M1 on |
| 10 | // ux/fixes-1): only a draft based on the revision the rename was made |
| 11 | // over. A rename's revision holds exactly that revision's body, so a |
| 12 | // draft on it is a draft on the new one; a draft on anything older is |
| 13 | // not, and carrying it would make its commit a plain save over the |
| 14 | // teammate's edits it never saw. |
| 15 | |
| 16 | // renameAs renames page as who, and fails unless it landed. |
| 17 | func (rig *rig) renameAs(t *testing.T, who, page, title string) { |
| 18 | t.Helper() |
| 19 | code, reply := rig.postJSON(who, page+"/rename", |
| 20 | url.Values{"title": {title}}) |
| 21 | if code != http.StatusOK || reply["title"] != title { |
| 22 | t.Fatalf("renaming to %q answered %d %v", title, code, reply) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // commitAs commits who's draft at version and answers the reply. |
| 27 | func (rig *rig) commitAs(t *testing.T, who, page, |
| 28 | version string) map[string]any { |
| 29 | t.Helper() |
| 30 | code, reply := rig.postJSON(who, page+"/draft/save", |
| 31 | url.Values{"version": {version}}) |
| 32 | if code != http.StatusOK || reply["saved"] != true { |
| 33 | t.Fatalf("%s's commit answered %d %v", who, code, reply) |
| 34 | } |
| 35 | return reply |
| 36 | } |
| 37 | |
| 38 | // Bo drafts on R0; Ana edits (R1), then renames (R2). Bo's draft is on |
| 39 | // R0, not R1, so it stays there: his commit merges, and both edits |
| 40 | // survive. Carried to R2, his commit would be a plain save of his text |
| 41 | // over Ana's edit, and say "Saved". |
| 42 | func TestAStaleDraftIsNotCarried(t *testing.T) { |
| 43 | rig := newRig(t) |
| 44 | acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") |
| 45 | first := revisionOf(t, rig, acme) |
| 46 | _, bo := rig.autosave(t, Bo, acme, first, "0", "One.\n\nTwo, Bo.") |
| 47 | rig.post(t, Ana, acme, url.Values{"revision": {first}, |
| 48 | "body": {"One, Ana.\n\nTwo."}}) |
| 49 | rig.renameAs(t, Ana, acme, "Acme Ltd") |
| 50 | if base, _ := rig.draftRow(t); idText(base) != first { |
| 51 | t.Fatalf("Bo's draft on %s was moved to %d by a rename over "+ |
| 52 | "Ana's edit", first, base) |
| 53 | } |
| 54 | saved := rig.commitAs(t, Bo, acme, bo["version"].(string)) |
| 55 | if saved["merged"] != true { |
| 56 | t.Fatalf("Bo's commit over Ana's edit was not a merge: %v", |
| 57 | saved) |
| 58 | } |
| 59 | if got := committed(t, rig, acme); got != "One, Ana.\n\nTwo, Bo." { |
| 60 | t.Fatalf("the page holds %q: an edit was lost", got) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Bo drafts on the head; Ana renames it. Bo's draft is carried with the |
| 65 | // rename, so his next autosave is not behind and his commit is his own, |
| 66 | // not a merge with a revision that changed only the title. |
| 67 | func TestATeammatesDraftOnTheHeadIsCarried(t *testing.T) { |
| 68 | rig := newRig(t) |
| 69 | acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") |
| 70 | first := revisionOf(t, rig, acme) |
| 71 | _, bo := rig.autosave(t, Bo, acme, first, "0", "One.\n\nTwo, Bo.") |
| 72 | rig.renameAs(t, Ana, acme, "Acme Ltd") |
| 73 | // Ana's editor, not Bo's: his starts from his draft's base. |
| 74 | renamed := revisionAs(t, rig, Ana, acme) |
| 75 | if base, _ := rig.draftRow(t); idText(base) != renamed { |
| 76 | t.Fatalf("Bo's draft stayed on %d, not the rename's %s", base, |
| 77 | renamed) |
| 78 | } |
| 79 | _, again := rig.autosave(t, Bo, acme, first, bo["version"].(string), |
| 80 | "One.\n\nTwo, Bo, again.") |
| 81 | if again["merge"] != nil || again["revision"] != renamed { |
| 82 | t.Fatalf("Bo's autosave after the rename: %v", again) |
| 83 | } |
| 84 | saved := rig.commitAs(t, Bo, acme, again["version"].(string)) |
| 85 | if saved["merged"] != false { |
| 86 | t.Fatalf("Bo's commit merged with a rename: %v", saved) |
| 87 | } |
| 88 | if got := committed(t, rig, acme); got != "One.\n\nTwo, Bo, again." { |
| 89 | t.Fatalf("the page holds %q", got) |
| 90 | } |
| 91 | } |
| 92 | |