| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // Steps on the three screens that show the talk as it stands: the |
| 14 | // projector, the audience's follow view and the phone remote (Task 6). |
| 15 | // stepsDeck's first slide, "steps", has four boxes, one per press. |
| 16 | |
| 17 | // controlForm is a rendered control form: its action and its fields. |
| 18 | type controlForm struct { |
| 19 | action string |
| 20 | fields url.Values |
| 21 | } |
| 22 | |
| 23 | var ( |
| 24 | formBlock = regexp.MustCompile(`(?s)<form method="post" action="([^"]+)"[^>]*>(.*?)</form>`) |
| 25 | formInput = regexp.MustCompile(`<input type="hidden" name="([^"]+)" value="([^"]*)">`) |
| 26 | waitingBox = regexp.MustCompile(`<[^>]*\bdata-press="(\d+)"[^>]*\bdata-waiting\b[^>]*>`) |
| 27 | pressedBox = regexp.MustCompile(`\bdata-press="\d+"`) |
| 28 | ) |
| 29 | |
| 30 | // formFor finds the control form for op on a page. |
| 31 | func formFor(t *testing.T, page, op string) controlForm { |
| 32 | t.Helper() |
| 33 | for _, m := range formBlock.FindAllStringSubmatch(page, -1) { |
| 34 | fields := url.Values{} |
| 35 | for _, in := range formInput.FindAllStringSubmatch(m[2], -1) { |
| 36 | fields.Set(in[1], strings.ReplaceAll(in[2], "&", "&")) |
| 37 | } |
| 38 | if fields.Get("op") == op { |
| 39 | return controlForm{action: strings.ReplaceAll(m[1], "&", "&"), fields: fields} |
| 40 | } |
| 41 | } |
| 42 | t.Fatalf("no %q form on the page:\n%s", op, page) |
| 43 | return controlForm{} |
| 44 | } |
| 45 | |
| 46 | // submit posts a form as a browser with scripts off does: the fields |
| 47 | // as rendered, the page it is on as the Referer. |
| 48 | func submit(t *testing.T, a *testApp, f controlForm, referer string) *httptest.ResponseRecorder { |
| 49 | t.Helper() |
| 50 | req := httptest.NewRequest(http.MethodPost, f.action, strings.NewReader(f.fields.Encode())) |
| 51 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 52 | req.Header.Set("Origin", testOrigin) |
| 53 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 54 | req.Header.Set("Referer", testOrigin+referer) |
| 55 | rec := httptest.NewRecorder() |
| 56 | a.ServeHTTP(rec, req) |
| 57 | return rec |
| 58 | } |
| 59 | |
| 60 | // landing is where a scriptless press sends the browser: same origin, |
| 61 | // and carrying slide and step. It returns the path and query. |
| 62 | func landing(t *testing.T, rec *httptest.ResponseRecorder) (string, url.Values) { |
| 63 | t.Helper() |
| 64 | if rec.Code != http.StatusSeeOther { |
| 65 | t.Fatalf("a scriptless press: status %d, want 303\n%s", rec.Code, rec.Body) |
| 66 | } |
| 67 | u, err := url.Parse(rec.Header().Get("Location")) |
| 68 | if err != nil { |
| 69 | t.Fatalf("the press's Location %q: %v", rec.Header().Get("Location"), err) |
| 70 | } |
| 71 | q := u.Query() |
| 72 | if q.Get("slide") == "" || q.Get("step") == "" { |
| 73 | t.Fatalf("a scriptless press redirected to %q, which says no slide and step", u) |
| 74 | } |
| 75 | return u.RequestURI(), q |
| 76 | } |
| 77 | |
| 78 | // waitingPresses is the press numbers still waiting on a page. |
| 79 | func waitingPresses(page string) []string { |
| 80 | var out []string |
| 81 | for _, m := range waitingBox.FindAllStringSubmatch(page, -1) { |
| 82 | out = append(out, m[1]) |
| 83 | } |
| 84 | return out |
| 85 | } |
| 86 | |
| 87 | // TestScriptlessPressWalksEveryStep: with scripts off, Next on the |
| 88 | // projector is a form; each one posts, comes back to a page that says |
| 89 | // which slide and step, and that page shows one press more, until the |
| 90 | // slide is built and the next one takes the talk to the next slide. |
| 91 | func TestScriptlessPressWalksEveryStep(t *testing.T) { |
| 92 | a := newApp(t) |
| 93 | cookies := signedIn(t, a, allowedAddress) |
| 94 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 95 | |
| 96 | at := "/p/" + session + "?t=" + url.QueryEscape(token) |
| 97 | page := mustGet(t, a, at, http.StatusOK, nil).Body.String() |
| 98 | if got := waitingPresses(page); strings.Join(got, ",") != "1,2,3,4" { |
| 99 | t.Fatalf("the projector at the start: waiting presses %v, want 1,2,3,4", got) |
| 100 | } |
| 101 | if !strings.Contains(page, `slides-op="next">Next<`) || !strings.Contains(page, `slides-op="prev">Back<`) { |
| 102 | t.Errorf("the projector's press buttons are not called Next and Back:\n%s", page) |
| 103 | } |
| 104 | for step := 1; step <= 4; step++ { |
| 105 | next, q := landing(t, submit(t, a, formFor(t, page, "next"), at)) |
| 106 | if q.Get("slide") != "1" || q.Get("step") != strconv.Itoa(step) { |
| 107 | t.Fatalf("next %d landed on slide %s step %s, want slide 1 step %d", step, q.Get("slide"), q.Get("step"), step) |
| 108 | } |
| 109 | at = next |
| 110 | page = mustGet(t, a, at, http.StatusOK, nil).Body.String() |
| 111 | if got := waitingPresses(page); len(got) != 4-step { |
| 112 | t.Fatalf("after %d presses %d boxes wait (%v), want %d", step, len(got), got, 4-step) |
| 113 | } |
| 114 | if !strings.Contains(page, `data-step="`+strconv.Itoa(step)+`"`) { |
| 115 | t.Errorf("after %d presses the surface does not say data-step=%q", step, strconv.Itoa(step)) |
| 116 | } |
| 117 | } |
| 118 | next, q := landing(t, submit(t, a, formFor(t, page, "next"), at)) |
| 119 | if q.Get("slide") != "2" || q.Get("step") != "0" { |
| 120 | t.Fatalf("the fifth next landed on slide %s step %s, want slide 2 step 0", q.Get("slide"), q.Get("step")) |
| 121 | } |
| 122 | page = mustGet(t, a, next, http.StatusOK, nil).Body.String() |
| 123 | if !strings.Contains(page, `data-slide="2"`) || !strings.Contains(page, `data-id="plain"`) { |
| 124 | t.Fatalf("the fifth next did not show the next slide:\n%s", page) |
| 125 | } |
| 126 | // The counter counts through the talk being given: the short one |
| 127 | // has steps, plain and last. |
| 128 | if !strings.Contains(page, "2 / 3") { |
| 129 | t.Errorf("the counter on the second slide of a three-slide talk does not say 2 / 3:\n%s", page) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // TestAResubmittedPressCountsOnce: the same form posted twice (a reload |
| 134 | // that resubmits, a double click with scripts off) is one press. |
| 135 | func TestAResubmittedPressCountsOnce(t *testing.T) { |
| 136 | a := newApp(t) |
| 137 | cookies := signedIn(t, a, allowedAddress) |
| 138 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 139 | |
| 140 | at := "/p/" + session + "?t=" + url.QueryEscape(token) |
| 141 | form := formFor(t, mustGet(t, a, at, http.StatusOK, nil).Body.String(), "next") |
| 142 | first, _ := landing(t, submit(t, a, form, at)) |
| 143 | again, _ := landing(t, submit(t, a, form, at)) |
| 144 | if _, step, _ := rowPosition(t, a, session); step != 1 { |
| 145 | t.Errorf("one press sent twice left the talk at step %d, want 1", step) |
| 146 | } |
| 147 | if first != again { |
| 148 | t.Errorf("the resubmitted press landed on %q, the first on %q", again, first) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // TestFollowDrawsTheCurrentStep: the audience's follow view draws the |
| 153 | // talk at its step, not the finished slide, and ?slide=N&step=k draws |
| 154 | // that state, its fragment included. |
| 155 | func TestFollowDrawsTheCurrentStep(t *testing.T) { |
| 156 | a := newApp(t) |
| 157 | cookies := signedIn(t, a, allowedAddress) |
| 158 | path := stepsDeck(t, a, cookies) |
| 159 | session, token := startPresenting(t, a, path, cookies) |
| 160 | share := mintShareLink(t, a, path, cookies) |
| 161 | |
| 162 | press(t, a, session, token, op(t, "next"), nil) |
| 163 | press(t, a, session, token, op(t, "next"), nil) |
| 164 | |
| 165 | follow := mustGet(t, a, "/s/"+share+"?follow=1", http.StatusOK, nil).Body.String() |
| 166 | if got := waitingPresses(follow); strings.Join(got, ",") != "3,4" { |
| 167 | t.Errorf("the follow view at step 2 has presses %v waiting, want 3,4", got) |
| 168 | } |
| 169 | if !strings.Contains(follow, `data-step="2"`) { |
| 170 | t.Errorf("the follow view's surface does not say data-step=\"2\"") |
| 171 | } |
| 172 | |
| 173 | named := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=3", http.StatusOK, nil).Body.String() |
| 174 | if got := waitingPresses(named); strings.Join(got, ",") != "4" { |
| 175 | t.Errorf("?slide=1&step=3 has presses %v waiting, want 4", got) |
| 176 | } |
| 177 | fragment := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=1&fragment=1", http.StatusOK, nil).Body.String() |
| 178 | if got := waitingPresses(fragment); strings.Join(got, ",") != "2,3,4" { |
| 179 | t.Errorf("the fragment at step 1 has presses %v waiting, want 2,3,4", got) |
| 180 | } |
| 181 | // A step past the slide's presses is the finished slide, still |
| 182 | // marked with its presses so a step back can apply in place. |
| 183 | over := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=99", http.StatusOK, nil).Body.String() |
| 184 | if got := waitingPresses(over); len(got) != 0 || len(pressedBox.FindAllString(over, -1)) != 4 { |
| 185 | t.Errorf("step 99 has %v waiting and %d marked presses, want none waiting and 4 marked", got, len(pressedBox.FindAllString(over, -1))) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // TestRemoteSaysWhichStep: the phone says which step under where the |
| 190 | // talk is, on a slide with presses only; its picture stays the |
| 191 | // finished slide (P3). |
| 192 | func TestRemoteSaysWhichStep(t *testing.T) { |
| 193 | a := newApp(t) |
| 194 | cookies := signedIn(t, a, allowedAddress) |
| 195 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 196 | remote := "/p/" + session + "/remote?t=" + url.QueryEscape(token) |
| 197 | |
| 198 | // The picture of the slide on screen is the finished slide whatever |
| 199 | // the step (P3): the same address at step 0 and at step 2. |
| 200 | finished := "/p/" + session + "/slides/steps.svg" |
| 201 | if got, _ := pictures(mustGet(t, a, remote, http.StatusOK, nil).Body.String()); got != finished { |
| 202 | t.Errorf("the remote at step 0 pictures %q, want %q", got, finished) |
| 203 | } |
| 204 | press(t, a, session, token, op(t, "next"), nil) |
| 205 | press(t, a, session, token, op(t, "next"), nil) |
| 206 | page := mustGet(t, a, remote, http.StatusOK, nil).Body.String() |
| 207 | if got, _ := pictures(page); got != finished { |
| 208 | t.Errorf("the remote at step 2 pictures %q, want the finished slide, %q", got, finished) |
| 209 | } |
| 210 | if !strings.Contains(page, "step 2 of 4") { |
| 211 | t.Errorf("the remote at step 2 of the steps slide does not say step 2 of 4:\n%s", page) |
| 212 | } |
| 213 | if !strings.Contains(page, `slides-op="next">Next<`) || !strings.Contains(page, `slides-op="prev">Back<`) { |
| 214 | t.Errorf("the remote's press buttons are not called Next and Back:\n%s", page) |
| 215 | } |
| 216 | |
| 217 | for range 3 { |
| 218 | press(t, a, session, token, op(t, "next"), nil) |
| 219 | } |
| 220 | page = mustGet(t, a, remote, http.StatusOK, nil).Body.String() |
| 221 | if !strings.Contains(page, `data-slide="2"`) { |
| 222 | t.Fatalf("five presses did not reach the plain slide:\n%s", page) |
| 223 | } |
| 224 | if strings.Contains(page, "step ") { |
| 225 | t.Errorf("the remote names a step on a slide with no presses:\n%s", page) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | var thumbSrc = regexp.MustCompile(`<img slides-thumb-img src="([^"]+)"`) |
| 230 | |
| 231 | // pictures is the remote's or the presenter view's two pictures: the |
| 232 | // slide on screen and the one coming next ("" when there is none). |
| 233 | func pictures(page string) (current, next string) { |
| 234 | m := thumbSrc.FindAllStringSubmatch(page, -1) |
| 235 | if len(m) > 0 { |
| 236 | current = m[0][1] |
| 237 | } |
| 238 | if len(m) > 1 { |
| 239 | next = m[1][1] |
| 240 | } |
| 241 | return current, next |
| 242 | } |
| 243 | |
| 244 | var panelSlide = regexp.MustCompile(`(?s)<div presenter-panel="(now|next)"[^>]*>.*?<section slides-slide[^>]*data-id="([^"]+)"`) |
| 245 | |
| 246 | // panelSlides is the presenter window's two slides, named as pictures |
| 247 | // would be, so one comparison serves both screens. |
| 248 | func panelSlides(session, page string) (current, next string) { |
| 249 | for _, m := range panelSlide.FindAllStringSubmatch(page, -1) { |
| 250 | name := "/p/" + session + "/slides/" + m[2] + ".svg" |
| 251 | if m[1] == "now" { |
| 252 | current = name |
| 253 | } else { |
| 254 | next = name |
| 255 | } |
| 256 | } |
| 257 | return current, next |
| 258 | } |
| 259 | |
| 260 | // TestComingNextIsInTheTalkBeingGiven: the slide the remote and the |
| 261 | // presenter view show coming next is the next one of the talk being |
| 262 | // given, so the short version never previews a full-only slide, and |
| 263 | // switching to the full version brings it in; the counter counts the |
| 264 | // same talk. |
| 265 | func TestComingNextIsInTheTalkBeingGiven(t *testing.T) { |
| 266 | a := newApp(t) |
| 267 | cookies := signedIn(t, a, allowedAddress) |
| 268 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 269 | for range 5 { |
| 270 | press(t, a, session, token, op(t, "next"), nil) |
| 271 | } |
| 272 | for _, c := range []struct{ mode, next, counter string }{ |
| 273 | {"core", "last", "2 / 3"}, |
| 274 | {"full", "extra", "2 / 4"}, |
| 275 | } { |
| 276 | if c.mode == "full" { |
| 277 | press(t, a, session, token, op(t, "mode"), nil) |
| 278 | } |
| 279 | for _, screen := range []string{"remote", "notes"} { |
| 280 | page := mustGet(t, a, "/p/"+session+"/"+screen+"?t="+url.QueryEscape(token), http.StatusOK, nil).Body.String() |
| 281 | current, next := pictures(page) |
| 282 | if screen == "notes" { |
| 283 | // The presenter window draws its slides live (Task 9). |
| 284 | current, next = panelSlides(session, page) |
| 285 | } |
| 286 | if current != "/p/"+session+"/slides/plain.svg" || next != "/p/"+session+"/slides/"+c.next+".svg" { |
| 287 | t.Errorf("%s in %s mode: pictures %q then %q, want plain then %s", screen, c.mode, current, next, c.next) |
| 288 | } |
| 289 | if !strings.Contains(page, c.counter) || !strings.Contains(page, `data-mode="`+c.mode+`"`) { |
| 290 | t.Errorf("%s in %s mode does not say %s and data-mode=%q", screen, c.mode, c.counter, c.mode) |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |