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 "io/fs"
5 "net/http"
6 "os"
7 "path"
8 "path/filepath"
9 "regexp"
10 "strings"
11 "testing"
12
13 "amadan.net/oficina/estilo.git/theme"
14)
15
16// The chrome is set in the system UI face and downloads no font, so it
17// can never flash (Paul, 2026-10-25: "There should never be a flash of
18// unstyled text. Ever." estilo AGENTS.md row 9). No page links or
19// preloads a webfont: not estilo's retired Onest, and no font file at
20// all. estilo's v1 files stay mounted (app.go) so a page cached before
21// this still resolves its stylesheet; nothing may name them.
22//
23// This replaces TestLayoutLinksEstilosFonts, which required the link.
24func TestNoPageLinksOrPreloadsAChromeWebfont(t *testing.T) {
25 // Every template, as source: whichever route renders a page, it is
26 // made of these.
27 pages := 0
28 root := filepath.Join("..", "slides", "templates")
29 err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
30 if err != nil || d.IsDir() || filepath.Ext(p) != ".html" {
31 return err
32 }
33 b, err := os.ReadFile(p)
34 if err != nil {
35 return err
36 }
37 pages++
38 src := string(b)
39 if filepath.Base(p) == "layout.html" {
40 // The font gate's block preloads the DECK's faces on the
41 // pages that draw a slide (fontgate.go) and nothing else;
42 // TestOnlySlideSurfacesPreloadAndOnlyDeckFaces holds what it
43 // writes. The chrome around it is what this scan is for.
44 gate := fontGateBlock.FindString(src)
45 if gate == "" {
46 t.Errorf("%s: no font gate block {{- with .Fonts}}…{{- end}} found to set aside", p)
47 }
48 src = strings.Replace(src, gate, "", 1)
49 }
50 for _, why := range chromeWebfonts(src) {
51 t.Errorf("%s %s", p, why)
52 }
53 return nil
54 })
55 if err != nil {
56 t.Fatal(err)
57 }
58 if pages < 10 {
59 t.Fatalf("read only %d templates under %s; the scan would pass vacuously", pages, root)
60 }
61
62 // And as served, on the page every visitor meets first.
63 h := newApp(t)
64 rec := get(t, h, "/signin")
65 if rec.Code != http.StatusOK {
66 t.Fatalf("GET /signin = %d", rec.Code)
67 }
68 for _, why := range chromeWebfonts(rec.Body.String()) {
69 t.Errorf("GET /signin %s", why)
70 }
71
72 // The retired files still answer, for pages cached before the change.
73 for _, name := range []string{"fonts.css", "onest-latin.woff2"} {
74 if rec := get(t, h, theme.FontPrefix+name); rec.Code != http.StatusOK {
75 t.Errorf("GET %s%s = %d; keep estilo's font handler mounted for old pages", theme.FontPrefix, name, rec.Code)
76 }
77 }
78}
79
80var (
81 fontGateBlock = regexp.MustCompile(`(?s)\{\{- with \.Fonts\}\}.*?\{\{- end\}\}`)
82 linkTag = regexp.MustCompile(`(?is)<link\b[^>]*>`)
83 asFont = regexp.MustCompile(`(?i)\bas\s*=\s*["']?font\b`)
84 fontFile = regexp.MustCompile(`(?i)\.(woff2?|ttf|otf|eot)\b`)
85)
86
87// chromeWebfonts names each way html would download a font for the
88// chrome: estilo's font path at any version, a <link> that preloads a
89// font or names a font file, or a face declared inline.
90func chromeWebfonts(html string) []string {
91 var found []string
92 if dir := path.Dir(strings.TrimSuffix(theme.FontPrefix, "/")) + "/"; strings.Contains(html, dir) {
93 found = append(found, "names estilo's webfont path "+dir)
94 }
95 for _, tag := range linkTag.FindAllString(html, -1) {
96 if asFont.MatchString(tag) || fontFile.MatchString(tag) {
97 found = append(found, "links or preloads a font: "+tag)
98 }
99 }
100 if strings.Contains(strings.ToLower(html), "@font-face") {
101 found = append(found, "declares a font face inline")
102 }
103 return found
104}
105