oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package render
2
3import (
4 "context"
5 "fmt"
6 "image"
7 "image/color"
8 "image/png"
9 "os"
10 "path/filepath"
11)
12
13// FixturePage is the page the installers and verify-installed render to
14// show the renderer works (spec section 3, "Pin and provisioning"): one
15// block of fixtureColour at fixtureBlock on fixtureBackground, static,
16// with nothing to load, so every render of it paints the same pixels.
17const FixturePage = `<!doctype html>
18<html lang="en">
19<head>
20<meta charset="utf-8">
21<title>Renderer check</title>
22<style>
23html, body { margin: 0; background: #ffffff; }
24#block { position: absolute; left: 100px; top: 100px; width: 200px; height: 200px; background: #1e6b52; }
25</style>
26</head>
27<body><div id="block"></div></body>
28</html>
29`
30
31// What FixturePage paints at the desktop viewport (CSS pixels are
32// device pixels there).
33var (
34 fixtureBlock = image.Rect(100, 100, 300, 300)
35 fixtureColour = color.RGBA{0x1e, 0x6b, 0x52, 0xff}
36 fixtureBackground = color.RGBA{0xff, 0xff, 0xff, 0xff}
37)
38
39// CheckFixture renders FixturePage at the desktop viewport with cfg's
40// renderer (cfg.Site is replaced by the fixture's own site) and checks
41// the capture: no error code, one page, and the sampled pixels the
42// page's. The site and output directories are made in scratch, a
43// directory only this user can write, and removed again. A tree that
44// does not verify is the launch's *release.RendererError.
45func CheckFixture(ctx context.Context, cfg LaunchConfig, scratch string) error {
46 dir, err := os.MkdirTemp(scratch, ".fixture-")
47 if err != nil {
48 return err
49 }
50 defer os.RemoveAll(dir)
51 site, out := filepath.Join(dir, "site"), filepath.Join(dir, "out")
52 if err = os.Mkdir(site, 0o700); err != nil {
53 return err
54 }
55 if err = os.WriteFile(filepath.Join(site, "index.html"), []byte(FixturePage), 0o600); err != nil {
56 return err
57 }
58 if err = os.Mkdir(out, 0o700); err != nil {
59 return err
60 }
61 // Mkdir's mode follows the umask: Render wants its output private.
62 if err = os.Chmod(out, 0o700); err != nil {
63 return err
64 }
65 cfg.Site = site
66 report, err := Render(ctx, cfg, out, []Page{{Path: "/", Viewport: Desktop}})
67 if err != nil {
68 return err
69 }
70 if report.Error != "" || len(report.Pages) != 1 || report.Pages[0].Capture != "page-1.png" {
71 return fmt.Errorf("render: the fixture page rendered as %+v", report)
72 }
73 f, err := os.Open(filepath.Join(out, report.Pages[0].Capture))
74 if err != nil {
75 return err
76 }
77 defer f.Close()
78 img, err := png.Decode(f)
79 if err != nil {
80 return fmt.Errorf("render: the fixture capture: %w", err)
81 }
82 return checkFixtureCapture(img)
83}
84
85// checkFixtureCapture samples a capture of FixturePage: the viewport's
86// size, the block's colour at its corners and centre, the background's
87// just outside it and far from it.
88func checkFixtureCapture(img image.Image) error {
89 w, h, _ := Desktop.Size()
90 if b := img.Bounds(); b != image.Rect(0, 0, w, h) {
91 return fmt.Errorf("render: the fixture capture is %v, not %dx%d", b, w, h)
92 }
93 in := fixtureBlock
94 samples := map[image.Point]color.RGBA{
95 in.Min: fixtureColour, {in.Max.X - 1, in.Max.Y - 1}: fixtureColour, {(in.Min.X + in.Max.X) / 2, (in.Min.Y + in.Max.Y) / 2}: fixtureColour,
96 {in.Min.X - 1, in.Min.Y}: fixtureBackground, {in.Max.X, in.Max.Y - 1}: fixtureBackground, {w - 1, h - 1}: fixtureBackground,
97 }
98 for p, want := range samples {
99 if got := color.RGBAModel.Convert(img.At(p.X, p.Y)).(color.RGBA); got != want {
100 return fmt.Errorf("render: the fixture capture is not the fixture page: %v at %v, want %v", got, p, want)
101 }
102 }
103 return nil
104}
105