package slidestest import ( "encoding/json" "fmt" "html" "net/http" "regexp" "strings" "testing" ) // The font gate's server half (internal/slides/fontgate.go): which // pages carry it, and what they preload. The browser half — that the // slides are then never painted in a face they will leave — is // fonts_browser_test.go. // opsDeck makes a deck over the API with the given batch and returns // its viewer path. func opsDeck(t *testing.T, a *testApp, token, title, ops string) string { t.Helper() sqid := apiDeck(t, a, token, title) apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":`+ops+`}`, http.StatusOK) return "/d/" + sqid } var ( preloadTag = regexp.MustCompile(`]*>`) preloadHref = regexp.MustCompile(`href="([^"]+)"`) deckFaceURL = regexp.MustCompile(`^/static/fonts/Liberation(Sans|Serif|Mono)-(Regular|Bold|Italic|BoldItalic)\.[0-9a-f]{16}\.woff2$`) // A deck's own font, under the surface's base (deckfonts.go). uploadedFaceURL = regexp.MustCompile(`^/(d|s|p)/[^/]+/fonts/[0-9a-f]{64}$`) holdScript = regexp.MustCompile(``) gateScript = regexp.MustCompile(``) firstSheet = regexp.MustCompile(`") for _, tag := range preloadTag.FindAllString(page, -1) { if !strings.Contains(head, tag) { t.Errorf("%s: a preload outside
: %s", where, tag) } m := preloadHref.FindStringSubmatch(tag) if m == nil || !deckFaceURL.MatchString(m[1]) && !uploadedFaceURL.MatchString(m[1]) { t.Errorf("%s: %s preloads something that is not one of the deck's faces", where, tag) continue } kind := `type="font/woff2"` if uploadedFaceURL.MatchString(m[1]) { kind = `type="font/` } if !strings.Contains(tag, `as="font"`) || !strings.Contains(tag, kind) || !strings.Contains(tag, " crossorigin") { t.Errorf("%s: %s is not a CORS font preload of its type (without crossorigin it is a second download)", where, tag) } preloads = append(preloads, m[1]) } _, hasManifest := readManifest(t, page) hold := holdScript.FindStringIndex(head) gate := gateScript.FindStringIndex(head) sheet := firstSheet.FindStringIndex(head) if heldByServer.MatchString(page) { t.Errorf("%s: the server rendered the hold; a page with no script must never be held", where) } carries = hasManifest || hold != nil || gate != nil || len(preloads) > 0 if !carries { return nil, false } if !hasManifest || hold == nil || gate == nil { t.Errorf("%s: half a gate (manifest %v, hold %v, gate %v)", where, hasManifest, hold != nil, gate != nil) return preloads, true } // Ahead of every stylesheet, so the fonts start with the page and // the hold runs before anything can be painted. if sheet == nil || hold[0] > sheet[0] || gate[0] > sheet[0] { t.Errorf("%s: the hold and the gate must come before the first stylesheet", where) } for _, tag := range preloadTag.FindAllStringIndex(head, -1) { if sheet != nil && tag[0] > sheet[0] { t.Errorf("%s: a preload after the first stylesheet", where) } } return preloads, true } // Every page that draws a slide in the deck's faces carries the gate — // the viewer in each of its modes, the projector, the follow view and // the editor, and the presenter window, whose slides are live — and no // other page does. The remote shows its slides as pictures and its // notes in the chrome's face, so there is nothing on it a web face // could reach. func TestOnlySlideSurfacesCarryTheGateAndPreloadOnlyDeckFaces(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() deck := deckPath.FindString(list) if deck == "" { t.Fatal("the sample deck is not on the list") } share := "/s/" + mintShareLink(t, a, deck, cookies) session, token := startPresenting(t, a, deck, cookies) for _, c := range []struct { path string cookies []*http.Cookie gated bool }{ {deck + "?slide=1", cookies, true}, {deck + "?slide=2&vp=phone", cookies, true}, {deck + "?present=1", cookies, true}, {deck + "/edit", cookies, true}, {share, nil, true}, {share + "?present=1", nil, true}, {share + "?follow=1", nil, true}, {"/p/" + session, nil, true}, {"/p/" + session + "?t=" + token, nil, true}, {"/p/" + session + "/notes?t=" + token, nil, true}, {"/p/" + session + "/remote?t=" + token, nil, false}, {"/", cookies, false}, {"/signin", nil, false}, {deck + "/settings", cookies, false}, {deck + "/notes", cookies, false}, {deck + "/share", cookies, false}, } { page := mustGet(t, a, c.path, http.StatusOK, c.cookies).Body.String() preloads, carries := gateOn(t, c.path, page) if carries != c.gated { t.Errorf("%s: carries the font gate = %v, want %v", c.path, carries, c.gated) } // The sample deck is on paper, with code marks in it: every // style of all three families, exactly once each. if c.gated && len(preloads) != 12 { t.Errorf("%s: %d preloads, want the sample deck's three families × four styles", c.path, len(preloads)) } } } // The preloads are exact: every style of every family the deck's slides // can be drawn in (render.FamiliesUsed), on every slide of the deck and // not only the one shown, and nothing else. The manifest's need is the // same families. func TestThePreloadsAreEveryStyleOfEveryFamilyTheDeckUses(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "fonts") code := `{"op":"add_block","slide":"s3","type":"code","region":"body","markdown":"x := 1"}` slides := `{"op":"add_slide","id":"s1","layout":"flow","template":"title-body"},` + `{"op":"add_block","slide":"s1","type":"heading","region":"heading","props":{"level":1},"markdown":"Hello"},` + `{"op":"add_slide","id":"s2","layout":"flow"},{"op":"add_slide","id":"s3","layout":"flow"}` for _, c := range []struct { name, ops string want []string }{ {"ink", `[{"op":"set_meta","theme":"ink"},` + slides + `]`, []string{"sans"}}, {"paper", `[{"op":"set_meta","theme":"paper"},` + slides + `]`, []string{"sans", "serif"}}, {"code on a later slide", `[{"op":"set_meta","theme":"ink"},` + slides + `,` + code + `]`, []string{"sans", "mono"}}, {"inline code", `[{"op":"set_meta","theme":"slate"},` + slides + `,{"op":"add_block","slide":"s2","type":"text","region":"body","markdown":"run ` + "`go test`" + `"}]`, []string{"sans", "mono"}}, {"system heading on paper", `[{"op":"set_meta","theme":"paper","fonts":{"heading":"system-ui, sans-serif","body":""}},` + slides + `]`, []string{"sans"}}, {"system faces only", `[{"op":"set_meta","theme":"paper","fonts":{"heading":"Georgia, serif","body":"system-ui, sans-serif"}},` + slides + `]`, nil}, } { t.Run(c.name, func(t *testing.T) { deck := opsDeck(t, a, token, c.name, c.ops) for _, path := range []string{deck, deck + "/edit"} { page := mustGet(t, a, path+"?slide=1", http.StatusOK, cookies).Body.String() preloads, carries := gateOn(t, path, page) if !carries { t.Fatalf("%s carries no gate", path) } m, _ := readManifest(t, page) if strings.Join(m.Need, " ") != strings.Join(c.want, " ") { t.Errorf("%s: the gate waits for %v, want %v", path, m.Need, c.want) } var want []string for _, key := range c.want { for _, f := range m.Families[key].Faces { want = append(want, f.URL) } } if strings.Join(preloads, " ") != strings.Join(want, " ") { t.Errorf("%s: preloads\n %v\nwant every style of %v:\n %v", path, preloads, c.want, want) } } }) } } // The follow view's heartbeat is for a reader with no scripts. With // them, present.js follows the stream and swaps the slide in place; a // reload every five seconds would blink the follower's slide. func TestTheFollowViewRefreshesOnlyWithoutScripts(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) deck := deckPath.FindString(mustGet(t, a, "/", http.StatusOK, cookies).Body.String()) share := "/s/" + mintShareLink(t, a, deck, cookies) startPresenting(t, a, deck, cookies) page := mustGet(t, a, share+"?follow=1", http.StatusOK, nil).Body.String() if !strings.Contains(page, ``) { t.Errorf("the follow view's refresh is not inside a