carlos / platform Public

console: opt-in screenshots of the operator pages built so far

TestOperatorScreenshots renders index, dashboard, boxes and store at 1280
and 390 px, light and dark, when CONSOLE_OPS_SHOTS_DIR is set and chromium
is on the PATH. A review aid, not acceptance. Comparing the shots with
the mockup found one fault, fixed here: the dashboard's Load meter was
wrapped in a span, so it did not fill its column as the mockup's does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3cf87557758de47139eea0134cedb250dd5c8cfd parent 868b337
2 files changed, +126 −1
  • internal/console/opshots_test.go +125 −0
  • internal/console/templates/ops_dashboard.html +1 −1

CI failed — run details

diff --git a/internal/console/opshots_test.go b/internal/console/opshots_test.go
new file mode 100644
index 000000000..bd44e1d7f
--- /dev/null
+++ b/internal/console/opshots_test.go
@@ -0,0 +1,125 @@
+package console
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "net/http/httputil"
+ "net/url"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+)
+
+// opShotPages is every operator page built so far, by its shot name.
+// Task 14 adds the rest.
+var opShotPages = []struct{ name, path string }{
+ {"index", "/fleet/index"},
+ {"dashboard", "/fleet"},
+ {"boxes", "/fleet/boxes"},
+ {"store", "/fleet/store"},
+}
+
+// TestOperatorScreenshots is a review aid, not acceptance: it renders every
+// operator page on the seeded world at 1280 and 390 px wide, light and
+// dark, with headless Chromium, into $CONSOLE_OPS_SHOTS_DIR as
+// <page>-<width>-<scheme>.png. It is skipped unless that directory is set
+// and chromium is on the PATH.
+//
+// Two servers on 127.0.0.1 stand in front of the console: each adds Paul's
+// session cookie to every request, and the dark one also marks every HTML
+// page data-theme="dark", which day.css honours, so the dark shot does not
+// depend on the machine's colour scheme.
+func TestOperatorScreenshots(t *testing.T) {
+ dir := os.Getenv("CONSOLE_OPS_SHOTS_DIR")
+ if dir == "" {
+ t.Skip("CONSOLE_OPS_SHOTS_DIR is not set")
+ }
+ chromium, err := exec.LookPath("chromium")
+ if err != nil {
+ t.Skip("no chromium on the PATH")
+ }
+ fx := newConsoleFixture(t)
+ w := seedOperatorWorld(t, fx)
+ target, err := url.Parse(fx.ts.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cookies := w.c.Jar.Cookies(target)
+ if len(cookies) == 0 {
+ t.Fatal("the signed-in browser holds no cookie")
+ }
+ front := func(dark bool) *httptest.Server {
+ proxy := httputil.NewSingleHostReverseProxy(target)
+ director := proxy.Director
+ proxy.Director = func(r *http.Request) {
+ director(r)
+ r.Header.Del("Accept-Encoding")
+ r.Header.Del("Cookie")
+ for _, c := range cookies {
+ r.AddCookie(c)
+ }
+ }
+ if dark {
+ proxy.ModifyResponse = func(resp *http.Response) error {
+ if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") {
+ return nil
+ }
+ body, err := io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if err != nil {
+ return err
+ }
+ body = bytes.Replace(body, []byte(`<html lang="en"`), []byte(`<html lang="en" data-theme="dark"`), 1)
+ resp.Body = io.NopCloser(bytes.NewReader(body))
+ resp.ContentLength = int64(len(body))
+ resp.Header.Set("Content-Length", strconv.Itoa(len(body)))
+ return nil
+ }
+ }
+ srv := httptest.NewServer(proxy)
+ t.Cleanup(srv.Close)
+ return srv
+ }
+ schemes := []struct {
+ name string
+ srv *httptest.Server
+ }{{"light", front(false)}, {"dark", front(true)}}
+
+ for _, pg := range opShotPages {
+ for _, width := range []int{1280, 390} {
+ for _, sc := range schemes {
+ out := filepath.Join(dir, pg.name+"-"+strconv.Itoa(width)+"-"+sc.name+".png")
+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
+ cmd := exec.CommandContext(ctx, chromium, "--headless=new", "--no-sandbox", "--disable-gpu", "--hide-scrollbars",
+ "--user-data-dir="+filepath.Join(dir, "profile"), "--window-size="+strconv.Itoa(width)+",2000",
+ "--screenshot="+out, sc.srv.URL+pg.path)
+ msg, err := cmd.CombinedOutput()
+ cancel()
+ if err != nil {
+ t.Fatalf("chromium %s: %v\n%s", out, err, msg)
+ }
+ if st, err := os.Stat(out); err != nil || st.Size() == 0 {
+ t.Fatalf("no screenshot at %s: %v", out, err)
+ }
+ }
+ }
+ }
+ light, err := os.ReadFile(filepath.Join(dir, "dashboard-1280-light.png"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ dark, err := os.ReadFile(filepath.Join(dir, "dashboard-1280-dark.png"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if bytes.Equal(light, dark) {
+ t.Error("the light and dark dashboard shots are the same image")
+ }
+}
diff --git a/internal/console/templates/ops_dashboard.html b/internal/console/templates/ops_dashboard.html
index 808a8b381..d1a8c5e89 100644
--- a/internal/console/templates/ops_dashboard.html
+++ b/internal/console/templates/ops_dashboard.html
@@ -73,7 +73,7 @@
<div rst-card co-cols="boxes">
<div rst-lrow="head"><span>Box</span><span>Status</span><span>Region</span><span>Load</span><span>Last report</span></div>
{{- range .Boxes}}
- <div rst-lrow><a class="rst-nm" href="/fleet/boxes#box-{{.Label}}">{{.Label}}</a><span>{{template "op_status" .Status}}</span>{{if eq .Region .Unknown}}<span class="rst-cell-mut">{{.Region}}</span>{{else}}<span>{{.Region}}</span>{{end}}{{with .Meter}}<span>{{template "op_meter" .}}</span>{{else}}<span class="rst-cell-mut">{{.Unknown}}</span>{{end}}{{if .LastSeen.IsZero}}<span class="rst-cell-mut">{{.Unknown}}</span>{{else}}<span class="rst-cell-mut" title="{{fmtTime .LastSeen}}">{{ago .LastSeen}}</span>{{end}}</div>
+ <div rst-lrow><a class="rst-nm" href="/fleet/boxes#box-{{.Label}}">{{.Label}}</a><span>{{template "op_status" .Status}}</span>{{if eq .Region .Unknown}}<span class="rst-cell-mut">{{.Region}}</span>{{else}}<span>{{.Region}}</span>{{end}}{{with .Meter}}{{template "op_meter" .}}{{else}}<span class="rst-cell-mut">{{.Unknown}}</span>{{end}}{{if .LastSeen.IsZero}}<span class="rst-cell-mut">{{.Unknown}}</span>{{else}}<span class="rst-cell-mut" title="{{fmtTime .LastSeen}}">{{ago .LastSeen}}</span>{{end}}</div>
{{- end}}
</div>
{{else}}{{template "op_empty" (opEmpty "" "No box has published a reading yet.")}}{{end}}{{end}}