//go:build linux package renderpage import ( "context" "crypto/sha256" "encoding/hex" "image/png" "os" "os/exec" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner/render" "amadan.net/oficina/equipo/internal/wire" ) func helperMain(args []string) (int, bool) { if len(args) > 1 { switch args[1] { case render.ExecSubcommand: return render.ExecMain(args[2:]), true case render.Subcommand: return render.SandboxMain(args[2:], os.Stderr), true } } return 0, false } // siteStep is a step whose sites are two tiny real sites: base a page // with a heading, work the same with a block added. type siteStep struct{ *fakeStep } func (s siteStep) Site(ctx context.Context, tree, stage string) (string, error) { if _, err := s.fakeStep.Site(ctx, tree, stage); err != nil { return "", err } block := "" if tree == TreeWork { block = `
` } page := `Shop ` + tree + ` ` + block + `

Book a cut

missing` return "digest-" + tree, os.WriteFile(filepath.Join(stage, "index.html"), []byte(page), 0o644) } // TestRealBrowserThroughTheTool (gated on EQUIPO_RENDERER_TREE): the // whole path with the pinned browser: the health renders its fixture, // then a compare call renders both sites, re-encodes the captures, // diffs them and quotes the page's text. func TestRealBrowserThroughTheTool(t *testing.T) { tree := os.Getenv("EQUIPO_RENDERER_TREE") if tree == "" { t.Skip("set EQUIPO_RENDERER_TREE to a tree built by scripts/build-renderer-tree.py --tree to render through the tool") } bwrap, err := exec.LookPath("bwrap") if err != nil { t.Skip("no bwrap") } self, err := os.Executable() if err != nil { t.Fatal(err) } manifest, err := os.ReadFile(filepath.Join(tree, "MANIFEST")) if err != nil { t.Fatal(err) } sum := sha256.Sum256(manifest) scratch := filepath.Join(t.TempDir(), "renderer") if err := os.Mkdir(scratch, 0o700); err != nil { t.Fatal(err) } cfg := render.LaunchConfig{Bwrap: bwrap, Binary: self, Tree: tree, Pin: hex.EncodeToString(sum[:])} h := &render.Health{Config: cfg, Scratch: scratch} ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() h.Start(ctx) if _, health, err := h.Ready(ctx); health != wire.ToolHealthy { t.Fatalf("health %q %v", health, err) } step := siteStep{newFakeStep(t)} tool := &Tool{Renderer: h, Config: cfg} text, caps, err := tool.Call(ctx, step, []byte(`{"pages": [{"path": "/", "viewport": "desktop"}, {"path": "/", "viewport": "phone", "full_page": true}], "compare": true}`)) if err != nil { t.Fatalf("%v\n%s", err, text) } t.Logf("reply:\n%s", text) if len(caps) != 6 || caps[4].File != "1-diff-1.png" || caps[4].Changed <= 0 || caps[4].Changed > 0.05 { t.Fatalf("captures %+v", caps) } for _, c := range caps { f, err := os.Open(filepath.Join(step.dir, c.File)) if err != nil { t.Fatal(err) } img, err := png.Decode(f) f.Close() if err != nil || img.Bounds().Dx() != c.Width || img.Bounds().Dy() != c.Height || c.Bytes > MaxCaptureBytes { t.Fatalf("%s: %v %v", c.File, img.Bounds(), err) } } for _, want := range []string{`title: "Shop work"`, `title: "Shop base"`, `404: "http://127.0.0.1:`, `| heading "Book a cut"`, "From the rendered page"} { if !strings.Contains(text, want) { t.Fatalf("the reply lacks %q", want) } } }