| 1 | package callstest |
| 2 | |
| 3 | import ( |
| 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. |
| 24 | func 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("..", "calls", "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 | for _, why := range chromeWebfonts(string(b)) { |
| 39 | t.Errorf("%s %s", p, why) |
| 40 | } |
| 41 | return nil |
| 42 | }) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if pages < 10 { |
| 47 | t.Fatalf("read only %d templates under %s; the scan would pass vacuously", pages, root) |
| 48 | } |
| 49 | |
| 50 | // And as served, on the page every visitor meets first. |
| 51 | h := newApp(t) |
| 52 | rec := get(t, h, "/signin") |
| 53 | if rec.Code != http.StatusOK { |
| 54 | t.Fatalf("GET /signin = %d", rec.Code) |
| 55 | } |
| 56 | for _, why := range chromeWebfonts(rec.Body.String()) { |
| 57 | t.Errorf("GET /signin %s", why) |
| 58 | } |
| 59 | |
| 60 | // The retired files still answer, for pages cached before the change. |
| 61 | for _, name := range []string{"fonts.css", "onest-latin.woff2"} { |
| 62 | if rec := get(t, h, theme.FontPrefix+name); rec.Code != http.StatusOK { |
| 63 | t.Errorf("GET %s%s = %d; keep estilo's font handler mounted for old pages", theme.FontPrefix, name, rec.Code) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | var ( |
| 69 | linkTag = regexp.MustCompile(`(?is)<link\b[^>]*>`) |
| 70 | asFont = regexp.MustCompile(`(?i)\bas\s*=\s*["']?font\b`) |
| 71 | fontFile = regexp.MustCompile(`(?i)\.(woff2?|ttf|otf|eot)\b`) |
| 72 | ) |
| 73 | |
| 74 | // chromeWebfonts names each way html would download a font for the |
| 75 | // chrome: estilo's font path at any version, a <link> that preloads a |
| 76 | // font or names a font file, or a face declared inline. |
| 77 | func chromeWebfonts(html string) []string { |
| 78 | var found []string |
| 79 | if dir := path.Dir(strings.TrimSuffix(theme.FontPrefix, "/")) + "/"; strings.Contains(html, dir) { |
| 80 | found = append(found, "names estilo's webfont path "+dir) |
| 81 | } |
| 82 | for _, tag := range linkTag.FindAllString(html, -1) { |
| 83 | if asFont.MatchString(tag) || fontFile.MatchString(tag) { |
| 84 | found = append(found, "links or preloads a font: "+tag) |
| 85 | } |
| 86 | } |
| 87 | if strings.Contains(strings.ToLower(html), "@font-face") { |
| 88 | found = append(found, "declares a font face inline") |
| 89 | } |
| 90 | return found |
| 91 | } |
| 92 | |