oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package memoriatest
2
3import (
4 "encoding/base64"
5 "errors"
6 "fmt"
7 "go/ast"
8 "go/parser"
9 "go/token"
10 "net/http"
11 "os"
12 "path/filepath"
13 "regexp"
14 "strings"
15 "testing"
16
17 memoria "amadan.net/oficina/memoria/internal/memoria"
18)
19
20// canary is text that must never appear in bytes at rest. It is a phrase
21// no library or schema would produce, so a hit is our leak and not a
22// coincidence.
23const canary = "ZEBRAFINCH-QUARTERLY-REVENUE-8834"
24
25// Gate 1. The one rule: the database file never holds a readable
26// passage.
27//
28// This reads the raw bytes rather than querying, because a query would
29// go through the code that decrypts and would pass no matter what is on
30// disk. The WAL is read too: SQLite writes there first, and a gate that
31// checks only the main file passes for as long as a checkpoint has not
32// happened — which in a short test is always.
33//
34// MUTATION-TESTED 2026-09-03: replacing s.sealer.Seal with a plain
35// []byte(...) cast in Store.Ingest makes this fail on both the chunk and
36// the title, naming the file it found the canary in. Reverted.
37func TestPlaintextNeverReachesTheDatabase(t *testing.T) {
38 dir := t.TempDir()
39 path := filepath.Join(dir, "app.db")
40 h, _ := newAppAt(t, path)
41
42 body := "# " + canary + "\n\nJune revenue was 1,350,000 and " + canary + " again in the body.\n"
43 rec := store(t, h, canary+" as a title", body)
44 // The vacuity guard, and it has to be this specific. An earlier
45 // version checked `rec.Code != 303` and then searched for the string
46 // "result" — but the handler redirects with 303 on FAILURE too
47 // (/?err=empty, /?stored=0&refused=1), and "result" is a substring
48 // of "0 results". With
49 // Ingest mutated to always error, that version PASSED against an
50 // empty database: the gate below was scanning a file with nothing in
51 // it. Assert the success location and real result markup.
52 if rec.Code != 303 {
53 t.Fatalf("store: status %d, want 303", rec.Code)
54 }
55 if loc := rec.Header().Get("Location"); loc != "/add?stored=1" {
56 t.Fatalf("store redirected to %q, want /add?stored=1 — the document was not stored, "+
57 "so the scan below would pass vacuously", loc)
58 }
59 if got := get(t, h, "/?q=zebrafinch").Body.String(); !strings.Contains(got, "<mark>") {
60 t.Fatal("no highlighted result: the document was not indexed, and the scan below proves nothing")
61 }
62
63 entries, err := os.ReadDir(dir)
64 if err != nil {
65 t.Fatal(err)
66 }
67 checked := 0
68 for _, e := range entries {
69 if e.IsDir() {
70 continue
71 }
72 raw, err := os.ReadFile(filepath.Join(dir, e.Name()))
73 if err != nil {
74 t.Fatal(err)
75 }
76 checked++
77 if bytesContainsFold(raw, canary) {
78 t.Errorf("%s holds the canary in plaintext: the one rule is broken", e.Name())
79 }
80 }
81 if checked == 0 {
82 t.Fatal("no files to check; the gate proved nothing")
83 }
84}
85
86// Gate 1's other half: no plaintext reaches a TEMP file either.
87//
88// ParseMultipartForm's argument is a MEMORY threshold, and Go spends it
89// cumulatively across parts — it holds file parts in memory until the
90// running total passes the threshold and writes every part after that to
91// a temp file, in the clear, with no error raised anywhere. One 2 MiB
92// file could not reach that with the old single-file cap. Twenty-five
93// can, trivially, which is why this gate exists now and did not before.
94//
95// It works because httptest calls the handler directly: net/http's own
96// server calls MultipartForm.RemoveAll() when the request finishes, so
97// in production a spilled file is deleted — after having been written.
98// Here nothing deletes it, so a spill is still on disk to be found.
99//
100// MUTATION-TESTED 2026-09-04: ParseMultipartForm's argument changed back
101// to maxUpload — the per-file cap, which is what it was before
102// multi-file — makes this fail naming the spilled file (the suffix is
103// random, e.g. "multipart-578803712 in the temp directory holds the
104// canary in plaintext: Go spilled a part to disk") and again with
105// "1 files were left in the temp directory". Reverted.
106func TestAMultiFileDropNeverSpillsPlaintextToATempFile(t *testing.T) {
107 // The app first, so its database does not land in the directory
108 // being scanned: TMPDIR is what os.CreateTemp and t.TempDir both
109 // read, and the point here is to watch one specific directory.
110 h := newApp(t)
111 spill := t.TempDir()
112 t.Setenv("TMPDIR", spill)
113
114 // Twelve parts of half a MiB. Under any per-file threshold this
115 // passes the budget several times over, because the budget is spent
116 // across parts and not per part.
117 body := strings.Repeat(canary+" and more revenue text to pad this passage out.\n\n", 8000)
118 files := make([]file, 0, 12)
119 for i := 0; i < 12; i++ {
120 files = append(files, file{fmt.Sprintf("spill-%02d.txt", i), body})
121 }
122 rec := submit(t, h, "", "", files...)
123
124 // The vacuity guard. A submission that was refused would leave the
125 // directory empty and this gate would pass having watched nothing.
126 if loc := rec.Header().Get("Location"); loc != "/add?stored=12" {
127 t.Fatalf("the drop answered %q rather than storing all 12; the scan below would "+
128 "prove nothing", loc)
129 }
130
131 found := 0
132 err := filepath.Walk(spill, func(p string, info os.FileInfo, err error) error {
133 if err != nil || info.IsDir() {
134 return err
135 }
136 found++
137 raw, err := os.ReadFile(p)
138 if err != nil {
139 return err
140 }
141 if bytesContainsFold(raw, canary) {
142 t.Errorf("%s in the temp directory holds the canary in plaintext: Go spilled a "+
143 "part to disk. ParseMultipartForm's argument is a memory threshold, not a "+
144 "limit, and it is spent across parts — it must be at least the whole body "+
145 "cap or a large drop writes somebody's documents to /tmp in the clear.",
146 filepath.Base(p))
147 }
148 return nil
149 })
150 if err != nil {
151 t.Fatal(err)
152 }
153 if found > 0 {
154 t.Errorf("%d files were left in the temp directory by a drop that should have been "+
155 "parsed entirely in memory", found)
156 }
157}
158
159// Gate 15's egress half. With the assistant off, nothing leaves the
160// instance — and in slice 0 there is no assistant at all, so the honest
161// form of this gate is that no app package can make an outbound request.
162//
163// A runtime gate would be better and is not available: httptest cannot
164// observe a dial that never happens. So this is a source gate, and it is
165// mutation-tested by adding an http.Get to the store, which makes it
166// fail.
167//
168// MUTATION-TESTED 2026-09-03: adding `http.Get("https://example.com")`
169// to Store.Ingest makes this fail naming store.go. Reverted.
170//
171// AMENDED 2026-09-05 for the Notion connector, which must dial
172// api.notion.com. The blanket ban survives; one directory is exempt and
173// pays for the exemption with a runtime host pin. See allowedNetworkDirs.
174//
175// AMENDED AGAIN 2026-09-06 for slice 1's sibling client. Same shape, same
176// price: the package is exempt from the grep and pays for it with a
177// runtime guard the grep cannot perform. Two entries now, and the comment
178// on the list says why a third should be harder.
179//
180// MUTATION-TESTED 2026-09-05: adding an `*http.Client` field to
181// internal/memoria/handlers.go still makes this fail, so narrowing the
182// gate did not blunt it. Reverted.
183// MUTATION-TESTED 2026-09-06: adding a third directory to
184// allowedNetworkDirs fails the count assertion; adding an
185// `http.NewRequest` to handlers.go still fails the blacklist. Reverted.
186func TestNoOutboundNetworkInAppCode(t *testing.T) {
187 // A blacklist, and it is worth saying so: this is a source
188 // heuristic, not a proof. A review defeated the first version with
189 // `new(http.Client).Post(...)`, which none of six spellings matched.
190 // The list is broader now and the walk covers cmd/ and tools/ too,
191 // but the honest statement of what it buys is "an accidental
192 // outbound call is caught"; a determined one is not.
193 forbidden := []string{
194 `http\.Get\(`, `http\.Post\(`, `http\.PostForm\(`, `http\.Head\(`,
195 `http\.Client\{`, `new\(http\.Client\)`, `\*http\.Client`,
196 `http\.DefaultClient`, `http\.DefaultTransport`, `http\.NewRequest`,
197 `net\.Dial`, `net\.Lookup`, `smtp\.`, `os/exec`, `exec\.Command`,
198 `url\.Parse\(.*https?://`,
199 }
200 pats := make([]*regexp.Regexp, 0, len(forbidden))
201 for _, f := range forbidden {
202 pats = append(pats, regexp.MustCompile(f))
203 }
204 // EXACTLY THREE packages may reach the network, and this is the list.
205 //
206 // It is a list rather than a deleted gate because the connector had
207 // to dial api.notion.com and the cheapest way to make this test pass
208 // would have been to widen it until it named nothing. The exemption
209 // is paid for by a RUNTIME guard that is stronger than the grep it
210 // replaces here: notion.pinnedTransport refuses every host but the
211 // one it was built for, and the client refuses redirects outright,
212 // both asserted by notion.TestClientDialsOnlyNotion. A source grep
213 // cannot tell an http.Client pointed at Notion from one pointed
214 // anywhere else; that test can.
215 //
216 // A SECOND entry was added on 2026-09-06, and it was meant to be hard
217 // to justify. Slice 1 gives memoria a sibling client, and a sibling
218 // call is outbound by definition — decision 1's whole point is that a
219 // team's instances are one trust domain, so this is egress the design
220 // requires rather than egress that crept in.
221 //
222 // It is paid for the same way Notion's is, by a runtime guard the grep
223 // could not perform: suite.NewSiblingClient refuses any host outside
224 // memoria's own <team> label and refuses redirects outright, both
225 // before the transport dials, and sibling.TestClientRefusesHostsOutsideTheTeam
226 // asserts it against the real constructor. A source grep cannot tell
227 // an http.Client bound to one team from a bare one; that test can.
228 //
229 // A third entry should be harder still. Two packages that may dial is
230 // two places to read when §3.2's leak list is next revised.
231 //
232 // **The third entry, 2026-09-27: the model provider.** Harder to
233 // justify than either before it, because what it sends is the team's
234 // passages to a party outside the suite — §3.2's largest leak class,
235 // not a sibling inside the trust domain. It is admitted on three
236 // conditions, and each is a thing a reviewer can check rather than a
237 // sentence to take on trust:
238 //
239 // - Paul ruled the egress in (decision 15 as amended, §8.9) under
240 // decision 4's terms: per-team opt-in, one named provider, a working
241 // floor when it is off. memoria.TestTheModelSwitch holds the opt-in.
242 // - The same runtime guard the other two carry: provider.pinnedTransport
243 // refuses every host and scheme but TensorX's and every redirect,
244 // before dialling, asserted against the real constructor by
245 // provider.TestClientDialsOnlyTensorX with requests counted at the
246 // listener. Mutation-tested 2026-09-27: passing everything through,
247 // dropping the scheme check, and following redirects each go red.
248 // - Nothing leaves when AI is off, asserted as an absence of traffic
249 // WITH its presence half (gate 39).
250 //
251 // **A fourth entry should need a spec section of its own.** Three is
252 // three places to read when §3.2 is next revised, and the third is the
253 // one that sends content outside the suite.
254 allowedNetworkDirs := []string{
255 filepath.Join("..", "memoria", "notion"),
256 filepath.Join("..", "memoria", "sibling"),
257 filepath.Join("..", "memoria", "provider"),
258 }
259 if len(allowedNetworkDirs) != 3 {
260 t.Fatalf("%d packages may reach the network, want exactly 3: %v",
261 len(allowedNetworkDirs), allowedNetworkDirs)
262 }
263 exempt := func(p string) bool {
264 for _, d := range allowedNetworkDirs {
265 if strings.HasPrefix(filepath.Clean(p), filepath.Clean(d)+string(filepath.Separator)) {
266 return true
267 }
268 }
269 return false
270 }
271
272 var hits []string
273 scanned := 0
274 walk := func(root string) error {
275 return filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
276 if err != nil {
277 return err
278 }
279 if info.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
280 return nil
281 }
282 if exempt(p) {
283 return nil
284 }
285 src, err := os.ReadFile(p)
286 if err != nil {
287 return err
288 }
289 scanned++
290 for _, pat := range pats {
291 if pat.Match(src) {
292 hits = append(hits, p+" matches "+pat.String())
293 }
294 }
295 return nil
296 })
297 }
298 for _, root := range []string{
299 filepath.Join("..", "memoria"),
300 filepath.Join("..", "..", "cmd"),
301 } {
302 if err := walk(root); err != nil {
303 t.Fatal(err)
304 }
305 }
306 // Vacuity guard. An exempt() that matched everything, or a walk root
307 // that moved, would leave this gate scanning nothing and passing —
308 // which is how a gate stops guarding without anyone noticing.
309 if scanned < 5 {
310 t.Fatalf("only %d files scanned; the gate is not looking at the app", scanned)
311 }
312 for _, h := range hits {
313 t.Errorf("app code can reach the network: %s", h)
314 }
315}
316
317// The footer says no DOCUMENT leaves this instance, and that is the claim
318// this file can support. It deliberately no longer says "nothing leaves",
319// because that was false: rastrillo's sign-in classifier fetches
320// https://<the address's domain>/.well-known/keymail on POST /auth/begin,
321// BEFORE the admission gate — so an anonymous visitor makes the instance
322// dial any domain they type. Found by a code review of this branch.
323//
324// This gate pins the copy, so nobody restores the stronger sentence
325// without also removing the dial.
326//
327// **Since §8.9 the sentence is conditional**: it is the AI-OFF branch of
328// the footer, and with AI on the footer names TensorX instead. This gate
329// reads the template source, so it pins the wording of both branches;
330// TestTheFooterStatesTheModelSwitch renders every declared page both ways
331// and is what checks the right branch is chosen.
332func TestFooterDoesNotOverclaim(t *testing.T) {
333 raw, err := os.ReadFile(filepath.Join("..", "memoria", "templates", "layout.html"))
334 if err != nil {
335 t.Fatal(err)
336 }
337 src := string(raw)
338 if strings.Contains(src, "nothing leaves this instance") {
339 t.Error("the footer claims nothing leaves this instance; sign-in dials the " +
340 "address's domain before anyone is admitted, so that is false")
341 }
342 if !strings.Contains(src, "no document ever") {
343 t.Error("the footer no longer makes the claim this app can actually support")
344 }
345}
346
347// Gate 4, absent until now: no content in any log line. §11 said slice 0
348// ships it. It did not, which is how a mailer that logs the magic link
349// went unnoticed.
350//
351// A FILENAME is content too, and that is the half multi-file upload
352// makes easy to get wrong: a refusal now names the file it refused, and
353// the obvious next line to write is that same name into a log. So the
354// secret is posted as a filename as well — once on a file that stores
355// and once on a file that is refused, because the refusal path is where
356// a filename is nearest to hand.
357//
358// The asynchronous path is exercised too, and for a sharper reason than
359// symmetry. It is the path where a filename is nearest to hand — a
360// request that answers per-file is the natural place to log which file it
361// answered about — and it is the one path whose RESPONSE body is allowed
362// to carry a filename, which makes it easy to forget that the log beside
363// it is not. So the same secret goes up over that path as well, on a file
364// that stores and a file that is refused.
365//
366// The store-failure line is reached DELIBERATELY, and the reason is the
367// whole of this comment's history. This gate used to claim, in a
368// MUTATION-TESTED note, that adding `"file", name` to createDocument's
369// ingest-failure log line made it go red. A code review replayed that
370// mutation and it stayed GREEN: nothing here made Store.Ingest fail —
371// the secret-named .png files are refused in readUpload well before that
372// line, and the .txt files store cleanly — so the line under test never
373// executed and the claim was false. That is exactly the shape AGENTS.md
374// opens with, written on the guard for the log line nearest a filename.
375// The database is now closed under the running app so that the line runs,
376// and its presence is asserted before its contents are, because a gate
377// that checks for an absence has to prove first that the thing whose
378// absence it checks was there to be found.
379//
380// MUTATION-TESTED 2026-09-04, each reverted, each observed red:
381//
382// (a) `"file", name` added to createDocument's ingest-failure log line
383// → "a log line carries document content, a filename or the query",
384// printing the buffer with WOMBAT…-fails.txt in it. This is the
385// mutation the previous version of this comment claimed and did not
386// have.
387// (b) Store.Ingest's post-transaction error return wrapped as
388// `fmt.Errorf("ingest %q: %w", title, err)` — the plausible "which
389// document was it?" edit, and the one that puts content in an ERROR
390// STRING rather than a log call → the same failure, printing
391// err="ingest \"WOMBAT…-fails\": sql: database is closed".
392// (c) `a.logger.Info("search", "q", q)` in the index handler
393// → the same failure, printing q=WOMBAT… for both searches.
394// (d) the vacuity guard's own subject: removing `d.Close()` → "the
395// ingest-failure log line never ran", which is the state this gate
396// was silently in before.
397func TestNoContentInLogs(t *testing.T) {
398 h, logs, d := newAppWithLog(t)
399 const secret = "WOMBATCONFIDENTIAL9931"
400 store(t, h, secret+" title", "## "+secret+"\n\nA passage about "+secret+" and revenue.")
401 submit(t, h, "", "",
402 file{secret + "-good.txt", "A passage about hosting costs and the migration."},
403 file{secret + "-refused.png", "\x00\x00 binary payload"},
404 )
405 // The same again, asynchronously. One file per request, as the tray
406 // sends them.
407 submitJSON(t, h, file{secret + "-async.txt", "A passage about the migration date."})
408 submitJSON(t, h, file{secret + "-async.png", "\x00\x00 binary payload"})
409 get(t, h, "/?q="+secret)
410 get(t, h, "/?q=revenue")
411 get(t, h, "/search?q="+secret)
412 get(t, h, "/search?q=revenue")
413 // The typeahead runs on every keystroke, so it is where a query in a
414 // log line is likeliest to be written next (Fable, 2026-11-02: a
415 // q logged in suggest() survived this gate; with these lines it is
416 // red, MUTATION-TESTED the same day and reverted). And the pages that name
417 // documents: the list, the front page's Recently updated, one page.
418 get(t, h, "/suggest?q="+secret)
419 get(t, h, "/suggest?q=revenue")
420 get(t, h, "/documents")
421 get(t, h, "/")
422 if m := regexp.MustCompile(`href="/documents/(\d+)"`).FindStringSubmatch(get(t, h, "/documents").Body.String()); m != nil {
423 get(t, h, "/documents/"+m[1])
424 }
425
426 // Now a file whose store genuinely fails. Last, because it takes the
427 // database away from everything above.
428 d.Close()
429 // With the database gone the typeahead's search fails, and its failure
430 // path is a log line of its own.
431 get(t, h, "/suggest?q="+secret)
432 reply := readReply(t, submitJSON(t, h, file{secret + "-fails.txt", "A passage about the migration date."}))
433 if reply.Stored != 0 || len(reply.Refused) != 1 ||
434 !strings.Contains(reply.Refused[0], secret+"-fails.txt") {
435 t.Fatalf("the store did not fail, so the log line this gate is aimed at never ran and "+
436 "everything below it proves nothing: stored=%d refused=%v",
437 reply.Stored, reply.Refused)
438 }
439
440 got := logs.String()
441 if !strings.Contains(got, "ingest failed") {
442 t.Fatalf("nothing in the log says a store failed, so the line that names a file was "+
443 "never written and the check below is vacuous:\n%s", got)
444 }
445 if strings.Contains(got, secret) {
446 t.Errorf("a log line carries document content, a filename or the query:\n%s", got)
447 }
448 for _, bad := range []string{"sign-in link", "auth/verify?"} {
449 if strings.Contains(got, bad) {
450 t.Errorf("a log line carries a credential (%q):\n%s", bad, got)
451 }
452 }
453}
454
455// requireSession fails closed. An app built without auth must refuse
456// every route rather than serve it.
457//
458// MUTATION-TESTED 2026-09-04: restoring the nil-auth pass-through makes
459// this fail. Reverted.
460func TestUnauthenticatedAppRefuses(t *testing.T) {
461 h := newProductionApp(t)
462 // /search is on this list because it is the newest way to read the
463 // corpus and the easiest one to mount outside the group by accident:
464 // it is not a page, so nothing about it looks like a door.
465 for _, target := range []string{"/", "/documents/1", "/?q=revenue", "/search?q=revenue"} {
466 rec := get(t, h, target)
467 if rec.Code == http.StatusOK {
468 t.Errorf("GET %s served 200 with no sign-in configured; requireSession fails open", target)
469 }
470 }
471}
472
473// The test seam must stay in tests. Production wiring may not reach
474// AppForTest, and this is the gate that the previous version of this file
475// only claimed to have.
476func TestProductionDoesNotUseTheTestConstructor(t *testing.T) {
477 err := filepath.Walk(filepath.Join("..", "..", "cmd"), func(p string, info os.FileInfo, err error) error {
478 if err != nil || info.IsDir() || !strings.HasSuffix(p, ".go") {
479 return err
480 }
481 raw, err := os.ReadFile(p)
482 if err != nil {
483 return err
484 }
485 // Every seam, not just the constructor. App() hands cmd/memoria
486 // the same *app that carries these, so `app.AsPerson("x")` in
487 // main would compile, make every request an owner, and pass a
488 // gate that greps for AppForTest alone. Found by a code review.
489 for _, seam := range []string{
490 "AppForTest", "AsPerson", "UseFakeSource", "WaitForSync",
491 // SeedTallyForDrive writes breaker counters into the past.
492 // The breaker's whole claim is that they record what
493 // happened when it happened.
494 "SeedTallyForDrive",
495 // sibling.NewForTest builds a plain http.Client with no team
496 // binding. In production that is §6.4's cross-team refusal
497 // removed — the guard that runs before the transport dials —
498 // and it would compile and serve perfectly well.
499 "NewForTest",
500 // UseFakeModel switches the model layer on without MEMORIA_AI:
501 // in main it bypasses the owner's opt-in, and handed a real
502 // client it sends content the owner never agreed to send.
503 "UseFakeModel",
504 // PreviewUnknownStanding is tools/drive's look at team mode's
505 // partial lists. It acts only under AppForTest, and in main it
506 // would be a page that withholds everything for no reason.
507 "PreviewUnknownStanding",
508 } {
509 if strings.Contains(string(raw), seam) {
510 t.Errorf("%s calls the test seam %s; production must not reach it", p, seam)
511 }
512 }
513 // AppWith is App from the whole Config (team mode, 2026-10-30):
514 // main builds the app through it, and the checks below must not
515 // quietly stop applying because the constructor's name grew.
516 if !strings.Contains(string(raw), "memoria.App(") && !strings.Contains(string(raw), "memoria.AppWith(") {
517 return nil
518 }
519 // Every setter the app needs and cannot supply itself.
520 //
521 // UseAuth's omission serves every route unauthenticated, which is
522 // the failure this gate was written for. The other two fail in the
523 // other direction and are here anyway, because a failure that is
524 // closed is not thereby a failure that is noticed:
525 //
526 // LoadSiblings omitted -> a roster is never read, so a
527 // misconfigured one is never refused at boot and the instance
528 // runs with a permanently short page instead.
529 // UseSiblings omitted -> custody stays nil, every sibling
530 // passage withholds, and the notice blames apps that are fine.
531 //
532 // Both present a working instance that quietly serves less than it
533 // holds, and neither shows up in a test that only asks whether the
534 // app starts.
535 //
536 // **Checked as calls, not as text.** A grep for the name passes on
537 // `_ = memoria.LoadSiblings` — a mention, not a call — and a
538 // review's mutation did exactly that and stayed green. The seam
539 // scan above can stay a grep, because there the string appearing
540 // at all is the problem; here the problem is the call NOT
541 // happening, and only the parse can tell those apart.
542 // **This proves the call is SPELLED, not that it is reached**, and
543 // saying so is the point. A review wrapped `app.UseAuth(a)` in a
544 // closure nobody invoked and this stayed green; so did putting
545 // the sibling wiring behind `if len(apps) > 1`. An AST walk over
546 // one file cannot see control flow, and a smarter walk is a race
547 // against however the next person writes it.
548 //
549 // What makes that survivable is that there is now almost nothing
550 // in main to get wrong: the sibling wiring is one call to
551 // `memoria.WireSiblings`, which has its own tests, and this gate
552 // only has to notice the call disappearing. The names below are
553 // the ones whose absence is silent — UseAuth serves every route
554 // unauthenticated, WireSiblings leaves every sibling passage
555 // withheld while blaming apps that are fine, and WireModel leaves
556 // MEMORIA_AI=on doing nothing, with nothing to say so.
557 for _, call := range []string{"UseAuth", "WireSiblings", "WireModel"} {
558 if !callsFunction(t, p, raw, call) {
559 t.Errorf("%s builds the app but never calls %s", p, call)
560 }
561 }
562 return nil
563 })
564 if err != nil {
565 t.Fatal(err)
566 }
567}
568
569// callsFunction reports whether the file actually CALLS name, as opposed
570// to merely mentioning it.
571//
572// The distinction is the whole point: `memoria.LoadSiblings(os.Getenv)` is
573// a call, `_ = memoria.LoadSiblings` is a reference that does nothing, and
574// a grep cannot tell them apart. It matches either a bare `name(...)` or a
575// selector `x.name(...)`, since the wiring here is a mix of package
576// functions and methods on the app.
577func callsFunction(t *testing.T, path string, src []byte, name string) bool {
578 t.Helper()
579 f, err := parser.ParseFile(token.NewFileSet(), path, src, 0)
580 if err != nil {
581 t.Fatalf("parse %s: %v", path, err)
582 }
583 found := false
584 ast.Inspect(f, func(n ast.Node) bool {
585 call, ok := n.(*ast.CallExpr)
586 if !ok {
587 return true
588 }
589 switch fn := call.Fun.(type) {
590 case *ast.Ident:
591 if fn.Name == name {
592 found = true
593 }
594 case *ast.SelectorExpr:
595 if fn.Sel.Name == name {
596 found = true
597 }
598 }
599 return true
600 })
601 return found
602}
603
604// Every colour in the app's own stylesheet is a token, so the reader's
605// light/dark choice resolves through theme.css. A literal here paints a
606// passage against the wrong ground in one theme and nobody notices until
607// a screenshot.
608//
609// MUTATION-TESTED 2026-09-03: adding `color: #fff` to memoria.css makes
610// this fail naming the line. Reverted.
611func TestNoColourLiteralsInAppCSS(t *testing.T) {
612 raw, err := os.ReadFile(filepath.Join("..", "memoria", "static", "memoria.css"))
613 if err != nil {
614 t.Fatal(err)
615 }
616 // Hex colours, rgb()/hsl()/oklch() and the named colours most likely
617 // to be typed by hand. `var(--rst-...)` is the only sanctioned way.
618 bad := regexp.MustCompile(`(?i)(#[0-9a-f]{3,8}\b|\brgba?\(|\bhsla?\(|\boklch\(|:\s*(white|black|red|blue|green|grey|gray)\b)`)
619 for i, line := range strings.Split(string(raw), "\n") {
620 if strings.HasPrefix(strings.TrimSpace(line), "*") || strings.HasPrefix(strings.TrimSpace(line), "/*") {
621 continue
622 }
623 if m := bad.FindString(line); m != "" {
624 t.Errorf("memoria.css:%d has the colour literal %q; use a token", i+1, m)
625 }
626 }
627}
628
629// Every --rst-* token memoria.css reads is either defined by the
630// stylesheets it ships with, or reached through var()'s fallback.
631//
632// This is the gate for a whole bug class, not for the one bug that
633// prompted it. An undefined custom property does not fall back to
634// anything: the declaration containing it is invalid at computed-value
635// time, so the RULE VANISHES rather than the colour going wrong. Nothing
636// warns, nothing looks broken in the markup, and the page simply renders
637// with body text where a muted grey was meant to be. That is what
638// `--rst-text-mut` (no D) did to the result trail, and it is the same
639// shape as the missing theme.css link AGENTS.md already records.
640//
641// A fallback is allowed and is not a smell: `var(--rst-font-mono,
642// ui-monospace, monospace)` is deliberate — rastrillo defines no mono
643// token and the fallback stack is the whole point. A misspelling with a
644// fallback degrades to the fallback; a misspelling without one deletes
645// the rule. Only the second is a bug, and only the second fails here.
646//
647// MUTATION-TESTED 2026-09-04, each reverted:
648//
649// (a) `color: var(--rst-text-mut)` restored on [rst-result-trail] —
650// the original bug → "memoria.css:121 reads var(--rst-text-mut),
651// which nothing defines and which has no fallback"
652// (b) `background: var(--rst-nonesuch)` added → the same failure at
653// the new line
654// (c) the same reference WITH a fallback, `var(--rst-nonesuch,
655// var(--rst-bg))` → stays green, which is the distinction this
656// gate is drawing
657func TestEveryTokenReferencedHereIsDefinedOrHasAFallback(t *testing.T) {
658 static := filepath.Join("..", "memoria", "static")
659
660 // Where tokens come from. memoria.css itself is included: a page
661 // may define its own token as long as it defines it somewhere.
662 defined := map[string]bool{}
663 def := regexp.MustCompile(`(?:^|[;{,\s])(--rst-[a-zA-Z0-9_-]+)\s*:`)
664 for _, name := range []string{"theme.css", "tokens.css", "memoria.css"} {
665 raw, err := os.ReadFile(filepath.Join(static, name))
666 if err != nil {
667 t.Fatalf("reading %s: %v", name, err)
668 }
669 for _, m := range def.FindAllStringSubmatch(string(raw), -1) {
670 defined[m[1]] = true
671 }
672 }
673 // Vacuity guard, and it has to be here: if the definition regexp
674 // stopped matching, `defined` would be empty and every reference
675 // below would be reported — loudly wrong rather than quietly wrong,
676 // but still not this gate doing its job. theme.css alone carries
677 // well over twenty tokens.
678 if len(defined) < 20 {
679 t.Fatalf("only %d tokens were found across theme.css, tokens.css and memoria.css; "+
680 "the definition pattern has stopped matching and this gate is measuring "+
681 "nothing", len(defined))
682 }
683 for _, must := range []string{"--rst-text-muted", "--rst-line", "--rst-bg"} {
684 if !defined[must] {
685 t.Fatalf("%s was not found among the defined tokens; the definition pattern is "+
686 "missing real definitions and this gate would report false failures", must)
687 }
688 }
689
690 raw, err := os.ReadFile(filepath.Join(static, "memoria.css"))
691 if err != nil {
692 t.Fatal(err)
693 }
694 // The character after the token name is the whole test: `)` means
695 // "no fallback, this must resolve", `,` means a fallback follows.
696 // Nested var()s in a fallback are separate matches and are checked
697 // on their own terms.
698 use := regexp.MustCompile(`var\(\s*(--rst-[a-zA-Z0-9_-]+)\s*([,)])`)
699 seen := 0
700 for i, line := range strings.Split(string(raw), "\n") {
701 for _, m := range use.FindAllStringSubmatch(line, -1) {
702 seen++
703 if m[2] == "," {
704 continue // a fallback carries it
705 }
706 if !defined[m[1]] {
707 t.Errorf("memoria.css:%d reads var(%s), which nothing defines and which has "+
708 "no fallback. The declaration is then invalid at computed-value time — "+
709 "the whole rule disappears, silently, and the element renders as though "+
710 "the line had never been written.", i+1, m[1])
711 }
712 }
713 }
714 if seen < 5 {
715 t.Fatalf("only %d token references were found in memoria.css; the reference pattern "+
716 "has stopped matching and this gate proves nothing", seen)
717 }
718}
719
720// requireSession falls through when auth is nil, which is what makes
721// these tests possible. That seam is only safe because the app refuses to
722// start without sign-in configured, so this pins the refusals.
723//
724// An earlier version of this gate grepped main.go for os.Exit near each
725// variable name. It PASSED when the DEK refusal was replaced with a
726// generated fallback, because the base64 error path below it supplied an
727// os.Exit within the search window. That is why the validation now lives
728// in a function and this tests its behaviour instead of its text.
729//
730// MUTATION-TESTED 2026-09-03: making any one of the three fields default
731// instead of erroring makes the matching subtest fail. Reverted.
732func TestConfigRefusesIncompleteEnvironment(t *testing.T) {
733 full := map[string]string{
734 "MEMORIA_DEK": base64.StdEncoding.EncodeToString(make([]byte, 32)),
735 "MEMORIA_INSTANCE_KEY": "instance-key",
736 "MEMORIA_OWNERS": "paul@example.com",
737 "MEMORIA_ORIGIN": "https://memoria.example.com",
738 "CARLOS_SMTP_HOST": "smtp.example.com",
739 "CARLOS_SMTP_FROM": "noreply@example.com",
740 }
741 // The complete environment must load, or every subtest below passes
742 // for the wrong reason.
743 if _, err := memoria.LoadConfig(envFrom(full)); err != nil {
744 t.Fatalf("a complete environment did not load: %v", err)
745 }
746
747 for _, missing := range []string{"MEMORIA_DEK", "MEMORIA_INSTANCE_KEY", "MEMORIA_OWNERS", "CARLOS_SMTP_HOST"} {
748 t.Run("without "+missing, func(t *testing.T) {
749 env := map[string]string{}
750 for k, v := range full {
751 if k != missing {
752 env[k] = v
753 }
754 }
755 _, err := memoria.LoadConfig(envFrom(env))
756 if err == nil {
757 t.Fatalf("LoadConfig succeeded without %s; the app would start "+
758 "with no %s and the nil-auth pass-through becomes reachable", missing, missing)
759 }
760 if !errors.Is(err, memoria.ErrMissing) {
761 t.Errorf("error for missing %s is %v, want ErrMissing", missing, err)
762 }
763 if !strings.Contains(err.Error(), missing) {
764 t.Errorf("error for missing %s does not name it: %v", missing, err)
765 }
766 })
767 }
768}
769
770// A DEK of the wrong length must be refused rather than padded. A short
771// key that got zero-extended would produce ciphertext that looks fine and
772// is trivially breakable.
773func TestConfigRefusesMalformedDEK(t *testing.T) {
774 for name, dek := range map[string]string{
775 "not base64": "!!!!not base64!!!!",
776 "too short": base64.StdEncoding.EncodeToString(make([]byte, 16)),
777 "too long": base64.StdEncoding.EncodeToString(make([]byte, 64)),
778 "empty base64": base64.StdEncoding.EncodeToString(nil),
779 } {
780 t.Run(name, func(t *testing.T) {
781 _, err := memoria.LoadConfig(envFrom(map[string]string{
782 "MEMORIA_DEK": dek,
783 "MEMORIA_INSTANCE_KEY": "k",
784 "MEMORIA_OWNERS": "a@b.c",
785 "CARLOS_SMTP_HOST": "smtp.example.com",
786 "CARLOS_SMTP_FROM": "noreply@example.com",
787 }))
788 if err == nil {
789 t.Fatalf("LoadConfig accepted a %s DEK", name)
790 }
791 })
792 }
793}
794
795// Origin is the CSRF yardstick and decides cookie attributes, so a
796// silent production default would be a real bug. It may default, but the
797// config must say that it did.
798func TestConfigFlagsADefaultedOrigin(t *testing.T) {
799 cfg, err := memoria.LoadConfig(envFrom(map[string]string{
800 "MEMORIA_DEK": base64.StdEncoding.EncodeToString(make([]byte, 32)),
801 "MEMORIA_INSTANCE_KEY": "k",
802 "MEMORIA_OWNERS": "a@b.c",
803 "CARLOS_SMTP_HOST": "smtp.example.com",
804 "CARLOS_SMTP_FROM": "noreply@example.com",
805 }))
806 if err != nil {
807 t.Fatal(err)
808 }
809 if !cfg.UsedDefaultOrigin() {
810 t.Error("origin was defaulted but UsedDefaultOrigin() is false; main would not warn")
811 }
812 cfg, err = memoria.LoadConfig(envFrom(map[string]string{
813 "MEMORIA_DEK": base64.StdEncoding.EncodeToString(make([]byte, 32)),
814 "MEMORIA_INSTANCE_KEY": "k",
815 "MEMORIA_OWNERS": "a@b.c",
816 "CARLOS_SMTP_HOST": "smtp.example.com",
817 "CARLOS_SMTP_FROM": "noreply@example.com",
818 "MEMORIA_ORIGIN": "https://real.example.com",
819 }))
820 if err != nil {
821 t.Fatal(err)
822 }
823 if cfg.UsedDefaultOrigin() {
824 t.Error("a real origin is reported as defaulted; main would warn spuriously")
825 }
826}
827
828// The explicit opt-in must work, or local development is impossible and
829// somebody will delete the refusal instead.
830func TestConfigAllowsLoggedMailWhenAskedExplicitly(t *testing.T) {
831 cfg, err := memoria.LoadConfig(envFrom(map[string]string{
832 "MEMORIA_DEK": base64.StdEncoding.EncodeToString(make([]byte, 32)),
833 "MEMORIA_INSTANCE_KEY": "k",
834 "MEMORIA_OWNERS": "a@b.c",
835 "MEMORIA_ALLOW_LOGGED_MAIL": "1",
836 }))
837 if err != nil {
838 t.Fatalf("the explicit dev opt-in was refused: %v", err)
839 }
840 if !cfg.AllowLoggedMail {
841 t.Error("AllowLoggedMail is false after opting in; main would not warn")
842 }
843}
844
845func envFrom(m map[string]string) func(string) string {
846 return func(k string) string { return m[k] }
847}
848
849func bytesContainsFold(hay []byte, needle string) bool {
850 return strings.Contains(strings.ToLower(string(hay)), strings.ToLower(needle))
851}
852
853// designCSS is every stylesheet the templates are styled by, as one
854// string.
855func designCSS(t *testing.T) string {
856 t.Helper()
857 css := ""
858 for _, name := range []string{"tokens.css", "theme.css", "memoria.css"} {
859 raw, err := os.ReadFile(filepath.Join("..", "memoria", "static", name))
860 if err != nil {
861 t.Fatalf("read %s: %v", name, err)
862 }
863 css += string(raw)
864 }
865 return css
866}
867
868// Every `rst-` attribute a template writes is one the stylesheets
869// actually style.
870//
871// **Both halves of this gate came from shipping the mistake.** The
872// connectors page grew `rst-list-item-warn`, an attribute invented on the
873// spot that styles nothing; and `rst-callout="danger"` had been in
874// results.html since it was written, when tokens.css styles
875// `[rst-callout][data-tone="negative"]` — a value on the attribute itself
876// matches the base selector and no tone at all. Two tests pinned the
877// second one in place, because they were written from the same guess as
878// the markup.
879//
880// This is the frontend twin of AGENTS.md's rule about opening the section
881// you cite: the design system has a vocabulary, and a word that is not in
882// it renders as nothing at all rather than as an error. Nobody notices
883// until a screenshot.
884//
885// **The exemption list is the honest part.** Some attributes are hooks for
886// tests and scripts rather than for CSS, and they are named here one by
887// one — a blanket "ignore anything not found" would turn this gate back
888// into the thing it replaces.
889//
890// MUTATION-TESTED 2026-09-07, each verified red then reverted:
891//
892// add rst-list-item-warn back to connectors.html -> red, naming it
893// write rst-callout="danger" again -> red
894// misspell rst-callout-body -> red
895func TestEveryTemplateAttributeIsInTheDesignSystem(t *testing.T) {
896 css := designCSS(t)
897
898 // **Hooks, not styles**, and the list is derived rather than typed:
899 // anything one of the vendored scripts reads is a hook by definition,
900 // and pattern-matching that is the honest version of an exemption.
901 js := ""
902 for _, name := range []string{"rastrillo.js", "search.js", "drop.js"} {
903 raw, err := os.ReadFile(filepath.Join("..", "memoria", "static", name))
904 if err != nil {
905 t.Fatalf("read %s: %v", name, err)
906 }
907 js += string(raw)
908 }
909
910 // And the ones that are neither: **pre-existing attributes that style
911 // nothing and no script reads.** They render as plain elements, which
912 // is a silent defect rather than an error, and this gate found them
913 // the day it was written (2026-09-07).
914 //
915 // Listed rather than ignored, so the list is visible and can shrink.
916 // A blanket "skip what is not found" would turn this gate back into
917 // the absence of the gate.
918 // **Pre-existing attributes that style nothing and no script reads.**
919 // They render as plain elements — a silent defect rather than an
920 // error. Listed rather than pattern-skipped so the list is visible and
921 // can shrink; a blanket "ignore what is missing" would be the absence
922 // of the gate wearing its name.
923 //
924 // The list shrank with the shared chrome (2026-09-30): rst-cell-mut
925 // (a class-only utility, written as an attribute nineteen times) is
926 // now memoria.css's [rst-muted]; the Sources page's rst-list-item and
927 // rst-list-title are [rst-sources] rows; rst-facts is styled; the
928 // header's rst-brand and the Delete button's rst-danger are gone.
929 unstyled := templateUnstyled
930
931 // **Values this app writes that the design system does not define.**
932 // Same treatment and same reason as the list above: named, dated, and
933 // visible rather than pattern-skipped.
934 //
935 // `rst-btn` takes primary, ghost and danger. Every button in this app
936 // spelled "quiet" or "compact" therefore renders as the default
937 // button — the wrong weight in the header, on the sign-out control,
938 // and on the connector actions. Found 2026-09-07 by the value half of
939 // this gate, the day it could see values at all.
940 //
941 // **Not fixed here on purpose.** Which variant a "quiet compact"
942 // button should actually be is a visual decision about how the app
943 // looks, not a defect with one correct repair, and making it silently
944 // inside a commit about a circuit breaker would be the drive-by this
945 // project's AGENTS.md warns about. Recorded for Paul.
946 unstyledValues := map[string]bool{
947 "rst-btn=quiet": true,
948 "rst-btn=compact": true,
949 }
950 exempt := templateExempt
951 attr := rstWord
952
953 // **Whole tokens, never substrings**, and this is not fussiness. The
954 // first version tested membership with strings.Contains, and
955 // `rst-callout` is a prefix of `rst-callout-body` — which the scripts
956 // read — so every check involving rst-callout was satisfied by a
957 // different attribute that merely starts the same way. Two mutations
958 // survived on exactly that, including the one this gate exists for.
959 tokens := func(in string) map[string]bool {
960 out := map[string]bool{}
961 for _, m := range attr.FindAllString(in, -1) {
962 out[m] = true
963 }
964 return out
965 }
966
967 // **The stylesheet vocabulary comes from ATTRIBUTE selectors only**,
968 // `[rst-…`, and not from every rst- word in the file. Two things were
969 // wrong with reading the text: it counts words in comments, and — the
970 // one that matters — it counts the CLASS spelling of utilities that
971 // have no attribute twin. tokens.css says which those are
972 // ("Utilities keep the class spelling and are not paired: rst-sr-only,
973 // rst-mono, … rst-cell-mut"), memoria.css says `rst-cell-mut` written
974 // as an attribute selects nothing, and AGENTS.md says utilities stay
975 // classes. The gate said they were fine.
976 cssAttrs := attrSelectors(css)
977 jsAttrs := tokens(js)
978 dir := filepath.Join("..", "memoria", "templates")
979 entries, err := os.ReadDir(dir)
980 if err != nil {
981 t.Fatal(err)
982 }
983 seen := map[string]bool{}
984 for _, e := range entries {
985 if e.IsDir() || !strings.HasSuffix(e.Name(), ".html") {
986 continue
987 }
988 raw, err := os.ReadFile(filepath.Join(dir, e.Name()))
989 if err != nil {
990 t.Fatal(err)
991 }
992 for _, m := range attrNames(string(raw)) {
993 if unstyled[m] || seen[m] || jsAttrs[m] {
994 continue
995 }
996 seen[m] = true
997 if !cssAttrs[m] {
998 t.Errorf("%s writes %q, which no stylesheet mentions: an "+
999 "attribute outside the design system's vocabulary renders "+
1000 "as nothing at all rather than as an error", e.Name(), m)
1001 }
1002 }
1003 }
1004 // The presence half: the walk really did find attributes, so the
1005 // absence check above is an assertion rather than a description of an
1006 // empty directory.
1007 if len(seen) < 5 {
1008 t.Fatalf("only %d attributes found across the templates; the walk is "+
1009 "not reaching them and this gate proves nothing", len(seen))
1010 }
1011
1012 // **And the half the name check cannot make.**
1013 //
1014 // `rst-callout="warn"` passes everything above: the NAME is in the
1015 // stylesheet, and the value is simply ignored. That is precisely the
1016 // bug this gate was written for — `rst-callout="danger"` shipped and
1017 // rendered untoned, because tokens.css styles `[rst-callout]` plus
1018 // `[data-tone="negative"]` and never a value on the attribute itself.
1019 // A mutation restoring it stayed green against the name check alone.
1020 //
1021 // So: an attribute written WITH a value must be one the stylesheets
1022 // select on with a value. `rst-btn="primary compact"` is fine because
1023 // tokens.css contains `[rst-btn~="primary"]`; `rst-callout="warn"` is
1024 // not, because nothing anywhere selects on a value of rst-callout.
1025 // Derived from the CSS rather than listed, so it stays true as the
1026 // design system grows.
1027 valued := regexp.MustCompile(`(rst-[a-z0-9-]+)="([^"]*)"`)
1028 for _, e := range entries {
1029 if e.IsDir() || !strings.HasSuffix(e.Name(), ".html") {
1030 continue
1031 }
1032 raw, err := os.ReadFile(filepath.Join(dir, e.Name()))
1033 if err != nil {
1034 t.Fatal(err)
1035 }
1036 for _, m := range valued.FindAllStringSubmatch(string(raw), -1) {
1037 name, value := m[1], m[2]
1038 if exempt[name] || unstyled[name] || jsAttrs[name] {
1039 continue
1040 }
1041 // **Each value, not merely the attribute taking values.**
1042 // Checking that something somewhere selects on a value of
1043 // this name passes `rst-btn="nonsense"`, because
1044 // `[rst-btn~="primary"]` exists. A value nobody styles is the
1045 // same silent nothing as an attribute nobody styles.
1046 //
1047 // A template expression is skipped and its guarantee moves to
1048 // the function producing it — see tone().
1049 if strings.Contains(value, "{{") {
1050 continue
1051 }
1052 for _, word := range strings.Fields(value) {
1053 if unstyledValues[name+"="+word] {
1054 continue
1055 }
1056 if strings.Contains(css, name+`~="`+word+`"`) ||
1057 strings.Contains(css, name+`="`+word+`"`) {
1058 continue
1059 }
1060 t.Errorf("%s writes %s=%q, and no stylesheet selects "+
1061 "%s~=%q: the attribute matches, the value is ignored, "+
1062 "and the element renders unstyled",
1063 e.Name(), name, value, name, word)
1064 }
1065 }
1066 }
1067
1068 // data-tone carries the tones, so its values must be ones the
1069 // stylesheets define — a tone nobody styles is the same silent
1070 // nothing by another spelling.
1071 // The attribute spelling of a tone is `rst-tone`; `data-tone` is the
1072 // class-grammar half of the same pair and selects nothing beside
1073 // `[rst-callout]`. Both are checked, so writing either where the
1074 // other belongs is caught.
1075 tone := regexp.MustCompile(`(rst-tone|data-tone)="([^"]*)"`)
1076 for _, e := range entries {
1077 if e.IsDir() || !strings.HasSuffix(e.Name(), ".html") {
1078 continue
1079 }
1080 raw, err := os.ReadFile(filepath.Join(dir, e.Name()))
1081 if err != nil {
1082 t.Fatal(err)
1083 }
1084 for _, m := range tone.FindAllStringSubmatch(string(raw), -1) {
1085 if m[1] == "data-tone" {
1086 t.Errorf("%s writes data-tone=%q on an attribute-spelled "+
1087 "element: that is the class half of the pair, and it "+
1088 "selects nothing here. The attribute spelling is rst-tone",
1089 e.Name(), m[2])
1090 continue
1091 }
1092 if strings.Contains(m[2], "{{") {
1093 // A template expression. This gate reads files, not
1094 // renders, so it cannot know what this evaluates to —
1095 // and guessing would be worse than declining.
1096 //
1097 // **The guarantee moves rather than disappearing**: every
1098 // dynamic tone in this app comes from `memoria.tone`, and
1099 // TestEveryToneIsOneTheStylesheetsDefine asserts that
1100 // function's whole range against the same stylesheets.
1101 // One place produces them, so one test covers them.
1102 continue
1103 }
1104 if !strings.Contains(css, `[rst-callout][rst-tone~="`+m[2]+`"]`) {
1105 t.Errorf("%s writes rst-tone=%q, which no stylesheet selects "+
1106 "on: the callout renders untoned", e.Name(), m[2])
1107 }
1108 }
1109 }
1110}
1111
1112// rstWord is one rst- name, wherever it is written.
1113var rstWord = regexp.MustCompile(`rst-[a-z0-9-]+`)
1114
1115// templateExempt names rst- words the design-system gate does not hold
1116// to the stylesheets at all.
1117var templateExempt = map[string]bool{
1118 "rst-withheld": true, // a test hook for §6.5's notice block
1119 // Hooks the tests find these blocks by. memoria.css spaced each with a
1120 // margin of its own until the spacing rhythm (2026-11-22) put them in
1121 // a stack, which spaces them now; nothing styles them any more.
1122 "rst-answer": true, // §8.11's answer slot
1123 "rst-search-empty": true, // the one quiet "No matches" line
1124 "rst-breadcrumb": true, // the back link over /add and a document
1125}
1126
1127// classOnlyUtilities are tokens.css utilities that keep the class
1128// spelling and have no attribute twin ("Utilities keep the class
1129// spelling and are not paired"). Inside a class="…" value they are
1130// correct and the gate skips them; anywhere else they style nothing and
1131// the gate reports them like any other unstyled name. This gate reads
1132// rst- words in class values too, and select.js's mention of
1133// rst-sr-only used to satisfy it; select.js went when the app moved off
1134// rastrillo's frozen path (no page links it).
1135var classOnlyUtilities = map[string]bool{
1136 "rst-sr-only": true,
1137}
1138
1139// classValue is one class attribute and its value, either quote.
1140var classValue = regexp.MustCompile(`(\sclass=)("[^"]*"|'[^']*')`)
1141
1142// **Pre-existing attributes that style nothing and no script reads.**
1143// See TestEveryTemplateAttributeIsInTheDesignSystem.
1144var templateUnstyled = map[string]bool{
1145 "rst-lede": true,
1146 "rst-steps": true,
1147 "rst-inline": true,
1148 "rst-bg": true,
1149}
1150
1151// attrSelectors is the stylesheets' ATTRIBUTE vocabulary: the names
1152// written as `[rst-…` selectors, never the class spelling.
1153func attrSelectors(css string) map[string]bool {
1154 out := map[string]bool{}
1155 for _, m := range regexp.MustCompile(`\[(rst-[a-z0-9-]+)`).FindAllStringSubmatch(css, -1) {
1156 out[m[1]] = true
1157 }
1158 return out
1159}
1160
1161// attrNames is every rst- name a template writes that the gate must
1162// find in the stylesheets' attribute vocabulary, exemptions removed.
1163func attrNames(markup string) []string {
1164 // Native details groups use name values, not styled attributes.
1165 // Mutation-checked 2026-09-11: rst-not-a-real-attribute still fails.
1166 markup = strings.ReplaceAll(markup, `name="rst-menus"`, `name="menu-group"`)
1167 // Class-only utilities leave the class values they belong in, and
1168 // only those: written as an attribute they are still scanned.
1169 markup = classValue.ReplaceAllStringFunc(markup, func(m string) string {
1170 return rstWord.ReplaceAllStringFunc(m, func(w string) string {
1171 if classOnlyUtilities[w] {
1172 return ""
1173 }
1174 return w
1175 })
1176 })
1177 var out []string
1178 for _, m := range rstWord.FindAllString(markup, -1) {
1179 if templateExempt[m] {
1180 continue
1181 }
1182 out = append(out, m)
1183 }
1184 return out
1185}
1186
1187// A class-only utility is exempt as a CLASS and only as a class.
1188// tokens.css styles .rst-sr-only and has no [rst-sr-only] twin, so
1189// `<p rst-sr-only>` renders on screen — the very text the utility was
1190// meant to hide. The exemption used to be a bare word, which waved that
1191// through along with the class it was written for (Astra, memoria #24).
1192func TestClassOnlyUtilityPassesOnlyAsAClass(t *testing.T) {
1193 cssAttrs := attrSelectors(designCSS(t))
1194 if cssAttrs["rst-sr-only"] {
1195 t.Fatal("the stylesheets now select [rst-sr-only]; the attribute spelling is styled and this test's premise is gone")
1196 }
1197 unstyled := func(markup string) []string {
1198 var out []string
1199 for _, m := range attrNames(markup) {
1200 if !cssAttrs[m] && !templateUnstyled[m] {
1201 out = append(out, m)
1202 }
1203 }
1204 return out
1205 }
1206 for _, ok := range []string{
1207 `<label for="q" class="rst-sr-only">Search</label>`,
1208 `<p class="rst-sr-only" role="status"></p>`,
1209 `<p class="hint rst-sr-only big">x</p>`,
1210 `<p class='rst-sr-only'>x</p>`,
1211 } {
1212 if got := unstyled(ok); len(got) != 0 {
1213 t.Errorf("%s: the gate rejects %v; a class-only utility written as a class is correct", ok, got)
1214 }
1215 }
1216 for _, bad := range []string{
1217 `<p rst-sr-only>x</p>`,
1218 `<p rst-sr-only="">x</p>`,
1219 `<p rst-sr-only class="hint">x</p>`,
1220 `<p class="hint" rst-sr-only>x</p>`,
1221 `<p data-x="rst-sr-only">x</p>`,
1222 } {
1223 if got := unstyled(bad); len(got) != 1 || got[0] != "rst-sr-only" {
1224 t.Errorf("%s: the gate reports %v, want [rst-sr-only]: written as an attribute the utility styles nothing", bad, got)
1225 }
1226 }
1227}
1228