oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo/harness"
12 "github.com/chromedp/chromedp"
13 "github.com/chromedp/chromedp/kb"
14
15 "amadan.net/oficina/mapa/internal/mapa"
16)
17
18// The in-page title (level-up §4.1, build note 78), in a real Chromium
19// through the real server: click it, type, Enter renames, and the
20// header, the sidebar, the tab's title and the links offer follow with
21// no reload; Esc writes nothing; a refusal is said beside it and keeps
22// the text; the header and the H1 never send two renames; daily notes
23// and scratch pages are not editable; entering it moves nothing; a
24// rename leaves the body's typing and its draft alone; and axe passes.
25// The shared state's logic is editor/title.test.mjs.
26
27// titleText is the H1's editable text, once the script has made it so.
28const titleText = `h1[mapa-title][mapa-rename] ` +
29 `[mapa-title-text][contenteditable]`
30
31// headerTitle is the bar's title field.
32const headerTitle = `form[estilo-bar-title] input[estilo-bar-title-input]`
33
34// titleOf is a page's current title and how many versions it has.
35func titleOf(t *testing.T, store *mapa.Store, pageID int64) (string, int) {
36 t.Helper()
37 ctx := context.Background()
38 viewer, _ := store.Viewer(ctx, Ana, nil)
39 history, err := store.History(ctx, viewer, pageID, 0)
40 if err != nil || len(history.Versions) == 0 {
41 t.Fatalf("history: %v", err)
42 }
43 chosen, err := store.ChooseVersion(ctx, viewer, pageID,
44 history.Versions[0].Revision)
45 if err != nil {
46 t.Fatal(err)
47 }
48 return chosen.Record.Title, len(history.Versions)
49}
50
51// titledPage is a team page Bo wrote, pinned by Ana so the sidebar
52// shows it, opened in Ana's editor.
53func titledPage(t *testing.T, rig *harness.Rig, store *mapa.Store,
54 title, body string) mapa.Page {
55 t.Helper()
56 ctx := context.Background()
57 page, err := store.CreateTeamPage(ctx, Bo, title, body)
58 if err != nil {
59 t.Fatal(err)
60 }
61 viewer, err := store.Viewer(ctx, Ana, nil)
62 if err == nil {
63 err = store.SetPin(ctx, viewer, page.ID, true)
64 }
65 if err != nil {
66 t.Fatal(err)
67 }
68 drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900),
69 chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
70 chromedp.WaitVisible(mounted), chromedp.WaitVisible(titleText),
71 chromedp.Evaluate[chromedp.Void](`window.__sameLoad = true`))
72 return page
73}
74
75// editTitle clicks the H1's text and puts the caret at its end.
76func editTitle() chromedp.Action[chromedp.Void] {
77 return chromedp.Steps(chromedp.Click(titleText),
78 chromedp.KeyEvent(kb.End))
79}
80
81// selectTitle clicks the H1's text and selects all of it, so typing
82// replaces it.
83func selectTitle() chromedp.Action[chromedp.Void] {
84 return chromedp.Steps(chromedp.Click(titleText),
85 chromedp.Evaluate[chromedp.Void](`(function (text) {
86 var range = document.createRange();
87 range.selectNodeContents(text);
88 getSelection().removeAllRanges();
89 getSelection().addRange(range);
90})(document.querySelector("[mapa-title-text]"))`))
91}
92
93func evaluate(t *testing.T, rig *harness.Rig, script string) string {
94 t.Helper()
95 return read(t, rig, chromedp.Evaluate[string](script))
96}
97
98func TestTitleClickTypeEnterRenamesInPlace(t *testing.T) {
99 rig, store := browserRig(t, 0)
100 if _, err := store.CreateTeamPage(context.Background(), Bo, "Plan",
101 "Call [[Halden]]."); err != nil {
102 t.Fatal(err)
103 }
104 page := titledPage(t, rig, store, "Halden", "A robotics customer.")
105 drive(t, rig, editTitle(), chromedp.KeyEvent(" Robotics"),
106 chromedp.KeyEvent("\r"))
107 eventually(t, "the rename", func() bool {
108 title, _ := titleOf(t, store, page.ID)
109 return title == "Halden Robotics"
110 })
111 notice := `[mapa-title-note] button[rst-btn="primary"]`
112 drive(t, rig, chromedp.WaitVisible(notice))
113 got := evaluate(t, rig, `[
114 document.querySelector("h1[mapa-title]").textContent,
115 document.querySelector("form[estilo-bar-title] input").value,
116 document.title,
117 document.querySelector('[mapa-side] a[mapa-side-page][href="`+
118 mapa.PagePath(page)+`"]').textContent.trim(),
119 mapaEditor.view.dom.getAttribute("aria-label"),
120 document.querySelector("[mapa-title-note] p").textContent,
121 String(window.__sameLoad)].join("|")`)
122 want := "Halden Robotics|Halden Robotics|Halden Robotics · Mapa|" +
123 "Halden Robotics|Page text, Halden Robotics|Renamed. 1 page you " +
124 "can edit link here by an old title."
125 if !strings.HasPrefix(got, want) || !strings.HasSuffix(got, "|true") {
126 t.Fatalf("after the rename:\n got %s\nwant %s…|true", got, want)
127 }
128 // The row's ⋯ is renamed with its row (build note 88).
129 more := evaluate(t, rig, `document.querySelector(
130 '[mapa-side] a[mapa-side-page][href="`+mapa.PagePath(page)+
131 `"] + [mapa-row-menu] [mapa-row-more]').getAttribute("aria-label")`)
132 if more != "More actions for Halden Robotics" {
133 t.Fatalf("after the rename the row's ⋯ is %q", more)
134 }
135 drive(t, rig, chromedp.Click(notice))
136 eventually(t, "the links updated", func() bool {
137 return strings.Contains(evaluate(t, rig,
138 `document.querySelector("[mapa-title-note]").textContent`),
139 "Updated links in 1 page.")
140 })
141}
142
143func TestTitleEscRevertsAndWritesNothing(t *testing.T) {
144 rig, store := browserRig(t, 0)
145 page := titledPage(t, rig, store, "Halden", "One.")
146 _, versions := titleOf(t, store, page.ID)
147 drive(t, rig, editTitle(), chromedp.KeyEvent(" Gone"),
148 chromedp.KeyEvent(kb.Escape))
149 time.Sleep(1500 * time.Millisecond)
150 if title, now := titleOf(t, store, page.ID); title != "Halden" ||
151 now != versions {
152 t.Fatalf("Esc wrote: %q, %d versions", title, now)
153 }
154 got := evaluate(t, rig, `[
155 document.querySelector("h1[mapa-title]").textContent,
156 document.activeElement === document.querySelector("[mapa-title-text]"),
157 document.querySelector("[mapa-title-note]").textContent].join("|")`)
158 if got != "Halden|false|" {
159 t.Fatalf("after Esc: %q", got)
160 }
161}
162
163func TestTitleRefusalIsInlineAndKeepsTheText(t *testing.T) {
164 rig, store := browserRig(t, 0)
165 if _, err := store.CreateTeamPage(context.Background(), Bo, "Acme",
166 "Taken."); err != nil {
167 t.Fatal(err)
168 }
169 page := titledPage(t, rig, store, "Halden", "One.")
170 drive(t, rig, selectTitle(),
171 chromedp.KeyEvent("acme"), chromedp.KeyEvent("\r"),
172 chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`))
173 got := evaluate(t, rig, `[
174 document.querySelector("h1[mapa-title]").textContent,
175 document.querySelector("[mapa-title-note] [role=alert]").textContent,
176 document.querySelector("[mapa-title-text]").getAttribute("aria-invalid"),
177 document.querySelector("form[estilo-bar-title] input").value].join("|")`)
178 if !strings.HasPrefix(got, "acme|A page with that title already exists. "+
179 "Open the page with that title|true|Halden") {
180 t.Fatalf("the refusal: %q", got)
181 }
182 if title, _ := titleOf(t, store, page.ID); title != "Halden" {
183 t.Fatalf("a refused title was written: %q", title)
184 }
185}
186
187// The header and the H1 share one pending state: the H1's rename is
188// out (held at least 650ms by the busy rule) when the header's Enter
189// comes, and only one rename is sent; then the other way round.
190func TestTitleHeaderAndH1NeverDoubleSubmit(t *testing.T) {
191 rig, store := browserRig(t, 0)
192 page := titledPage(t, rig, store, "Halden", "One.")
193 _, versions := titleOf(t, store, page.ID)
194 drive(t, rig, editTitle(), chromedp.KeyEvent(" One"),
195 chromedp.Focus(headerTitle))
196 pending := evaluate(t, rig, `(function (input) {
197 return [input.value, input.readOnly,
198 document.querySelector("[mapa-title-note]").textContent].join("|");
199})(document.querySelector("form[estilo-bar-title] input"))`)
200 if pending != "Halden One|true|Renaming…" {
201 t.Fatalf("while the H1's rename is out, the header shows %q",
202 pending)
203 }
204 drive(t, rig, chromedp.KeyEvent(" Two"), chromedp.KeyEvent("\r"))
205 eventually(t, "the H1's rename", func() bool {
206 title, _ := titleOf(t, store, page.ID)
207 return title == "Halden One"
208 })
209 time.Sleep(1500 * time.Millisecond)
210 if title, now := titleOf(t, store, page.ID); title != "Halden One" ||
211 now != versions+1 {
212 t.Fatalf("two renames: %q, %d versions after %d", title, now,
213 versions)
214 }
215 drive(t, rig, chromedp.WaitNotPresent(`[mapa-title-note] [rst-spin]`),
216 chromedp.SetValue(headerTitle, "Halden Three"),
217 chromedp.Focus(headerTitle), chromedp.KeyEvent("\r"),
218 chromedp.Click(titleText), chromedp.KeyEvent("Four"),
219 chromedp.KeyEvent("\r"))
220 eventually(t, "the header's rename", func() bool {
221 title, _ := titleOf(t, store, page.ID)
222 return title == "Halden Three"
223 })
224 time.Sleep(1500 * time.Millisecond)
225 if title, now := titleOf(t, store, page.ID); title != "Halden Three" ||
226 now != versions+2 {
227 t.Fatalf("the header then the H1: %q, %d versions", title, now)
228 }
229 if got := text(t, rig, "h1[mapa-title]"); got != "Halden Three" {
230 t.Fatalf("the H1 shows %q", got)
231 }
232}
233
234func TestTitleIsNotEditableOnDailyOrScratchPages(t *testing.T) {
235 rig, store := browserRig(t, 0)
236 scratch, err := store.CreateScratch(context.Background(), Ana, "Mine.")
237 if err != nil {
238 t.Fatal(err)
239 }
240 team, err := store.CreateTeamPage(context.Background(), Bo, "Halden",
241 "One.")
242 if err != nil {
243 t.Fatal(err)
244 }
245 // The team page is the control: its title is editable, so the same
246 // question on the others means what it says.
247 for _, path := range []string{mapa.PagePath(team), "/", "/d/2026-11-12",
248 mapa.PagePath(scratch), "/scratch/new",
249 mapa.PagePath(team) + "/history"} {
250 drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900),
251 chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(mounted))
252 want := map[bool]string{true: "1", false: "0"}[path ==
253 mapa.PagePath(team)]
254 got := evaluate(t, rig, `String(document.querySelectorAll(
255 "h1[mapa-title] [contenteditable], h1[mapa-title][contenteditable]"
256).length)`)
257 if got != want {
258 t.Errorf("%s: %s editable titles, want %s", path, got, want)
259 }
260 }
261}
262