//go:build browser // Steps with the script on: present.js applies a step in place, from // each box's data-press, and marks data-arriving only on what has just // arrived (Task 6). The Go tests read the markup; only a browser shows // that the slide stays the same node and that reduced motion appears. package slidestest import ( "encoding/json" "net/http" "net/url" "strconv" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // stepsTalk is a browser beside a session on stepsDeck, which the test // drives over HTTP with the remote token. type stepsTalk struct { rig *harness.Rig app *testApp cookies []*http.Cookie deck string // /d/{sqid} session, token string } func newStepsTalk(t *testing.T) *stepsTalk { t.Helper() rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) deck := stepsDeck(t, app, cookies) session, token := startPresenting(t, app, deck, cookies) return &stepsTalk{rig: rig, app: app, cookies: cookies, deck: deck, session: session, token: token} } func (st *stepsTalk) press(t *testing.T, name string) { t.Helper() press(t, st.app, st.session, st.token, op(t, name), nil) } // stageState is what the stage shows: the presses still waiting, the // ones marked arriving, the surface's data-slide and data-step, and // whether the slide is the node marked by markSection. type stageState struct { Waiting []string `json:"waiting"` Arriving []string `json:"arriving"` Slide string `json:"slide"` Step string `json:"step"` Same bool `json:"same"` } const markSection = `(window.__section = document.querySelector('[slides-stage] [slides-slide]'), true)` func (st *stepsTalk) stage(t *testing.T) stageState { t.Helper() var raw string st.rig.Run(evalInto(&raw, `JSON.stringify((() => { const stage = document.querySelector('[slides-stage]'); const root = document.querySelector('[slides-surface]'); const presses = sel => [...stage.querySelectorAll(sel)].map(e => e.dataset.press); return { waiting: presses('[data-waiting]'), arriving: presses('[data-arriving]'), slide: root.getAttribute('data-slide'), step: root.getAttribute('data-step'), same: stage.querySelector('[slides-slide]') === window.__section, }; })())`)) var out stageState if err := json.Unmarshal([]byte(raw), &out); err != nil { t.Fatalf("read the stage: %v\n%s", err, raw) } return out } // waitWaiting waits until n presses are waiting on the stage. func waitWaiting(n string) chromedp.Action[chromedp.Void] { return waitFor(`document.querySelectorAll('[slides-stage] [data-waiting]').length === ` + n) } // TestFollowRevealsInPlace: a step heard on the stream changes the // follow view's slide in place, the section the same node, and only // what has just arrived is marked arriving; a step back marks nothing. func TestFollowRevealsInPlace(t *testing.T) { st := newStepsTalk(t) share := mintShareLink(t, st.app, st.deck, st.cookies) st.rig.Run( chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), waitWaiting("4"), chromedp.Evaluate[chromedp.Void](markSection), ) for _, want := range []struct{ op, waiting, arriving, step string }{ {"next", "2,3,4", "1", "1"}, {"next", "3,4", "2", "2"}, {"prev", "2,3,4", "", "1"}, } { st.press(t, want.op) st.rig.Run(waitWaiting(strconv.Itoa(len(strings.Split(want.waiting, ","))))) got := st.stage(t) if !got.Same { t.Fatalf("after %s the slide is a new node; a step applies in place", want.op) } if strings.Join(got.Waiting, ",") != want.waiting || strings.Join(got.Arriving, ",") != want.arriving || got.Step != want.step { t.Errorf("after %s: waiting %v arriving %v step %q, want waiting %s arriving [%s] step %s", want.op, got.Waiting, got.Arriving, got.Step, want.waiting, want.arriving, want.step) } } } // TestBackFromTheNextSlideLandsFullyBuilt: back from the slide after a // stepped one lands on it with every press shown, and nothing arriving; // Back again, from the projector's own button, steps back in place. func TestBackFromTheNextSlideLandsFullyBuilt(t *testing.T) { st := newStepsTalk(t) st.rig.Run( chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"?t="+url.QueryEscape(st.token)), chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), waitWaiting("4"), chromedp.Evaluate[chromedp.Void](markSection), // The projector's own Next, on the page as it loaded: a step, // in place. chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="next"]`)), waitWaiting("3"), ) if got := st.stage(t); !got.Same || strings.Join(got.Arriving, ",") != "1" || got.Step != "1" { t.Fatalf("Next on the loaded projector: same node %v, arriving %v, step %q, want press 1 arriving in place at step 1", got.Same, got.Arriving, got.Step) } for range 4 { st.press(t, "next") } st.rig.Run( waitFor(`document.querySelector('[slides-surface]').getAttribute('data-slide') === '2'`), chromedp.KeyEvent(kb.ArrowLeft), waitFor(`document.querySelector('[slides-surface]').getAttribute('data-slide') === '1'`), ) got := st.stage(t) if len(got.Waiting) != 0 || len(got.Arriving) != 0 || got.Step != "4" { t.Fatalf("back onto the stepped slide: waiting %v arriving %v step %q, want it built (step 4) with nothing arriving", got.Waiting, got.Arriving, got.Step) } st.rig.Run( chromedp.Evaluate[chromedp.Void](markSection), chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="prev"]`)), waitWaiting("1"), ) got = st.stage(t) if !got.Same || strings.Join(got.Waiting, ",") != "4" || len(got.Arriving) != 0 || got.Step != "3" { 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", got.Same, got.Waiting, got.Arriving, got.Step) } } // TestAModeChangeRecountsInPlace: switching between the short and the // full talk on a stepped slide, with a full-only slide later in the // deck, recounts the projector's counter and bar at once and leaves // the slide where it is, the same node at the same step; the phone // recounts too. func TestAModeChangeRecountsInPlace(t *testing.T) { st := newStepsTalk(t) counter := func(want string) chromedp.Action[chromedp.Void] { return waitFor(`(document.querySelector('[slides-counter]')?.textContent ?? '').includes('` + want + `')`) } st.rig.Run( chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"?t="+url.QueryEscape(st.token)), chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide]`)), waitWaiting("4"), counter("1 / 3"), chromedp.Click(chromedp.CSS(`[slides-bar] button[slides-op="next"]`)), waitWaiting("3"), chromedp.Evaluate[chromedp.Void](markSection), ) var max, mode string for _, want := range []struct{ op, counter, max, mode, step string }{ {"mode", "1 / 4", "4", "full", "1"}, {"next", "1 / 4", "4", "full", "2"}, {"mode", "1 / 3", "3", "core", "2"}, } { st.press(t, want.op) st.rig.Run( counter(want.counter), waitFor(`document.querySelector('[slides-surface]').getAttribute('data-step') === '`+want.step+`'`), evalInto(&max, `document.querySelector('[slides-progress]').getAttribute('max')`), evalInto(&mode, `document.querySelector('[slides-surface]').getAttribute('data-mode')`), ) got := st.stage(t) if !got.Same || got.Slide != "1" || max != want.max || mode != want.mode { 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", want.op, got.Same, got.Slide, max, mode, want.max, want.mode) } } var text string st.rig.Run( chromedp.Navigate(st.rig.Origin+"/p/"+st.session+"/remote?t="+url.QueryEscape(st.token)), chromedp.WaitVisible(chromedp.CSS(`[slides-remote]`)), counter("1 / 3"), ) st.press(t, "mode") st.rig.Run( counter("1 / 4"), waitFor(`document.querySelector('[slides-remote]')?.getAttribute('data-mode') === 'full'`), evalInto(&text, `document.querySelector('[slides-counter]').textContent`), ) if !strings.Contains(text, "1 / 4") { t.Errorf("the remote after a switch to the full talk says %q, want 1 / 4", text) } } // TestReducedMotionAppears: with the deck set to fade, what arrives // fades and is unmarked once it has; with reduced motion asked for it // simply appears. func TestReducedMotionAppears(t *testing.T) { st := newStepsTalk(t) token := mintAgent(t, st.app, st.cookies, "arrive") apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(st.deck, "/d/")+"/ops", token, `{"ops":[{"op":"set_meta","arrive":"fade"}]}`, http.StatusOK) share := mintShareLink(t, st.app, st.deck, st.cookies) // Each element as it is marked arriving: its animation, opacity and // visibility at that moment. const watch = `(() => { window.__seen = []; if (window.__watch) window.__watch.disconnect(); (window.__watch = new MutationObserver(ms => { for (const m of ms) { if (!m.target.hasAttribute('data-arriving')) continue; const cs = getComputedStyle(m.target); window.__seen.push({press: m.target.dataset.press, animation: cs.animationName, opacity: cs.opacity, visibility: cs.visibility}); } })).observe(document.querySelector('[slides-stage]'), {attributes: true, subtree: true, attributeFilter: ['data-arriving']}); return true; })()` type seen struct{ Press, Animation, Opacity, Visibility string } read := func() []seen { var raw string st.rig.Run(evalInto(&raw, `JSON.stringify(window.__seen)`)) var out []seen if err := json.Unmarshal([]byte(raw), &out); err != nil { t.Fatalf("read what arrived: %v\n%s", err, raw) } return out } st.rig.Run( chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), chromedp.WaitVisible(chromedp.CSS(`[slides-stage] [slides-slide][data-arrive="fade"]`)), waitWaiting("4"), chromedp.Evaluate[chromedp.Void](watch), ) st.press(t, "next") st.rig.Run( waitWaiting("3"), waitFor(`!document.querySelector('[slides-stage] [data-arriving]')`), ) if got := read(); len(got) != 1 || got[0].Press != "1" || got[0].Animation != "slides-arrive-fade" { t.Fatalf("with motion allowed, press 1 arrived as %+v, want it alone, fading", got) } st.rig.Run( cdpStep(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{ {Name: "prefers-reduced-motion", Value: "reduce"}, }}), chromedp.Evaluate[chromedp.Void](watch), ) st.press(t, "next") st.rig.Run(waitWaiting("2")) got := read() if len(got) != 1 || got[0].Press != "2" || got[0].Animation != "none" || got[0].Opacity != "1" || got[0].Visibility != "visible" { t.Errorf("with reduced motion, press 2 arrived as %+v, want it shown at once with no animation", got) } } // TestFollowFindsTheSlideByItsID: a follower showing B, second in the // talk, when the deck is reordered B, A and the talk moves on to A, // which is second too: the slide's number is the same, the slide is // not, and the follower fetches A rather than taking it for a step of B. func TestFollowFindsTheSlideByItsID(t *testing.T) { st := newStepsTalk(t) agent := mintAgent(t, st.app, st.cookies, "reorder") sqid := strings.TrimPrefix(st.deck, "/d/") view := apiJSON(t, st.app, http.MethodGet, "/api/v1/decks/"+sqid, agent, "", http.StatusOK) ops := []string{} for _, s := range view["slides"].([]any) { ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) } for _, s := range []struct{ id, title string }{{"slidea", "Alpha"}, {"slideb", "Bravo"}, {"slidec", "Charlie"}} { ops = append(ops, `{"op":"add_slide","id":"`+s.id+`","layout":"flow","template":"title-body"}`, `{"op":"add_block","slide":"`+s.id+`","id":"`+s.id+`h","type":"heading","region":"heading","props":{"level":1},"markdown":"`+s.title+`"}`) } apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) press(t, st.app, st.session, st.token, url.Values{"op": {"goto"}, "slide": {"2"}, "press": {newPressID(t)}}, nil) share := mintShareLink(t, st.app, st.deck, st.cookies) heading := `document.querySelector('[slides-stage] [slides-slide]')?.innerText.trim()` st.rig.Run( chromedp.Navigate(st.rig.Origin+"/s/"+share+"?follow=1"), waitFor(heading+` === 'Bravo'`), ) apiJSON(t, st.app, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[{"op":"move_slide","id":"slideb","after":null}]}`, http.StatusOK) at := press(t, st.app, st.session, st.token, op(t, "next"), nil) if at.Slide != "slidea" { t.Fatalf("next from Bravo, first after the reorder, reached %q, want slidea", at.Slide) } st.rig.Run(waitFor(heading + ` === 'Alpha'`)) }