| 1 | //go:build browser |
| 2 | |
| 3 | // Steps with the script on: present.js applies a step in place, from |
| 4 | // each box's data-press, and marks data-arriving only on what has just |
| 5 | // arrived (Task 6). The Go tests read the markup; only a browser shows |
| 6 | // that the slide stays the same node and that reduced motion appears. |
| 7 | package slidestest |
| 8 | |
| 9 | import ( |
| 10 | "encoding/json" |
| 11 | "net/http" |
| 12 | "net/url" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "amadan.net/rastrillo/rastrillo/harness" |
| 18 | "github.com/chromedp/cdproto/emulation" |
| 19 | "github.com/chromedp/chromedp" |
| 20 | "github.com/chromedp/chromedp/kb" |
| 21 | ) |
| 22 | |
| 23 | // stepsTalk is a browser beside a session on stepsDeck, which the test |
| 24 | // drives over HTTP with the remote token. |
| 25 | type stepsTalk struct { |
| 26 | rig *harness.Rig |
| 27 | app *testApp |
| 28 | cookies []*http.Cookie |
| 29 | deck string // /d/{sqid} |
| 30 | session, token string |
| 31 | } |
| 32 | |
| 33 | func newStepsTalk(t *testing.T) *stepsTalk { |
| 34 | t.Helper() |
| 35 | rig, app := newRigApp(t, nil) |
| 36 | cookies := signedIn(t, app, allowedAddress) |
| 37 | deck := stepsDeck(t, app, cookies) |
| 38 | session, token := startPresenting(t, app, deck, cookies) |
| 39 | return &stepsTalk{rig: rig, app: app, cookies: cookies, deck: deck, session: session, token: token} |
| 40 | } |
| 41 | |
| 42 | func (st *stepsTalk) press(t *testing.T, name string) { |
| 43 | t.Helper() |
| 44 | press(t, st.app, st.session, st.token, op(t, name), nil) |
| 45 | } |
| 46 | |
| 47 | // stageState is what the stage shows: the presses still waiting, the |
| 48 | // ones marked arriving, the surface's data-slide and data-step, and |
| 49 | // whether the slide is the node marked by markSection. |
| 50 | type stageState struct { |
| 51 | Waiting []string `json:"waiting"` |
| 52 | Arriving []string `json:"arriving"` |
| 53 | Slide string `json:"slide"` |
| 54 | Step string `json:"step"` |
| 55 | Same bool `json:"same"` |
| 56 | } |
| 57 | |
| 58 | const markSection = `(window.__section = document.querySelector('[slides-stage] [slides-slide]'), true)` |
| 59 | |
| 60 | func (st *stepsTalk) stage(t *testing.T) stageState { |
| 61 | t.Helper() |
| 62 | var raw string |
| 63 | st.rig.Run(evalInto(&raw, `JSON.stringify((() => { |
| 64 | const stage = document.querySelector('[slides-stage]'); |
| 65 | const root = document.querySelector('[slides-surface]'); |
| 66 | const presses = sel => [...stage.querySelectorAll(sel)].map(e => e.dataset.press); |
| 67 | return { |
| 68 | waiting: presses('[data-waiting]'), |
| 69 | arriving: presses('[data-arriving]'), |
| 70 | slide: root.getAttribute('data-slide'), |
| 71 | step: root.getAttribute('data-step'), |
| 72 | same: stage.querySelector('[slides-slide]') === window.__section, |
| 73 | }; |
| 74 | })())`)) |
| 75 | var out stageState |
| 76 | if err := json.Unmarshal([]byte(raw), &out); err != nil { |
| 77 | t.Fatalf("read the stage: %v\n%s", err, raw) |
| 78 | } |
| 79 | return out |
| 80 | } |
| 81 | |
| 82 | // waitWaiting waits until n presses are waiting on the stage. |
| 83 | func waitWaiting(n string) chromedp.Action[chromedp.Void] { |
| 84 | return waitFor(`document.querySelectorAll('[slides-stage] [data-waiting]').length === ` + n) |
| 85 | } |
| 86 | |
| 87 | // TestFollowRevealsInPlace: a step heard on the stream changes the |
| 88 | // follow view's slide in place, the section the same node, and only |
| 89 | // what has just arrived is marked arriving; a step back marks nothing. |
| 90 | func TestFollowRevealsInPlace(t *testing.T) { |
| 91 | st := newStepsTalk(t) |
| 92 | share := mintShareLink(t, st.app, st.deck, st.cookies) |
| 93 | st.rig.Run( |
| 94 | chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), |
| 95 | chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), |
| 96 | waitWaiting("4"), |
| 97 | chromedp.Evaluate[chromedp.Void](markSection), |
| 98 | ) |
| 99 | |
| 100 | for _, want := range []struct{ op, waiting, arriving, step string }{ |
| 101 | {"next", "2,3,4", "1", "1"}, |
| 102 | {"next", "3,4", "2", "2"}, |
| 103 | {"prev", "2,3,4", "", "1"}, |
| 104 | } { |
| 105 | st.press(t, want.op) |
| 106 | st.rig.Run(waitWaiting(strconv.Itoa(len(strings.Split(want.waiting, ","))))) |
| 107 | got := st.stage(t) |
| 108 | if !got.Same { |
| 109 | t.Fatalf("after %s the slide is a new node; a step applies in place", want.op) |
| 110 | } |
| 111 | if strings.Join(got.Waiting, ",") != want.waiting || strings.Join(got.Arriving, ",") != want.arriving || got.Step != want.step { |
| 112 | t.Errorf("after %s: waiting %v arriving %v step %q, want waiting %s arriving [%s] step %s", |
| 113 | want.op, got.Waiting, got.Arriving, got.Step, want.waiting, want.arriving, want.step) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // TestBackFromTheNextSlideLandsFullyBuilt: back from the slide after a |
| 119 | // stepped one lands on it with every press shown, and nothing arriving; |
| 120 | // Back again, from the projector's own button, steps back in place. |
| 121 | func TestBackFromTheNextSlideLandsFullyBuilt(t *testing.T) { |
| 122 | st := newStepsTalk(t) |
| 123 | st.rig.Run( |
| 124 | chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"?t="+url.QueryEscape(st.token)), |
| 125 | chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), |
| 126 | waitWaiting("4"), |
| 127 | chromedp.Evaluate[chromedp.Void](markSection), |
| 128 | // The projector's own Next, on the page as it loaded: a step, |
| 129 | // in place. |
| 130 | chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="next"]`)), |
| 131 | waitWaiting("3"), |
| 132 | ) |
| 133 | if got := st.stage(t); !got.Same || strings.Join(got.Arriving, ",") != "1" || got.Step != "1" { |
| 134 | t.Fatalf("Next on the loaded projector: same node %v, arriving %v, step %q, want press 1 arriving in place at step 1", |
| 135 | got.Same, got.Arriving, got.Step) |
| 136 | } |
| 137 | for range 4 { |
| 138 | st.press(t, "next") |
| 139 | } |
| 140 | st.rig.Run( |
| 141 | waitFor(`document.querySelector('[slides-surface]').getAttribute('data-slide') === '2'`), |
| 142 | chromedp.KeyEvent(kb.ArrowLeft), |
| 143 | waitFor(`document.querySelector('[slides-surface]').getAttribute('data-slide') === '1'`), |
| 144 | ) |
| 145 | got := st.stage(t) |
| 146 | if len(got.Waiting) != 0 || len(got.Arriving) != 0 || got.Step != "4" { |
| 147 | t.Fatalf("back onto the stepped slide: waiting %v arriving %v step %q, want it built (step 4) with nothing arriving", |
| 148 | got.Waiting, got.Arriving, got.Step) |
| 149 | } |
| 150 | |
| 151 | st.rig.Run( |
| 152 | chromedp.Evaluate[chromedp.Void](markSection), |
| 153 | chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="prev"]`)), |
| 154 | waitWaiting("1"), |
| 155 | ) |
| 156 | got = st.stage(t) |
| 157 | if !got.Same || strings.Join(got.Waiting, ",") != "4" || len(got.Arriving) != 0 || got.Step != "3" { |
| 158 | t.Errorf("Back within the slide: same node %v, waiting %v arriving %v step %q, want the same node at step 3 with 4 waiting and nothing arriving", |
| 159 | got.Same, got.Waiting, got.Arriving, got.Step) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // TestAModeChangeRecountsInPlace: switching between the short and the |
| 164 | // full talk on a stepped slide, with a full-only slide later in the |
| 165 | // deck, recounts the projector's counter and bar at once and leaves |
| 166 | // the slide where it is, the same node at the same step; the phone |
| 167 | // recounts too. |
| 168 | func TestAModeChangeRecountsInPlace(t *testing.T) { |
| 169 | st := newStepsTalk(t) |
| 170 | counter := func(want string) chromedp.Action[chromedp.Void] { |
| 171 | return waitFor(`(document.querySelector('[slides-counter]')?.textContent ?? '').includes('` + want + `')`) |
| 172 | } |
| 173 | st.rig.Run( |
| 174 | chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"?t="+url.QueryEscape(st.token)), |
| 175 | chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), |
| 176 | waitWaiting("4"), |
| 177 | counter("1 / 3"), |
| 178 | chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="next"]`)), |
| 179 | waitWaiting("3"), |
| 180 | chromedp.Evaluate[chromedp.Void](markSection), |
| 181 | ) |
| 182 | |
| 183 | var max, mode string |
| 184 | for _, want := range []struct{ op, counter, max, mode, step string }{ |
| 185 | {"mode", "1 / 4", "4", "full", "1"}, |
| 186 | {"next", "1 / 4", "4", "full", "2"}, |
| 187 | {"mode", "1 / 3", "3", "core", "2"}, |
| 188 | } { |
| 189 | st.press(t, want.op) |
| 190 | st.rig.Run( |
| 191 | counter(want.counter), |
| 192 | waitFor(`document.querySelector('[slides-surface]').getAttribute('data-step') === '`+want.step+`'`), |
| 193 | evalInto(&max, `document.querySelector('[slides-progress]').getAttribute('max')`), |
| 194 | evalInto(&mode, `document.querySelector('[slides-surface]').getAttribute('data-mode')`), |
| 195 | ) |
| 196 | got := st.stage(t) |
| 197 | if !got.Same || got.Slide != "1" || max != want.max || mode != want.mode { |
| 198 | t.Errorf("after %s: same node %v, data-slide %q, progress max %q, data-mode %q; want the same node on slide 1, max %s, %s", |
| 199 | want.op, got.Same, got.Slide, max, mode, want.max, want.mode) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | var text string |
| 204 | st.rig.Run( |
| 205 | chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"/remote?t="+url.QueryEscape(st.token)), |
| 206 | chromedp.WaitVisible(chromedp.CSS(`[slides-remote]`)), |
| 207 | counter("1 / 3"), |
| 208 | ) |
| 209 | st.press(t, "mode") |
| 210 | st.rig.Run( |
| 211 | counter("1 / 4"), |
| 212 | waitFor(`document.querySelector('[slides-remote]')?.getAttribute('data-mode') === 'full'`), |
| 213 | evalInto(&text, `document.querySelector('[slides-counter]').textContent`), |
| 214 | ) |
| 215 | if !strings.Contains(text, "1 / 4") { |
| 216 | t.Errorf("the remote after a switch to the full talk says %q, want 1 / 4", text) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // TestReducedMotionAppears: with the deck set to fade, what arrives |
| 221 | // fades and is unmarked once it has; with reduced motion asked for it |
| 222 | // simply appears. |
| 223 | func TestReducedMotionAppears(t *testing.T) { |
| 224 | st := newStepsTalk(t) |
| 225 | token := mintAgent(t, st.app, st.cookies, "arrive") |
| 226 | apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(st.deck, "/d/")+"/ops", token, |
| 227 | `{"ops":[{"op":"set_meta","arrive":"fade"}]}`, http.StatusOK) |
| 228 | share := mintShareLink(t, st.app, st.deck, st.cookies) |
| 229 | |
| 230 | // Each element as it is marked arriving: its animation, opacity and |
| 231 | // visibility at that moment. |
| 232 | const watch = `(() => { |
| 233 | window.__seen = []; |
| 234 | if (window.__watch) window.__watch.disconnect(); |
| 235 | (window.__watch = new MutationObserver(ms => { |
| 236 | for (const m of ms) { |
| 237 | if (!m.target.hasAttribute('data-arriving')) continue; |
| 238 | const cs = getComputedStyle(m.target); |
| 239 | window.__seen.push({press: m.target.dataset.press, animation: cs.animationName, opacity: cs.opacity, visibility: cs.visibility}); |
| 240 | } |
| 241 | })).observe(document.querySelector('[slides-stage]'), {attributes: true, subtree: true, attributeFilter: ['data-arriving']}); |
| 242 | return true; |
| 243 | })()` |
| 244 | type seen struct{ Press, Animation, Opacity, Visibility string } |
| 245 | read := func() []seen { |
| 246 | var raw string |
| 247 | st.rig.Run(evalInto(&raw, `JSON.stringify(window.__seen)`)) |
| 248 | var out []seen |
| 249 | if err := json.Unmarshal([]byte(raw), &out); err != nil { |
| 250 | t.Fatalf("read what arrived: %v\n%s", err, raw) |
| 251 | } |
| 252 | return out |
| 253 | } |
| 254 | |
| 255 | st.rig.Run( |
| 256 | chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), |
| 257 | chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide][data-arrive="fade"]`)), |
| 258 | waitWaiting("4"), |
| 259 | chromedp.Evaluate[chromedp.Void](watch), |
| 260 | ) |
| 261 | st.press(t, "next") |
| 262 | st.rig.Run( |
| 263 | waitWaiting("3"), |
| 264 | waitFor(`!document.querySelector('[slides-stage] [data-arriving]')`), |
| 265 | ) |
| 266 | if got := read(); len(got) != 1 || got[0].Press != "1" || got[0].Animation != "slides-arrive-fade" { |
| 267 | t.Fatalf("with motion allowed, press 1 arrived as %+v, want it alone, fading", got) |
| 268 | } |
| 269 | |
| 270 | st.rig.Run( |
| 271 | cdpStep(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{ |
| 272 | {Name: "prefers-reduced-motion", Value: "reduce"}, |
| 273 | }}), |
| 274 | chromedp.Evaluate[chromedp.Void](watch), |
| 275 | ) |
| 276 | st.press(t, "next") |
| 277 | st.rig.Run(waitWaiting("2")) |
| 278 | got := read() |
| 279 | if len(got) != 1 || got[0].Press != "2" || got[0].Animation != "none" || got[0].Opacity != "1" || got[0].Visibility != "visible" { |
| 280 | t.Errorf("with reduced motion, press 2 arrived as %+v, want it shown at once with no animation", got) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // TestFollowFindsTheSlideByItsID: a follower showing B, second in the |
| 285 | // talk, when the deck is reordered B, A and the talk moves on to A, |
| 286 | // which is second too: the slide's number is the same, the slide is |
| 287 | // not, and the follower fetches A rather than taking it for a step of B. |
| 288 | func TestFollowFindsTheSlideByItsID(t *testing.T) { |
| 289 | st := newStepsTalk(t) |
| 290 | agent := mintAgent(t, st.app, st.cookies, "reorder") |
| 291 | sqid := strings.TrimPrefix(st.deck, "/d/") |
| 292 | view := apiJSON(t, st.app, http.MethodGet, "/api/v1/decks/"+sqid, agent, "", http.StatusOK) |
| 293 | ops := []string{} |
| 294 | for _, s := range view["slides"].([]any) { |
| 295 | ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) |
| 296 | } |
| 297 | for _, s := range []struct{ id, title string }{{"slidea", "Alpha"}, {"slideb", "Bravo"}, {"slidec", "Charlie"}} { |
| 298 | ops = append(ops, |
| 299 | `{"op":"add_slide","id":"`+s.id+`","layout":"flow","template":"title-body"}`, |
| 300 | `{"op":"add_block","slide":"`+s.id+`","id":"`+s.id+`h","type":"heading","region":"heading","props":{"level":1},"markdown":"`+s.title+`"}`) |
| 301 | } |
| 302 | apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 303 | press(t, st.app, st.session, st.token, url.Values{"op": {"goto"}, "slide": {"2"}, "press": {newPressID(t)}}, nil) |
| 304 | |
| 305 | share := mintShareLink(t, st.app, st.deck, st.cookies) |
| 306 | heading := `document.querySelector('[slides-stage] [slides-slide]')?.innerText.trim()` |
| 307 | st.rig.Run( |
| 308 | chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), |
| 309 | waitFor(heading+` === 'Bravo'`), |
| 310 | ) |
| 311 | |
| 312 | apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[{"op":"move_slide","id":"slideb","after":null}]}`, http.StatusOK) |
| 313 | at := press(t, st.app, st.session, st.token, op(t, "next"), nil) |
| 314 | if at.Slide != "slidea" { |
| 315 | t.Fatalf("next from Bravo, first after the reorder, reached %q, want slidea", at.Slide) |
| 316 | } |
| 317 | st.rig.Run(waitFor(heading + ` === 'Alpha'`)) |
| 318 | } |
| 319 | |