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 "bytes"
5 "encoding/xml"
6 "io"
7 "io/fs"
8 "net/http"
9 "net/http/httptest"
10 "strings"
11 "testing"
12)
13
14// The bundle is committed, not built at boot. This asserts exactly one
15// is present and that the path the template emits resolves to it — a
16// mismatch would serve a 404 and leave the editor silently absent,
17// which looks identical to "editing is not implemented yet".
18func TestExactlyOneBundlePerKindIsEmbedded(t *testing.T) {
19 entries, err := fs.ReadDir(staticFS, "static")
20 if err != nil {
21 t.Fatalf("read static: %v", err)
22 }
23
24 // One committed file per bundle kind. This was "exactly one editor
25 // bundle" until the jump-to palette became a second one: it ships
26 // separately so the document list does not download 300KB of
27 // ProseMirror in order to offer a keyboard shortcut (assets.go's
28 // bundleNames). The invariant has not weakened — it is still
29 // exactly one file per name, and bundleKinds is a written list
30 // rather than whatever happens to be on disk, so a build that
31 // silently stopped emitting one panics at init instead of starting
32 // without it.
33 paths := map[string]string{
34 "editor": editorScriptPath(),
35 "jumpto": jumpToScriptPath(),
36 "share": shareScriptPath(),
37
38 "fontsgate": fontsGateScriptPath(),
39 "fontshold": fontsHoldScriptPath(),
40 }
41 accounted := map[string]bool{}
42 for _, kind := range bundleKinds {
43 var found []string
44 for _, e := range entries {
45 if strings.HasPrefix(e.Name(), kind+".") && strings.HasSuffix(e.Name(), ".js") {
46 found = append(found, e.Name())
47 }
48 }
49 if len(found) != 1 {
50 t.Fatalf("want exactly one %q bundle, got %v", kind, found)
51 }
52 accounted[found[0]] = true
53 want, ok := paths[kind]
54 if !ok {
55 t.Fatalf("bundleKinds names %q but this test knows no path accessor for it", kind)
56 }
57 if want != "/static/"+found[0] {
58 t.Errorf("%s path = %q, want %q", kind, want, "/static/"+found[0])
59 }
60 }
61
62 // And nothing else in static/ at all. That directory is the bundle
63 // build's output: .amadan/ci.d/10-gate rebuilds into it and fails
64 // on any dirt whatsoever there, then restores it with git checkout
65 // and git clean — so an extra file both reddens the gate and is
66 // deleted by it. The tab icon was written here first and lost
67 // exactly that way, which is why this is a test and not a note.
68 // Hand-authored assets go in brand/ (brandFS).
69 //
70 // Kept as "everything present is accounted for" rather than as a
71 // count, so it still catches a stray file now that the expected
72 // number is no longer one. assets.go's own registry cannot do this
73 // job: it looks up the names it knows and would never notice a
74 // third file nobody named.
75 for _, e := range entries {
76 if !accounted[e.Name()] {
77 t.Errorf("static/ holds %q, which no bundle kind accounts for — it must hold only the bundle build's output; put hand-authored assets in brand/", e.Name())
78 }
79 }
80}
81
82// The bundle must be served without a session, or a signed-out reader
83// gets a broken page: a script tag pointing at a 302-to-signin looks
84// exactly like an unimplemented editor. Built directly with
85// httptest.NewRequest/app.Handler().ServeHTTP rather than
86// app.TestRequest, which always signs someone in — there is
87// deliberately no signed-in identity anywhere in this request.
88func TestEditorBundleIsServed(t *testing.T) {
89 app, err := NewForTest(t)
90 if err != nil {
91 t.Fatalf("NewForTest: %v", err)
92 }
93 req := httptest.NewRequest(http.MethodGet, editorScriptPath(), nil)
94 rec := httptest.NewRecorder()
95 app.Handler().ServeHTTP(rec, req)
96
97 if rec.Code != http.StatusOK {
98 t.Fatalf("GET %s = %d, want 200", editorScriptPath(), rec.Code)
99 }
100 if ct := rec.Header().Get("Content-Type"); !strings.Contains(ct, "javascript") {
101 t.Errorf("Content-Type = %q, want javascript", ct)
102 }
103 if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "immutable") {
104 t.Errorf("Cache-Control = %q, want immutable (the filename carries the content hash)", cc)
105 }
106}
107
108// The tab icon must be served, unauthenticated, with an image content
109// type — and the markup that links it must point at the constant this
110// package actually routes on. Those two drifting apart is the exact
111// failure this pins: a wrong href does not break a page, it quietly
112// restores the blank-page tab mark the icon exists to replace, which
113// nobody files a bug about and no other test here would notice.
114func TestIconIsServedAndLinkedConsistently(t *testing.T) {
115 app, err := NewForTest(t)
116 if err != nil {
117 t.Fatalf("NewForTest: %v", err)
118 }
119 // No session on this request, on purpose: the sign-in page needs
120 // the icon as much as any signed-in screen does, and it is
121 // reached without one.
122 req := httptest.NewRequest(http.MethodGet, iconPath, nil)
123 rec := httptest.NewRecorder()
124 app.Handler().ServeHTTP(rec, req)
125
126 if rec.Code != http.StatusOK {
127 t.Fatalf("GET %s = %d, want 200", iconPath, rec.Code)
128 }
129 if ct := rec.Header().Get("Content-Type"); !strings.Contains(ct, "image/svg+xml") {
130 t.Errorf("Content-Type = %q, want image/svg+xml", ct)
131 }
132 if body := rec.Body.String(); !strings.Contains(body, "<svg") {
133 t.Errorf("body does not look like an SVG: %.80q", body)
134 }
135
136 // And the link must reach a real page. Asserted on rendered output
137 // rather than on views/head.html's bytes: the file containing the
138 // href proves nothing on its own, because "head" only reaches a
139 // page if newRenderer parses it into the base template ahead of the
140 // per-page clones (render.go). Were that wiring dropped, a
141 // bytes-of-the-file check would stay green while every page lost
142 // its icon — the icon would still serve, the template would still
143 // contain the href, and nothing would be able to tell. Rendering
144 // the page subsumes the file check and closes that gap.
145 //
146 // The sign-in page, because it needs no session and so exercises
147 // the same unauthenticated path the icon itself is served on.
148 pageReq := httptest.NewRequest(http.MethodGet, "/signin", nil)
149 pageRec := httptest.NewRecorder()
150 app.Handler().ServeHTTP(pageRec, pageReq)
151 if pageRec.Code != http.StatusOK {
152 t.Fatalf("GET /signin = %d, want 200", pageRec.Code)
153 }
154 if wantHref := `href="` + iconPath + `"`; !strings.Contains(pageRec.Body.String(), wantHref) {
155 t.Errorf("the rendered sign-in page does not contain %s — the shell's head block is not carrying the icon link", wantHref)
156 }
157}
158
159// The icon must be well-formed XML, because SVG is XML and a browser
160// will not render one that is not — it fails closed, showing the same
161// blank tab mark as no icon at all.
162//
163// This exists because the first version of icon.svg was not: its
164// comment named a CSS custom property with the two leading hyphens a
165// custom property has, and two consecutive hyphens are illegal inside
166// an XML comment. Everything a request-level check can see was
167// correct — 200, image/svg+xml, a plausible Content-Length, the
168// expected bytes — and the file was still unparseable. Only rendering
169// it showed anything wrong, which is exactly the failure shape
170// AGENTS.md warns about: a verification that cannot tell "no" from
171// "I can't see it".
172//
173// Decoding every token, rather than checking the root element, is the
174// point: a malformed comment or attribute halfway down is precisely
175// what a shallower check misses.
176func TestIconIsWellFormedXML(t *testing.T) {
177 b, err := fs.ReadFile(brandFS, "brand/icon.svg")
178 if err != nil {
179 t.Fatalf("read icon: %v", err)
180 }
181 dec := xml.NewDecoder(bytes.NewReader(b))
182 var root string
183 for {
184 tok, err := dec.Token()
185 if err == io.EOF {
186 break
187 }
188 if err != nil {
189 t.Fatalf("icon.svg is not well-formed XML, so no browser will render it: %v", err)
190 }
191 if se, ok := tok.(xml.StartElement); ok && root == "" {
192 root = se.Name.Local
193 }
194 }
195 if root != "svg" {
196 t.Errorf("icon.svg root element = %q, want svg", root)
197 }
198}
199