//go:build browser package browsertest import ( "context" "fmt" "os" "path/filepath" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/oficina/base/internal/store" ) // TestScreenshots walks the first slice's screens as a signed-in owner and, // when BASE_BROWSER_ARTIFACTS names a directory, saves a full-page PNG of // each there for a person to compare with the approved mockup. Nothing here // judges pixels; each screen still passes the harness's checks. func TestScreenshots(t *testing.T) { dir := os.Getenv("BASE_BROWSER_ARTIFACTS") if dir == "" { t.Log("BASE_BROWSER_ARTIFACTS is not set: walking the screens without saving them") } else if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } w := newWorld(t) rig := w.rig drive(t, rig, chromedp.EmulateViewport(1280, 800)) shot := func(name, selector, note string) { t.Helper() rig.Screen(selector, note) // The page is drawn in its own face, not a fallback caught mid-load. drive(t, rig, chromedp.Poll[chromedp.Void](`document.readyState === "complete"`, chromedp.WithPollingTimeout(10*time.Second)), 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 })) if dir == "" { return } 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.WriteFile(filepath.Join(dir, name+".png"), png, 0o644); err != nil { t.Fatal(err) } } setInstall := func(app, state string) { t.Helper() if err := store.SetInstallState(context.Background(), w.base.db, app, state, ""); err != nil { t.Fatal(err) } } drive(t, rig, chromedp.Navigate(w.baseOrigin+"/signin"), chromedp.WaitVisible(chromedp.CSS(passkeyDoor))) shot("1-signin", ".card", "Sign in") signInByEmail(t, rig, w.baseOrigin, w.base.mail) drive(t, rig, chromedp.WaitVisible(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), chromedp.WaitVisible(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.welcomeOrigin+"/")))) shot("2-home", "main", "Home, Welcome and Scratchpad installed") invite := chromedp.CSS(`form[action="/people"] input[name="email"]`) drive(t, rig, chromedp.Navigate(w.baseOrigin+"/people"), chromedp.WaitVisible(invite), chromedp.SendKeys(invite, "maria@acme.co"), chromedp.Click(chromedp.CSS(`form[action="/people"] button[type="submit"]`)), // The invited member's face, drawn only once the invite has landed. chromedp.WaitVisible(chromedp.CSS(`.people .face.off`))) shot("3-people", ".sheet", "People, one member invited") // Store: Welcome installed, Scratchpad on its way. setInstall("scratchpad", "installing") drive(t, rig, chromedp.Navigate(w.baseOrigin+"/store"), chromedp.WaitVisible(chromedp.CSS(`button.installing`))) shot("4-store", ".sheet", "Store, one app installed and one installing") setInstall("scratchpad", "installed") drive(t, rig, chromedp.Navigate(w.baseOrigin+"/settings"), chromedp.WaitVisible(chromedp.CSS(addPasskey))) shot("5-settings", ".sheet", "Settings") drive(t, rig, chromedp.Navigate(w.baseOrigin+"/"), chromedp.Click(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), chromedp.WaitVisible(chromedp.CSS(`textarea[name="note"]`))) shot("6-scratchpad", ".sheet", "Scratchpad") if dir != "" { t.Logf("screenshots in %s", dir) } }