oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "io/fs"
5 "path"
6
7 "amadan.net/oficina/estilo.git/busy"
8 "bytes"
9 "crypto/sha256"
10 "fmt"
11 "net/http"
12 "net/http/httptest"
13 "strings"
14 "testing"
15 "testing/fstest"
16
17 "amadan.net/oficina/estilo.git/theme"
18 "amadan.net/rastrillo/rastrillo"
19 "amadan.net/rastrillo/rastrillo/ui"
20)
21
22func TestSharedThemeAssetFingerprintAndCache(t *testing.T) {
23 app, err := NewForTest(t)
24 if err != nil {
25 t.Fatal(err)
26 }
27 page := serve(app, httptest.NewRequest(http.MethodGet, "/signin", nil))
28 if page.Code != http.StatusOK {
29 t.Fatalf("signin status %d", page.Code)
30 }
31 // Calma with Docs' accent (estilo row 8), not rastrillo's day.
32 docsTheme, ok := theme.ForApp("docs")
33 if !ok {
34 t.Fatal("estilo's theme ring has no docs")
35 }
36 for _, item := range []struct {
37 name string
38 data []byte
39 }{
40 {"theme", docsTheme}, {"tokens", ui.TokensCSS()},
41 } {
42 served := servedAs("static/"+item.name+".css", item.data)
43 sum := sha256.Sum256(served)
44 hashed := fmt.Sprintf("/static/%s.%x.css", item.name, sum[:8])
45 if !strings.Contains(page.Body.String(), `href="`+hashed+`"`) {
46 t.Fatalf("missing selected %s fingerprint", item.name)
47 }
48 for _, path := range []string{hashed, "/static/" + item.name + ".css", "/static/" + item.name + ".0000000000000000.css"} {
49 res := serve(app, httptest.NewRequest(http.MethodGet, path, nil))
50 if res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), served) {
51 t.Fatalf("%s asset response mismatch status%d", item.name, res.Code)
52 }
53 if !strings.HasPrefix(res.Header().Get("Content-Type"), "text/css") {
54 t.Fatal("asset MIME mismatch")
55 }
56 want := "no-cache"
57 if path == hashed {
58 want = "public, max-age=31536000, immutable"
59 }
60 if res.Header().Get("Cache-Control") != want {
61 t.Fatal("asset cache policy mismatch")
62 }
63 }
64 }
65}
66
67// The busy rule (estilo's busy): the page links busy.css and busy.js, and
68// the shim it serves is estilo's — rastrillo's without the old busy rule,
69// which would fight the new one.
70func TestBusyRuleIsLinkedAndServed(t *testing.T) {
71 app, err := NewForTest(t)
72 if err != nil {
73 t.Fatal(err)
74 }
75 page := serve(app, httptest.NewRequest(http.MethodGet, "/signin", nil)).Body.String()
76 for name, body := range map[string][]byte{"static/estilo/busy.css": busy.CSS(), "static/rastrillo.js": busy.ShimJS(), "static/estilo/busy.js": busy.JS()} {
77 body = servedAs(name, body)
78 sum := sha256.Sum256(body)
79 hashed := "/" + strings.TrimSuffix(name, path.Ext(name)) + fmt.Sprintf(".%x", sum[:8]) + path.Ext(name)
80 if !strings.Contains(page, `"`+hashed+`"`) {
81 t.Errorf("sign-in page does not link %s", hashed)
82 }
83 if res := serve(app, httptest.NewRequest(http.MethodGet, hashed, nil)); res.Code != http.StatusOK || !bytes.Equal(res.Body.Bytes(), body) {
84 t.Errorf("GET %s = %d or the wrong bytes", hashed, res.Code)
85 }
86 }
87 // No rastrillo script on disk. Docs serves them from memory
88 // (vendoredStaticFS); `rastrillo doctor --fix` copies the ones the
89 // library ships into a static/ it finds, and the shim it would copy
90 // still carries the old busy rule, which fights estilo's busy.js.
91 // Delete anything --fix adds.
92 for _, tree := range []fs.FS{staticFS, brandFS} {
93 _ = fs.WalkDir(tree, ".", func(name string, d fs.DirEntry, err error) error {
94 if err == nil && !d.IsDir() {
95 switch path.Base(name) {
96 case "rastrillo.js", "busy.js", "select.js", "datetime.js", "calendar.js", "tokens.css", "theme.css":
97 t.Errorf("%s is on disk; Docs serves rastrillo's and estilo's assets from memory, and doctor's shim would bring the old busy rule back", name)
98 }
99 }
100 return nil
101 })
102 }
103}
104
105// servedAs is what rastrillo.Assets itself sends for name with body at
106// that path, asked of rastrillo rather than re-derived: since v0.31 it
107// minifies a file at one of the framework's vendored paths
108// (static/tokens.css, static/theme.css, static/rastrillo.js and the rest),
109// and serves any other as written. Docs puts estilo's theme and busy shim
110// at two of those paths, so they are minified too.
111func servedAs(name string, body []byte) []byte {
112 assets := rastrillo.NewAssets(fstest.MapFS{name: {Data: body}})
113 rec := httptest.NewRecorder()
114 assets.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, assets.Path(name), nil))
115 if rec.Code != http.StatusOK {
116 panic(fmt.Sprintf("rastrillo.Assets would not serve %s: %d", name, rec.Code))
117 }
118 return rec.Body.Bytes()
119}
120