//go:build browser package browsertest import ( "context" "encoding/json" "os" "path/filepath" "testing" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" "amadan.net/oficina/base/internal/config" ) // teamApps is every Oficina app, as the team's BASE_OFICINA_APPS lists them. func teamApps(t *testing.T) []config.OficinaApp { t.Helper() cfg, err := config.Load(func(k string) string { return map[string]string{ "BASE_ORIGIN": "https://base.example.com", "BASE_OWNER": ownerAddress, "BASE_OFICINA_APPS": "calendar=https://calendar.oficina.works,docs=https://docs.oficina.works,sheets=https://sheets.oficina.works," + "slides=https://slides.oficina.works,memoria=https://memoria.oficina.works,calls=https://calls.oficina.works," + "fichas=https://fichas.oficina.works,equipo=https://equipo.bds.oncarlos.com", }[k] }) if err != nil { t.Fatal(err) } return cfg.OficinaApps } // wantApps is the apps' names and hrefs as JSON, for a check in the page. func wantApps(t *testing.T, apps []config.OficinaApp) string { t.Helper() type app struct{ Name, Href string } var out []app for _, a := range apps { out = append(out, app{a.Name, a.Origin + "/"}) } b, err := json.Marshal(out) if err != nil { t.Fatal(err) } return string(b) } // viewportShot saves what is on screen (not the full page) when // BASE_BROWSER_ARTIFACTS is set. func viewportShot(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(() => Promise.all([...document.images].map(i => i.decode().catch(() => {})))).then(() => true)`, chromedp.EvalAwaitPromise)(ctx, target) return err })) png := harness.Read(rig, chromedp.CaptureScreenshot()) 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) } }