oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "amadan.net/oficina/estilo.git/busy"
5 "bytes"
6 "fmt"
7 "os"
8 "path/filepath"
9 "regexp"
10 "sort"
11 "testing"
12
13 "amadan.net/oficina/estilo.git/theme"
14 "amadan.net/rastrillo/rastrillo/ui"
15)
16
17// vendoredTheme is the theme this app was scaffolded with. Change it
18// here when you swap static/theme.css for another of ui.ThemeNames().
19const vendoredTheme = "day"
20
21// vendoredIsMine names the vendored files this app has edited
22// DELIBERATELY. Add one here when you mean to diverge: this test stops
23// pinning it, and rastrillo doctor leaves it alone too. The file was
24// yours from the moment the scaffold wrote it — this is only where you
25// record that the edit was on purpose, so the next reader (and the next
26// upgrade) can tell it from drift.
27var vendoredIsMine = map[string]bool{
28 // rastrillo's shim as estilo carries it: without the old busy rule,
29 // which would fight busy.js. Pinned to estilo by TestBusyFilesAreEstilos.
30 "rastrillo.js": true,
31 // estilo's busy.js, for the same reason: the busy rule as the suite
32 // carries it. ui's own copy moved on at rastrillo fb806a2 (pow.js
33 // arming) and estilo's has not, so the two can no longer both be
34 // pinned. Pinned to estilo by TestBusyFilesAreEstilos.
35 "busy.js": true,
36 // Calma with Slides' accent, from estilo (estilo AGENTS.md, decisions
37 // table, row 8) rather than rastrillo's day. Pinned to estilo instead,
38 // by TestThemeIsEstilosForSlides below.
39 "theme.css": true,
40 // Files the library ships that Slides deliberately does not vendor
41 // (removed on purpose, so doctor lists them as yours and --fix leaves
42 // them alone). No layout links them: live.js (Slides' live sync is its
43 // own, live.go), anchor.js (the menus are estilo's), light.js,
44 // transition.js and copy.js (no page uses them), and fiesta.js, which
45 // pairs with rastrillo.js, and Slides' rastrillo.js is estilo's shim.
46 // Leaving them out kept the pages as they were across the v0.27.1 to
47 // v0.42.0 upgrade.
48 "live.js": true,
49 "anchor.js": true,
50 "light.js": true,
51 "transition.js": true,
52 "copy.js": true,
53 "fiesta.js": true,
54}
55
56// TestThemeIsEstilosForSlides pins static/theme.css to the estilo this app
57// is built against. After an estilo upgrade, re-write it with
58// `go run ./scripts/writeestilo` — never by hand.
59func TestThemeIsEstilosForSlides(t *testing.T) {
60 want, ok := theme.ForApp("slides")
61 if !ok {
62 t.Fatal("estilo's theme ring has no slides")
63 }
64 got, err := os.ReadFile(filepath.Join("..", "slides", "static", "theme.css"))
65 if err != nil {
66 t.Fatal(err)
67 }
68 if !bytes.Equal(got, want) {
69 t.Error("static/theme.css differs from estilo's theme for slides; run go run ./scripts/writeestilo")
70 }
71}
72
73// The scaffold delivered these files once; they are app-owned from
74// then on. This test pins each vendored copy byte-identical to the
75// library it came from: a reviewer runs the suite instead of reading
76// ~56KB of assets as app diff, and a framework upgrade that forgets
77// to re-copy is caught instead of drifting silently.
78//
79// The list is ui.VendoredAssets — the same function rastrillo new
80// wrote these files from and rastrillo doctor compares them against —
81// so a file added to the library's vendored set is pinned here without
82// this file changing.
83func TestVendoredAssetsMatchTheLibrary(t *testing.T) {
84 assets, ok := ui.VendoredAssets(vendoredTheme)
85 if !ok {
86 t.Fatalf("unknown theme %q", vendoredTheme)
87 }
88 for name, lib := range assets {
89 if vendoredIsMine[name] {
90 continue
91 }
92 vendored, err := os.ReadFile(filepath.Join("..", "slides", "static", name))
93 if err != nil {
94 t.Errorf("read vendored %s: %v", name, err)
95 continue
96 }
97 if !bytes.Equal(vendored, lib) {
98 t.Errorf("static/%s differs from the library copy; re-copy it with rastrillo doctor --fix (or add it to vendoredIsMine if the edit was deliberate)", name)
99 }
100 }
101}
102
103// TestEveryRastrilloTokenSlidesUsesExists makes a misspelled design token a
104// test failure instead of a browser falling back silently. The vocabulary is
105// the committed tokens.css; usages include both authored templates and CSS.
106func TestEveryRastrilloTokenSlidesUsesExists(t *testing.T) {
107 declared := map[string]bool{}
108 definitions := append(append([]byte{}, ui.TokensCSS()...), theme.CSS()...)
109 for _, match := range regexp.MustCompile(`--(rst-[a-z0-9-]+)\s*:`).FindAllSubmatch(definitions, -1) {
110 declared[string(match[1])] = true
111 }
112 if len(declared) == 0 {
113 t.Fatal("tokens.css declares no --rst-* tokens")
114 }
115 files, err := filepath.Glob(filepath.Join("..", "slides", "templates", "*.html"))
116 if err != nil {
117 t.Fatal(err)
118 }
119 css, err := filepath.Glob(filepath.Join("..", "slides", "static", "*.css"))
120 if err != nil {
121 t.Fatal(err)
122 }
123 for _, file := range css {
124 // tokens.css is rastrillo's vendored component implementation;
125 // TestVendoredAssetsMatchTheLibrary pins it. This gate is for
126 // Slides-authored CSS and inline template styles.
127 if filepath.Base(file) != "tokens.css" {
128 files = append(files, file)
129 }
130 }
131 used := map[string]bool{}
132 pattern := regexp.MustCompile(`var\(--(rst-[a-z0-9-]+)`)
133 comments := regexp.MustCompile(`(?s)/\*.*?\*/`)
134 for _, file := range files {
135 body, err := os.ReadFile(file)
136 if err != nil {
137 t.Fatal(err)
138 }
139 for _, match := range pattern.FindAllSubmatch(comments.ReplaceAll(body, nil), -1) {
140 used[string(match[1])] = true
141 }
142 }
143 var missing []string
144 for token := range used {
145 if !declared[token] {
146 missing = append(missing, token)
147 }
148 }
149 sort.Strings(missing)
150 for _, token := range missing {
151 t.Error(fmt.Sprintf("Slides uses --%s, but tokens.css does not declare it", token))
152 }
153}
154
155// rastrillo's busy rule (Paul, 2026-09-29): a submit button's label gives
156// way to a spinner for at least 650ms, the button disables, and Back
157// resets it. static/ carries it as estilo does — the shim without the old
158// rule, busy.js and busy.css — pinned here to the estilo this app builds
159// against. After an estilo upgrade: go run ./scripts/writeestilo.
160func TestBusyFilesAreEstilos(t *testing.T) {
161 for name, want := range map[string][]byte{
162 "rastrillo.js": busy.ShimJS(),
163 "busy.js": busy.JS(),
164 "busy.css": busy.CSS(),
165 } {
166 got, err := os.ReadFile(filepath.Join("..", "slides", "static", name))
167 if err != nil {
168 t.Fatal(err)
169 }
170 if !bytes.Equal(got, want) {
171 t.Errorf("static/%s differs from estilo's; run go run ./scripts/writeestilo", name)
172 }
173 }
174}
175