//go:build browser package slidestest import ( "encoding/json" "io" "log/slog" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/rastrillo/db" slides "amadan.net/oficina/slides/internal/slides" ) // Once slide text is on screen it never changes, in WebKit as well as // Chromium: fixtures/engines/first-reveal.mjs drives both through // Playwright against Slides behind its production serving stack, on the // viewer, the projector and the editor, with the hold or the gate // missing, the editor late, a family late, and a change waiting for its // family, comparing the slide's pixels capture by capture. Two control // pages that flash on purpose must be caught, or the check fails as // blind. (Docs' TestFontEnginesFirstRevealIsTheFinalLayout, adopted.) // // Needs fixtures/engines installed: `cd fixtures/engines && npm ci && // npx playwright install chromium webkit`. Without it this fails rather // than skipping, unless RASTRILLO_BROWSER_OPTIONAL is set. func TestFontEnginesFirstRevealIsTheFinalLayout(t *testing.T) { if out, err := runEngines(t, "first-reveal.mjs"); err != nil { t.Fatalf("the first reveal is not the final layout in every engine: %v\n%s", err, out) } } // Moving between slides never shows a frame without a slide, in WebKit as // well as Chromium (Astra's finding 4 on #27): the viewer's links and the // browser's Back, the scriptless presenting mode's links, the presenter's // buttons on the projector, and a next slide whose family the page has // not loaded, with every font file held 800 ms. A control that pages by // loading the page must be caught. fixtures/engines/navigate.mjs. func TestFontEnginesSlidesChangeWithoutAGap(t *testing.T) { if out, err := runEngines(t, "navigate.mjs"); err != nil { t.Fatalf("moving between slides showed a frame without one: %v\n%s", err, out) } } // During a wait for a font the keys a person was editing with stay on // the page, in WebKit as well as Chromium: with the editor inert, focus // is on the page, where Backspace went back in history in WebKit and a // waiting change was lost (Docs' review 8). fixtures/engines/wait-keys.mjs. func TestFontEnginesAWaitKeepsThePage(t *testing.T) { if out, err := runEngines(t, "wait-keys.mjs"); err != nil { t.Fatalf("a wait for a font let a key leave or move the page: %v\n%s", err, out) } } // runEngines serves Slides behind its production serving stack, makes // fresh decks and a signed-in cookie, and runs a Playwright script in // fixtures/engines against them, in Chromium and WebKit. func runEngines(t *testing.T, script string) (string, error) { t.Helper() out, _, err := runEnginesRig(t, script) return out, err } // runEnginesRig is runEngines, also handing back the rig the script ran // against, for a test that checks the server afterwards. func runEnginesRig(t *testing.T, script string) (string, *fontRig, error) { t.Helper() if _, err := os.Stat(filepath.Join("..", "..", "fixtures", "engines", "node_modules", "playwright")); err != nil { if os.Getenv("RASTRILLO_BROWSER_OPTIONAL") != "" { t.Skip("fixtures/engines is not installed") } t.Fatal("fixtures/engines is not installed: cd fixtures/engines && npm ci && npx playwright install chromium webkit") } server := httptest.NewUnstartedServer(nil) origin := "http://" + server.Listener.Addr().String() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatal(err) } t.Cleanup(func() { d.Close() }) mailer := &fakeMailer{} mux, a, err := slides.New(d, slides.Config{KeymailServers: testKeymailServers, Origin: origin, InstanceKey: testInstanceKey, Mailer: mailer, AllowedAddresses: []string{allowedAddress}, Logger: logger}) if err != nil { t.Fatal(err) } t.Cleanup(func() { a.Close() }) mailer.flush = a.FlushMail served, closeFn, err := rastrillo.Handler(a.ServeOptions(rastrillo.Options{Logger: logger}, mux)) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = closeFn() }) server.Config.Handler = served server.Start() t.Cleanup(server.Close) app := &testApp{Handler: served, mailer: mailer, db: d, mux: mux, app: a} cookies := signedIn(t, app, allowedAddress) fr := &fontRig{app: app, cookies: cookies, token: mintAgent(t, app, cookies, "engines")} // A fresh deck per engine and scenario: a scenario that changes its // deck must not hand the next one a deck already using the family it // slows (Docs' review 6). kinds := map[string]string{ "paper": paperDeck, "plain": strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper"`, 1), "systemHeading": strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper","fonts":{"heading":"system-ui, sans-serif","body":""}`, 1), "edges": edgesDeck, } decks := map[string][]string{} for name, ops := range kinds { for range 2 * 8 { decks[name] = append(decks[name], fr.deck(t, name, ops)) } } three := strings.TrimSuffix(paperDeck, "]") + `, {"op":"add_slide","id":"slidethree03","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"slidethree03","id":"head00000003","type":"heading","region":"heading","props":{"level":1},"markdown":"Third"}]` plainThree := strings.TrimSuffix(strings.Replace(inkDeck, `"theme":"ink"`, `"theme":"paper"`, 1), "]") + `, {"op":"add_slide","id":"slidethree03","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"slidethree03","id":"head00000003","type":"heading","region":"heading","props":{"level":1},"markdown":"Third"}]` for range 2 * 3 { decks["three"] = append(decks["three"], fr.deck(t, "three", three)) decks["plainThree"] = append(decks["plainThree"], fr.deck(t, "plainThree", plainThree)) } for range 2 * 4 { sqid := fr.deck(t, "projector", paperDeck) session, token := startPresenting(t, app, "/d/"+sqid, cookies) decks["projector"] = append(decks["projector"], "/p/"+session) sqid = fr.deck(t, "projector", paperDeck) session, token = startPresenting(t, app, "/d/"+sqid, cookies) decks["projectorToken"] = append(decks["projectorToken"], "/p/"+session+"?t="+token) } // The replay cases (replay.mjs): a fresh deck per engine and case, // its text block two paragraphs, nothing in code. if script == "replay.mjs" { for range 2 * 11 { decks["replay"] = append(decks["replay"], fr.deck(t, "replay", replayDeck)) } } // The edits-while-diverged cases (diverged.mjs): the same deck, a // fresh one per engine and case. // The undo and redo cases (undo.mjs): the same deck, one per engine and // case. if script == "undo.mjs" { for range 2 * 17 { decks["undo"] = append(decks["undo"], fr.deck(t, "undo", replayDeck)) } } if script == "diverged.mjs" { for range 2 * 4 { decks["diverged"] = append(decks["diverged"], fr.deck(t, "diverged", replayDeck)) } } // Generated decks whose first slide holds every shape of text the // renderer and the editor could draw differently, for the late // editor (Astra's finding 3 on #27). decks["generated"] = fr.randomDecks(t, 12, fontSeed()) config, _ := json.Marshal(map[string]any{ "origin": origin, "decks": decks, "cookies": cookies, "token": fr.token, "probe": probeJS, }) cmd := exec.Command("node", script) cmd.Dir = filepath.Join("..", "..", "fixtures", "engines") // Captures must not wait for document.fonts.ready: the interval // before a slow font is exactly what is being looked at. cmd.Env = append(os.Environ(), "SLIDES_ENGINES_CONFIG="+string(config), "PW_TEST_SCREENSHOT_NO_FONTS_READY=1") out, err := cmd.CombinedOutput() t.Logf("%s", out) return string(out), fr, err } // edgesDeck is the text an editor draws differently from the renderer // unless the stage says otherwise (editor.css): an empty heading, an // empty text block and an empty list, double and trailing spaces, a // long word, lines joined in a heading. const edgesDeck = `[{"op":"set_meta","theme":"paper","title":"Edges"}, {"op":"add_slide","id":"slideone0001","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"slideone0001","id":"head00000001","type":"heading","region":"heading","props":{"level":1},"markdown":""}, {"op":"add_block","slide":"slideone0001","id":"text00000001","type":"text","region":"body","markdown":""}, {"op":"add_block","slide":"slideone0001","id":"text00000002","type":"text","region":"body","markdown":"Two spaces, a Supercalifragilisticexpialidociouslylongwordthatwraps, **bold** and *italic*."}, {"op":"add_block","slide":"slideone0001","id":"list00000001","type":"list","region":"body","markdown":""}, {"op":"add_block","slide":"slideone0001","id":"list00000002","type":"list","region":"body","markdown":"- one\n- two"}, {"op":"add_block","slide":"slideone0001","id":"tabl00000001","type":"table","region":"body","props":{"rows":2,"cols":2,"header":true}}]`