package mapatest import ( "net/http" "net/url" "testing" ) // Whose drafts a rename carries (level-up build note 78, Fable's M1 on // ux/fixes-1): only a draft based on the revision the rename was made // over. A rename's revision holds exactly that revision's body, so a // draft on it is a draft on the new one; a draft on anything older is // not, and carrying it would make its commit a plain save over the // teammate's edits it never saw. // renameAs renames page as who, and fails unless it landed. func (rig *rig) renameAs(t *testing.T, who, page, title string) { t.Helper() code, reply := rig.postJSON(who, page+"/rename", url.Values{"title": {title}}) if code != http.StatusOK || reply["title"] != title { t.Fatalf("renaming to %q answered %d %v", title, code, reply) } } // commitAs commits who's draft at version and answers the reply. func (rig *rig) commitAs(t *testing.T, who, page, version string) map[string]any { t.Helper() code, reply := rig.postJSON(who, page+"/draft/save", url.Values{"version": {version}}) if code != http.StatusOK || reply["saved"] != true { t.Fatalf("%s's commit answered %d %v", who, code, reply) } return reply } // Bo drafts on R0; Ana edits (R1), then renames (R2). Bo's draft is on // R0, not R1, so it stays there: his commit merges, and both edits // survive. Carried to R2, his commit would be a plain save of his text // over Ana's edit, and say "Saved". func TestAStaleDraftIsNotCarried(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") first := revisionOf(t, rig, acme) _, bo := rig.autosave(t, Bo, acme, first, "0", "One.\n\nTwo, Bo.") rig.post(t, Ana, acme, url.Values{"revision": {first}, "body": {"One, Ana.\n\nTwo."}}) rig.renameAs(t, Ana, acme, "Acme Ltd") if base, _ := rig.draftRow(t); idText(base) != first { t.Fatalf("Bo's draft on %s was moved to %d by a rename over "+ "Ana's edit", first, base) } saved := rig.commitAs(t, Bo, acme, bo["version"].(string)) if saved["merged"] != true { t.Fatalf("Bo's commit over Ana's edit was not a merge: %v", saved) } if got := committed(t, rig, acme); got != "One, Ana.\n\nTwo, Bo." { t.Fatalf("the page holds %q: an edit was lost", got) } } // Bo drafts on the head; Ana renames it. Bo's draft is carried with the // rename, so his next autosave is not behind and his commit is his own, // not a merge with a revision that changed only the title. func TestATeammatesDraftOnTheHeadIsCarried(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") first := revisionOf(t, rig, acme) _, bo := rig.autosave(t, Bo, acme, first, "0", "One.\n\nTwo, Bo.") rig.renameAs(t, Ana, acme, "Acme Ltd") // Ana's editor, not Bo's: his starts from his draft's base. renamed := revisionAs(t, rig, Ana, acme) if base, _ := rig.draftRow(t); idText(base) != renamed { t.Fatalf("Bo's draft stayed on %d, not the rename's %s", base, renamed) } _, again := rig.autosave(t, Bo, acme, first, bo["version"].(string), "One.\n\nTwo, Bo, again.") if again["merge"] != nil || again["revision"] != renamed { t.Fatalf("Bo's autosave after the rename: %v", again) } saved := rig.commitAs(t, Bo, acme, again["version"].(string)) if saved["merged"] != false { t.Fatalf("Bo's commit merged with a rename: %v", saved) } if got := committed(t, rig, acme); got != "One.\n\nTwo, Bo, again." { t.Fatalf("the page holds %q", got) } }