| 1 | package slidestest |
| 2 | |
| 3 | // The presenter window drawn by the server (Task 9): what a Positional |
| 4 | // session's GET /p/{session}/notes serves, read as markup. The browser |
| 5 | // half, which measures the window, is presenter_window_browser_test.go. |
| 6 | |
| 7 | import ( |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // presenterTalk is a session on stepsDeck, with notes on "steps" split |
| 16 | // at the press line and plain notes on "plain". |
| 17 | type presenterTalk struct { |
| 18 | app *testApp |
| 19 | cookies []*http.Cookie |
| 20 | deck string |
| 21 | session, token string |
| 22 | } |
| 23 | |
| 24 | // stepsNotes are the notes on "steps": five parts for four presses. |
| 25 | 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." |
| 26 | |
| 27 | func newPresenterTalk(t *testing.T) *presenterTalk { |
| 28 | t.Helper() |
| 29 | a := newApp(t) |
| 30 | cookies := signedIn(t, a, allowedAddress) |
| 31 | deck := stepsDeck(t, a, cookies) |
| 32 | setNotes(t, a, cookies, deck, map[string]string{ |
| 33 | "steps": stepsNotes, |
| 34 | "plain": "Plain notes with PLAINWORDS in them.", |
| 35 | }) |
| 36 | session, token := startPresenting(t, a, deck, cookies) |
| 37 | return &presenterTalk{app: a, cookies: cookies, deck: deck, session: session, token: token} |
| 38 | } |
| 39 | |
| 40 | // setNotes writes slides' notes over the API. |
| 41 | func setNotes(t *testing.T, a *testApp, cookies []*http.Cookie, deck string, notes map[string]string) { |
| 42 | t.Helper() |
| 43 | token := mintAgent(t, a, cookies, "notes-writer") |
| 44 | var ops []string |
| 45 | for id, md := range notes { |
| 46 | ops = append(ops, `{"op":"set_notes","id":`+quote(id)+`,"markdown":`+quote(md)+`}`) |
| 47 | } |
| 48 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(deck, "/d/")+"/ops", token, |
| 49 | `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 50 | } |
| 51 | |
| 52 | // window is the presenter window at query (without the leading ?), read |
| 53 | // by the token holder. |
| 54 | func (pt *presenterTalk) window(t *testing.T, query string, cookies ...*http.Cookie) string { |
| 55 | t.Helper() |
| 56 | target := "/p/" + pt.session + "/notes?t=" + url.QueryEscape(pt.token) |
| 57 | if query != "" { |
| 58 | target += "&" + query |
| 59 | } |
| 60 | return mustGet(t, pt.app, target, http.StatusOK, cookies).Body.String() |
| 61 | } |
| 62 | |
| 63 | func (pt *presenterTalk) press(t *testing.T, name string, times int) { |
| 64 | t.Helper() |
| 65 | for range times { |
| 66 | press(t, pt.app, pt.session, pt.token, op(t, name), nil) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // region is the markup of the first element carrying attr, up to the |
| 71 | // element that follows it in the page: enough to tell which panel holds |
| 72 | // what, without parsing the page. |
| 73 | func region(t *testing.T, page, attr, until string) string { |
| 74 | t.Helper() |
| 75 | start := strings.Index(page, attr) |
| 76 | if start < 0 { |
| 77 | t.Fatalf("no %s on the page:\n%s", attr, page) |
| 78 | } |
| 79 | rest := page[start:] |
| 80 | if end := strings.Index(rest, until); end > 0 { |
| 81 | return rest[:end] |
| 82 | } |
| 83 | return rest |
| 84 | } |
| 85 | |
| 86 | func TestPresenterStepCountOnlyWithSteps(t *testing.T) { |
| 87 | pt := newPresenterTalk(t) |
| 88 | |
| 89 | page := pt.window(t, "") |
| 90 | if !strings.Contains(page, "presenter-stepcount") || !strings.Contains(page, "step 0 of 4") { |
| 91 | t.Errorf("on a slide with four presses the header has no step count:\n%s", page) |
| 92 | } |
| 93 | pt.press(t, "next", 2) |
| 94 | if page := pt.window(t, ""); !strings.Contains(page, "step 2 of 4") { |
| 95 | t.Errorf("two presses on, the step count does not say step 2 of 4:\n%s", page) |
| 96 | } |
| 97 | // The scriptless address draws its own step too. |
| 98 | if page := pt.window(t, "slide=1&step=3"); !strings.Contains(page, "step 3 of 4") { |
| 99 | t.Errorf("?slide=1&step=3 does not count step 3 of 4:\n%s", page) |
| 100 | } |
| 101 | |
| 102 | pt.press(t, "next", 3) // onto "plain", which has no presses |
| 103 | page = pt.window(t, "") |
| 104 | if !strings.Contains(page, `data-id="plain"`) { |
| 105 | t.Fatalf("five presses on, the window is not on the plain slide:\n%s", page) |
| 106 | } |
| 107 | if strings.Contains(page, "presenter-stepcount") || strings.Contains(page, " of 4") { |
| 108 | t.Errorf("a slide with no steps draws a step count:\n%s", page) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestNextPanelNamesWhatComesNext(t *testing.T) { |
| 113 | pt := newPresenterTalk(t) |
| 114 | |
| 115 | // At step 1 of four: the same slide, one press on. |
| 116 | page := pt.window(t, "slide=1&step=1") |
| 117 | next := region(t, page, `<div presenter-panel="next"`, `<div presenter-panel="notes"`) |
| 118 | if !strings.Contains(next, "Next step") { |
| 119 | t.Errorf("mid-slide the next panel is not labelled Next step:\n%s", next) |
| 120 | } |
| 121 | if !strings.Contains(next, `data-id="steps"`) || strings.Count(next, "data-waiting") != 2 { |
| 122 | t.Errorf("mid-slide the next panel is not this slide at step 2 (two presses waiting):\n%s", next) |
| 123 | } |
| 124 | |
| 125 | // At the last step: the next slide in the short version, as it |
| 126 | // arrives. "extra" is full only, so the short talk goes plain → last. |
| 127 | page = pt.window(t, "slide=2&step=0") |
| 128 | next = region(t, page, `<div presenter-panel="next"`, `<div presenter-panel="notes"`) |
| 129 | if !strings.Contains(next, "Next slide") || !strings.Contains(next, `data-id="last"`) { |
| 130 | t.Errorf("on plain in the short version the next panel is not Next slide, last:\n%s", next) |
| 131 | } |
| 132 | page = pt.window(t, "slide=1&step=4") |
| 133 | next = region(t, page, `<div presenter-panel="next"`, `<div presenter-panel="notes"`) |
| 134 | if !strings.Contains(next, "Next slide") || !strings.Contains(next, `data-id="plain"`) || strings.Contains(next, "data-waiting") { |
| 135 | t.Errorf("at the last step the next panel is not the next slide as it arrives:\n%s", next) |
| 136 | } |
| 137 | |
| 138 | // The full version's next slide after plain is extra. |
| 139 | pt.press(t, "mode", 1) |
| 140 | page = pt.window(t, "slide=2&step=0") |
| 141 | next = region(t, page, `<div presenter-panel="next"`, `<div presenter-panel="notes"`) |
| 142 | if !strings.Contains(next, `data-id="extra"`) { |
| 143 | t.Errorf("in the full version the slide after plain is not extra:\n%s", next) |
| 144 | } |
| 145 | |
| 146 | // The end of the talk: an empty frame. |
| 147 | page = pt.window(t, "slide=4&step=0") |
| 148 | next = region(t, page, `<div presenter-panel="next"`, `<div presenter-panel="notes"`) |
| 149 | if !strings.Contains(next, "End of the talk") { |
| 150 | t.Errorf("on the last slide the next panel does not say End of the talk:\n%s", next) |
| 151 | } |
| 152 | if strings.Contains(next, "slides-slide") { |
| 153 | t.Errorf("at the end of the talk the next panel draws a slide:\n%s", next) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // presenterCookie is the preferences cookie as a browser sends it: the |
| 158 | // value percent-encoded, because a cookie value cannot hold a ";". |
| 159 | func presenterCookie(value string) *http.Cookie { |
| 160 | return &http.Cookie{Name: "slides-presenter", Value: url.QueryEscape(value)} |
| 161 | } |
| 162 | |
| 163 | // arrangement is the window's arrangement as served: each panel's grid |
| 164 | // area, in the order now, next, notes. |
| 165 | var arrangement = regexp.MustCompile(`\[presenter-panel="(now|next|notes)"\]\s*\{\s*grid-area:\s*(\d+) / (\d+) / span (\d+) / span (\d+)`) |
| 166 | |
| 167 | func arranged(page string) string { |
| 168 | var out []string |
| 169 | for _, m := range arrangement.FindAllStringSubmatch(page, -1) { |
| 170 | out = append(out, m[1]+":"+m[2]+","+m[3]+","+m[4]+","+m[5]) |
| 171 | } |
| 172 | return strings.Join(out, " ") |
| 173 | } |
| 174 | |
| 175 | const defaultArrangement = "now:1,1,8,7 next:9,1,4,7 notes:1,8,12,5" |
| 176 | |
| 177 | func TestPresenterFirstPaintIsFinal(t *testing.T) { |
| 178 | pt := newPresenterTalk(t) |
| 179 | if got := arranged(pt.window(t, "")); got != defaultArrangement { |
| 180 | t.Errorf("with no cookie the arrangement is %q, want the default %q", got, defaultArrangement) |
| 181 | } |
| 182 | |
| 183 | page := pt.window(t, "", presenterCookie("scroll.32.0,0,6,6;6,0,6,6;0,6,12,6")) |
| 184 | if got, want := arranged(page), "now:1,1,6,6 next:1,7,6,6 notes:7,1,6,12"; got != want { |
| 185 | t.Errorf("the cookie's arrangement is served as %q, want %q", got, want) |
| 186 | } |
| 187 | if !regexp.MustCompile(`\[presenter-notes\]\s*\{\s*font-size:\s*32px`).MatchString(page) { |
| 188 | t.Errorf("the cookie's notes size of 32 is not in the served page") |
| 189 | } |
| 190 | if !regexp.MustCompile(`presenter-notes-scroll[^>]*aria-pressed="true"`).MatchString(page) || |
| 191 | !regexp.MustCompile(`presenter-notes-fit[^>]*aria-pressed="false"`).MatchString(page) { |
| 192 | t.Errorf("the cookie's choice of Scroll is not pressed in the served page") |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func TestPresenterPrefsRefuseNonsense(t *testing.T) { |
| 197 | pt := newPresenterTalk(t) |
| 198 | for _, bad := range []string{ |
| 199 | "", |
| 200 | "garbage", |
| 201 | "fit.24", |
| 202 | "fit.24.0,0,7,8;0,8,7,4", // two panels |
| 203 | "fit.24.0,0,7,8;0,8,7,4;7,0,5,12;0,0,2,2", // four |
| 204 | "wide.24.0,0,7,8;0,8,7,4;7,0,5,12", // not fit or scroll |
| 205 | "fit.25.0,0,7,8;0,8,7,4;7,0,5,12", // odd |
| 206 | "fit.14.0,0,7,8;0,8,7,4;7,0,5,12", // under 16 |
| 207 | "fit.50.0,0,7,8;0,8,7,4;7,0,5,12", // over 48 |
| 208 | "fit.x.0,0,7,8;0,8,7,4;7,0,5,12", // not a number |
| 209 | "fit.24.0,0,7,8;0,8,7,4;7,0,6,12", // off the grid |
| 210 | "fit.24.-1,0,7,8;0,8,7,4;7,0,5,12", // off the grid |
| 211 | "fit.24.0,0,7,8;0,8,7,4;7,0,5,13", // off the grid |
| 212 | "fit.24.0,0,7,8;0,8,7,4;7,0,1,12", // under 2 wide |
| 213 | "fit.24.0,0,7,8;0,8,7,1;7,0,5,12", // under 2 tall |
| 214 | "fit.24.0,0,7,8;0,6,7,6;7,0,5,12", // now and next overlap |
| 215 | "fit.24.0,0,8,8;0,8,7,4;7,0,5,12", // now and notes overlap |
| 216 | "fit.24.0,0,7,8;0,8,7,4;7,0,5,12,1", // five numbers |
| 217 | "fit.24.0,0,7,8;0,8,7,4;7,0,5", // three numbers |
| 218 | "fit.24.0,0,7,8;0,8,7,4;7,0,5,12.extra", // a fourth part |
| 219 | "fit.24.0, 0,7,8;0,8,7,4;7,0,5,12", // a space |
| 220 | "fit.024.0,0,7,8;0,8,7,4;7,0,5,12x", // trailing junk |
| 221 | "%%%", |
| 222 | } { |
| 223 | page := pt.window(t, "", presenterCookie(bad)) |
| 224 | if got := arranged(page); got != defaultArrangement { |
| 225 | t.Errorf("cookie %q: arrangement %q, want the default %q", bad, got, defaultArrangement) |
| 226 | } |
| 227 | if !regexp.MustCompile(`\[presenter-notes\]\s*\{\s*font-size:\s*24px`).MatchString(page) || |
| 228 | !regexp.MustCompile(`presenter-notes-fit[^>]*aria-pressed="true"`).MatchString(page) { |
| 229 | t.Errorf("cookie %q: the notes are not the default Fit at 24", bad) |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // notesSections is the notes panel's sections, as "state:text" pairs. |
| 235 | var notesSection = regexp.MustCompile(`(?s)<section presenter-part(?: data-part="(\w+)")?>(.*?)</section>`) |
| 236 | |
| 237 | func sections(t *testing.T, page string) []string { |
| 238 | t.Helper() |
| 239 | notes := region(t, page, "<div presenter-notes", "presenter-tools") |
| 240 | var out []string |
| 241 | for _, m := range notesSection.FindAllStringSubmatch(notes, -1) { |
| 242 | text := regexp.MustCompile(`<[^>]+>`).ReplaceAllString(m[2], "") |
| 243 | out = append(out, m[1]+":"+strings.TrimSpace(text)) |
| 244 | } |
| 245 | return out |
| 246 | } |
| 247 | |
| 248 | func TestPresenterNotesWithoutThePressLineStayPlain(t *testing.T) { |
| 249 | pt := newPresenterTalk(t) |
| 250 | |
| 251 | // Split notes: the part for the step is bright, the rest dimmed, |
| 252 | // and the press lines are not shown. |
| 253 | page := pt.window(t, "slide=1&step=2") |
| 254 | if strings.Contains(page, "(Press.)") { |
| 255 | t.Errorf("the presenter window shows the press lines") |
| 256 | } |
| 257 | got := strings.Join(sections(t, page), " | ") |
| 258 | want := "past:Arrive on PARTZERO. | past:Then PARTONE. | now:Then PARTTWO. | later:Then PARTTHREE. | later:And PARTFOUR." |
| 259 | if got != want { |
| 260 | t.Errorf("notes at step 2:\n got %s\nwant %s", got, want) |
| 261 | } |
| 262 | |
| 263 | // Without the line: one plain section, neither bright nor dimmed. |
| 264 | page = pt.window(t, "slide=2&step=0") |
| 265 | if got := strings.Join(sections(t, page), " | "); got != ":Plain notes with PLAINWORDS in them." { |
| 266 | t.Errorf("notes with no press line are %q, want one plain section", got) |
| 267 | } |
| 268 | |
| 269 | // No notes at all: the catalog's words. |
| 270 | page = pt.window(t, "slide=4&step=0") |
| 271 | if !strings.Contains(region(t, page, "<div presenter-notes", "presenter-tools"), "No notes on this slide.") { |
| 272 | t.Errorf("a slide with no notes does not say so") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | func TestPresenterIsOnlyForThePresenter(t *testing.T) { |
| 277 | pt := newPresenterTalk(t) |
| 278 | base := "/p/" + pt.session + "/notes" |
| 279 | |
| 280 | // The signed-in presenter, and the token holder. |
| 281 | if page := mustGet(t, pt.app, base, http.StatusOK, pt.cookies).Body.String(); !strings.Contains(page, "presenter-head") { |
| 282 | t.Errorf("the signed-in presenter is not shown the presenter window:\n%s", page) |
| 283 | } |
| 284 | if page := mustGet(t, pt.app, base+"?t="+url.QueryEscape(pt.token), http.StatusOK, nil).Body.String(); !strings.Contains(page, "presenter-head") { |
| 285 | t.Errorf("the token holder is not shown the presenter window:\n%s", page) |
| 286 | } |
| 287 | |
| 288 | // Nobody else: no credential, a wrong token, the picker, a state. |
| 289 | for _, target := range []string{base, base + "?t=wrong", base + "?pick=1", base + "?slide=1&step=1"} { |
| 290 | mustGet(t, pt.app, target, http.StatusNotFound, nil) |
| 291 | } |
| 292 | // Somebody else signed in. |
| 293 | if err := pt.app.db.G.Exec("UPDATE presentations SET actor = ? WHERE sqid = ?", "user:someone-else", pt.session).Error; err != nil { |
| 294 | t.Fatal(err) |
| 295 | } |
| 296 | mustGet(t, pt.app, base, http.StatusNotFound, pt.cookies) |
| 297 | mustGet(t, pt.app, base+"?pick=1", http.StatusNotFound, pt.cookies) |
| 298 | |
| 299 | // A session begun before positions keeps today's presenter view. |
| 300 | if err := pt.app.db.G.Exec(`UPDATE presentations |
| 301 | SET positional = NULL, slide_id = NULL, step = NULL, mode = NULL, seq = NULL, timer_at = NULL |
| 302 | WHERE sqid = ?`, pt.session).Error; err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | page := mustGet(t, pt.app, base+"?t="+url.QueryEscape(pt.token), http.StatusOK, nil).Body.String() |
| 306 | if !strings.Contains(page, "slides-preview") || strings.Contains(page, "presenter-head") { |
| 307 | t.Errorf("a pre-deploy session is not served the old presenter view:\n%s", page) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | func TestScriptlessPickerMovesTheTalk(t *testing.T) { |
| 312 | pt := newPresenterTalk(t) |
| 313 | window := "/p/" + pt.session + "/notes?t=" + url.QueryEscape(pt.token) |
| 314 | |
| 315 | // The window's position opens the picker. |
| 316 | page := pt.window(t, "") |
| 317 | if !strings.Contains(page, `href="/p/`+pt.session+`/notes?pick=1&t=`) { |
| 318 | t.Errorf("the position does not link to the picker:\n%s", page) |
| 319 | } |
| 320 | |
| 321 | pick := pt.window(t, "pick=1") |
| 322 | if strings.Contains(pick, "PARKED") || strings.Contains(pick, "parked.svg") || strings.Contains(pick, `value="parked"`) { |
| 323 | t.Errorf("the picker lists the parked slide:\n%s", pick) |
| 324 | } |
| 325 | // The slide on screen is a way back to the window, moving nothing; |
| 326 | // every other slide is a form that goes there. |
| 327 | if !strings.Contains(pick, `aria-current="true" href="/p/`+pt.session+`/notes?t=`) || |
| 328 | strings.Contains(pick, `<input type="hidden" name="slide" value="1">`) { |
| 329 | t.Errorf("the slide on screen is not a plain link back to the window:\n%s", pick) |
| 330 | } |
| 331 | for _, n := range []string{"2", "3", "4"} { |
| 332 | if !strings.Contains(pick, `<input type="hidden" name="slide" value="`+n+`">`) { |
| 333 | t.Errorf("the picker has no form for slide %s:\n%s", n, pick) |
| 334 | } |
| 335 | } |
| 336 | if strings.Count(pick, "full version only") != 1 { |
| 337 | t.Errorf("the picker does not mark the one full-only slide:\n%s", pick) |
| 338 | } |
| 339 | if !strings.Contains(pick, "4 slides") { |
| 340 | t.Errorf("the picker does not count the talk's four slides:\n%s", pick) |
| 341 | } |
| 342 | |
| 343 | // Choosing extra, a full-only slide, in the short version: the talk |
| 344 | // goes there in full, and the browser lands on the window, not the |
| 345 | // picker, drawing that state. |
| 346 | rec := command(t, pt.app, pt.session, "goto", pt.token, testOrigin+window+"&pick=1", url.Values{"slide": {"3"}}) |
| 347 | if rec.Code != http.StatusSeeOther { |
| 348 | t.Fatalf("goto from the picker: status %d, want 303\n%s", rec.Code, rec.Body) |
| 349 | } |
| 350 | loc, err := url.Parse(rec.Header().Get("Location")) |
| 351 | if err != nil { |
| 352 | t.Fatal(err) |
| 353 | } |
| 354 | if loc.Path != "/p/"+pt.session+"/notes" || loc.Query().Has("pick") || |
| 355 | loc.Query().Get("slide") != "3" || loc.Query().Get("step") != "0" || loc.Query().Get("t") != pt.token { |
| 356 | t.Errorf("goto from the picker lands on %s, want the window at slide 3, step 0", loc) |
| 357 | } |
| 358 | page = mustGet(t, pt.app, loc.RequestURI(), http.StatusOK, nil).Body.String() |
| 359 | now := region(t, page, `<div presenter-panel="now"`, `<div presenter-panel="next"`) |
| 360 | if !strings.Contains(now, `data-id="extra"`) { |
| 361 | t.Errorf("after goto 3 the window does not show extra:\n%s", now) |
| 362 | } |
| 363 | if !regexp.MustCompile(`presenter-mode[^>]*>\s*Full\s*<`).MatchString(page) { |
| 364 | t.Errorf("a goto to a full-only slide did not switch to the full version:\n%s", page) |
| 365 | } |
| 366 | |
| 367 | // And the window's own Next is a form that moves the talk. |
| 368 | next := regexp.MustCompile(`(?s)<form method="post" action="/p/` + pt.session + `/command"[^>]*>\s*<input type="hidden" name="op" value="next">.*?</form>`).FindString(page) |
| 369 | if next == "" { |
| 370 | t.Fatalf("the window has no Next form:\n%s", page) |
| 371 | } |
| 372 | rec = command(t, pt.app, pt.session, "next", pt.token, testOrigin+loc.RequestURI(), nil) |
| 373 | if rec.Code != http.StatusSeeOther { |
| 374 | t.Fatalf("next: status %d", rec.Code) |
| 375 | } |
| 376 | if got := rec.Header().Get("Location"); !strings.Contains(got, "slide=4") { |
| 377 | t.Errorf("next from extra lands on %s, want slide 4", got) |
| 378 | } |
| 379 | } |
| 380 | |