| new file mode 100644 |
| index 0000000..a918dbe |
| --- /dev/null |
| +++ b/internal/browsertest/mobile_test.go |
| @@ -0,0 +1,153 @@ |
| +//go:build browser |
| + |
| +package browsertest |
| + |
| +import ( |
| + "context" |
| + "net/http" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| + |
| + "github.com/chromedp/chromedp" |
| + |
| + "amadan.net/rastrillo/rastrillo/harness" |
| + |
| + "amadan.net/carlos/base/internal/equipo/equipotest" |
| +) |
| + |
| +// noOverflow fails if the page is wider than the phone's screen. |
| +func noOverflow(t *testing.T, rig *harness.Rig, page string) { |
| + t.Helper() |
| + if got := harness.Evaluate[string](rig, `(() => { |
| + const w = document.documentElement.clientWidth, s = document.documentElement.scrollWidth; |
| + if (s <= w) return ""; |
| + const wide = [...document.querySelectorAll("body *")].filter(e => e.getBoundingClientRect().right > w + 0.5) |
| + .slice(0, 5).map(e => e.tagName.toLowerCase() + "." + e.className + " " + Math.round(e.getBoundingClientRect().right)); |
| + return s + " > " + w + ": " + wide.join(", "); |
| + })()`); got != "" { |
| + t.Errorf("%s overflows a 390px screen: %s", page, got) |
| + } |
| +} |
| + |
| +// mobileShot saves a full-page PNG when BASE_BROWSER_ARTIFACTS is set. |
| +func mobileShot(t *testing.T, rig *harness.Rig, name string) { |
| + t.Helper() |
| + dir := os.Getenv("BASE_BROWSER_ARTIFACTS") |
| + if dir == "" { |
| + return |
| + } |
| + drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| + _, err := chromedp.Evaluate[bool](`document.fonts.ready.then(() => true)`, chromedp.EvalAwaitPromise)(ctx, target) |
| + return err |
| + })) |
| + var png []byte |
| + drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| + var err error |
| + png, err = chromedp.FullScreenshot(100)(ctx, target) |
| + return err |
| + })) |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, name+".png"), png, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// Base on an iPhone-sized screen: every page fits 390px, Home's top bar |
| +// stays on one line, and the panel is a full-screen sheet whose composer |
| +// stays on screen. |
| +func TestMobile(t *testing.T) { |
| + f := equipotest.New(t) |
| + f.Reply = func(string) string { |
| + return "You can ask me to plan work, check on a project, or start a task. What would you like to do?" |
| + } |
| + var b *baseApp |
| + rig := newRig(t, func(origin string) http.Handler { |
| + cfg := baseConfig(origin) |
| + cfg.EquipoOrigin = f.URL |
| + b = newBase(t, cfg) |
| + return b.handler |
| + }) |
| + drive(t, rig, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| + drive(t, rig, chromedp.Navigate(rig.Origin+"/signin"), chromedp.WaitVisible(chromedp.CSS(`input[name="address"]`))) |
| + noOverflow(t, rig, "sign-in") |
| + signInByEmail(t, rig, rig.Origin, b.mail) |
| + |
| + // Home: the top bar on one line, no "/" hint. |
| + noOverflow(t, rig, "Home") |
| + if got := harness.Evaluate[string](rig, `(() => { |
| + const bar = document.querySelector(".topbar"), ask = document.querySelector("a.ask"); |
| + const tops = [...bar.children].filter(e => getComputedStyle(e).display !== "none").map(e => Math.round(e.getBoundingClientRect().top + e.getBoundingClientRect().height / 2)); |
| + const problems = []; |
| + if (Math.max(...tops) - Math.min(...tops) > 2) problems.push("top bar wraps: " + tops.join(",")); |
| + if (ask.getBoundingClientRect().height > 44) problems.push("Ask field is " + ask.getBoundingClientRect().height + "px tall"); |
| + if (ask.scrollWidth > ask.clientWidth) problems.push("Ask field text overflows"); |
| + const hint = getComputedStyle(ask, "::after"); |
| + if (hint.content !== "none" && hint.display !== "none") problems.push("shows the / hint"); |
| + return problems.join("; "); |
| + })()`); got != "" { |
| + t.Errorf("Home's top bar on a phone: %s", got) |
| + } |
| + mobileShot(t, rig, "1-home") |
| + |
| + // The panel before connecting. |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`a.ask`)), |
| + chromedp.WaitVisible(chromedp.CSS(`#ask-panel [data-when="connect"] button`))) |
| + mobileShot(t, rig, "2-panel-connect") |
| + |
| + // Connect from the panel: the code page, then back to the open panel. |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel [data-when="connect"] button`)), chromedp.WaitVisible(chromedp.CSS(`p.code`))) |
| + noOverflow(t, rig, "the connect page") |
| + mobileShot(t, rig, "3-connect-code") |
| + if err := f.Approve(harness.Evaluate[string](rig, `document.querySelector("p.code").textContent`)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := b.db.Exec(`UPDATE equipo_links SET polled_at = ?`, time.Now().Add(-time.Minute).UTC()); err != nil { |
| + t.Fatal(err) |
| + } |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`form[action="/ask/connect/check"] button`)), |
| + chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`))) |
| + |
| + // Mid-conversation: the sheet fills the screen, the header is at the |
| + // top, the composer on screen at the bottom, targets 44px. |
| + drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => !m.classList.contains("draft"))`, |
| + chromedp.WithPollingTimeout(10*time.Second)), |
| + chromedp.Poll[chromedp.Void](`(() => { const r = document.querySelector("#ask-panel").getBoundingClientRect(); return r.left === 0 && r.top === 0 })()`, |
| + chromedp.WithPollingTimeout(3*time.Second))) |
| + if got := harness.Evaluate[string](rig, `(() => { |
| + const vh = window.visualViewport.height, vw = window.visualViewport.width, problems = []; |
| + const p = document.querySelector("#ask-panel").getBoundingClientRect(); |
| + if (Math.round(p.width) !== Math.round(vw) || Math.round(p.height) !== Math.round(vh)) problems.push("sheet " + p.width + "x" + p.height); |
| + const head = document.querySelector("#ask-panel .ask-head").getBoundingClientRect(); |
| + if (head.top < 0 || head.top > 1) problems.push("header at " + head.top); |
| + const c = document.querySelector("#ask-panel form.composer").getBoundingClientRect(); |
| + if (c.top < 0 || c.bottom > vh + 0.5 || c.height === 0) problems.push("composer at " + c.top + "-" + c.bottom + " of " + vh); |
| + for (const el of document.querySelectorAll("#ask-panel .ask-head .btn, #ask-panel form.composer .btn, #ask-panel textarea")) { |
| + const r = el.getBoundingClientRect(); |
| + if (r.height < 44 || r.width < 44) problems.push(el.textContent.trim() + " is " + r.width + "x" + r.height); |
| + } |
| + const m = document.querySelector("#ask-panel .msg.coordinator").getBoundingClientRect(); |
| + if (m.right > vw) problems.push("a message overflows"); |
| + return problems.join("; "); |
| + })()`); got != "" { |
| + t.Errorf("the panel on a phone: %s", got) |
| + } |
| + mobileShot(t, rig, "4-panel-conversation") |
| + drive(t, rig, chromedp.KeyEvent("\u001b")) |
| + |
| + for _, p := range []struct{ path, wait, name string }{ |
| + {"/store", ".sheet", "5-store"}, |
| + {"/people", ".sheet", "6-people"}, |
| + {"/settings", ".sheet", "7-settings"}, |
| + {"/ask", ".ask-page .composer", "8-ask"}, |
| + } { |
| + drive(t, rig, chromedp.Navigate(rig.Origin+p.path), chromedp.WaitVisible(chromedp.CSS(p.wait))) |
| + noOverflow(t, rig, p.path) |
| + mobileShot(t, rig, p.name) |
| + } |
| +} |