| 1 | //go:build browser |
| 2 | |
| 3 | package sheets |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/estilo.git/googleimport" |
| 14 | "amadan.net/rastrillo/fiesta/check/rhythm" |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | "github.com/chromedp/cdproto/emulation" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | ) |
| 19 | |
| 20 | // TestEveryScreenKeepsTheRhythm: each of Sheets' own screens, read by |
| 21 | // Fiesta's rhythm audit (Paul, 2026-11-22: "Can we somehow put |
| 22 | // something in place to avoid weird spacing issues like this in |
| 23 | // future?"), served through the production stack. Every gap between |
| 24 | // two of a page's blocks is a step of the spacing scale, no text rests |
| 25 | // on a control, and every heading sits closer to its own content than |
| 26 | // to what comes before it, at a desktop width and a phone's. |
| 27 | // |
| 28 | // The audit reads around what is not the page's to space: the grid |
| 29 | // (Jspreadsheet's canvas, its toolbar, menus and tab strip, which have |
| 30 | // a density of their own) and the cells of the scriptless table view, |
| 31 | // which are the sheet's. |
| 32 | func TestEveryScreenKeepsTheRhythm(t *testing.T) { |
| 33 | app, cookies := signIn(t) |
| 34 | s, cells := newSheet(t, app, cookies, "Quarterly plan") |
| 35 | postCell(t, app, cookies, cells, `{"r":0,"c":0,"raw":"Revenue"}`) |
| 36 | // A workbook whose report has lines, so the report's list is read. |
| 37 | report := importWorkbook(t, app, cookies, googleWorkbook(t, googleRows)) |
| 38 | |
| 39 | // The import page with Google Drive configured: its section is |
| 40 | // drawn only then. |
| 41 | google := New(Config{DB: app.db, Auth: app.auth, Sealer: app.sealer, Origin: testOrigin, Admit: app.admit, |
| 42 | GoogleImport: googleimport.Config{ClientID: "123456789-client.apps.googleusercontent.com", APIKey: "AIzaPublicBrowserKey123456789", AppID: "123456789"}}) |
| 43 | |
| 44 | // Both instances on one origin: a page asked for with |
| 45 | // ?instance=google, and what it loads, are the Google one's. |
| 46 | served, googleServed := productionHandler(t, app), productionHandler(t, google) |
| 47 | rig := harness.New(t, func(origin string) http.Handler { |
| 48 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 49 | if r.URL.Query().Get("instance") == "google" || strings.Contains(r.Header.Get("Referer"), "instance=google") { |
| 50 | googleServed.ServeHTTP(w, r) |
| 51 | return |
| 52 | } |
| 53 | served.ServeHTTP(w, r) |
| 54 | }) |
| 55 | }) |
| 56 | // Each screen is loaded once, at a desktop width, and read there and |
| 57 | // again at a phone's. ready is a selector only that screen draws, the |
| 58 | // browser must still be on path when it is read (a lost session |
| 59 | // lands on the sign-in door, whose rhythm is not this page's), and |
| 60 | // main must hold at least blocks children, so a page that drew |
| 61 | // nothing cannot pass by having nothing to measure. |
| 62 | audit := func(name, path, ready string, blocks int, skip []string) { |
| 63 | t.Helper() |
| 64 | for i, width := range []int64{1280, 390} { |
| 65 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 66 | var found []rhythm.Finding |
| 67 | var at string |
| 68 | var drawn int |
| 69 | actions := []chromedp.Action[chromedp.Void]{ |
| 70 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1}), |
| 71 | } |
| 72 | if i == 0 { |
| 73 | actions = append(actions, chromedp.Navigate(rig.Origin+path)) |
| 74 | } |
| 75 | actions = append(actions, |
| 76 | chromedp.WaitVisible(chromedp.CSS(ready)), |
| 77 | evalInto(&at, `location.pathname + location.search`), |
| 78 | evalInto(&drawn, `[...document.querySelector('main').children].filter(e => e.getClientRects().length > 0).length`), |
| 79 | evalInto(&found, rhythm.Expression("main", rhythm.Options{Skip: skip}))) |
| 80 | err := chromedp.Do(ctx, actions...) |
| 81 | cancel() |
| 82 | if err != nil { |
| 83 | t.Fatalf("%s at %dpx: %v", name, width, err) |
| 84 | } |
| 85 | if at != path { |
| 86 | t.Fatalf("%s: the browser is at %s, not %s", name, at, path) |
| 87 | } |
| 88 | if drawn < blocks { |
| 89 | t.Fatalf("%s at %dpx: main draws %d blocks, want at least %d", name, width, drawn, blocks) |
| 90 | } |
| 91 | if len(found) > 0 { |
| 92 | t.Errorf("%s at %dpx: %s", name, width, rhythm.Report(found)) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | audit("the sign-in door", "/signin", `[estilo-signin-screen] input[name="address"]`, 1, nil) |
| 98 | |
| 99 | var set []chromedp.Action[chromedp.Void] |
| 100 | for _, c := range cookies { |
| 101 | set = append(set, setCookie(rig.Origin, c)) |
| 102 | } |
| 103 | rig.Run(set...) |
| 104 | |
| 105 | sheet := fmt.Sprintf("/sheets/%d", s.ID) |
| 106 | audit("the list", "/sheets", `[data-sheet-list]`, 2, nil) |
| 107 | // The sheet is the grid and its chrome, all of it a component's |
| 108 | // own: what is left is the formula row and the notices. |
| 109 | audit("the sheet", sheet, `td[data-x="0"][data-y="0"]`, 1, |
| 110 | []string{"[data-sheet-grid]", "[data-menubar]", "[data-toolbar]", "[data-tabstrip]"}) |
| 111 | audit("share", sheet+"/share", `#role`, 6, nil) |
| 112 | audit("comments", sheet+"/comments", `main textarea[name], main form[rst-form]`, 3, nil) |
| 113 | audit("history", sheet+"/history", `main form[action$="/undo"]`, 3, nil) |
| 114 | audit("the table view", sheet+"/table", `table[data-cell-table]`, 3, []string{"table[data-cell-table]"}) |
| 115 | audit("import", "/sheets/import", `main input[type=file]`, 2, nil) |
| 116 | audit("an import report", report, `[data-import-report]`, 3, nil) |
| 117 | |
| 118 | audit("import, with Google Drive", "/sheets/import?instance=google", `main [estilo-google-import]`, 3, nil) |
| 119 | } |
| 120 | |