| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/chromedp/chromedp" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/harness" |
| 15 | |
| 16 | "amadan.net/oficina/base/internal/config" |
| 17 | ) |
| 18 | |
| 19 | // teamApps is every Oficina app, as the team's BASE_OFICINA_APPS lists them. |
| 20 | func teamApps(t *testing.T) []config.OficinaApp { |
| 21 | t.Helper() |
| 22 | cfg, err := config.Load(func(k string) string { |
| 23 | return map[string]string{ |
| 24 | "BASE_ORIGIN": "https://base.example.com", "BASE_OWNER": ownerAddress, |
| 25 | "BASE_OFICINA_APPS": "calendar=https://calendar.oficina.works,docs=https://docs.oficina.works,sheets=https://sheets.oficina.works," + |
| 26 | "slides=https://slides.oficina.works,memoria=https://memoria.oficina.works,calls=https://calls.oficina.works," + |
| 27 | "fichas=https://fichas.oficina.works,equipo=https://equipo.bds.oncarlos.com", |
| 28 | }[k] |
| 29 | }) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | return cfg.OficinaApps |
| 34 | } |
| 35 | |
| 36 | // wantApps is the apps' names and hrefs as JSON, for a check in the page. |
| 37 | func wantApps(t *testing.T, apps []config.OficinaApp) string { |
| 38 | t.Helper() |
| 39 | type app struct{ Name, Href string } |
| 40 | var out []app |
| 41 | for _, a := range apps { |
| 42 | out = append(out, app{a.Name, a.Origin + "/"}) |
| 43 | } |
| 44 | b, err := json.Marshal(out) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | return string(b) |
| 49 | } |
| 50 | |
| 51 | // viewportShot saves what is on screen (not the full page) when |
| 52 | // BASE_BROWSER_ARTIFACTS is set. |
| 53 | func viewportShot(t *testing.T, rig *harness.Rig, name string) { |
| 54 | t.Helper() |
| 55 | dir := os.Getenv("BASE_BROWSER_ARTIFACTS") |
| 56 | if dir == "" { |
| 57 | return |
| 58 | } |
| 59 | drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 60 | _, err := chromedp.Evaluate[bool](`document.fonts.ready.then(() => Promise.all([...document.images].map(i => i.decode().catch(() => {})))).then(() => true)`, chromedp.EvalAwaitPromise)(ctx, target) |
| 61 | return err |
| 62 | })) |
| 63 | png := harness.Read(rig, chromedp.CaptureScreenshot()) |
| 64 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if err := os.WriteFile(filepath.Join(dir, name+".png"), png, 0o644); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | } |
| 71 | |