| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "log/slog" |
| 11 | "net/http" |
| 12 | "path/filepath" |
| 13 | "slices" |
| 14 | "testing" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo/db" |
| 17 | "amadan.net/rastrillo/rastrillo/harness" |
| 18 | "github.com/chromedp/chromedp" |
| 19 | |
| 20 | "amadan.net/oficina/mapa/internal/mapa" |
| 21 | ) |
| 22 | |
| 23 | // File is the first menu, as in Docs and Slides (level-up build note |
| 24 | // 87; Paul, 2026-11-22: "Why is there no 'File' menu? I would have |
| 25 | // thought File -> Import was a thing"). Import from Notion is in File |
| 26 | // for an importer and nowhere for anyone else, and never in the |
| 27 | // account menu. Below 800px the menus are the bar's own second row |
| 28 | // (estilo's Menu sheet carries the places and actions, not the menus), |
| 29 | // so a phone reaches File's very rows; with scripts off, File is a |
| 30 | // <details> and its rows are links. |
| 31 | |
| 32 | // importerRig is the app with Ana as its one importer, signed in as |
| 33 | // who, over pages Bo wrote. |
| 34 | func importerRig(t *testing.T, who string) *harness.Rig { |
| 35 | return harness.New(t, func(origin string) http.Handler { |
| 36 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 37 | database, err := db.Open(filepath.Join(t.TempDir(), "mapa.db"), |
| 38 | logger) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | t.Cleanup(func() { database.Close() }) |
| 43 | env := testEnv(Ana + "," + Bo) |
| 44 | env["MAPA_ORIGIN"], env["MAPA_IMPORTERS"] = origin, Ana |
| 45 | config, err := mapa.LoadConfig(func(key string) string { |
| 46 | return env[key] |
| 47 | }) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | built, err := mapa.AppForTest(database, config, logger, who) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if err := mapa.SeedSynthetic(context.Background(), built.Store, |
| 56 | Bo, 1); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | return asking("en-IE", built.Mux) |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | // fileRows is the File menu's rows as drawn, a divider as "—", and |
| 64 | // whether File is the first menu and shown. |
| 65 | const fileRows = `(function () { |
| 66 | var first = document.querySelector("[estilo-menubar] [estilo-menubar-title]"); |
| 67 | var file = document.getElementById("mapa-menu-file"); |
| 68 | var rows = Array.prototype.map.call( |
| 69 | file.querySelectorAll("[estilo-menu] > hr, [estilo-menu-label]"), |
| 70 | function (row) { return row.tagName === "HR" ? "—" : row.textContent; }); |
| 71 | return JSON.stringify({first: first.textContent, |
| 72 | shown: first.checkVisibility(), rows: rows}); |
| 73 | })()` |
| 74 | |
| 75 | type fileState struct { |
| 76 | First string |
| 77 | Shown bool |
| 78 | Rows []string |
| 79 | } |
| 80 | |
| 81 | // rowsOffScreen names File's rows that are hidden or cross the |
| 82 | // viewport's sides. |
| 83 | const rowsOffScreen = `Array.prototype.filter.call( |
| 84 | document.querySelectorAll("#mapa-menu-file [estilo-menu-item]"), |
| 85 | function (row) { |
| 86 | var box = row.getBoundingClientRect(); |
| 87 | return !row.checkVisibility() || box.left < 0 || |
| 88 | box.right > document.documentElement.clientWidth; |
| 89 | }).map(function (row) { return row.textContent; }).join(", ")` |
| 90 | |
| 91 | // importLinks counts links to the import: in all, in File and in the |
| 92 | // account menu. |
| 93 | const importLinks = `JSON.stringify([ |
| 94 | document.querySelectorAll('a[href^="/import"]').length, |
| 95 | document.querySelectorAll('#mapa-menu-file a[href="/import/notion"]').length, |
| 96 | document.querySelectorAll('[estilo-account] a[href^="/import"]').length])` |
| 97 | |
| 98 | func TestFileIsTheFirstMenuAndHoldsTheImport(t *testing.T) { |
| 99 | for _, who := range []string{Ana, Bo} { |
| 100 | rig := importerRig(t, who) |
| 101 | path := firstTeamPage(t, rig) |
| 102 | importer := who == Ana |
| 103 | for _, live := range []bool{false, true} { |
| 104 | for _, at := range []string{"/", "/pages", path} { |
| 105 | var wide []string |
| 106 | for _, width := range []int64{1440, 390} { |
| 107 | name := fmt.Sprintf("%s at %dpx as %s, scripts %v", at, |
| 108 | width, who, live) |
| 109 | drive(t, rig, scripts(live), |
| 110 | chromedp.EmulateViewport(width, 900), |
| 111 | chromedp.Navigate(rig.Origin+at), |
| 112 | chromedp.WaitVisible("[estilo-bar]")) |
| 113 | var state fileState |
| 114 | readJSON(t, rig, fileRows, &state) |
| 115 | if state.First != "File" || !state.Shown { |
| 116 | t.Errorf("%s: the first menu is %q, shown %v", |
| 117 | name, state.First, state.Shown) |
| 118 | } |
| 119 | var links [3]int |
| 120 | readJSON(t, rig, importLinks, &links) |
| 121 | switch { |
| 122 | case importer && links != [3]int{1, 1, 0}: |
| 123 | t.Errorf("%s: import links (all, File, account) "+ |
| 124 | "%v, want [1 1 0]", name, links) |
| 125 | case !importer && links != [3]int{0, 0, 0}: |
| 126 | t.Errorf("%s: a non-importer is offered the "+ |
| 127 | "import: %v", name, links) |
| 128 | } |
| 129 | if slices.Contains(state.Rows, "Import from Notion…") != |
| 130 | importer { |
| 131 | t.Errorf("%s: File is %q", name, state.Rows) |
| 132 | } |
| 133 | // The phone reaches File's very rows: open, each is |
| 134 | // shown and inside the viewport's width. |
| 135 | drive(t, rig, press(`#mapa-menu-file > summary`)) |
| 136 | if off := read(t, rig, chromedp.Evaluate[string]( |
| 137 | rowsOffScreen)); off != "" { |
| 138 | t.Errorf("%s: File's rows not on screen: %s", name, |
| 139 | off) |
| 140 | } |
| 141 | if wide == nil { |
| 142 | wide = state.Rows |
| 143 | } else if !slices.Equal(wide, state.Rows) { |
| 144 | t.Errorf("%s: File is %q, at 1440 %q", name, |
| 145 | state.Rows, wide) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | if !importer { |
| 151 | continue |
| 152 | } |
| 153 | // File → Import from Notion… opens the import, by pointer and |
| 154 | // by keyboard, on a phone and wide, scripts on and off. |
| 155 | for _, live := range []bool{false, true} { |
| 156 | for _, width := range []int64{1440, 390} { |
| 157 | drive(t, rig, scripts(live), |
| 158 | chromedp.EmulateViewport(width, 900), |
| 159 | chromedp.Navigate(rig.Origin+path), |
| 160 | chromedp.Click(`#mapa-menu-file > summary`), |
| 161 | chromedp.Click(`#mapa-menu-file a[href="/import/notion"]`), |
| 162 | chromedp.WaitVisible(`input[name="token"]`)) |
| 163 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), |
| 164 | press(`#mapa-menu-file > summary`), |
| 165 | press(`#mapa-menu-file a[href="/import/notion"]`), |
| 166 | chromedp.WaitVisible(`input[name="token"]`)) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // axe finds nothing serious with File open, for an importer, wide and |
| 173 | // on a phone. |
| 174 | func TestFileOpenPassesAxe(t *testing.T) { |
| 175 | rig := importerRig(t, Ana) |
| 176 | path := firstTeamPage(t, rig) |
| 177 | for _, width := range []int64{1440, 390} { |
| 178 | drive(t, rig, scripts(true), chromedp.EmulateViewport(width, 900), |
| 179 | chromedp.Navigate(rig.Origin+path), |
| 180 | chromedp.Click(`#mapa-menu-file > summary`), |
| 181 | chromedp.WaitVisible(`#mapa-menu-file a[href="/import/notion"]`)) |
| 182 | if found := axeSerious(t, rig); found != "" { |
| 183 | t.Errorf("%dpx, File open: axe:\n%s", width, found) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // readJSON evaluates script, which returns JSON, into out. |
| 189 | func readJSON(t *testing.T, rig *harness.Rig, script string, out any) { |
| 190 | t.Helper() |
| 191 | raw := read(t, rig, chromedp.Evaluate[string](script)) |
| 192 | if err := json.Unmarshal([]byte(raw), out); err != nil { |
| 193 | t.Fatalf("reading %s: %v", raw, err) |
| 194 | } |
| 195 | } |
| 196 | |