package slidestest
// The presenter window drawn by the server (Task 9): what a Positional
// session's GET /p/{session}/notes serves, read as markup. The browser
// half, which measures the window, is presenter_window_browser_test.go.
import (
"net/http"
"net/url"
"regexp"
"strings"
"testing"
)
// presenterTalk is a session on stepsDeck, with notes on "steps" split
// at the press line and plain notes on "plain".
type presenterTalk struct {
app *testApp
cookies []*http.Cookie
deck string
session, token string
}
// stepsNotes are the notes on "steps": five parts for four presses.
const stepsNotes = "Arrive on PARTZERO.\n\n(Press.)\n\nThen PARTONE.\n\n(Press.)\n\nThen PARTTWO.\n\n(Press.)\n\nThen PARTTHREE.\n\n(Press.)\n\nAnd PARTFOUR."
func newPresenterTalk(t *testing.T) *presenterTalk {
t.Helper()
a := newApp(t)
cookies := signedIn(t, a, allowedAddress)
deck := stepsDeck(t, a, cookies)
setNotes(t, a, cookies, deck, map[string]string{
"steps": stepsNotes,
"plain": "Plain notes with PLAINWORDS in them.",
})
session, token := startPresenting(t, a, deck, cookies)
return &presenterTalk{app: a, cookies: cookies, deck: deck, session: session, token: token}
}
// setNotes writes slides' notes over the API.
func setNotes(t *testing.T, a *testApp, cookies []*http.Cookie, deck string, notes map[string]string) {
t.Helper()
token := mintAgent(t, a, cookies, "notes-writer")
var ops []string
for id, md := range notes {
ops = append(ops, `{"op":"set_notes","id":`+quote(id)+`,"markdown":`+quote(md)+`}`)
}
apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(deck, "/d/")+"/ops", token,
`{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK)
}
// window is the presenter window at query (without the leading ?), read
// by the token holder.
func (pt *presenterTalk) window(t *testing.T, query string, cookies ...*http.Cookie) string {
t.Helper()
target := "/p/" + pt.session + "/notes?t=" + url.QueryEscape(pt.token)
if query != "" {
target += "&" + query
}
return mustGet(t, pt.app, target, http.StatusOK, cookies).Body.String()
}
func (pt *presenterTalk) press(t *testing.T, name string, times int) {
t.Helper()
for range times {
press(t, pt.app, pt.session, pt.token, op(t, name), nil)
}
}
// region is the markup of the first element carrying attr, up to the
// element that follows it in the page: enough to tell which panel holds
// what, without parsing the page.
func region(t *testing.T, page, attr, until string) string {
t.Helper()
start := strings.Index(page, attr)
if start < 0 {
t.Fatalf("no %s on the page:\n%s", attr, page)
}
rest := page[start:]
if end := strings.Index(rest, until); end > 0 {
return rest[:end]
}
return rest
}
func TestPresenterStepCountOnlyWithSteps(t *testing.T) {
pt := newPresenterTalk(t)
page := pt.window(t, "")
if !strings.Contains(page, "presenter-stepcount") || !strings.Contains(page, "step 0 of 4") {
t.Errorf("on a slide with four presses the header has no step count:\n%s", page)
}
pt.press(t, "next", 2)
if page := pt.window(t, ""); !strings.Contains(page, "step 2 of 4") {
t.Errorf("two presses on, the step count does not say step 2 of 4:\n%s", page)
}
// The scriptless address draws its own step too.
if page := pt.window(t, "slide=1&step=3"); !strings.Contains(page, "step 3 of 4") {
t.Errorf("?slide=1&step=3 does not count step 3 of 4:\n%s", page)
}
pt.press(t, "next", 3) // onto "plain", which has no presses
page = pt.window(t, "")
if !strings.Contains(page, `data-id="plain"`) {
t.Fatalf("five presses on, the window is not on the plain slide:\n%s", page)
}
if strings.Contains(page, "presenter-stepcount") || strings.Contains(page, " of 4") {
t.Errorf("a slide with no steps draws a step count:\n%s", page)
}
}
func TestNextPanelNamesWhatComesNext(t *testing.T) {
pt := newPresenterTalk(t)
// At step 1 of four: the same slide, one press on.
page := pt.window(t, "slide=1&step=1")
next := region(t, page, `
]*aria-pressed="true"`).MatchString(page) ||
!regexp.MustCompile(`presenter-notes-fit[^>]*aria-pressed="false"`).MatchString(page) {
t.Errorf("the cookie's choice of Scroll is not pressed in the served page")
}
}
func TestPresenterPrefsRefuseNonsense(t *testing.T) {
pt := newPresenterTalk(t)
for _, bad := range []string{
"",
"garbage",
"fit.24",
"fit.24.0,0,7,8;0,8,7,4", // two panels
"fit.24.0,0,7,8;0,8,7,4;7,0,5,12;0,0,2,2", // four
"wide.24.0,0,7,8;0,8,7,4;7,0,5,12", // not fit or scroll
"fit.25.0,0,7,8;0,8,7,4;7,0,5,12", // odd
"fit.14.0,0,7,8;0,8,7,4;7,0,5,12", // under 16
"fit.50.0,0,7,8;0,8,7,4;7,0,5,12", // over 48
"fit.x.0,0,7,8;0,8,7,4;7,0,5,12", // not a number
"fit.24.0,0,7,8;0,8,7,4;7,0,6,12", // off the grid
"fit.24.-1,0,7,8;0,8,7,4;7,0,5,12", // off the grid
"fit.24.0,0,7,8;0,8,7,4;7,0,5,13", // off the grid
"fit.24.0,0,7,8;0,8,7,4;7,0,1,12", // under 2 wide
"fit.24.0,0,7,8;0,8,7,1;7,0,5,12", // under 2 tall
"fit.24.0,0,7,8;0,6,7,6;7,0,5,12", // now and next overlap
"fit.24.0,0,8,8;0,8,7,4;7,0,5,12", // now and notes overlap
"fit.24.0,0,7,8;0,8,7,4;7,0,5,12,1", // five numbers
"fit.24.0,0,7,8;0,8,7,4;7,0,5", // three numbers
"fit.24.0,0,7,8;0,8,7,4;7,0,5,12.extra", // a fourth part
"fit.24.0, 0,7,8;0,8,7,4;7,0,5,12", // a space
"fit.024.0,0,7,8;0,8,7,4;7,0,5,12x", // trailing junk
"%%%",
} {
page := pt.window(t, "", presenterCookie(bad))
if got := arranged(page); got != defaultArrangement {
t.Errorf("cookie %q: arrangement %q, want the default %q", bad, got, defaultArrangement)
}
if !regexp.MustCompile(`\[presenter-notes\]\s*\{\s*font-size:\s*24px`).MatchString(page) ||
!regexp.MustCompile(`presenter-notes-fit[^>]*aria-pressed="true"`).MatchString(page) {
t.Errorf("cookie %q: the notes are not the default Fit at 24", bad)
}
}
}
// notesSections is the notes panel's sections, as "state:text" pairs.
var notesSection = regexp.MustCompile(`(?s)(.*?)`)
func sections(t *testing.T, page string) []string {
t.Helper()
notes := region(t, page, "
]+>`).ReplaceAllString(m[2], "")
out = append(out, m[1]+":"+strings.TrimSpace(text))
}
return out
}
func TestPresenterNotesWithoutThePressLineStayPlain(t *testing.T) {
pt := newPresenterTalk(t)
// Split notes: the part for the step is bright, the rest dimmed,
// and the press lines are not shown.
page := pt.window(t, "slide=1&step=2")
if strings.Contains(page, "(Press.)") {
t.Errorf("the presenter window shows the press lines")
}
got := strings.Join(sections(t, page), " | ")
want := "past:Arrive on PARTZERO. | past:Then PARTONE. | now:Then PARTTWO. | later:Then PARTTHREE. | later:And PARTFOUR."
if got != want {
t.Errorf("notes at step 2:\n got %s\nwant %s", got, want)
}
// Without the line: one plain section, neither bright nor dimmed.
page = pt.window(t, "slide=2&step=0")
if got := strings.Join(sections(t, page), " | "); got != ":Plain notes with PLAINWORDS in them." {
t.Errorf("notes with no press line are %q, want one plain section", got)
}
// No notes at all: the catalog's words.
page = pt.window(t, "slide=4&step=0")
if !strings.Contains(region(t, page, "
`) {
t.Errorf("the slide on screen is not a plain link back to the window:\n%s", pick)
}
for _, n := range []string{"2", "3", "4"} {
if !strings.Contains(pick, ``) {
t.Errorf("the picker has no form for slide %s:\n%s", n, pick)
}
}
if strings.Count(pick, "full version only") != 1 {
t.Errorf("the picker does not mark the one full-only slide:\n%s", pick)
}
if !strings.Contains(pick, "4 slides") {
t.Errorf("the picker does not count the talk's four slides:\n%s", pick)
}
// Choosing extra, a full-only slide, in the short version: the talk
// goes there in full, and the browser lands on the window, not the
// picker, drawing that state.
rec := command(t, pt.app, pt.session, "goto", pt.token, testOrigin+window+"&pick=1", url.Values{"slide": {"3"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("goto from the picker: status %d, want 303\n%s", rec.Code, rec.Body)
}
loc, err := url.Parse(rec.Header().Get("Location"))
if err != nil {
t.Fatal(err)
}
if loc.Path != "/p/"+pt.session+"/notes" || loc.Query().Has("pick") ||
loc.Query().Get("slide") != "3" || loc.Query().Get("step") != "0" || loc.Query().Get("t") != pt.token {
t.Errorf("goto from the picker lands on %s, want the window at slide 3, step 0", loc)
}
page = mustGet(t, pt.app, loc.RequestURI(), http.StatusOK, nil).Body.String()
now := region(t, page, `
]*>\s*Full\s*<`).MatchString(page) {
t.Errorf("a goto to a full-only slide did not switch to the full version:\n%s", page)
}
// And the window's own Next is a form that moves the talk.
next := regexp.MustCompile(`(?s)`).FindString(page)
if next == "" {
t.Fatalf("the window has no Next form:\n%s", page)
}
rec = command(t, pt.app, pt.session, "next", pt.token, testOrigin+loc.RequestURI(), nil)
if rec.Code != http.StatusSeeOther {
t.Fatalf("next: status %d", rec.Code)
}
if got := rec.Header().Get("Location"); !strings.Contains(got, "slide=4") {
t.Errorf("next from extra lands on %s, want slide 4", got)
}
}