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
10 "amadan.net/rastrillo/rastrillo/harness"
11 "github.com/chromedp/chromedp"
12
13 "amadan.net/oficina/mapa/internal/mapa"
14)
15
16// What an open description field does when the page around it moves
17// (build note I22, Fable M2, L3, L6): leaving by a link, closing the
18// tab and ⌘S take what was typed with them; a block drawn again keeps
19// it; another window taking focus does not close it, and coming back
20// to somewhere else does.
21
22// bareImagePage is a page holding one image with no description, opened
23// in Ana's editor, with a word typed so that leaving commits.
24func bareImagePage(t *testing.T, rig *harness.Rig,
25 store *mapa.Store) mapa.Page {
26 t.Helper()
27 bare := addImage(t, store, Bo, 600, 300)
28 page := teamPage(t, rig, store, "Photo:\n\n"+bare.Markdown("")+
29 "\n\nBelow.")
30 drive(t, rig, caretAt(2), chromedp.KeyEvent(" Edited."))
31 eventually(t, "a draft", func() bool {
32 _, found := draftOf(t, store, page.ID)
33 return found
34 })
35 drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`),
36 chromedp.WaitVisible(altField))
37 return page
38}
39
40// commitHolds waits for the page's committed text to hold want.
41func commitHolds(t *testing.T, store *mapa.Store, page mapa.Page,
42 want string) {
43 t.Helper()
44 eventually(t, "the commit holds "+want, func() bool {
45 _, body := current(t, store, page.ID)
46 return strings.Contains(body, want)
47 })
48}
49
50// A link pressed without taking focus (Safari's tap does not) leaves
51// with the description in the commit.
52func TestAltGoesWithALinkPressed(t *testing.T) {
53 rig, store := browserRig(t, 0)
54 page := bareImagePage(t, rig, store)
55 drive(t, rig, chromedp.KeyEvent("A harbour"),
56 chromedp.Evaluate[chromedp.Void](
57 `document.querySelector('[mapa-side] a[href="/pages"]').click()`))
58 arrive(t, rig, "/pages")
59 commitHolds(t, store, page, "![A harbour](/i/")
60 // One commit, the leaving one, and it held the description: not a
61 // commit without it, mended by the tab's closing after (Fable M2).
62 viewer, _ := store.Viewer(context.Background(), Ana, nil)
63 history, err := store.History(context.Background(), viewer, page.ID, 0)
64 if err != nil || len(history.Versions) != 2 {
65 t.Fatalf("%d versions after leaving, not 2: %v",
66 len(history.Versions), err)
67 }
68}
69
70// Closing the tab (here, going elsewhere by address) commits it too.
71func TestAltGoesWithTheTabClosing(t *testing.T) {
72 rig, store := browserRig(t, 0)
73 page := bareImagePage(t, rig, store)
74 drive(t, rig, chromedp.KeyEvent("A quay"),
75 chromedp.Navigate(rig.Origin+"/pages"))
76 commitHolds(t, store, page, "![A quay](/i/")
77}
78
79// ⌘S in the field keeps the text and saves the page.
80func TestAltSavesWithCommandS(t *testing.T) {
81 rig, store := browserRig(t, 0)
82 page := bareImagePage(t, rig, store)
83 drive(t, rig, chromedp.KeyEvent("A pier"), chromedp.KeyEvent("s", mod()))
84 commitHolds(t, store, page, "![A pier](/i/")
85 if read(t, rig, chromedp.Evaluate[bool](
86 `document.querySelector('[mapa-alt]') !== null`)) {
87 t.Fatal("the field is still open after ⌘S")
88 }
89}
90
91// The image's block drawn again while the field is open (a rebase, the
92// inline choice, here a heading made of it): the field goes with the
93// image and keeps its focus and its text (Fable L6).
94func TestAltSurvivesItsBlockDrawnAgain(t *testing.T) {
95 rig, store := browserRig(t, 0)
96 bareImagePage(t, rig, store)
97 drive(t, rig, chromedp.KeyEvent("Kept on redraw"),
98 chromedp.Evaluate[chromedp.Void](`(function () {
99 var view = mapaEditor.view, found = -1;
100 view.state.doc.descendants(function (node, pos) {
101 if (found < 0 && node.type.name === "image") found = pos;
102 });
103 var heading = view.state.schema.nodes.heading;
104 view.dispatch(view.state.tr.setBlockType(found, found + 1, heading,
105 { level: 2 }));
106})()`), settle())
107 if !read(t, rig, chromedp.Evaluate[bool](`document.activeElement ===
108 document.querySelector('[mapa-alt] textarea')`)) {
109 t.Fatal("the field lost its focus when its block was drawn again")
110 }
111 drive(t, rig, chromedp.KeyEvent(" too"), enter())
112 saidAlt(t, rig, "Kept on redraw too")
113}
114
115// Another window taking focus does not close the field; focus coming
116// back to somewhere else does, keeping the text.
117func TestAltWaitsForAnotherWindow(t *testing.T) {
118 rig, store := browserRig(t, 0)
119 bareImagePage(t, rig, store)
120 drive(t, rig, chromedp.KeyEvent("Away and back"),
121 chromedp.Evaluate[chromedp.Void](`(function () {
122 document.hasFocus = function () { return false; };
123 document.querySelector('[mapa-alt] textarea').blur();
124})()`), settle())
125 if read(t, rig, chromedp.Evaluate[bool](
126 `document.querySelector('[mapa-alt]') === null`)) {
127 t.Fatal("another window taking focus closed the field")
128 }
129 drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () {
130 delete document.hasFocus;
131 window.dispatchEvent(new Event("focus"));
132})()`))
133 saidAlt(t, rig, "Away and back")
134}
135
136// A press on the field's label or its band keeps the field open, with
137// focus in it (Fable M1).
138func TestAltStaysOpenWhenItsLabelIsPressed(t *testing.T) {
139 rig, store := browserRig(t, 0)
140 bareImagePage(t, rig, store)
141 drive(t, rig, chromedp.KeyEvent("Still"),
142 chromedp.Click(`[mapa-alt] label`), settle())
143 if !read(t, rig, chromedp.Evaluate[bool](`document.activeElement ===
144 document.querySelector('[mapa-alt] textarea')`)) {
145 t.Fatal("pressing the label closed the field")
146 }
147 drive(t, rig, chromedp.KeyEvent(" here"), enter())
148 saidAlt(t, rig, "Still here")
149}
150
151// A long description on a short image stays inside the image's box: the
152// field scrolls instead of growing past the picture's top (Fable L1).
153func TestAltOnAShortImageStaysInside(t *testing.T) {
154 rig, store := browserRig(t, 0)
155 page := teamPage(t, rig, store, "Strip:\n\nBelow.")
156 upload(t, rig, page, 600, 120)
157 drive(t, rig, chromedp.KeyEvent(strings.Repeat("A long strip of sky. ",
158 20)))
159 inside := read(t, rig, chromedp.Evaluate[bool](`(function () {
160 var wrap = document.querySelector('[mapa-alt]').getBoundingClientRect();
161 var pic = document.querySelector('article[mapa-body] img[src^="/i/"]')
162 .getBoundingClientRect();
163 return wrap.top >= pic.top - 0.5 && wrap.bottom <= pic.bottom + 0.5;
164})()`))
165 if !inside {
166 t.Fatal("a long description grew the field past the picture")
167 }
168}
169
170// settle lets the page's timers run: a wait the page itself measures,
171// since a sleep on this side may find the page's timers held.
172func settle() chromedp.Action[chromedp.Void] {
173 return chromedp.Evaluate[chromedp.Void](`new Promise(function (done) {
174 setTimeout(function () { setTimeout(done, 50); }, 50);
175})`, chromedp.EvalAwaitPromise)
176}
177