package slidestest import ( "net/http" "net/http/httptest" "net/url" "regexp" "strconv" "strings" "testing" ) // Steps on the three screens that show the talk as it stands: the // projector, the audience's follow view and the phone remote (Task 6). // stepsDeck's first slide, "steps", has four boxes, one per press. // controlForm is a rendered control form: its action and its fields. type controlForm struct { action string fields url.Values } var ( formBlock = regexp.MustCompile(`(?s)
`) formInput = regexp.MustCompile(``) waitingBox = regexp.MustCompile(`<[^>]*\bdata-press="(\d+)"[^>]*\bdata-waiting\b[^>]*>`) pressedBox = regexp.MustCompile(`\bdata-press="\d+"`) ) // formFor finds the control form for op on a page. func formFor(t *testing.T, page, op string) controlForm { t.Helper() for _, m := range formBlock.FindAllStringSubmatch(page, -1) { fields := url.Values{} for _, in := range formInput.FindAllStringSubmatch(m[2], -1) { fields.Set(in[1], strings.ReplaceAll(in[2], "&", "&")) } if fields.Get("op") == op { return controlForm{action: strings.ReplaceAll(m[1], "&", "&"), fields: fields} } } t.Fatalf("no %q form on the page:\n%s", op, page) return controlForm{} } // submit posts a form as a browser with scripts off does: the fields // as rendered, the page it is on as the Referer. func submit(t *testing.T, a *testApp, f controlForm, referer string) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(http.MethodPost, f.action, strings.NewReader(f.fields.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Origin", testOrigin) req.Header.Set("Sec-Fetch-Site", "same-origin") req.Header.Set("Referer", testOrigin+referer) rec := httptest.NewRecorder() a.ServeHTTP(rec, req) return rec } // landing is where a scriptless press sends the browser: same origin, // and carrying slide and step. It returns the path and query. func landing(t *testing.T, rec *httptest.ResponseRecorder) (string, url.Values) { t.Helper() if rec.Code != http.StatusSeeOther { t.Fatalf("a scriptless press: status %d, want 303\n%s", rec.Code, rec.Body) } u, err := url.Parse(rec.Header().Get("Location")) if err != nil { t.Fatalf("the press's Location %q: %v", rec.Header().Get("Location"), err) } q := u.Query() if q.Get("slide") == "" || q.Get("step") == "" { t.Fatalf("a scriptless press redirected to %q, which says no slide and step", u) } return u.RequestURI(), q } // waitingPresses is the press numbers still waiting on a page. func waitingPresses(page string) []string { var out []string for _, m := range waitingBox.FindAllStringSubmatch(page, -1) { out = append(out, m[1]) } return out } // TestScriptlessPressWalksEveryStep: with scripts off, Next on the // projector is a form; each one posts, comes back to a page that says // which slide and step, and that page shows one press more, until the // slide is built and the next one takes the talk to the next slide. func TestScriptlessPressWalksEveryStep(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) at := "/p/" + session + "?t=" + url.QueryEscape(token) page := mustGet(t, a, at, http.StatusOK, nil).Body.String() if got := waitingPresses(page); strings.Join(got, ",") != "1,2,3,4" { t.Fatalf("the projector at the start: waiting presses %v, want 1,2,3,4", got) } if !strings.Contains(page, `slides-op="next">Next<`) || !strings.Contains(page, `slides-op="prev">Back<`) { t.Errorf("the projector's press buttons are not called Next and Back:\n%s", page) } for step := 1; step <= 4; step++ { next, q := landing(t, submit(t, a, formFor(t, page, "next"), at)) if q.Get("slide") != "1" || q.Get("step") != strconv.Itoa(step) { t.Fatalf("next %d landed on slide %s step %s, want slide 1 step %d", step, q.Get("slide"), q.Get("step"), step) } at = next page = mustGet(t, a, at, http.StatusOK, nil).Body.String() if got := waitingPresses(page); len(got) != 4-step { t.Fatalf("after %d presses %d boxes wait (%v), want %d", step, len(got), got, 4-step) } if !strings.Contains(page, `data-step="`+strconv.Itoa(step)+`"`) { t.Errorf("after %d presses the surface does not say data-step=%q", step, strconv.Itoa(step)) } } next, q := landing(t, submit(t, a, formFor(t, page, "next"), at)) if q.Get("slide") != "2" || q.Get("step") != "0" { t.Fatalf("the fifth next landed on slide %s step %s, want slide 2 step 0", q.Get("slide"), q.Get("step")) } page = mustGet(t, a, next, http.StatusOK, nil).Body.String() if !strings.Contains(page, `data-slide="2"`) || !strings.Contains(page, `data-id="plain"`) { t.Fatalf("the fifth next did not show the next slide:\n%s", page) } // The counter counts through the talk being given: the short one // has steps, plain and last. if !strings.Contains(page, "2 / 3") { t.Errorf("the counter on the second slide of a three-slide talk does not say 2 / 3:\n%s", page) } } // TestAResubmittedPressCountsOnce: the same form posted twice (a reload // that resubmits, a double click with scripts off) is one press. func TestAResubmittedPressCountsOnce(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) at := "/p/" + session + "?t=" + url.QueryEscape(token) form := formFor(t, mustGet(t, a, at, http.StatusOK, nil).Body.String(), "next") first, _ := landing(t, submit(t, a, form, at)) again, _ := landing(t, submit(t, a, form, at)) if _, step, _ := rowPosition(t, a, session); step != 1 { t.Errorf("one press sent twice left the talk at step %d, want 1", step) } if first != again { t.Errorf("the resubmitted press landed on %q, the first on %q", again, first) } } // TestFollowDrawsTheCurrentStep: the audience's follow view draws the // talk at its step, not the finished slide, and ?slide=N&step=k draws // that state, its fragment included. func TestFollowDrawsTheCurrentStep(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := stepsDeck(t, a, cookies) session, token := startPresenting(t, a, path, cookies) share := mintShareLink(t, a, path, cookies) press(t, a, session, token, op(t, "next"), nil) press(t, a, session, token, op(t, "next"), nil) follow := mustGet(t, a, "/s/"+share+"?follow=1", http.StatusOK, nil).Body.String() if got := waitingPresses(follow); strings.Join(got, ",") != "3,4" { t.Errorf("the follow view at step 2 has presses %v waiting, want 3,4", got) } if !strings.Contains(follow, `data-step="2"`) { t.Errorf("the follow view's surface does not say data-step=\"2\"") } named := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=3", http.StatusOK, nil).Body.String() if got := waitingPresses(named); strings.Join(got, ",") != "4" { t.Errorf("?slide=1&step=3 has presses %v waiting, want 4", got) } fragment := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=1&fragment=1", http.StatusOK, nil).Body.String() if got := waitingPresses(fragment); strings.Join(got, ",") != "2,3,4" { t.Errorf("the fragment at step 1 has presses %v waiting, want 2,3,4", got) } // A step past the slide's presses is the finished slide, still // marked with its presses so a step back can apply in place. over := mustGet(t, a, "/s/"+share+"?follow=1&slide=1&step=99", http.StatusOK, nil).Body.String() if got := waitingPresses(over); len(got) != 0 || len(pressedBox.FindAllString(over, -1)) != 4 { t.Errorf("step 99 has %v waiting and %d marked presses, want none waiting and 4 marked", got, len(pressedBox.FindAllString(over, -1))) } } // TestRemoteSaysWhichStep: the phone says which step under where the // talk is, on a slide with presses only; its picture stays the // finished slide (P3). func TestRemoteSaysWhichStep(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) remote := "/p/" + session + "/remote?t=" + url.QueryEscape(token) // The picture of the slide on screen is the finished slide whatever // the step (P3): the same address at step 0 and at step 2. finished := "/p/" + session + "/slides/steps.svg" if got, _ := pictures(mustGet(t, a, remote, http.StatusOK, nil).Body.String()); got != finished { t.Errorf("the remote at step 0 pictures %q, want %q", got, finished) } press(t, a, session, token, op(t, "next"), nil) press(t, a, session, token, op(t, "next"), nil) page := mustGet(t, a, remote, http.StatusOK, nil).Body.String() if got, _ := pictures(page); got != finished { t.Errorf("the remote at step 2 pictures %q, want the finished slide, %q", got, finished) } if !strings.Contains(page, "step 2 of 4") { t.Errorf("the remote at step 2 of the steps slide does not say step 2 of 4:\n%s", page) } if !strings.Contains(page, `slides-op="next">Next<`) || !strings.Contains(page, `slides-op="prev">Back<`) { t.Errorf("the remote's press buttons are not called Next and Back:\n%s", page) } for range 3 { press(t, a, session, token, op(t, "next"), nil) } page = mustGet(t, a, remote, http.StatusOK, nil).Body.String() if !strings.Contains(page, `data-slide="2"`) { t.Fatalf("five presses did not reach the plain slide:\n%s", page) } if strings.Contains(page, "step ") { t.Errorf("the remote names a step on a slide with no presses:\n%s", page) } } var thumbSrc = regexp.MustCompile(`