oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3import (
4 "amadan.net/oficina/estilo.git/busy"
5 "amadan.net/oficina/estilo.git/chrome"
6 "amadan.net/oficina/estilo.git/menu"
7 "amadan.net/oficina/estilo.git/savestatus"
8 "amadan.net/oficina/estilo.git/sky"
9 "amadan.net/oficina/estilo.git/theme"
10 "bytes"
11 "os"
12 "path/filepath"
13 "regexp"
14 "strings"
15 "testing"
16
17 "amadan.net/rastrillo/rastrillo/ui"
18)
19
20// vendoredTheme is the theme this app was scaffolded with. Change it
21// here when you swap static/theme.css for another of ui.ThemeNames().
22const vendoredTheme = "day"
23
24// vendoredIsMine names the vendored files this app has edited
25// DELIBERATELY. Add one here when you mean to diverge: this test stops
26// pinning it, and rastrillo doctor leaves it alone too. The file was
27// yours from the moment the scaffold wrote it — this is only where you
28// record that the edit was on purpose, so the next reader (and the next
29// upgrade) can tell it from drift.
30var vendoredIsMine = map[string]bool{
31 // rastrillo's shim as estilo carries it: without the old busy rule,
32 // which would fight busy.js. Pinned to estilo by TestBusyFilesAreEstilos.
33 "rastrillo.js": true,
34 // Calma with Fichas' accent, from estilo (estilo AGENTS.md, decisions
35 // table, row 8). Pinned to estilo instead, by TestThemeIsEstilosForFichas.
36 "theme.css": true,
37 // Deliberately absent. datetime.js opens rastrillo's calendar overlay
38 // when calendar.js is on the page and the browser's own picker when it
39 // is not — which is what Fichas has always had. Adopting the overlay
40 // is a visible change of its own, not part of moving rastrillo's path.
41 "calendar.js": true,
42 // Deliberately absent. The library splits its script into fiesta.js
43 // plus a thin rastrillo.js; estilo's shim (rastrillo.js above) is
44 // whole on its own, so fiesta.js beside it would be a second copy of
45 // the same behaviour.
46 "fiesta.js": true,
47}
48
49// TestThemeIsEstilosForFichas pins static/theme.css to the estilo this app
50// is built against. After an estilo upgrade, re-write it with
51// `go run ./scripts/writeestilo` — never by hand.
52func TestThemeIsEstilosForFichas(t *testing.T) {
53 want, ok := theme.ForApp("fichas")
54 if !ok {
55 t.Fatal("estilo's theme ring has no fichas")
56 }
57 got, err := os.ReadFile(filepath.Join("..", "fichas", "static", "theme.css"))
58 if err != nil {
59 t.Fatal(err)
60 }
61 if !bytes.Equal(got, want) {
62 t.Error("static/theme.css differs from estilo's theme for fichas; run go run ./scripts/writeestilo")
63 }
64}
65
66// The scaffold delivered these files once; they are app-owned from
67// then on. This test pins each vendored copy byte-identical to the
68// library it came from: a reviewer runs the suite instead of reading
69// ~56KB of assets as app diff, and a framework upgrade that forgets
70// to re-copy is caught instead of drifting silently.
71//
72// The list is ui.VendoredAssets — the same function rastrillo new
73// wrote these files from and rastrillo doctor compares them against —
74// so a file added to the library's vendored set is pinned here without
75// this file changing.
76func TestVendoredAssetsMatchTheLibrary(t *testing.T) {
77 assets, ok := ui.VendoredAssets(vendoredTheme)
78 if !ok {
79 t.Fatalf("unknown theme %q", vendoredTheme)
80 }
81 for name, lib := range assets {
82 if vendoredIsMine[name] {
83 continue
84 }
85 vendored, err := os.ReadFile(filepath.Join("..", "fichas", "static", name))
86 if err != nil {
87 t.Errorf("read vendored %s: %v", name, err)
88 continue
89 }
90 if !bytes.Equal(vendored, lib) {
91 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)
92 }
93 }
94}
95
96// rastrillo's busy rule (Paul, 2026-09-29; GETs revised 2026-11-10): a
97// POST's button gives way to a spinner for at least 650ms; a GET goes at
98// once and shows a spinner only after 500ms; the button disables, and
99// Back resets it. static/ carries it as estilo does — the shim without the old
100// rule, busy.js and busy.css — pinned here to the estilo this app builds
101// against. After an estilo upgrade: go run ./scripts/writeestilo.
102func TestBusyFilesAreEstilos(t *testing.T) {
103 for name, want := range map[string][]byte{
104 "rastrillo.js": busy.ShimJS(),
105 "busy.js": busy.JS(),
106 "busy.css": busy.CSS(),
107 } {
108 got, err := os.ReadFile(filepath.Join("..", "fichas", "static", name))
109 if err != nil {
110 t.Fatal(err)
111 }
112 if !bytes.Equal(got, want) {
113 t.Errorf("static/%s differs from estilo's; run go run ./scripts/writeestilo", name)
114 }
115 }
116}
117
118// The sign-in door (estilo chrome's estilo-signin and friends) is styled
119// by chrome.css and sky.css, which static/ carries as estilo does and the
120// sign-in page links. Pinned here to the estilo this app builds against.
121// After an estilo upgrade: go run ./scripts/writeestilo.
122func TestDoorFilesAreEstilos(t *testing.T) {
123 for name, want := range map[string][]byte{
124 "chrome.css": chrome.CSS(),
125 "sky.css": sky.CSS(),
126 // The account menu in the rail: its panel and rows, and the script
127 // that closes it on Escape or an outside click.
128 "menu.css": menu.CSS(),
129 "chrome.js": chrome.JS(),
130 // The bar's save state (level-up §3.1).
131 "savestatus.css": savestatus.CSS(),
132 } {
133 got, err := os.ReadFile(filepath.Join("..", "fichas", "static", name))
134 if err != nil {
135 t.Fatal(err)
136 }
137 if !bytes.Equal(got, want) {
138 t.Errorf("static/%s differs from estilo's; run go run ./scripts/writeestilo", name)
139 }
140 }
141}
142
143// scriptSrc is every <script src> on a page.
144var scriptSrc = regexp.MustCompile(`<script[^>]*\ssrc="([^"]+)"`)
145
146// TestNoOldBusyRuleIsServed: the shim rastrillo ships still carries the
147// old busy rule inside it, and loaded beside estilo's busy.js the two
148// rules fight over the same button. `rastrillo doctor --fix --force`, or
149// a hand re-copy after an upgrade, would put it back without a word, so
150// this reads what the pages actually link and the server actually serves
151// — both shells — rather than the files on disk.
152func TestNoOldBusyRuleIsServed(t *testing.T) {
153 h := newApp(t)
154 // The door: a signed-in browser is sent on from a bare /signin, so
155 // one of its states, drawn for anybody.
156 for _, page := range []string{"/", "/signin?sent=1", "/signed-out"} {
157 srcs := scriptSrc.FindAllStringSubmatch(get(t, h, page).Body.String(), -1)
158 var busyJS int
159 for _, m := range srcs {
160 if strings.Contains(m[1], "/busy.") {
161 busyJS++
162 continue
163 }
164 // busy.js is the rule; any other script carrying it is a second copy.
165 body := get(t, h, m[1]).Body.String()
166 if strings.Contains(body, "function busySubmit") {
167 t.Errorf("%s links %s, which carries the old busy rule; serve estilo's shim (go run ./scripts/writeestilo)", page, m[1])
168 }
169 }
170 if busyJS != 1 {
171 t.Errorf("%s links busy.js %d times, want once", page, busyJS)
172 }
173 }
174}
175