oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package renderpage
4
5import (
6 "context"
7 "crypto/sha256"
8 "encoding/hex"
9 "image/png"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14 "testing"
15 "time"
16
17 "amadan.net/oficina/equipo/internal/runner/render"
18 "amadan.net/oficina/equipo/internal/wire"
19)
20
21func helperMain(args []string) (int, bool) {
22 if len(args) > 1 {
23 switch args[1] {
24 case render.ExecSubcommand:
25 return render.ExecMain(args[2:]), true
26 case render.Subcommand:
27 return render.SandboxMain(args[2:], os.Stderr), true
28 }
29 }
30 return 0, false
31}
32
33// siteStep is a step whose sites are two tiny real sites: base a page
34// with a heading, work the same with a block added.
35type siteStep struct{ *fakeStep }
36
37func (s siteStep) Site(ctx context.Context, tree, stage string) (string, error) {
38 if _, err := s.fakeStep.Site(ctx, tree, stage); err != nil {
39 return "", err
40 }
41 block := ""
42 if tree == TreeWork {
43 block = `<div style="position:absolute;left:0;top:0;width:144px;height:90px;background:#1e6b52"></div>`
44 }
45 page := `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Shop ` + tree + `</title>
46<style>html,body{margin:0;background:#fff}h1{margin:200px 0 0 300px;font:20px sans-serif}</style></head>
47<body>` + block + `<h1>Book a cut</h1><a href="/missing.html">missing</a></body></html>`
48 return "digest-" + tree, os.WriteFile(filepath.Join(stage, "index.html"), []byte(page), 0o644)
49}
50
51// TestRealBrowserThroughTheTool (gated on EQUIPO_RENDERER_TREE): the
52// whole path with the pinned browser: the health renders its fixture,
53// then a compare call renders both sites, re-encodes the captures,
54// diffs them and quotes the page's text.
55func TestRealBrowserThroughTheTool(t *testing.T) {
56 tree := os.Getenv("EQUIPO_RENDERER_TREE")
57 if tree == "" {
58 t.Skip("set EQUIPO_RENDERER_TREE to a tree built by scripts/build-renderer-tree.py --tree to render through the tool")
59 }
60 bwrap, err := exec.LookPath("bwrap")
61 if err != nil {
62 t.Skip("no bwrap")
63 }
64 self, err := os.Executable()
65 if err != nil {
66 t.Fatal(err)
67 }
68 manifest, err := os.ReadFile(filepath.Join(tree, "MANIFEST"))
69 if err != nil {
70 t.Fatal(err)
71 }
72 sum := sha256.Sum256(manifest)
73 scratch := filepath.Join(t.TempDir(), "renderer")
74 if err := os.Mkdir(scratch, 0o700); err != nil {
75 t.Fatal(err)
76 }
77 cfg := render.LaunchConfig{Bwrap: bwrap, Binary: self, Tree: tree, Pin: hex.EncodeToString(sum[:])}
78 h := &render.Health{Config: cfg, Scratch: scratch}
79 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
80 defer cancel()
81 h.Start(ctx)
82 if _, health, err := h.Ready(ctx); health != wire.ToolHealthy {
83 t.Fatalf("health %q %v", health, err)
84 }
85 step := siteStep{newFakeStep(t)}
86 tool := &Tool{Renderer: h, Config: cfg}
87 text, caps, err := tool.Call(ctx, step, []byte(`{"pages": [{"path": "/", "viewport": "desktop"}, {"path": "/", "viewport": "phone", "full_page": true}], "compare": true}`))
88 if err != nil {
89 t.Fatalf("%v\n%s", err, text)
90 }
91 t.Logf("reply:\n%s", text)
92 if len(caps) != 6 || caps[4].File != "1-diff-1.png" || caps[4].Changed <= 0 || caps[4].Changed > 0.05 {
93 t.Fatalf("captures %+v", caps)
94 }
95 for _, c := range caps {
96 f, err := os.Open(filepath.Join(step.dir, c.File))
97 if err != nil {
98 t.Fatal(err)
99 }
100 img, err := png.Decode(f)
101 f.Close()
102 if err != nil || img.Bounds().Dx() != c.Width || img.Bounds().Dy() != c.Height || c.Bytes > MaxCaptureBytes {
103 t.Fatalf("%s: %v %v", c.File, img.Bounds(), err)
104 }
105 }
106 for _, want := range []string{`title: "Shop work"`, `title: "Shop base"`, `404: "http://127.0.0.1:`, `| heading "Book a cut"`, "From the rendered page"} {
107 if !strings.Contains(text, want) {
108 t.Fatalf("the reply lacks %q", want)
109 }
110 }
111}
112