//go:build browser package mapatest import ( "context" "encoding/json" "fmt" "io" "log/slog" "net/http" "path/filepath" "slices" "testing" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // File is the first menu, as in Docs and Slides (level-up build note // 87; Paul, 2026-11-22: "Why is there no 'File' menu? I would have // thought File -> Import was a thing"). Import from Notion is in File // for an importer and nowhere for anyone else, and never in the // account menu. Below 800px the menus are the bar's own second row // (estilo's Menu sheet carries the places and actions, not the menus), // so a phone reaches File's very rows; with scripts off, File is a //
and its rows are links. // importerRig is the app with Ana as its one importer, signed in as // who, over pages Bo wrote. func importerRig(t *testing.T, who string) *harness.Rig { return harness.New(t, func(origin string) http.Handler { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) database, err := db.Open(filepath.Join(t.TempDir(), "mapa.db"), logger) if err != nil { t.Fatal(err) } t.Cleanup(func() { database.Close() }) env := testEnv(Ana + "," + Bo) env["MAPA_ORIGIN"], env["MAPA_IMPORTERS"] = origin, Ana config, err := mapa.LoadConfig(func(key string) string { return env[key] }) if err != nil { t.Fatal(err) } built, err := mapa.AppForTest(database, config, logger, who) if err != nil { t.Fatal(err) } if err := mapa.SeedSynthetic(context.Background(), built.Store, Bo, 1); err != nil { t.Fatal(err) } return asking("en-IE", built.Mux) }) } // fileRows is the File menu's rows as drawn, a divider as "—", and // whether File is the first menu and shown. const fileRows = `(function () { var first = document.querySelector("[estilo-menubar] [estilo-menubar-title]"); var file = document.getElementById("mapa-menu-file"); var rows = Array.prototype.map.call( file.querySelectorAll("[estilo-menu] > hr, [estilo-menu-label]"), function (row) { return row.tagName === "HR" ? "—" : row.textContent; }); return JSON.stringify({first: first.textContent, shown: first.checkVisibility(), rows: rows}); })()` type fileState struct { First string Shown bool Rows []string } // rowsOffScreen names File's rows that are hidden or cross the // viewport's sides. const rowsOffScreen = `Array.prototype.filter.call( document.querySelectorAll("#mapa-menu-file [estilo-menu-item]"), function (row) { var box = row.getBoundingClientRect(); return !row.checkVisibility() || box.left < 0 || box.right > document.documentElement.clientWidth; }).map(function (row) { return row.textContent; }).join(", ")` // importLinks counts links to the import: in all, in File and in the // account menu. const importLinks = `JSON.stringify([ document.querySelectorAll('a[href^="/import"]').length, document.querySelectorAll('#mapa-menu-file a[href="/import/notion"]').length, document.querySelectorAll('[estilo-account] a[href^="/import"]').length])` func TestFileIsTheFirstMenuAndHoldsTheImport(t *testing.T) { for _, who := range []string{Ana, Bo} { rig := importerRig(t, who) path := firstTeamPage(t, rig) importer := who == Ana for _, live := range []bool{false, true} { for _, at := range []string{"/", "/pages", path} { var wide []string for _, width := range []int64{1440, 390} { name := fmt.Sprintf("%s at %dpx as %s, scripts %v", at, width, who, live) drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+at), chromedp.WaitVisible("[estilo-bar]")) var state fileState readJSON(t, rig, fileRows, &state) if state.First != "File" || !state.Shown { t.Errorf("%s: the first menu is %q, shown %v", name, state.First, state.Shown) } var links [3]int readJSON(t, rig, importLinks, &links) switch { case importer && links != [3]int{1, 1, 0}: t.Errorf("%s: import links (all, File, account) "+ "%v, want [1 1 0]", name, links) case !importer && links != [3]int{0, 0, 0}: t.Errorf("%s: a non-importer is offered the "+ "import: %v", name, links) } if slices.Contains(state.Rows, "Import from Notion…") != importer { t.Errorf("%s: File is %q", name, state.Rows) } // The phone reaches File's very rows: open, each is // shown and inside the viewport's width. drive(t, rig, press(`#mapa-menu-file > summary`)) if off := read(t, rig, chromedp.Evaluate[string]( rowsOffScreen)); off != "" { t.Errorf("%s: File's rows not on screen: %s", name, off) } if wide == nil { wide = state.Rows } else if !slices.Equal(wide, state.Rows) { t.Errorf("%s: File is %q, at 1440 %q", name, state.Rows, wide) } } } } if !importer { continue } // File → Import from Notion… opens the import, by pointer and // by keyboard, on a phone and wide, scripts on and off. for _, live := range []bool{false, true} { for _, width := range []int64{1440, 390} { drive(t, rig, scripts(live), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+path), chromedp.Click(`#mapa-menu-file > summary`), chromedp.Click(`#mapa-menu-file a[href="/import/notion"]`), chromedp.WaitVisible(`input[name="token"]`)) drive(t, rig, chromedp.Navigate(rig.Origin+"/"), press(`#mapa-menu-file > summary`), press(`#mapa-menu-file a[href="/import/notion"]`), chromedp.WaitVisible(`input[name="token"]`)) } } } } // axe finds nothing serious with File open, for an importer, wide and // on a phone. func TestFileOpenPassesAxe(t *testing.T) { rig := importerRig(t, Ana) path := firstTeamPage(t, rig) for _, width := range []int64{1440, 390} { drive(t, rig, scripts(true), chromedp.EmulateViewport(width, 900), chromedp.Navigate(rig.Origin+path), chromedp.Click(`#mapa-menu-file > summary`), chromedp.WaitVisible(`#mapa-menu-file a[href="/import/notion"]`)) if found := axeSerious(t, rig); found != "" { t.Errorf("%dpx, File open: axe:\n%s", width, found) } } } // readJSON evaluates script, which returns JSON, into out. func readJSON(t *testing.T, rig *harness.Rig, script string, out any) { t.Helper() raw := read(t, rig, chromedp.Evaluate[string](script)) if err := json.Unmarshal([]byte(raw), out); err != nil { t.Fatalf("reading %s: %v", raw, err) } }