package mapatest import ( "net/http" "net/url" "strings" "testing" ) // The in-page title (level-up §4.1, build note 78): the H1 and the // header rename through POST /p/{id}/rename asking for JSON, which // answers the title, the revisions, and the confirm page's honest // count; a refusal is the form's sentence and writes nothing; Update // links answers JSON too; and a rename carries the person's draft with // it, so their next commit is theirs alone, not a merge. func TestARenameAnswersTheTitleInPlace(t *testing.T) { rig := newRig(t) halden := rig.createPage(t, Ana, "Halden", "A robotics customer.") rig.createPage(t, Bo, "Plan", "Call [[Halden]].") before := revisionOf(t, rig, halden) since := rig.lastEvent(t) code, reply := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {" Halden Robotics "}}) if code != http.StatusOK || reply["title"] != "Halden Robotics" || reply["href"] != halden { t.Fatalf("rename answered %d %v", code, reply) } if got := rig.verbsSince(t, since); got != "renamed" { t.Fatalf("the rename logged %q", got) } if reply["from"] != before || reply["to"] != revisionOf(t, rig, halden) { t.Fatalf("revisions %v, %v: want from %s to the page's now", reply["from"], reply["to"], before) } if reply["links"] != 1.0 || reply["count"] != "1 page" || len(reply["aliases"].([]any)) != 1 { t.Fatalf("the offer: %v", reply) } if offer := rig.page(t, Ana, halden+"/links"); !strings.Contains(offer, "1 page you can edit link here") { t.Fatal("the confirm page's count differs from the answer's") } } // A rename whose reply was lost is sent again (Fable's L3 on // ux/fixes-1): the title is already the page's, so nothing is written, // and the answer is the rename that put it there, from and to, so the // tab's saver follows it as it would have. Once anything else mints a // revision, an unchanged title answers 0 and 0 again. func TestARepeatedRenameAnswersTheRenameThatLanded(t *testing.T) { rig := newRig(t) halden := rig.createPage(t, Ana, "Halden", "A robotics customer.") if code, same := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {"Halden"}}); code != http.StatusOK || same["from"] != "0" || same["to"] != "0" { t.Fatalf("a title never renamed answered %d %v", code, same) } before := revisionOf(t, rig, halden) _, first := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {"Halden Robotics"}}) since := rig.lastEvent(t) code, again := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {" Halden Robotics"}}) if code != http.StatusOK || again["title"] != "Halden Robotics" || again["from"] != before || again["to"] != first["to"] || rig.lastEvent(t) != since { t.Fatalf("the repeat answered %d %v (the rename: from %s, %v), "+ "or wrote", code, again, before, first) } rig.post(t, Bo, halden, url.Values{"revision": {first["to"].(string)}, "body": {"A robotics customer, Bo."}}) if _, later := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {"Halden Robotics"}}); later["from"] != "0" || later["to"] != "0" { t.Fatalf("after an edit, an unchanged title answered %v", later) } } func TestARefusedRenameIsTheSentenceAndWritesNothing(t *testing.T) { rig := newRig(t) halden := rig.createPage(t, Ana, "Halden", "One.") acme := rig.createPage(t, Bo, "Acme", "Two.") since := rig.lastEvent(t) for title, want := range map[string]string{ "acme": "already exists", "2026-11-12": "A date can't be a page title", strings.Repeat("x", 121): "too long", "A [[b]]": "link syntax", } { code, reply := rig.postJSON(Ana, halden+"/rename", url.Values{"title": {title}}) said, _ := reply["error"].(string) if code != http.StatusUnprocessableEntity || !strings.Contains(said, want) { t.Errorf("%q answered %d %v", title, code, reply) } if title == "acme" && reply["taken"] != acme { t.Errorf("a taken title names %v, not %s", reply["taken"], acme) } } if rig.lastEvent(t) != since { t.Fatal("a refused rename wrote") } if code, _ := rig.postJSON(Ana, "/p/nothere/rename", url.Values{"title": {"X"}}); code != http.StatusNotFound { t.Fatalf("a page that is not there answered %d", code) } } func TestUpdateLinksAnswersTheEditor(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "A customer.") plan := rig.createPage(t, Bo, "Plan", "Call [[Acme]].") rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) code, reply := rig.postJSON(Ana, acme+"/links", nil) if code != http.StatusOK || reply["changed"] != 1.0 || reply["notice"] != "Updated links in 1 page." { t.Fatalf("Update links answered %d %v", code, reply) } if !strings.Contains(rig.page(t, Bo, plan+"/edit"), "[[Acme Ltd]]") { t.Fatal("the link was not updated") } } // A rename while a draft is open (§3.2: a rename mints a revision for // that page only, holding the same body): the draft's base moves with // it, so committing the draft is Saved, not merged against a // "teammate's" revision that changed nothing, and its text is intact. func TestARenameCarriesTheDraftWithIt(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.") _, saved := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "One, and mine.") version, _ := saved["version"].(string) rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) code, again := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), version, "One, and mine, still.") if code != http.StatusOK || again["merge"] != nil { t.Fatalf("the autosave after a rename answered %d %v", code, again) } code, committed := rig.postJSON(Ana, acme+"/draft/save", url.Values{"version": {again["version"].(string)}}) if code != http.StatusOK || committed["saved"] != true || committed["merged"] != false { t.Fatalf("the commit after a rename answered %d %v", code, committed) } if body := islandOf(t, rig.page(t, Bo, acme)).Source; body != "One, and mine, still." { t.Fatalf("the page holds %q", body) } } // Only a team page's title is editable in place, and only where the // editor mounts: never a daily note, a scratch page, History, the saved // version beside a draft, or the Markdown editor; and the server never // draws it editable (scriptless, it is a plain heading). func TestOnlyATeamPagesTitleIsRenamableInPlace(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.") rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Day."}, "revision": {"0"}}) scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}). Header().Get("Location") page := rig.page(t, Ana, acme) if !strings.Contains(page, `