| new file mode 100644 |
| index 0000000..9a4912b |
| --- /dev/null |
| +++ b/internal/slidestest/talk_readers_test.go |
| @@ -0,0 +1,212 @@ |
| +package slidestest |
| + |
| +import ( |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// talkSlide is one slide of a talk-marked deck: its id, how it is |
| +// marked ("" is core), and the words that identify it wherever they |
| +// leak. |
| +type talkSlide struct { |
| + id, talk, marker string |
| +} |
| + |
| +// markedDeck makes a deck over the API whose slides carry a marker word |
| +// in their heading and their notes. It returns the deck's path, its sqid |
| +// and the agent token. |
| +func markedDeck(t *testing.T, a *testApp, cookies []*http.Cookie, slides []talkSlide) (path, sqid, token string) { |
| + t.Helper() |
| + token = mintAgent(t, a, cookies, "marked-deck") |
| + sqid = apiDeck(t, a, token, "Marked") |
| + view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| + var ops []string |
| + list, _ := view["slides"].([]any) |
| + for _, s := range list { |
| + id, _ := s.(map[string]any)["id"].(string) |
| + ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`) |
| + } |
| + for _, s := range slides { |
| + ops = append(ops, |
| + `{"op":"add_slide","id":`+quote(s.id)+`,"layout":"flow","template":"title-body"}`, |
| + `{"op":"add_block","slide":`+quote(s.id)+`,"id":`+quote("h"+s.id)+`,"type":"heading","region":"heading","markdown":`+quote(s.marker)+`,"level":1}`, |
| + `{"op":"set_notes","id":`+quote(s.id)+`,"markdown":`+quote("notes "+s.marker)+`}`) |
| + if s.talk != "" { |
| + ops = append(ops, `{"op":"set_slide","id":`+quote(s.id)+`,"talk":`+quote(s.talk)+`}`) |
| + } |
| + } |
| + apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| + return "/d/" + sqid, sqid, token |
| +} |
| + |
| +var mixedTalk = []talkSlide{ |
| + {"pone", "parked", "PARKEDONE"}, |
| + {"stwo", "", "SHOWNTWO"}, |
| + {"fthree", "full", "FULLTHREE"}, |
| + {"pfour", "parked", "PARKEDFOUR"}, |
| + {"sfive", "", "SHOWNFIVE"}, |
| +} |
| + |
| +// pageCount is the pages in a PDF, either spelling of the entry. |
| +func pageCount(pdf string) int { |
| + n := func(entry string) int { |
| + return strings.Count(pdf, entry) + strings.Count(pdf, strings.Replace(entry, " /", "/", 1)) |
| + } |
| + return n("/Type /Page") - n("/Type /Pages") |
| +} |
| + |
| +// exportPDF runs the export flow and returns the PDF. |
| +func exportPDF(t *testing.T, a *testApp, deck string, cookies []*http.Cookie) string { |
| + t.Helper() |
| + status := startExport(t, a, deck, cookies, url.Values{}) |
| + fragment := waitForExport(t, a, status, cookies) |
| + if strings.Contains(fragment, "rst-tone=\"negative\"") { |
| + t.Fatalf("the export failed:\n%s", fragment) |
| + } |
| + return mustGet(t, a, status+".pdf", http.StatusOK, cookies).Body.String() |
| +} |
| + |
| +// TestAParkedSlideReachesNoReader: every place a reader looks leaves |
| +// the parked slides out, and the numbers they see count only what is |
| +// there. |
| +func TestAParkedSlideReachesNoReader(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + path, _, _ := markedDeck(t, a, cookies, mixedTalk) |
| + share := "/s/" + mintShareLink(t, a, path, cookies) |
| + session, _ := startPresenting(t, a, path, cookies) |
| + |
| + cases := []struct { |
| + name string |
| + target string |
| + want int |
| + cookies []*http.Cookie |
| + shows string // present when the page is 200 |
| + }{ |
| + {"viewer first slide", path + "?slide=1", 200, cookies, "SHOWNTWO"}, |
| + {"viewer second slide", path + "?slide=2", 200, cookies, "FULLTHREE"}, |
| + {"viewer past the end", path + "?slide=4", 404, cookies, ""}, |
| + {"share link", share, 200, nil, "SHOWNTWO"}, |
| + {"share link past the end", share + "?slide=4", 404, nil, ""}, |
| + {"notes page", path + "/notes", 200, cookies, "notes SHOWNFIVE"}, |
| + {"projector", "/p/" + session, 200, cookies, "SHOWNTWO"}, |
| + {"projector past the end", "/p/" + session + "?slide=4", 404, cookies, ""}, |
| + {"presenter notes", "/p/" + session + "/notes", 200, cookies, "SHOWNTWO"}, |
| + {"follow link", share + "?follow=1", 200, nil, "SHOWNTWO"}, |
| + {"follow past the end", share + "?follow=1&slide=4", 404, nil, ""}, |
| + {"share thumbnail of a parked slide", share + "/slides/pone.svg", 404, nil, ""}, |
| + {"share thumbnail of a shown slide", share + "/slides/stwo.svg", 200, nil, ""}, |
| + {"session thumbnail of a parked slide", "/p/" + session + "/slides/pfour.svg", 404, nil, ""}, |
| + {"session thumbnail of a shown slide", "/p/" + session + "/slides/sfive.svg", 200, nil, ""}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + rec := get(t, a, c.target, c.cookies...) |
| + if rec.Code != c.want { |
| + t.Fatalf("GET %s: status %d, want %d\n%s", c.target, rec.Code, c.want, rec.Body) |
| + } |
| + body := rec.Body.String() |
| + if rec.Code == 200 && c.shows != "" && !strings.Contains(body, c.shows) { |
| + t.Errorf("GET %s does not show %q:\n%s", c.target, c.shows, body) |
| + } |
| + if strings.Contains(body, "PARKED") || strings.Contains(body, "/pone.svg") || strings.Contains(body, "/pfour.svg") { |
| + t.Errorf("GET %s carries a parked slide:\n%s", c.target, body) |
| + } |
| + }) |
| + } |
| + |
| + t.Run("the counter counts what is there", func(t *testing.T) { |
| + page := mustGet(t, a, share+"?slide=3", http.StatusOK, nil).Body.String() |
| + if !strings.Contains(page, "SHOWNFIVE") || !strings.Contains(page, "3 of 3") { |
| + t.Errorf("the third slide over a share link is not SHOWNFIVE of 3:\n%s", page) |
| + } |
| + }) |
| + |
| + t.Run("pdf", func(t *testing.T) { |
| + if got := pageCount(exportPDF(t, a, path, cookies)); got != 3 { |
| + t.Errorf("the PDF has %d pages, want 3 (the parked slides are left out)", got) |
| + } |
| + }) |
| +} |
| + |
| +// TestAFullOnlySlideShowsToReaders: full only is a way of giving the |
| +// talk, not a way of hiding a slide: readers get it. |
| +func TestAFullOnlySlideShowsToReaders(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + path, _, _ := markedDeck(t, a, cookies, mixedTalk) |
| + share := "/s/" + mintShareLink(t, a, path, cookies) |
| + if page := mustGet(t, a, share+"?slide=2", http.StatusOK, nil).Body.String(); !strings.Contains(page, "FULLTHREE") { |
| + t.Errorf("a share link does not show the full-only slide:\n%s", page) |
| + } |
| + if rec := get(t, a, share+"/slides/fthree.svg"); rec.Code != http.StatusOK { |
| + t.Errorf("the full-only slide's thumbnail over a share link: status %d, want 200", rec.Code) |
| + } |
| +} |
| + |
| +// TestTheEditorAndAPIStillDescribeAParkedSlide: people with access to |
| +// the deck still see everything in it. |
| +func TestTheEditorAndAPIStillDescribeAParkedSlide(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + path, sqid, token := markedDeck(t, a, cookies, mixedTalk) |
| + |
| + if rec := get(t, a, path+"/slides/pone.svg", cookies...); rec.Code != http.StatusOK { |
| + t.Errorf("the editor's thumbnail of a parked slide: status %d, want 200", rec.Code) |
| + } |
| + editor := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String() |
| + if !strings.Contains(editor, "pone") { |
| + t.Errorf("the editor does not describe the parked slide:\n%s", editor) |
| + } |
| + view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| + if got := mustJSON(t, view["slides"]); !strings.Contains(got, "pone") || !strings.Contains(got, "pfour") { |
| + t.Errorf("the API deck leaves a parked slide out:\n%s", got) |
| + } |
| + if rec := send(t, a, bearerRequest(t, http.MethodGet, "/api/v1/decks/"+sqid+"/slides/pone.svg", token, "")); rec.Code != http.StatusOK { |
| + t.Errorf("the API's thumbnail of a parked slide: status %d, want 200\n%s", rec.Code, rec.Body) |
| + } |
| +} |
| + |
| +// TestEveryParkedDeckIsEmptyToReaders: a deck with nothing but parked |
| +// slides is, for a reader, the deck with nothing in it. |
| +func TestEveryParkedDeckIsEmptyToReaders(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + path, _, _ := markedDeck(t, a, cookies, []talkSlide{ |
| + {"pone", "parked", "PARKEDONE"}, |
| + {"ptwo", "parked", "PARKEDTWO"}, |
| + }) |
| + share := "/s/" + mintShareLink(t, a, path, cookies) |
| + |
| + for _, target := range []string{share, path + "?slide=1", path + "/notes"} { |
| + rec := get(t, a, target, cookies...) |
| + if target == share { |
| + rec = get(t, a, target) |
| + } |
| + body := rec.Body.String() |
| + if rec.Code != http.StatusOK || strings.Contains(body, "PARKED") || strings.Contains(body, "<section slides-slide") { |
| + t.Errorf("GET %s: status %d, want an empty page with no slide:\n%s", target, rec.Code, body) |
| + } |
| + } |
| + // An empty deck answers any slide with its empty page, and so does this. |
| + if rec := get(t, a, share+"?slide=1"); rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), "<section slides-slide") { |
| + t.Errorf("slide 1 of an all-parked deck over a share link: status %d, want the empty page", rec.Code) |
| + } |
| +} |
| + |
| +// TestACoverSkipsAParkedFirstSlide: the deck list's cover is the first |
| +// slide that is not parked. |
| +func TestACoverSkipsAParkedFirstSlide(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + _, sqid, _ := markedDeck(t, a, cookies, mixedTalk) |
| + list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| + if !strings.Contains(list, "/d/"+sqid+"/slides/stwo.svg") { |
| + t.Errorf("the list's cover for the deck is not its first unparked slide:\n%s", list) |
| + } |
| + if strings.Contains(list, "/d/"+sqid+"/slides/pone.svg") { |
| + t.Errorf("the list's cover is a parked slide:\n%s", list) |
| + } |
| +} |