| 1 | //go:build browser |
| 2 | |
| 3 | // The presenting screens with their script on. The Go tests read the |
| 4 | // markup; only a browser runs static/present.js against it, and the |
| 5 | // failure this file was written for (polish audit B3, 2026-09-23) was |
| 6 | // the script and the markup disagreeing: the presenter view and the |
| 7 | // remote carried no data-slide, so the page believed it was on slide |
| 8 | // 0, the stream's first event said slide 1, and the page reloaded — |
| 9 | // and did the same on every load after, about fifty times a second. |
| 10 | package slidestest |
| 11 | |
| 12 | import ( |
| 13 | "fmt" |
| 14 | "regexp" |
| 15 | "sync/atomic" |
| 16 | "testing" |
| 17 | "time" |
| 18 | |
| 19 | "amadan.net/rastrillo/rastrillo/harness" |
| 20 | "github.com/chromedp/cdproto/emulation" |
| 21 | "github.com/chromedp/cdproto/page" |
| 22 | "github.com/chromedp/chromedp" |
| 23 | "github.com/chromedp/chromedp/kb" |
| 24 | ) |
| 25 | |
| 26 | // TestBrowserProjectorFitsTheViewport keeps the projector a stage rather |
| 27 | // than a long document: its slide is bounded by the viewport and its |
| 28 | // controls are overlaid where the presenter can still reach them. |
| 29 | func TestBrowserProjectorFitsTheViewport(t *testing.T) { |
| 30 | rig, served := newRigApp(t, nil) |
| 31 | browserSignIn(t, rig, served.mailer) |
| 32 | |
| 33 | var deck string |
| 34 | rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`)) |
| 35 | if !deckPath.MatchString(deck) { |
| 36 | t.Fatalf("the deck list links no deck: %q", deck) |
| 37 | } |
| 38 | |
| 39 | session, token := browserPresent(t, rig, deck) |
| 40 | |
| 41 | for _, viewport := range []struct{ width, height int }{{1920, 1080}, {1280, 720}} { |
| 42 | t.Run(fmt.Sprintf("%dx%d", viewport.width, viewport.height), func(t *testing.T) { |
| 43 | var geometry struct { |
| 44 | ScrollHeight float64 `json:"scrollHeight"` |
| 45 | Viewport float64 `json:"viewport"` |
| 46 | ScrollY float64 `json:"scrollY"` |
| 47 | Top float64 `json:"top"` |
| 48 | Bottom float64 `json:"bottom"` |
| 49 | } |
| 50 | rig.Run( |
| 51 | cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: int64(viewport.width), Height: int64(viewport.height), DeviceScaleFactor: 1, Mobile: false}), |
| 52 | chromedp.Navigate(rig.Origin+session+"?t="+token), |
| 53 | chromedp.WaitVisible(chromedp.CSS(`[slides-bar]`)), |
| 54 | evalInto(&geometry, `(() => { |
| 55 | window.scrollTo(0, 1); |
| 56 | const controls = document.querySelector('[slides-bar]').getBoundingClientRect(); |
| 57 | return { |
| 58 | scrollHeight: document.documentElement.scrollHeight, |
| 59 | viewport: window.innerHeight, |
| 60 | scrollY: window.scrollY, |
| 61 | top: controls.top, |
| 62 | bottom: controls.bottom, |
| 63 | }; |
| 64 | })()`), |
| 65 | ) |
| 66 | if geometry.ScrollHeight > geometry.Viewport || geometry.ScrollY != 0 { |
| 67 | t.Errorf("projector scrolls: document %.0fpx, viewport %.0fpx, scrollY %.0f", geometry.ScrollHeight, geometry.Viewport, geometry.ScrollY) |
| 68 | } |
| 69 | if geometry.Top < 0 || geometry.Bottom > geometry.Viewport { |
| 70 | t.Errorf("controls are outside the viewport: top %.0fpx, bottom %.0fpx, viewport %.0fpx", geometry.Top, geometry.Bottom, geometry.Viewport) |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // sessionLocation is where Present lands: the session's own slide (S12). |
| 77 | var sessionLocation = regexp.MustCompile(`^/p/[23456789abcdefghjkmnpqrstuvwxyz]{10}$`) |
| 78 | |
| 79 | // browserPresent presses Present on the deck the browser is signed in |
| 80 | // to, waits for the slide it lands on, and pairs a phone as the |
| 81 | // presenter's browser does (POST /p/{session}/pair, signed in). It |
| 82 | // returns the session's path, /p/{session}, and the remote token. |
| 83 | func browserPresent(t *testing.T, rig *harness.Rig, deck string) (string, string) { |
| 84 | t.Helper() |
| 85 | var path, paired string |
| 86 | rig.Run( |
| 87 | chromedp.Navigate(rig.Origin+deck+"?slide=1"), |
| 88 | chromedp.Click(chromedp.CSS(`form[action$="/present"] button`)), |
| 89 | waitFor(`/^\/p\/[^/]+$/.test(location.pathname)`), |
| 90 | evalInto(&path, `location.pathname + location.search`), |
| 91 | ) |
| 92 | if !sessionLocation.MatchString(path) { |
| 93 | t.Fatalf("Present landed on %q, want /p/{session}", path) |
| 94 | } |
| 95 | rig.Run(evalInto(&paired, `fetch(location.pathname + '/pair', { method: 'POST' }).then(r => r.text())`, |
| 96 | awaitPromise)) |
| 97 | return path, tokenOn(t, paired) |
| 98 | } |
| 99 | |
| 100 | // TestBrowserPresentingScreensHoldStill: the presenter view and the |
| 101 | // phone remote each load once and then stay put while nothing |
| 102 | // changes. When the slide moves the remote reloads exactly once, which |
| 103 | // is how it follows a talk (present.js reloads it rather than |
| 104 | // re-rendering the slide itself); the presenter window of a session |
| 105 | // with a position moves in place, from the talk it holds, and never |
| 106 | // reloads (fixtures/presenter-window.mjs). |
| 107 | func TestBrowserPresentingScreensHoldStill(t *testing.T) { |
| 108 | rig, served := newRigApp(t, nil) |
| 109 | browserSignIn(t, rig, served.mailer) |
| 110 | |
| 111 | // Every top-level navigation the tab makes, reloads included. |
| 112 | var loads atomic.Int64 |
| 113 | harness.Listen(rig, rig.Context(), page.FrameNavigated, func(e *page.EventFrameNavigated) { |
| 114 | if e.Frame.ParentID == "" { |
| 115 | loads.Add(1) |
| 116 | } |
| 117 | }) |
| 118 | |
| 119 | var deck string |
| 120 | rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`)) |
| 121 | if !deckPath.MatchString(deck) { |
| 122 | t.Fatalf("the deck list links no deck: %q", deck) |
| 123 | } |
| 124 | |
| 125 | session, token := browserPresent(t, rig, deck) |
| 126 | rig.Run(chromedp.Navigate(rig.Origin + session + "/notes?t=" + token)) |
| 127 | |
| 128 | // holdsStill waits for the page to settle and the stream to have |
| 129 | // said its piece (one event on connect), then counts loads over a |
| 130 | // window long enough for the broken page to have reloaded hundreds |
| 131 | // of times. |
| 132 | holdsStill := func(name string) { |
| 133 | t.Helper() |
| 134 | rig.Run(chromedp.Sleep(1500 * time.Millisecond)) |
| 135 | before := loads.Load() |
| 136 | rig.Run(chromedp.Sleep(3 * time.Second)) |
| 137 | if n := loads.Load() - before; n != 0 { |
| 138 | t.Errorf("the %s loaded %d more times in 3s with nothing changing, want 0", name, n) |
| 139 | } |
| 140 | // And it holds still because it knows where it is, not only |
| 141 | // because present.js refuses to reload twice for one slide: |
| 142 | // the script's root names the slide the counter shows. |
| 143 | var marked, counter string |
| 144 | rig.Run( |
| 145 | evalInto(&marked, `document.querySelector('[data-session]')?.getAttribute('data-slide') ?? 'missing'`), |
| 146 | evalInto(&counter, `(document.querySelector('[slides-counter]')?.textContent ?? '').trim().split(' ')[0]`), |
| 147 | ) |
| 148 | if marked != counter { |
| 149 | t.Errorf("the %s's script root says data-slide=%q; the counter shows slide %q", name, marked, counter) |
| 150 | } |
| 151 | } |
| 152 | // movesOnce presses the right arrow — the page posts `next` — and |
| 153 | // the page must show the new slide having loaded exactly loads |
| 154 | // times. |
| 155 | movesOnce := func(name, counter string, want int64) { |
| 156 | t.Helper() |
| 157 | before := loads.Load() |
| 158 | rig.Run( |
| 159 | chromedp.KeyEvent(kb.ArrowRight), |
| 160 | waitFor(`document.querySelector('[slides-counter]')?.textContent.trim().startsWith('`+counter+` /')`), |
| 161 | chromedp.Sleep(3*time.Second), |
| 162 | ) |
| 163 | if n := loads.Load() - before; n != want { |
| 164 | t.Errorf("the %s loaded %d times after one slide change, want exactly %d", name, n, want) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | holdsStill("presenter view") |
| 169 | rig.Screen("body", "presenter view") |
| 170 | movesOnce("presenter view", "2", 0) |
| 171 | |
| 172 | rig.Run(chromedp.Navigate(rig.Origin + session + "/remote?t=" + token)) |
| 173 | holdsStill("remote") |
| 174 | rig.Screen("body", "remote") |
| 175 | movesOnce("remote", "3", 1) |
| 176 | } |
| 177 | |