| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo" |
| 17 | "amadan.net/rastrillo/rastrillo/db" |
| 18 | |
| 19 | slides "amadan.net/oficina/slides/internal/slides" |
| 20 | ) |
| 21 | |
| 22 | // Once slide text is on screen it never changes, in WebKit as well as |
| 23 | // Chromium: fixtures/engines/first-reveal.mjs drives both through |
| 24 | // Playwright against Slides behind its production serving stack, on the |
| 25 | // viewer, the projector and the editor, with the hold or the gate |
| 26 | // missing, the editor late, a family late, and a change waiting for its |
| 27 | // family, comparing the slide's pixels capture by capture. Two control |
| 28 | // pages that flash on purpose must be caught, or the check fails as |
| 29 | // blind. (Docs' TestFontEnginesFirstRevealIsTheFinalLayout, adopted.) |
| 30 | // |
| 31 | // Needs fixtures/engines installed: `cd fixtures/engines && npm ci && |
| 32 | // npx playwright install chromium webkit`. Without it this fails rather |
| 33 | // than skipping, unless RASTRILLO_BROWSER_OPTIONAL is set. |
| 34 | func TestFontEnginesFirstRevealIsTheFinalLayout(t *testing.T) { |
| 35 | if out, err := runEngines(t, "first-reveal.mjs"); err != nil { |
| 36 | t.Fatalf("the first reveal is not the final layout in every engine: %v\n%s", err, out) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Moving between slides never shows a frame without a slide, in WebKit as |
| 41 | // well as Chromium (Astra's finding 4 on #27): the viewer's links and the |
| 42 | // browser's Back, the scriptless presenting mode's links, the presenter's |
| 43 | // buttons on the projector, and a next slide whose family the page has |
| 44 | // not loaded, with every font file held 800 ms. A control that pages by |
| 45 | // loading the page must be caught. fixtures/engines/navigate.mjs. |
| 46 | func TestFontEnginesSlidesChangeWithoutAGap(t *testing.T) { |
| 47 | if out, err := runEngines(t, "navigate.mjs"); err != nil { |
| 48 | t.Fatalf("moving between slides showed a frame without one: %v\n%s", err, out) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // During a wait for a font the keys a person was editing with stay on |
| 53 | // the page, in WebKit as well as Chromium: with the editor inert, focus |
| 54 | // is on the page, where Backspace went back in history in WebKit and a |
| 55 | // waiting change was lost (Docs' review 8). fixtures/engines/wait-keys.mjs. |
| 56 | func TestFontEnginesAWaitKeepsThePage(t *testing.T) { |
| 57 | if out, err := runEngines(t, "wait-keys.mjs"); err != nil { |
| 58 | t.Fatalf("a wait for a font let a key leave or move the page: %v\n%s", err, out) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // runEngines serves Slides behind its production serving stack, makes |
| 63 | // fresh decks and a signed-in cookie, and runs a Playwright script in |
| 64 | // fixtures/engines against them, in Chromium and WebKit. |
| 65 | func runEngines(t *testing.T, script string) (string, error) { |
| 66 | t.Helper() |
| 67 | out, _, err := runEnginesRig(t, script) |
| 68 | return out, err |
| 69 | } |
| 70 | |
| 71 | // runEnginesRig is runEngines, also handing back the rig the script ran |
| 72 | // against, for a test that checks the server afterwards. |
| 73 | func runEnginesRig(t *testing.T, script string) (string, *fontRig, error) { |
| 74 | t.Helper() |
| 75 | if _, err := os.Stat(filepath.Join("..", "..", "fixtures", "engines", "node_modules", "playwright")); err != nil { |
| 76 | if os.Getenv("RASTRILLO_BROWSER_OPTIONAL") != "" { |
| 77 | t.Skip("fixtures/engines is not installed") |
| 78 | } |
| 79 | t.Fatal("fixtures/engines is not installed: cd fixtures/engines && npm ci && npx playwright install chromium webkit") |
| 80 | } |
| 81 | server := httptest.NewUnstartedServer(nil) |
| 82 | origin := "http://" + server.Listener.Addr().String() |
| 83 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 84 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | t.Cleanup(func() { d.Close() }) |
| 89 | mailer := &fakeMailer{} |
| 90 | mux, a, err := slides.New(d, slides.Config{KeymailServers: testKeymailServers, Origin: origin, InstanceKey: testInstanceKey, Mailer: mailer, AllowedAddresses: []string{allowedAddress}, Logger: logger}) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | t.Cleanup(func() { a.Close() }) |
| 95 | mailer.flush = a.FlushMail |
| 96 | served, closeFn, err := rastrillo.Handler(a.ServeOptions(rastrillo.Options{Logger: logger}, mux)) |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | t.Cleanup(func() { _ = closeFn() }) |
| 101 | server.Config.Handler = served |
| 102 | server.Start() |
| 103 | t.Cleanup(server.Close) |
| 104 | |
| 105 | app := &testApp{Handler: served, mailer: mailer, db: d, mux: mux, app: a} |
| 106 | cookies := signedIn(t, app, allowedAddress) |
| 107 | fr := &fontRig{app: app, cookies: cookies, token: mintAgent(t, app, cookies, "engines")} |
| 108 | |
| 109 | // A fresh deck per engine and scenario: a scenario that changes its |
| 110 | // deck must not hand the next one a deck already using the family it |
| 111 | // slows (Docs' review 6). |
| 112 | kinds := map[string]string{ |
| 113 | "paper": paperDeck, |
| 114 | "plain": strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper"`, 1), |
| 115 | "systemHeading": strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper","fonts":{"heading":"system-ui, sans-serif","body":""}`, 1), |
| 116 | "edges": edgesDeck, |
| 117 | } |
| 118 | decks := map[string][]string{} |
| 119 | for name, ops := range kinds { |
| 120 | for range 2 * 8 { |
| 121 | decks[name] = append(decks[name], fr.deck(t, name, ops)) |
| 122 | } |
| 123 | } |
| 124 | three := strings.TrimSuffix(paperDeck, "]") + `, |
| 125 | {"op":"add_slide","id":"slidethree03","layout":"flow","template":"title-body"}, |
| 126 | {"op":"add_block","slide":"slidethree03","id":"head00000003","type":"heading","region":"heading","props":{"level":1},"markdown":"Third"}]` |
| 127 | plainThree := strings.TrimSuffix(strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper"`, 1), "]") + `, |
| 128 | {"op":"add_slide","id":"slidethree03","layout":"flow","template":"title-body"}, |
| 129 | {"op":"add_block","slide":"slidethree03","id":"head00000003","type":"heading","region":"heading","props":{"level":1},"markdown":"Third"}]` |
| 130 | for range 2 * 3 { |
| 131 | decks["three"] = append(decks["three"], fr.deck(t, "three", three)) |
| 132 | decks["plainThree"] = append(decks["plainThree"], fr.deck(t, "plainThree", plainThree)) |
| 133 | } |
| 134 | for range 2 * 4 { |
| 135 | sqid := fr.deck(t, "projector", paperDeck) |
| 136 | session, token := startPresenting(t, app, "/d/"+sqid, cookies) |
| 137 | decks["projector"] = append(decks["projector"], "/p/"+session) |
| 138 | sqid = fr.deck(t, "projector", paperDeck) |
| 139 | session, token = startPresenting(t, app, "/d/"+sqid, cookies) |
| 140 | decks["projectorToken"] = append(decks["projectorToken"], "/p/"+session+"?t="+token) |
| 141 | } |
| 142 | // The replay cases (replay.mjs): a fresh deck per engine and case, |
| 143 | // its text block two paragraphs, nothing in code. |
| 144 | if script == "replay.mjs" { |
| 145 | for range 2 * 11 { |
| 146 | decks["replay"] = append(decks["replay"], fr.deck(t, "replay", replayDeck)) |
| 147 | } |
| 148 | } |
| 149 | // The edits-while-diverged cases (diverged.mjs): the same deck, a |
| 150 | // fresh one per engine and case. |
| 151 | // The undo and redo cases (undo.mjs): the same deck, one per engine and |
| 152 | // case. |
| 153 | if script == "undo.mjs" { |
| 154 | for range 2 * 17 { |
| 155 | decks["undo"] = append(decks["undo"], fr.deck(t, "undo", replayDeck)) |
| 156 | } |
| 157 | } |
| 158 | if script == "diverged.mjs" { |
| 159 | for range 2 * 4 { |
| 160 | decks["diverged"] = append(decks["diverged"], fr.deck(t, "diverged", replayDeck)) |
| 161 | } |
| 162 | } |
| 163 | // Generated decks whose first slide holds every shape of text the |
| 164 | // renderer and the editor could draw differently, for the late |
| 165 | // editor (Astra's finding 3 on #27). |
| 166 | decks["generated"] = fr.randomDecks(t, 12, fontSeed()) |
| 167 | config, _ := json.Marshal(map[string]any{ |
| 168 | "origin": origin, |
| 169 | "decks": decks, |
| 170 | "cookies": cookies, |
| 171 | "token": fr.token, |
| 172 | "probe": probeJS, |
| 173 | }) |
| 174 | cmd := exec.Command("node", script) |
| 175 | cmd.Dir = filepath.Join("..", "..", "fixtures", "engines") |
| 176 | // Captures must not wait for document.fonts.ready: the interval |
| 177 | // before a slow font is exactly what is being looked at. |
| 178 | cmd.Env = append(os.Environ(), "SLIDES_ENGINES_CONFIG="+string(config), "PW_TEST_SCREENSHOT_NO_FONTS_READY=1") |
| 179 | out, err := cmd.CombinedOutput() |
| 180 | t.Logf("%s", out) |
| 181 | return string(out), fr, err |
| 182 | } |
| 183 | |
| 184 | // edgesDeck is the text an editor draws differently from the renderer |
| 185 | // unless the stage says otherwise (editor.css): an empty heading, an |
| 186 | // empty text block and an empty list, double and trailing spaces, a |
| 187 | // long word, lines joined in a heading. |
| 188 | const edgesDeck = `[{"op":"set_meta","theme":"paper","title":"Edges"}, |
| 189 | {"op":"add_slide","id":"slideone0001","layout":"flow","template":"title-body"}, |
| 190 | {"op":"add_block","slide":"slideone0001","id":"head00000001","type":"heading","region":"heading","props":{"level":1},"markdown":""}, |
| 191 | {"op":"add_block","slide":"slideone0001","id":"text00000001","type":"text","region":"body","markdown":""}, |
| 192 | {"op":"add_block","slide":"slideone0001","id":"text00000002","type":"text","region":"body","markdown":"Two spaces, a Supercalifragilisticexpialidociouslylongwordthatwraps, **bold** and *italic*."}, |
| 193 | {"op":"add_block","slide":"slideone0001","id":"list00000001","type":"list","region":"body","markdown":""}, |
| 194 | {"op":"add_block","slide":"slideone0001","id":"list00000002","type":"list","region":"body","markdown":"- one\n- two"}, |
| 195 | {"op":"add_block","slide":"slideone0001","id":"tabl00000001","type":"table","region":"body","props":{"rows":2,"cols":2,"header":true}}]` |
| 196 | |