| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // The in-page title (level-up §4.1, build note 78): the H1 and the |
| 11 | // header rename through POST /p/{id}/rename asking for JSON, which |
| 12 | // answers the title, the revisions, and the confirm page's honest |
| 13 | // count; a refusal is the form's sentence and writes nothing; Update |
| 14 | // links answers JSON too; and a rename carries the person's draft with |
| 15 | // it, so their next commit is theirs alone, not a merge. |
| 16 | |
| 17 | func TestARenameAnswersTheTitleInPlace(t *testing.T) { |
| 18 | rig := newRig(t) |
| 19 | halden := rig.createPage(t, Ana, "Halden", "A robotics customer.") |
| 20 | rig.createPage(t, Bo, "Plan", "Call [[Halden]].") |
| 21 | before := revisionOf(t, rig, halden) |
| 22 | since := rig.lastEvent(t) |
| 23 | code, reply := rig.postJSON(Ana, halden+"/rename", |
| 24 | url.Values{"title": {" Halden Robotics "}}) |
| 25 | if code != http.StatusOK || reply["title"] != "Halden Robotics" || |
| 26 | reply["href"] != halden { |
| 27 | t.Fatalf("rename answered %d %v", code, reply) |
| 28 | } |
| 29 | if got := rig.verbsSince(t, since); got != "renamed" { |
| 30 | t.Fatalf("the rename logged %q", got) |
| 31 | } |
| 32 | if reply["from"] != before || reply["to"] != revisionOf(t, rig, halden) { |
| 33 | t.Fatalf("revisions %v, %v: want from %s to the page's now", |
| 34 | reply["from"], reply["to"], before) |
| 35 | } |
| 36 | if reply["links"] != 1.0 || reply["count"] != "1 page" || |
| 37 | len(reply["aliases"].([]any)) != 1 { |
| 38 | t.Fatalf("the offer: %v", reply) |
| 39 | } |
| 40 | if offer := rig.page(t, Ana, halden+"/links"); !strings.Contains(offer, |
| 41 | "1 page you can edit link here") { |
| 42 | t.Fatal("the confirm page's count differs from the answer's") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // A rename whose reply was lost is sent again (Fable's L3 on |
| 47 | // ux/fixes-1): the title is already the page's, so nothing is written, |
| 48 | // and the answer is the rename that put it there, from and to, so the |
| 49 | // tab's saver follows it as it would have. Once anything else mints a |
| 50 | // revision, an unchanged title answers 0 and 0 again. |
| 51 | func TestARepeatedRenameAnswersTheRenameThatLanded(t *testing.T) { |
| 52 | rig := newRig(t) |
| 53 | halden := rig.createPage(t, Ana, "Halden", "A robotics customer.") |
| 54 | if code, same := rig.postJSON(Ana, halden+"/rename", |
| 55 | url.Values{"title": {"Halden"}}); code != http.StatusOK || |
| 56 | same["from"] != "0" || same["to"] != "0" { |
| 57 | t.Fatalf("a title never renamed answered %d %v", code, same) |
| 58 | } |
| 59 | before := revisionOf(t, rig, halden) |
| 60 | _, first := rig.postJSON(Ana, halden+"/rename", |
| 61 | url.Values{"title": {"Halden Robotics"}}) |
| 62 | since := rig.lastEvent(t) |
| 63 | code, again := rig.postJSON(Ana, halden+"/rename", |
| 64 | url.Values{"title": {" Halden Robotics"}}) |
| 65 | if code != http.StatusOK || again["title"] != "Halden Robotics" || |
| 66 | again["from"] != before || again["to"] != first["to"] || |
| 67 | rig.lastEvent(t) != since { |
| 68 | t.Fatalf("the repeat answered %d %v (the rename: from %s, %v), "+ |
| 69 | "or wrote", code, again, before, first) |
| 70 | } |
| 71 | rig.post(t, Bo, halden, url.Values{"revision": {first["to"].(string)}, |
| 72 | "body": {"A robotics customer, Bo."}}) |
| 73 | if _, later := rig.postJSON(Ana, halden+"/rename", |
| 74 | url.Values{"title": {"Halden Robotics"}}); later["from"] != "0" || |
| 75 | later["to"] != "0" { |
| 76 | t.Fatalf("after an edit, an unchanged title answered %v", later) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestARefusedRenameIsTheSentenceAndWritesNothing(t *testing.T) { |
| 81 | rig := newRig(t) |
| 82 | halden := rig.createPage(t, Ana, "Halden", "One.") |
| 83 | acme := rig.createPage(t, Bo, "Acme", "Two.") |
| 84 | since := rig.lastEvent(t) |
| 85 | for title, want := range map[string]string{ |
| 86 | "acme": "already exists", |
| 87 | "2026-11-12": "A date can't be a page title", |
| 88 | strings.Repeat("x", 121): "too long", |
| 89 | "A [[b]]": "link syntax", |
| 90 | } { |
| 91 | code, reply := rig.postJSON(Ana, halden+"/rename", |
| 92 | url.Values{"title": {title}}) |
| 93 | said, _ := reply["error"].(string) |
| 94 | if code != http.StatusUnprocessableEntity || |
| 95 | !strings.Contains(said, want) { |
| 96 | t.Errorf("%q answered %d %v", title, code, reply) |
| 97 | } |
| 98 | if title == "acme" && reply["taken"] != acme { |
| 99 | t.Errorf("a taken title names %v, not %s", reply["taken"], acme) |
| 100 | } |
| 101 | } |
| 102 | if rig.lastEvent(t) != since { |
| 103 | t.Fatal("a refused rename wrote") |
| 104 | } |
| 105 | if code, _ := rig.postJSON(Ana, "/p/nothere/rename", |
| 106 | url.Values{"title": {"X"}}); code != http.StatusNotFound { |
| 107 | t.Fatalf("a page that is not there answered %d", code) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestUpdateLinksAnswersTheEditor(t *testing.T) { |
| 112 | rig := newRig(t) |
| 113 | acme := rig.createPage(t, Ana, "Acme", "A customer.") |
| 114 | plan := rig.createPage(t, Bo, "Plan", "Call [[Acme]].") |
| 115 | rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) |
| 116 | code, reply := rig.postJSON(Ana, acme+"/links", nil) |
| 117 | if code != http.StatusOK || reply["changed"] != 1.0 || |
| 118 | reply["notice"] != "Updated links in 1 page." { |
| 119 | t.Fatalf("Update links answered %d %v", code, reply) |
| 120 | } |
| 121 | if !strings.Contains(rig.page(t, Bo, plan+"/edit"), "[[Acme Ltd]]") { |
| 122 | t.Fatal("the link was not updated") |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // A rename while a draft is open (§3.2: a rename mints a revision for |
| 127 | // that page only, holding the same body): the draft's base moves with |
| 128 | // it, so committing the draft is Saved, not merged against a |
| 129 | // "teammate's" revision that changed nothing, and its text is intact. |
| 130 | func TestARenameCarriesTheDraftWithIt(t *testing.T) { |
| 131 | rig := newRig(t) |
| 132 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 133 | _, saved := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", |
| 134 | "One, and mine.") |
| 135 | version, _ := saved["version"].(string) |
| 136 | rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}}) |
| 137 | code, again := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), |
| 138 | version, "One, and mine, still.") |
| 139 | if code != http.StatusOK || again["merge"] != nil { |
| 140 | t.Fatalf("the autosave after a rename answered %d %v", code, again) |
| 141 | } |
| 142 | code, committed := rig.postJSON(Ana, acme+"/draft/save", |
| 143 | url.Values{"version": {again["version"].(string)}}) |
| 144 | if code != http.StatusOK || committed["saved"] != true || |
| 145 | committed["merged"] != false { |
| 146 | t.Fatalf("the commit after a rename answered %d %v", code, committed) |
| 147 | } |
| 148 | if body := islandOf(t, rig.page(t, Bo, acme)).Source; body != |
| 149 | "One, and mine, still." { |
| 150 | t.Fatalf("the page holds %q", body) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Only a team page's title is editable in place, and only where the |
| 155 | // editor mounts: never a daily note, a scratch page, History, the saved |
| 156 | // version beside a draft, or the Markdown editor; and the server never |
| 157 | // draws it editable (scriptless, it is a plain heading). |
| 158 | func TestOnlyATeamPagesTitleIsRenamableInPlace(t *testing.T) { |
| 159 | rig := newRig(t) |
| 160 | acme := rig.createPage(t, Ana, "Acme", "One.") |
| 161 | rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Day."}, |
| 162 | "revision": {"0"}}) |
| 163 | scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}). |
| 164 | Header().Get("Location") |
| 165 | page := rig.page(t, Ana, acme) |
| 166 | if !strings.Contains(page, `<h1 mapa-title mapa-rename="`+acme+ |
| 167 | `/rename"><span mapa-title-text>Acme</span></h1>`) { |
| 168 | t.Fatalf("the team page's title is not renamable:\n%s", |
| 169 | between(page, "<header mapa-page-head rst-page-header>", |
| 170 | "</header>")) |
| 171 | } |
| 172 | rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Draft.") |
| 173 | for _, path := range []string{"/d/2026-11-12", "/", scratch, |
| 174 | acme + "/history", acme + "?saved=1", acme + "/edit"} { |
| 175 | if got := rig.page(t, Ana, path); strings.Contains(got, "mapa-rename") { |
| 176 | t.Errorf("%s: the title is renamable in place", path) |
| 177 | } |
| 178 | } |
| 179 | if strings.Contains(page, "contenteditable") { |
| 180 | t.Fatal("the server drew something editable") |
| 181 | } |
| 182 | } |
| 183 | |