| 1 | //go:build browser |
| 2 | |
| 3 | package fichastest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "slices" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/chromedp" |
| 13 | ) |
| 14 | |
| 15 | // File is the first menu, as in Docs, Slides and Mapa (level-up build |
| 16 | // note 24; Paul, 2026-11-22: "Why is there no 'File' menu?"). It keeps |
| 17 | // Card's rows, and has no Import until the Notion databases phase gives |
| 18 | // Fichas an importer. Below 800px the menus are the bar's own second |
| 19 | // row (estilo's Menu sheet carries the types and actions, not the |
| 20 | // menus), so a phone reaches File's very rows; with scripts off File is |
| 21 | // a <details> of links. |
| 22 | |
| 23 | // fileMenu is File's rows as drawn, a divider as "—", the first menu's |
| 24 | // title and whether it is shown, and any import link on the page. |
| 25 | const fileMenu = `(function () { |
| 26 | var first = document.querySelector("[estilo-menubar] [estilo-menubar-title]"); |
| 27 | var file = document.getElementById("fichas-menu-file"); |
| 28 | var rows = Array.prototype.map.call( |
| 29 | file.querySelectorAll("[estilo-menu] > hr, [estilo-menu-label]"), |
| 30 | function (row) { return row.tagName === "HR" ? "—" : row.textContent; }); |
| 31 | return JSON.stringify({first: first.textContent, |
| 32 | shown: first.checkVisibility(), rows: rows, |
| 33 | imports: document.querySelectorAll('a[href*="import"]').length}); |
| 34 | })()` |
| 35 | |
| 36 | // rowsOffScreen names File's rows that are hidden or cross the |
| 37 | // viewport's sides. |
| 38 | const rowsOffScreen = `Array.prototype.filter.call( |
| 39 | document.querySelectorAll("#fichas-menu-file [estilo-menu-item]"), |
| 40 | function (row) { |
| 41 | var box = row.getBoundingClientRect(); |
| 42 | return !row.checkVisibility() || box.left < 0 || box.right > document.documentElement.clientWidth; |
| 43 | }).map(function (row) { return row.textContent; }).join(", ")` |
| 44 | |
| 45 | type fileState struct { |
| 46 | First string |
| 47 | Shown bool |
| 48 | Rows []string |
| 49 | Imports int |
| 50 | } |
| 51 | |
| 52 | func TestFileIsTheFirstMenu(t *testing.T) { |
| 53 | s := seedCards(t, 2) |
| 54 | card := cardPath(s.ids[0]) |
| 55 | want := map[string][]string{ |
| 56 | "/t/t_customer": {"New customer"}, |
| 57 | card: {"New customer", "Open full page", "History", "—", "Delete…"}, |
| 58 | } |
| 59 | for _, live := range []bool{false, true} { |
| 60 | for path, rows := range want { |
| 61 | for _, width := range []int64{1440, 390} { |
| 62 | name := fmt.Sprintf("%s at %dpx, scripts %v", path, width, live) |
| 63 | var raw string |
| 64 | s.rig.Run(scriptsOn(live), viewport(width), |
| 65 | chromedp.Navigate(s.rig.Origin+path), chromedp.WaitReady(`[estilo-bar]`), |
| 66 | harness.Into(&raw, chromedp.Evaluate[string](fileMenu))) |
| 67 | var got fileState |
| 68 | if err := json.Unmarshal([]byte(raw), &got); err != nil { |
| 69 | t.Fatalf("%s: %v", name, err) |
| 70 | } |
| 71 | if got.First != "File" || !got.Shown { |
| 72 | t.Errorf("%s: the first menu is %q, shown %v", name, got.First, got.Shown) |
| 73 | } |
| 74 | if !slices.Equal(got.Rows, rows) { |
| 75 | t.Errorf("%s: File is %q, want %q", name, got.Rows, rows) |
| 76 | } |
| 77 | // The phone reaches File's very rows: open, each is shown |
| 78 | // and inside the viewport's width. |
| 79 | var off string |
| 80 | s.rig.Run(chromedp.Click(`#fichas-menu-file > summary`), |
| 81 | harness.Into(&off, chromedp.Evaluate[string](rowsOffScreen))) |
| 82 | if off != "" { |
| 83 | t.Errorf("%s: File's rows not on screen: %s", name, off) |
| 84 | } |
| 85 | if got.Imports != 0 { |
| 86 | t.Errorf("%s: %d import links, before Fichas has an importer", name, got.Imports) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | // File → New customer opens the form, by pointer, on a phone |
| 91 | // and wide. |
| 92 | for _, width := range []int64{1440, 390} { |
| 93 | s.rig.Run(scriptsOn(live), viewport(width), |
| 94 | chromedp.Navigate(s.rig.Origin+card), |
| 95 | chromedp.Click(`#fichas-menu-file > summary`), |
| 96 | chromedp.Click(`#fichas-menu-file a[href^="/cards/new"]`), |
| 97 | chromedp.WaitReady(`form[action="/cards"]`)) |
| 98 | } |
| 99 | } |
| 100 | s.rig.Run(scriptsOn(true)) |
| 101 | for _, width := range []int64{1440, 390} { |
| 102 | axeCheckWith(t, s.rig, card, width, chromedp.Click(`#fichas-menu-file > summary`), |
| 103 | chromedp.WaitVisible(`#fichas-menu-file a[href$="/history"]`)) |
| 104 | } |
| 105 | } |
| 106 | |