oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs_test
2
3import (
4 "io/fs"
5 "net/http"
6 "net/http/httptest"
7 "os"
8 "path"
9 "path/filepath"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/estilo.git/theme"
15
16 "github.com/carlosframework/docs/internal/docs"
17 "github.com/carlosframework/docs/internal/fonts"
18)
19
20// The chrome is set in the system UI face and downloads no font, so it
21// can never flash (Paul, 2026-10-25: "There should never be a flash of
22// unstyled text. Ever." estilo AGENTS.md row 9). No page links or
23// preloads a chrome webfont: not estilo's retired Onest, and no font file
24// outside Docs' own. estilo's v1 files stay mounted (app.go) so a page
25// cached before this still resolves its stylesheet; nothing may name them.
26//
27// The document's fonts are a different thing and are untouched: a
28// document page preloads the faces its text is set in, from
29// internal/fonts under /static/fonts/ (fontgate.go). estilo's are under
30// /static/estilo/fonts/. The test tells the two apart by that prefix, and
31// requires the document page to still carry its own preloads, so an
32// allowance that matched nothing could not pass it.
33//
34// This replaces TestOnestIsLinkedAndServed, which required the link.
35func TestNoPageLinksOrPreloadsAChromeWebfont(t *testing.T) {
36 estiloFonts := path.Dir(strings.TrimSuffix(theme.FontPrefix, "/")) + "/"
37 if estiloFonts != "/static/estilo/fonts/" || fonts.PathPrefix != "/static/fonts/" ||
38 strings.HasPrefix(estiloFonts, fonts.PathPrefix) || strings.HasPrefix(fonts.PathPrefix, estiloFonts) {
39 t.Fatalf("estilo's fonts are under %s and Docs' under %s; this test tells them apart by those prefixes", estiloFonts, fonts.PathPrefix)
40 }
41
42 // Every template, as source: whichever route renders a page, it is
43 // made of these, including any this test does not request.
44 templates := 0
45 err := filepath.WalkDir("views", func(p string, d fs.DirEntry, err error) error {
46 if err != nil || d.IsDir() || filepath.Ext(p) != ".html" {
47 return err
48 }
49 b, err := os.ReadFile(p)
50 if err != nil {
51 return err
52 }
53 templates++
54 if strings.Contains(string(b), estiloFonts) {
55 t.Errorf("%s names estilo's webfont path %s", p, estiloFonts)
56 }
57 return nil
58 })
59 if err != nil {
60 t.Fatal(err)
61 }
62 if templates < 10 {
63 t.Fatalf("read only %d templates under views; the scan would pass vacuously", templates)
64 }
65
66 // And as served, on every screen the layout wraps.
67 app, err := docs.NewForTest(t)
68 if err != nil {
69 t.Fatal(err)
70 }
71 alice := signInCookies(t, app, "alice@example.com")
72 sqid := createPrivateDoc(t, app, alice, "Minutes")
73 public := linkFor(t, app, alice, sqid)
74 // Someone it is not shared with meets the error page, not a 404 line.
75 bob := signInCookies(t, app, "bob@example.com")
76 pages := []struct {
77 name string
78 cookies []*http.Cookie
79 path string
80 status int
81 }{
82 {"the sign-in door", nil, "/signin", http.StatusOK},
83 {"the list", alice, "/", http.StatusOK},
84 {"a document", alice, "/d/" + sqid, http.StatusOK},
85 {"the share page", alice, "/d/" + sqid + "/share", http.StatusOK},
86 {"a public link", nil, public, http.StatusOK},
87 {"the error page", bob, "/d/" + sqid, http.StatusNotFound},
88 {"the colophon", nil, "/help/colophon", http.StatusOK},
89 {"a licence in the colophon", alice, "/help/colophon/carlito", http.StatusOK},
90 {"the fonts help", alice, "/help/fonts", http.StatusOK},
91 }
92 documentFonts := 0
93 for _, p := range pages {
94 // As a browser asks: the error page is only for a request that
95 // accepts HTML (errors.go's wantsHTML).
96 req := httptest.NewRequest(http.MethodGet, p.path, nil)
97 req.Header.Set("Accept", "text/html")
98 for _, c := range p.cookies {
99 req.AddCookie(c)
100 }
101 rec := httptest.NewRecorder()
102 app.Handler().ServeHTTP(rec, req)
103 if rec.Code != p.status {
104 t.Fatalf("GET %s (%s) = %d, want %d", p.path, p.name, rec.Code, p.status)
105 }
106 body := rec.Body.String()
107 if !strings.Contains(body, "<head>") {
108 t.Fatalf("GET %s (%s) served no page", p.path, p.name)
109 }
110 bad, own := chromeWebfonts(body, estiloFonts)
111 for _, why := range bad {
112 t.Errorf("%s (GET %s) %s", p.name, p.path, why)
113 }
114 if p.name == "a document" {
115 documentFonts = own
116 }
117 }
118 // The document page's own preloads (TestANewDocumentIsInCarlito holds
119 // which ones) are still there, and this test saw them as Docs'.
120 if documentFonts == 0 {
121 t.Errorf("a document page preloads none of Docs' own faces under %s; the allowance for them matched nothing", fonts.PathPrefix)
122 }
123
124 // The retired files still answer, for pages cached before the change.
125 for _, name := range []string{"fonts.css", "onest-latin.woff2"} {
126 if rec := get(t, app, nil, theme.FontPrefix+name); rec.Code != http.StatusOK {
127 t.Errorf("GET %s%s = %d; keep estilo's font handler mounted for old pages", theme.FontPrefix, name, rec.Code)
128 }
129 }
130}
131
132var (
133 linkTag = regexp.MustCompile(`(?is)<link\b[^>]*>`)
134 linkHref = regexp.MustCompile(`(?is)\bhref\s*=\s*["']?([^"'\s>]+)`)
135 asFont = regexp.MustCompile(`(?i)\bas\s*=\s*["']?font\b`)
136 fontFile = regexp.MustCompile(`(?i)\.(woff2?|ttf|otf|eot)\b`)
137)
138
139// chromeWebfonts names each way html would download a font for the
140// chrome: estilo's font path at any version, a <link> that preloads a
141// font or names a font file anywhere but Docs' own /static/fonts/, or a
142// face declared inline (Docs declares its faces in internal/fonts'
143// generated stylesheet, never in a page). own counts the font links that
144// are Docs' document faces.
145func chromeWebfonts(html, estiloFonts string) (found []string, own int) {
146 if strings.Contains(html, estiloFonts) {
147 found = append(found, "names estilo's webfont path "+estiloFonts)
148 }
149 for _, tag := range linkTag.FindAllString(html, -1) {
150 if !asFont.MatchString(tag) && !fontFile.MatchString(tag) {
151 continue
152 }
153 if m := linkHref.FindStringSubmatch(tag); m != nil && strings.HasPrefix(m[1], fonts.PathPrefix) {
154 own++
155 continue
156 }
157 found = append(found, "links or preloads a chrome font: "+tag)
158 }
159 if strings.Contains(strings.ToLower(html), "@font-face") {
160 found = append(found, "declares a font face inline")
161 }
162 return found, own
163}
164