| 1 | //go:build browser |
| 2 | |
| 3 | package docs_test |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo" |
| 14 | |
| 15 | "github.com/carlosframework/docs/internal/docs" |
| 16 | ) |
| 17 | |
| 18 | // Once text is on screen it never changes, in WebKit as well as |
| 19 | // Chromium: fixtures/engines/first-reveal.mjs drives both through |
| 20 | // Playwright against Docs behind its production middleware, with the |
| 21 | // hold or the gate script missing, the editor late or absent, and a font |
| 22 | // used only by spaces late (branch reviews on #83). A control page that |
| 23 | // flashes on purpose must be caught, or the check fails as blind. |
| 24 | // |
| 25 | // Needs fixtures/engines installed: `cd fixtures/engines && npm ci && |
| 26 | // npx playwright install chromium webkit`. Without it this fails rather |
| 27 | // than skipping, unless RASTRILLO_BROWSER_OPTIONAL is set. |
| 28 | func TestFontEnginesFirstRevealIsTheFinalLayout(t *testing.T) { |
| 29 | if out, err := runEngines(t, "first-reveal.mjs"); err != nil { |
| 30 | t.Fatalf("the first reveal is not the final layout in every engine: %v\n%s", err, out) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // During a wait for a font the keys a person was editing with stay on |
| 35 | // the page, in WebKit as well as Chromium: Backspace on the page, with |
| 36 | // the editor inert, went back in history in WebKit and the waiting paste |
| 37 | // was lost (branch review 8). fixtures/engines/wait-keys.mjs. |
| 38 | func TestFontEnginesAWaitKeepsThePage(t *testing.T) { |
| 39 | if out, err := runEngines(t, "wait-keys.mjs"); err != nil { |
| 40 | t.Fatalf("a wait for a font let a key leave or move the page: %v\n%s", err, out) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // runEngines serves Docs behind its production middleware, makes fresh |
| 45 | // documents and a signed-in cookie, and runs a Playwright script in |
| 46 | // fixtures/engines against them, in Chromium and WebKit. |
| 47 | func runEngines(t *testing.T, script string) (string, error) { |
| 48 | t.Helper() |
| 49 | out, _, err := runEnginesApp(t, script) |
| 50 | return out, err |
| 51 | } |
| 52 | |
| 53 | // runEnginesApp is runEngines, also handing back the app the script ran |
| 54 | // against, for a test that checks the server afterwards. |
| 55 | func runEnginesApp(t *testing.T, script string) (string, *docs.App, error) { |
| 56 | t.Helper() |
| 57 | if _, err := os.Stat("../../fixtures/engines/node_modules/playwright"); err != nil { |
| 58 | if os.Getenv("RASTRILLO_BROWSER_OPTIONAL") != "" { |
| 59 | t.Skip("fixtures/engines is not installed") |
| 60 | } |
| 61 | t.Fatal("fixtures/engines is not installed: cd fixtures/engines && npm ci && npx playwright install chromium webkit") |
| 62 | } |
| 63 | server := httptest.NewUnstartedServer(nil) |
| 64 | origin := "http://" + server.Listener.Addr().String() |
| 65 | app, err := docs.NewForTestWith(t, docs.Config{Origin: origin}) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | served, done, err := rastrillo.Handler(docs.ServeOptions(rastrillo.Options{}, app)) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if done != nil { |
| 74 | t.Cleanup(func() { _ = done() }) |
| 75 | } |
| 76 | server.Config.Handler = served |
| 77 | server.Start() |
| 78 | t.Cleanup(server.Close) |
| 79 | |
| 80 | // A fresh document per engine and scenario: a scenario that edits its |
| 81 | // document must not hand the next one a document already using the |
| 82 | // family it slows (branch review 6: WebKit ran against Chromium's |
| 83 | // pasted Caveat, so its family was no longer cold). |
| 84 | ids := map[string][]string{} |
| 85 | names := []string{"long", "spaces", "inline", "simple"} |
| 86 | // The replay cases (replay.mjs): a fresh simple document per engine |
| 87 | // and case. |
| 88 | perName := map[string]int{} |
| 89 | if script == "replay.mjs" { |
| 90 | names = append(names, "replay") |
| 91 | perName["replay"] = 2 * 11 |
| 92 | } |
| 93 | // The edits-while-diverged cases (diverged.mjs): the same document. |
| 94 | // The undo and redo cases (undo.mjs): the same document. |
| 95 | if script == "undo.mjs" { |
| 96 | names = append(names, "undo") |
| 97 | perName["undo"] = 2 * 17 |
| 98 | } |
| 99 | if script == "diverged.mjs" { |
| 100 | names = append(names, "diverged") |
| 101 | perName["diverged"] = 2 * 4 |
| 102 | } |
| 103 | // The pickers (pickers.mjs): one simple document per leg and width. |
| 104 | if script == "pickers.mjs" { |
| 105 | names = []string{"simple"} |
| 106 | perName["simple"] = 3 * 3 |
| 107 | } |
| 108 | // The richer-UI checks (richer.mjs): one simple document and one empty |
| 109 | // one per engine, and the list's search. |
| 110 | // The Share popover (share-pop.mjs): desk, busy, first load, phone. |
| 111 | if script == "share-pop.mjs" { |
| 112 | names = []string{"simple"} |
| 113 | perName["simple"] = 2 * 4 |
| 114 | } |
| 115 | // The I2 layout (i2.mjs): a fresh simple document per leg, ten legs |
| 116 | // per engine (the 720px leg, design/phone-web P1, is the tenth). |
| 117 | if script == "i2.mjs" { |
| 118 | names = []string{"simple"} |
| 119 | perName["simple"] = 2 * 10 |
| 120 | } |
| 121 | if script == "richer.mjs" { |
| 122 | names = []string{"simple", "empty"} |
| 123 | perName["simple"] = 2 |
| 124 | perName["empty"] = 2 |
| 125 | } |
| 126 | for _, name := range names { |
| 127 | vector := name |
| 128 | if name == "replay" || name == "diverged" || name == "undo" { |
| 129 | vector = "simple" |
| 130 | } |
| 131 | count := perName[name] |
| 132 | if count == 0 { |
| 133 | count = 2 * 16 |
| 134 | } |
| 135 | var update []byte |
| 136 | if name != "empty" { |
| 137 | update, err = os.ReadFile("../../fixtures/vectors/fragment/" + vector + ".bin") |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | } |
| 142 | for i := 0; i < count; i++ { |
| 143 | doc, err := app.Store().Create(context.Background(), app.TestPerson(t, "engines@example.com"), name) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | if update == nil { |
| 148 | ids[name] = append(ids[name], doc.Sqid) |
| 149 | continue |
| 150 | } |
| 151 | if _, err := app.Store().ApplyUpdate(context.Background(), doc, app.TestActorFor(t, doc), update); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | ids[name] = append(ids[name], doc.Sqid) |
| 155 | } |
| 156 | } |
| 157 | config, _ := json.Marshal(map[string]any{ |
| 158 | "origin": origin, |
| 159 | "docs": ids, |
| 160 | "cookies": signInCookies(t, app, "engines@example.com"), |
| 161 | "probe": probeJS, |
| 162 | }) |
| 163 | cmd := exec.Command("node", script) |
| 164 | cmd.Dir = "../../fixtures/engines" |
| 165 | // Captures must not wait for document.fonts.ready: the interval |
| 166 | // before a slow font is exactly what is being looked at. |
| 167 | cmd.Env = append(os.Environ(), "DOCS_ENGINES_CONFIG="+string(config), "PW_TEST_SCREENSHOT_NO_FONTS_READY=1") |
| 168 | out, err := cmd.CombinedOutput() |
| 169 | t.Logf("%s", out) |
| 170 | return string(out), app, err |
| 171 | } |
| 172 | |