| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/oficina/base/internal/store" |
| 16 | ) |
| 17 | |
| 18 | // TestScreenshots walks the first slice's screens as a signed-in owner and, |
| 19 | // when BASE_BROWSER_ARTIFACTS names a directory, saves a full-page PNG of |
| 20 | // each there for a person to compare with the approved mockup. Nothing here |
| 21 | // judges pixels; each screen still passes the harness's checks. |
| 22 | func TestScreenshots(t *testing.T) { |
| 23 | dir := os.Getenv("BASE_BROWSER_ARTIFACTS") |
| 24 | if dir == "" { |
| 25 | t.Log("BASE_BROWSER_ARTIFACTS is not set: walking the screens without saving them") |
| 26 | } else if err := os.MkdirAll(dir, 0o755); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | w := newWorld(t) |
| 30 | rig := w.rig |
| 31 | drive(t, rig, chromedp.EmulateViewport(1280, 800)) |
| 32 | |
| 33 | shot := func(name, selector, note string) { |
| 34 | t.Helper() |
| 35 | rig.Screen(selector, note) |
| 36 | // The page is drawn in its own face, not a fallback caught mid-load. |
| 37 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.readyState === "complete"`, chromedp.WithPollingTimeout(10*time.Second)), |
| 38 | chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 39 | _, err := chromedp.Evaluate[bool](`document.fonts.ready.then(() => true)`, chromedp.EvalAwaitPromise)(ctx, target) |
| 40 | return err |
| 41 | })) |
| 42 | if dir == "" { |
| 43 | return |
| 44 | } |
| 45 | var png []byte |
| 46 | drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 47 | var err error |
| 48 | png, err = chromedp.FullScreenshot(100)(ctx, target) |
| 49 | return err |
| 50 | })) |
| 51 | if err := os.WriteFile(filepath.Join(dir, name+".png"), png, 0o644); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | setInstall := func(app, state string) { |
| 56 | t.Helper() |
| 57 | if err := store.SetInstallState(context.Background(), w.base.db, app, state, ""); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | drive(t, rig, chromedp.Navigate(w.baseOrigin+"/signin"), chromedp.WaitVisible(chromedp.CSS(passkeyDoor))) |
| 63 | shot("1-signin", ".card", "Sign in") |
| 64 | |
| 65 | signInByEmail(t, rig, w.baseOrigin, w.base.mail) |
| 66 | drive(t, rig, chromedp.WaitVisible(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), |
| 67 | chromedp.WaitVisible(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.welcomeOrigin+"/")))) |
| 68 | shot("2-home", "main", "Home, Welcome and Scratchpad installed") |
| 69 | |
| 70 | invite := chromedp.CSS(`form[action="/people"] input[name="email"]`) |
| 71 | drive(t, rig, chromedp.Navigate(w.baseOrigin+"/people"), chromedp.WaitVisible(invite), |
| 72 | chromedp.SendKeys(invite, "maria@acme.co"), |
| 73 | chromedp.Click(chromedp.CSS(`form[action="/people"] button[type="submit"]`)), |
| 74 | // The invited member's face, drawn only once the invite has landed. |
| 75 | chromedp.WaitVisible(chromedp.CSS(`.people .face.off`))) |
| 76 | shot("3-people", ".sheet", "People, one member invited") |
| 77 | |
| 78 | // Store: Welcome installed, Scratchpad on its way. |
| 79 | setInstall("scratchpad", "installing") |
| 80 | drive(t, rig, chromedp.Navigate(w.baseOrigin+"/store"), chromedp.WaitVisible(chromedp.CSS(`button.installing`))) |
| 81 | shot("4-store", ".sheet", "Store, one app installed and one installing") |
| 82 | setInstall("scratchpad", "installed") |
| 83 | |
| 84 | drive(t, rig, chromedp.Navigate(w.baseOrigin+"/settings"), chromedp.WaitVisible(chromedp.CSS(addPasskey))) |
| 85 | shot("5-settings", ".sheet", "Settings") |
| 86 | |
| 87 | drive(t, rig, chromedp.Navigate(w.baseOrigin+"/"), |
| 88 | chromedp.Click(chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))), |
| 89 | chromedp.WaitVisible(chromedp.CSS(`textarea[name="note"]`))) |
| 90 | shot("6-scratchpad", ".sheet", "Scratchpad") |
| 91 | if dir != "" { |
| 92 | t.Logf("screenshots in %s", dir) |
| 93 | } |
| 94 | } |
| 95 | |