oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chrome
2
3import (
4 "bytes"
5 "html/template"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "os/exec"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/oficina/estilo.git/internal/tokencheck"
15 "amadan.net/oficina/estilo.git/menu"
16 "amadan.net/oficina/estilo.git/sky"
17 "github.com/carlosframework/rastrillo/ui"
18)
19
20func TestChromeUsesDefinedTokens(t *testing.T) {
21 missing, n := tokencheck.Undefined(CSS())
22 if n == 0 {
23 t.Fatal("chrome.css reads no rastrillo token; the check would pass vacuously")
24 }
25 if len(missing) > 0 {
26 t.Fatal("chrome.css reads undefined tokens:", missing)
27 }
28}
29
30// The bar's height is public contract and must be fixed, never a minimum:
31// nothing inside it may grow it.
32func TestBarHeightIsFixedAndPublic(t *testing.T) {
33 src := string(CSS())
34 for _, want := range []string{
35 "--estilo-bar-block-size: 3.25rem;",
36 "--estilo-z-bar: 45;",
37 "block-size: var(--estilo-bar-block-size);",
38 "flex-wrap: nowrap;",
39 } {
40 if !strings.Contains(src, want) {
41 t.Errorf("chrome.css lost %q", want)
42 }
43 }
44 // Narrow: the title field may shrink to nothing and the Rename button
45 // floats under it rather than taking room from Menu and the switcher.
46 // The browser proof is polish-audit/r2-chrome-check.mjs's reach checks.
47 main := src[:strings.Index(src, barStackMarker)]
48 narrow := main[strings.LastIndex(main, "@media (max-width: 799.98px)"):]
49 for _, want := range []string{"[estilo-bar-title-input] { min-inline-size: 0; }", "[estilo-bar-title]:focus-within [estilo-bar-title-submit] {\n position: absolute;"} {
50 if !strings.Contains(narrow, want) {
51 t.Errorf("the narrow title layout lost %q", want)
52 }
53 }
54 if regexp.MustCompile(`\[estilo-bar\]\s*\{[^}]*min-block-size`).MatchString(src) {
55 t.Error("the bar declares a min-block-size: it could grow")
56 }
57}
58
59func parse(t *testing.T) *template.Template {
60 t.Helper()
61 view := template.New("").Funcs(ui.Funcs())
62 view, err := view.ParseFS(ui.Templates(), "*.html")
63 if err != nil {
64 t.Fatal(err)
65 }
66 if view, err = view.ParseFS(menu.Templates(), "templates/*.html"); err != nil {
67 t.Fatal(err)
68 }
69 if view, err = view.ParseFS(Templates(), "templates/*.html"); err != nil {
70 t.Fatal(err)
71 }
72 return view
73}
74
75func render(t *testing.T, name string, data any) string {
76 t.Helper()
77 var out bytes.Buffer
78 if err := parse(t).ExecuteTemplate(&out, name, data); err != nil {
79 t.Fatal(err)
80 }
81 return out.String()
82}
83
84// One word per locale, used for every caller-supplied label, so a label
85// the template dropped or hard-coded shows up as a missing string.
86var locales = map[string]string{"en": "Account", "de": "Konto", "es": "Cuenta", "fr": "Compte", "it": "Account", "pt": "Conta", "nl": "Account", "pl": "Konto", "ar": "الحساب", "ja": "アカウント", "zh": "账户", "ko": "계정"}
87
88func signedIn(label string) Bar {
89 return Bar{
90 App: App{Name: label + "-app", Slug: "docs", Home: "/", Icon: "/static/estilo/brand/docs.svg"},
91 ContextEdit: &TitleEdit{Action: "/d/abc/title", Name: "title", Value: label + "-title", Label: label + "-title-label", Submit: label + "-rename"},
92 Nav: []Link{{Href: "/", Label: label + "-nav", Current: true}},
93 NavLabel: label + "-navlabel",
94 Actions: []Action{
95 {Item: menu.Item{Href: "/d/abc/share", Label: label + "-share"}},
96 {Item: menu.Item{Action: "/d/new", Label: label + "-create"}, Primary: true},
97 },
98 Switcher: &Switcher{Label: label + "-apps", AllApps: menu.Item{Href: "https://oficina.example/", Label: label + "-allapps"}},
99 Account: &Account{
100 TriggerLabel: label + "-trigger", Initials: "P", Address: "paul@example.test",
101 Labels: AccountLabels{SignedInAs: label + "-signedinas", Appearance: label + "-appearance", Help: label + "-help"},
102 Items: []menu.Item{{Href: "/account/home", Label: label + "-connect"}},
103 Appearance: &AppearanceMenu{Action: "/appearance", Return: "/d/abc", Options: AppearanceOptions(Dark, label+"-light", label+"-dark", label+"-system")},
104 Help: []menu.Item{{Href: "/credits", Label: label + "-opensource"}},
105 SignOut: SignOut{Action: "/signout", Label: label + "-signout"},
106 },
107 Labels: BarLabels{Skip: label + "-skip", Menu: label + "-menu"},
108 }
109}
110
111func TestBarRendersCallerLabelsInEveryLocale(t *testing.T) {
112 for locale, label := range locales {
113 t.Run(locale, func(t *testing.T) {
114 html := render(t, "estilo-bar", signedIn(label))
115 esc := template.HTMLEscapeString
116 for _, want := range []string{
117 `<a estilo-bar-brand href="/">`, esc(label) + "-app",
118 `<form estilo-bar-title method="post" action="/d/abc/title">`, `value="` + esc(label) + `-title"`, `aria-label="` + esc(label) + `-title-label"`, esc(label) + "-rename",
119 `aria-label="` + esc(label) + `-navlabel"`, `aria-current="page"`,
120 esc(label) + "-share", esc(label) + "-create", esc(label) + "-menu",
121 `aria-label="` + esc(label) + `-apps"`, esc(label) + "-allapps",
122 `aria-label="` + esc(label) + `-trigger"`, esc(label) + "-signedinas", "paul@example.test",
123 esc(label) + "-connect", esc(label) + "-appearance", esc(label) + "-light", esc(label) + "-dark", esc(label) + "-system",
124 esc(label) + "-help", esc(label) + "-opensource", esc(label) + "-signout",
125 } {
126 if !strings.Contains(html, want) {
127 t.Fatalf("missing %q", want)
128 }
129 }
130 if strings.Contains(html, "<script") {
131 t.Fatal("the bar requires no script")
132 }
133 })
134 }
135}
136
137// The "same corner" rule: the account is the last element in the end
138// group, after actions, Menu and the switcher, in that order.
139func TestEndGroupOrderIsFixed(t *testing.T) {
140 html := render(t, "estilo-bar", signedIn("x"))
141 order := []string{"<div estilo-bar-actions>", "<details estilo-bar-more", "<details estilo-switcher", "<details estilo-account"}
142 last := -1
143 for _, marker := range order {
144 i := strings.Index(html, marker)
145 if i < 0 || i < last {
146 t.Fatalf("%q is missing or out of order", marker)
147 }
148 last = i
149 }
150 tail := html[strings.LastIndex(html, "</details>"):]
151 if strings.Count(tail, "<") != 3 { // </details> </div> </header>
152 t.Fatalf("something follows the account in the end group: %q", tail)
153 }
154}
155
156// Every top-level trigger shares one exclusivity group, so opening one
157// closes the others; nothing nested inside a panel carries the name.
158func TestEveryBarTriggerJoinsTheOneMenuGroup(t *testing.T) {
159 html := render(t, "estilo-bar", signedIn("x"))
160 details := regexp.MustCompile(`<details [^>]*>`).FindAllString(html, -1)
161 if len(details) != 3 {
162 t.Fatalf("want three bar disclosures, got %d", len(details))
163 }
164 for _, d := range details {
165 if !strings.Contains(d, `name="estilo-menus"`) {
166 t.Errorf("%s is not in the estilo-menus group", d)
167 }
168 }
169}
170
171func TestSignOutAppearsExactlyOnce(t *testing.T) {
172 html := render(t, "estilo-bar", signedIn("x"))
173 if n := strings.Count(html, "x-signout"); n != 1 {
174 t.Fatalf("Sign out rendered %d times", n)
175 }
176}
177
178func TestAppearancePressedStateOnEveryOption(t *testing.T) {
179 html := render(t, "estilo-bar", signedIn("x"))
180 pressed := regexp.MustCompile(`value="(light|dark|system)" aria-pressed="(true|false)"`).FindAllStringSubmatch(html, -1)
181 if len(pressed) != 3 {
182 t.Fatalf("want three appearance options with aria-pressed, got %v", pressed)
183 }
184 for _, p := range pressed {
185 if (p[1] == "dark") != (p[2] == "true") {
186 t.Errorf("%s pressed=%s, want only dark pressed", p[1], p[2])
187 }
188 }
189 if !strings.Contains(html, `role="group" aria-labelledby="estilo-appearance"`) {
190 t.Error("the appearance options are not a labelled group")
191 }
192}
193
194func TestSignedOutCornerAndEmptyCorner(t *testing.T) {
195 b := Bar{App: App{Name: "Docs", Home: "/", Icon: "/i.svg"}, SignIn: &Link{Href: "/signin?return_to=%2Fd%2Fabc", Label: "Sign in"}}
196 html := render(t, "estilo-bar", b)
197 if !strings.Contains(html, `<a rst-btn href="/signin?return_to=%2Fd%2Fabc" estilo-bar-signin>Sign in</a>`) {
198 t.Fatal("signed-out corner missing", html)
199 }
200 if strings.Contains(html, "estilo-account") || strings.Contains(html, "estilo-bar-more") {
201 t.Fatal("signed-out bar has an account or Menu", html)
202 }
203 b.SignIn = nil
204 if html = render(t, "estilo-bar", b); strings.Contains(html, "estilo-bar-signin") {
205 t.Fatal("sign-in page bar shows Sign in", html)
206 }
207}
208
209func TestPublicBrandIsPlainText(t *testing.T) {
210 html := render(t, "estilo-bar", Bar{App: App{Name: "Docs", Icon: "/i.svg"}, Context: "A document"})
211 if strings.Contains(html, "<a estilo-bar-brand") || !strings.Contains(html, "<span estilo-bar-brand>") {
212 t.Fatal("public brand is a link", html)
213 }
214 if !strings.Contains(html, `<span estilo-bar-context dir="auto" title="A document">A document</span>`) {
215 t.Fatal("read-only context missing", html)
216 }
217}
218
219func TestSwitcherWithoutTilesIsAllAppsOnly(t *testing.T) {
220 b := signedIn("x")
221 html := render(t, "estilo-bar", b)
222 if strings.Contains(html, "estilo-switcher-grid") || strings.Contains(html, "estilo-switcher-app") {
223 t.Fatal("a switcher with no tiles renders a grid")
224 }
225 b.Switcher.Apps = []SwitcherApp{{Name: "Docs", Href: "https://dir.example/apps/docs", Icon: "/d.svg", Current: true}, {Name: "Sheets", Href: "https://dir.example/apps/sheets", Icon: "/s.svg"}}
226 b.Switcher.CurrentLabel = "(this app)"
227 html = render(t, "estilo-bar", b)
228 if !strings.Contains(html, `aria-current="true"`) || !strings.Contains(html, "(this app)") {
229 t.Fatal("the current tile is not marked in words", html)
230 }
231 if strings.Count(html, "(this app)") != 1 {
232 t.Fatal("more than one tile is current")
233 }
234}
235
236func TestNoMenuTriggerWithoutNavOrActions(t *testing.T) {
237 b := signedIn("x")
238 b.Nav, b.Actions = nil, nil
239 if html := render(t, "estilo-bar", b); strings.Contains(html, "estilo-bar-more") || strings.Contains(html, "estilo-bar-actions") {
240 t.Fatal("an empty Menu is rendered", html)
241 }
242}
243
244// An action is not always a link. Sheets' Create is a POST, and it must
245// be one in both places the bar draws it: a primary button at wide widths
246// and a row in the narrow Menu sheet. Drawn as a link in the sheet, it
247// would be a GET to the list and create nothing.
248func TestActionsCanPostInTheBarAndTheSheet(t *testing.T) {
249 html := render(t, "estilo-bar", signedIn("x"))
250 actions := html[strings.Index(html, "<div estilo-bar-actions>"):strings.Index(html, "<details estilo-bar-more")]
251 for _, want := range []string{
252 `<a rst-btn href="/d/abc/share">x-share</a>`,
253 `<form method="post" action="/d/new"><button rst-btn="primary" type="submit">x-create</button></form>`,
254 } {
255 if !strings.Contains(actions, want) {
256 t.Errorf("bar actions lack %q in %s", want, actions)
257 }
258 }
259 sheet := html[strings.Index(html, `<div estilo-menu="sheet">`):strings.Index(html, "<details estilo-switcher")]
260 for _, want := range []string{
261 `<a estilo-menu-item href="/d/abc/share"><span estilo-menu-label>x-share</span></a>`,
262 `<form method="post" action="/d/new"><button estilo-menu-item type="submit"><span estilo-menu-label>x-create</span></button></form>`,
263 } {
264 if !strings.Contains(sheet, want) {
265 t.Errorf("the Menu sheet lacks %q in %s", want, sheet)
266 }
267 }
268 b := signedIn("x")
269 b.Actions = []Action{{Item: menu.Item{Dialog: "help", Label: "x-help"}}}
270 html = render(t, "estilo-bar", b)
271 if !strings.Contains(html, `<button rst-btn type="button" commandfor="help" command="show-modal">x-help</button>`) {
272 t.Error("a dialog action is not an invoker button", html)
273 }
274}
275
276// A full-height app (Sheets' grid) keys its layout on the shell. The rule
277// is estilo's, not each app's: the working area is the viewport less the
278// bar, and the bar must neither grow nor shrink inside it.
279func TestTheFillShellGivesTheWorkingAreaTheRestOfTheViewport(t *testing.T) {
280 src := string(CSS())
281 for _, want := range []string{
282 "[estilo-shell~=\"fill\"] {\n display: flex;\n flex-direction: column;\n block-size: 100dvh;",
283 "[estilo-shell~=\"fill\"] > [estilo-bar] { flex: none; }",
284 "[estilo-shell~=\"fill\"] > main { flex: 1 1 auto; min-block-size: 0; }",
285 } {
286 if !strings.Contains(src, want) {
287 t.Errorf("chrome.css lacks the fill shell rule %q", want)
288 }
289 }
290}
291
292func TestUntrustedValuesAreEscaped(t *testing.T) {
293 b := signedIn("x")
294 b.ContextEdit.Value = `"><script>bad()</script>`
295 b.Account.Address = `<img src=x onerror=bad>`
296 b.Actions[0].Href = "javascript:bad()"
297 b.Actions[1].Action = "javascript:bad()"
298 html := render(t, "estilo-bar", b)
299 if strings.Contains(html, "<script>") || strings.Contains(html, "<img src=x") || !strings.Contains(html, "#ZgotmplZ") {
300 t.Fatal("unsafe bar markup", html)
301 }
302}
303
304func TestErrorPage(t *testing.T) {
305 for locale, label := range locales {
306 t.Run(locale, func(t *testing.T) {
307 html := render(t, "estilo-error", ErrorPage{Status: 500, Title: label + "-t", Body: label + "-b", Home: Link{Href: "/", Label: label + "-home"}, Back: &Link{Href: "/d/x", Label: label + "-back"}, Ref: "k3f9tq", RefLabel: label + "-ref"})
308 esc := template.HTMLEscapeString
309 for _, want := range []string{"<p rst-error-status>500</p>", esc(label) + "-t", esc(label) + "-b", `<a rst-btn="primary" href="/">` + esc(label) + "-home", `<a rst-btn href="/d/x">` + esc(label) + "-back", esc(label) + "-ref k3f9tq"} {
310 if !strings.Contains(html, want) {
311 t.Fatalf("missing %q in %s", want, html)
312 }
313 }
314 })
315 }
316 if html := render(t, "estilo-error", ErrorPage{Status: 404, Title: "t", Home: Link{Href: "/", Label: "h"}}); strings.Contains(html, "rst-error-ref") || strings.Contains(html, "rst-btn href") {
317 t.Fatal("a 404 with no ref and no checked referer shows either", html)
318 }
319}
320
321func TestSignInPages(t *testing.T) {
322 for locale, label := range locales {
323 t.Run(locale, func(t *testing.T) {
324 esc := template.HTMLEscapeString
325 html := render(t, "estilo-signin", SignIn{Icon: "/i.svg", Copy: &DoorCopy{Heading: label + "-h", AddressLabel: label + "-addr", Help: label + "-help", Submit: label + "-submit"}, Lede: label + "-lede", Error: label + "-err", Action: "/signin", Return: "/d/abc", Hidden: []Hidden{{"force", "1"}}})
326 for _, want := range []string{"<h1>" + esc(label) + "-h</h1>", esc(label) + "-lede", `rst-tone="negative" role="alert"`, esc(label) + "-err", `action="/signin" novalidate`, `name="return_to" value="/d/abc"`, `name="force" value="1"`, `type="email"`, `autocomplete="email"`, esc(label) + "-addr", esc(label) + "-help", esc(label) + "-submit"} {
327 if !strings.Contains(html, want) {
328 t.Fatalf("missing %q in %s", want, html)
329 }
330 }
331 html = render(t, "estilo-signin-sent", SignInNotice{Icon: "/i.svg", Heading: label + "-sent", Body: label + "-body", Link: &Link{Href: "/signin", Label: label + "-again"}})
332 if !strings.Contains(html, esc(label)+"-sent") || !strings.Contains(html, `<a rst-btn="ghost" estilo-signin-action="quiet" href="/signin">`+esc(label)+"-again") {
333 t.Fatal("sent page", html)
334 }
335 html = render(t, "estilo-signed-out", SignInNotice{Icon: "/i.svg", Heading: label + "-out", Body: label + "-body", Link: &Link{Href: "/signin", Label: label + "-in"}})
336 if !strings.Contains(html, `<a rst-btn="primary" estilo-signin-action href="/signin">`+esc(label)+"-in") {
337 t.Fatal("signed-out page", html)
338 }
339 })
340 }
341 // An address error belongs in the field, not a callout above it.
342 html := render(t, "estilo-signin", SignIn{Icon: "/i.svg", Action: "/signin", AddressError: "bad address"})
343 if strings.Contains(html, "rst-callout") || !strings.Contains(html, `rst-field-error id="estilo-signin-address-error" role="alert">bad address`) || !strings.Contains(html, `aria-invalid="true"`) {
344 t.Fatal("address error not in the field slot", html)
345 }
346}
347
348func TestInitials(t *testing.T) {
349 cases := []struct{ name, address, want string }{
350 {"Paul Campbell", "paul@tito.io", "P"},
351 {"", "paul@tito.io", "P"},
352 {"", " émile@example.test", "É"},
353 {" ", "", ""},
354 {"", "@example.test", ""},
355 {"", "._@example.test", ""},
356 {"李小龙", "", "李"},
357 }
358 for _, c := range cases {
359 if got := Initials(c.name, c.address); got != c.want {
360 t.Errorf("Initials(%q, %q) = %q, want %q", c.name, c.address, got, c.want)
361 }
362 }
363}
364
365func TestAppearanceAndThemeAttr(t *testing.T) {
366 for value, want := range map[string]Mode{"light": Light, "dark": Dark, "system": System, "": System, "auto": System, "Dark": System, "<x>": System} {
367 r := httptest.NewRequest("GET", "/", nil)
368 r.AddCookie(&http.Cookie{Name: AppearanceCookie, Value: value})
369 if got := Appearance(r); got != want {
370 t.Errorf("cookie %q: Appearance = %q, want %q", value, got, want)
371 }
372 }
373 if Appearance(httptest.NewRequest("GET", "/", nil)) != System {
374 t.Error("no cookie is not System")
375 }
376 for m, want := range map[Mode]string{Light: "light", Dark: "dark", System: "", Mode("x"): ""} {
377 if got := ThemeAttr(m); got != want {
378 t.Errorf("ThemeAttr(%q) = %q, want %q", m, got, want)
379 }
380 }
381}
382
383func post(form url.Values) *httptest.ResponseRecorder {
384 r := httptest.NewRequest("POST", "/appearance", strings.NewReader(form.Encode()))
385 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
386 w := httptest.NewRecorder()
387 HandleAppearance(w, r)
388 return w
389}
390
391func TestHandleAppearance(t *testing.T) {
392 w := post(url.Values{"mode": {"dark"}, "return_to": {"/d/abc?x=1"}})
393 if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/d/abc?x=1" {
394 t.Fatalf("got %d %q", w.Code, w.Header().Get("Location"))
395 }
396 c := w.Result().Cookies()
397 if len(c) != 1 || c[0].Name != AppearanceCookie || c[0].Value != "dark" || !c[0].Secure || !c[0].HttpOnly || c[0].SameSite != http.SameSiteLaxMode || c[0].Path != "/" || c[0].MaxAge < 364*24*3600 || c[0].Domain != "" {
398 t.Fatalf("cookie %+v", c)
399 }
400 // return_to that leaves the site falls back to the root; SafeReturn's
401 // control-character case is the one a prefix test misses.
402 for _, bad := range []string{"https://evil.example/", "//evil.example/", "/\\evil.example", "/\t/evil.example", "evil", ""} {
403 w := post(url.Values{"mode": {"light"}, "return_to": {bad}})
404 if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/" {
405 t.Errorf("return_to %q: got %d %q", bad, w.Code, w.Header().Get("Location"))
406 }
407 }
408 for _, bad := range []string{"auto", "", "DARK", "sepia"} {
409 if w := post(url.Values{"mode": {bad}, "return_to": {"/"}}); w.Code != http.StatusBadRequest || len(w.Result().Cookies()) != 0 {
410 t.Errorf("mode %q: got %d with cookies %v", bad, w.Code, w.Result().Cookies())
411 }
412 }
413 g := httptest.NewRecorder()
414 HandleAppearance(g, httptest.NewRequest("GET", "/appearance?mode=dark", nil))
415 if g.Code != http.StatusMethodNotAllowed || g.Header().Get("Allow") != "POST" || len(g.Result().Cookies()) != 0 {
416 t.Fatalf("GET: %d", g.Code)
417 }
418}
419
420// chrome.js must stay a classic, dependency-free script.
421func TestScriptIsClassicAndSelfContained(t *testing.T) {
422 src := string(JS())
423 if strings.Contains(src, "import ") || strings.Contains(src, "export ") {
424 t.Fatal("chrome.js must be a classic script")
425 }
426 // Escape and outside-click close every member of the one top-level
427 // group, the menubar's menus included: Sheets' menubar lost both when
428 // it left rastrillo's dropdown. And an invoker row closes the menu it
429 // was in, with a fallback where the platform lacks invoker commands.
430 // It fits a stacked title by the field's own scroll width, and switches
431 // Appearance in place on the form's own hook.
432 for _, want := range []string{"Escape", "estilo-bar-title-input", "defaultValue", `details[name="estilo-menus"][open]`, "commandfor", "showModal", "scrollWidth", "document.fonts", "estilo-appearance", "data-theme"} {
433 if !strings.Contains(src, want) {
434 t.Errorf("chrome.js lost %q", want)
435 }
436 }
437 // Escape is heard in the capture phase: an app that stops a key from
438 // propagating out of its menus (Sheets, whose grid library listens on
439 // document) would otherwise swallow it before a bubble listener ran.
440 keydown := src[strings.Index(src, "addEventListener('keydown'"):]
441 keydown = keydown[:strings.Index(keydown[1:], "document.addEventListener(")+1]
442 if !strings.HasSuffix(strings.TrimSpace(keydown), "}, true)") {
443 t.Error("chrome.js's Escape handler is not in the capture phase")
444 }
445}
446
447// chrome.js's behaviour runs under Node against a minimal DOM
448// (chrome.test.mjs), the way savestatus's does.
449func TestScriptBehaviour(t *testing.T) {
450 out, err := exec.Command("node", "--test", "chrome.test.mjs").CombinedOutput()
451 if err != nil {
452 t.Fatalf("node --test chrome.test.mjs: %v\n%s", err, out)
453 }
454}
455
456// The door: the sign-in pages are one card on the suite's sky, headed by
457// the app's mark and name. Its sky markup must be exactly the sky
458// package's own, or the two would drift.
459func TestSignInIsTheOficinaDoor(t *testing.T) {
460 v := sky.For("calendar.oficina.works")
461 html := render(t, "estilo-signin", SignIn{Icon: "/i.svg", App: "Calendar", Sky: v, Action: "/signin"})
462 var want bytes.Buffer
463 if err := template.Must(template.ParseFS(sky.Templates(), "templates/*.html")).ExecuteTemplate(&want, "estilo-sky", v); err != nil {
464 t.Fatal(err)
465 }
466 if !strings.Contains(html, strings.TrimSpace(want.String())) {
467 t.Errorf("the door's sky differs from estilo-sky's:\n%s\nwant it to contain:\n%s", html, want.String())
468 }
469 for _, s := range []string{`<div estilo-door estilo-sky-host>`, `<section estilo-signin>`, `<span>Calendar</span>`, ` autofocus`, `</section>
470</div>`} {
471 if !strings.Contains(html, s) {
472 t.Errorf("door lacks %q", s)
473 }
474 }
475 for _, name := range []string{"estilo-signin-sent", "estilo-signed-out"} {
476 if h := render(t, name, SignInNotice{Icon: "/i.svg", App: "Calendar", Sky: v, Heading: "h", Body: "b"}); !strings.Contains(h, `<div estilo-door estilo-sky-host>`) || !strings.Contains(h, "estilo-sky-layer") {
477 t.Errorf("%s is not drawn as the door", name)
478 }
479 }
480 // No sky given, no sky drawn: the card still stands.
481 if h := render(t, "estilo-signin", SignIn{Icon: "/i.svg", Action: "/s"}); strings.Contains(h, "estilo-sky-layer") {
482 t.Error("a sign-in with no Sky drew one")
483 }
484}
485
486// The card styles its own links: an app that styles no bare <a> would
487// otherwise show the browser's default blue on the door. And the card has
488// no footer links (Paul, 2026-11-19): the OpenSource slot is gone, with
489// its styles.
490func TestTheDoorStylesItsLinks(t *testing.T) {
491 src := string(CSS())
492 if !strings.Contains(src, ":where([estilo-signin]) a") {
493 t.Error("chrome.css does not style the door's links")
494 }
495 if strings.Contains(src, "estilo-signin-foot") {
496 t.Error("chrome.css still styles the door's footer")
497 }
498 if h := render(t, "estilo-signin", SignIn{Icon: "/i.svg", App: "Docs", Action: "/signin"}); strings.Contains(h, "estilo-signin-foot") {
499 t.Error("the door still draws a footer", h)
500 }
501}
502
503// iOS zooms the page into any text control under 16px. rastrillo's
504// tokens.css brings controls up to 16px on a coarse pointer with a bare
505// `input` selector, and the bar's title field outweighs that with
506// `font: inherit` at 14px, so it zoomed on every tap in every app with a
507// title (found in Sheets, 2026-10-01: its TestNoZoomOnTap). chrome.css
508// restates rastrillo's rule for the field, at the field's own weight,
509// after the field's own rule.
510func TestTitleFieldDoesNotZoomOnTap(t *testing.T) {
511 src := string(CSS())
512 field := strings.Index(src, "[estilo-bar-title-input] {")
513 if field < 0 {
514 t.Fatal("chrome.css has no title field rule")
515 }
516 block := regexp.MustCompile(`@media \(pointer: coarse\) \{\s*\[estilo-bar-title-input\] \{\s*font-size: max\(1rem, 1em\);\s*\}\s*\}`)
517 at := block.FindStringIndex(src)
518 if at == nil {
519 t.Fatal("chrome.css does not size the title field to max(1rem, 1em) under (pointer: coarse)")
520 }
521 if at[0] < field {
522 t.Error("the coarse-pointer size comes before the title field's own rule, whose font: inherit then wins")
523 }
524}
525
526// The account at a sidebar's foot (Account.Rail), for an app whose chrome
527// is a rail, not the bar (Fichas, Paul 2026-10-01: "put any shared stuff in
528// the bottom left and maybe roll it into a dropup"). The same menu, the
529// same group and the same one Sign out; only where it sits changes.
530func TestTheAccountCanSitAtARailsFoot(t *testing.T) {
531 acc := signedIn("x").Account
532 bar := render(t, "estilo-account", acc)
533 if strings.Contains(bar, `estilo-account="rail"`) || strings.Contains(bar, "estilo-account-who") {
534 t.Error("the bar's account changed: Rail is off by default")
535 }
536 acc.Rail = true
537 html := render(t, "estilo-account", acc)
538 if !strings.Contains(html, `<details estilo-account="rail" name="estilo-menus">`) {
539 t.Errorf("a rail account is not marked, or left the one menu group:\n%s", html)
540 }
541 // In a rail the trigger is a row: who is signed in, beside the avatar.
542 summary := html[strings.Index(html, "<summary"):strings.Index(html, "</summary>")]
543 if !strings.Contains(summary, `<span estilo-account-who dir="auto">paul@example.test</span>`) {
544 t.Errorf("the rail trigger does not say who is signed in:\n%s", summary)
545 }
546 if n := strings.Count(html, "x-signout"); n != 1 {
547 t.Errorf("Sign out rendered %d times", n)
548 }
549
550 src := string(CSS())
551 // It opens upward where the rail is the viewport's height, and it is
552 // the foot's own width: hung from the inline end of a foot narrower
553 // than the bar's panel it ran off the window's start edge, and from the
554 // inline start it ran past the rail, which scrolls and so clips.
555 wide := regexp.MustCompile(`@media \(min-width: 800px\) \{\s*\[estilo-account~="rail"\] > \[estilo-account-panel\] \{[^}]*inset-block-start: auto;[^}]*inset-block-end: calc\(100% \+ 4px\);`)
556 if !wide.MatchString(src) {
557 t.Error("chrome.css does not open the rail account upward from 800px")
558 }
559 if !regexp.MustCompile(`\[estilo-account~="rail"\] > \[estilo-account-panel\] \{[^}]*inset-inline: 0;[^}]*inline-size: auto;[^}]*min-inline-size: 0;`).MatchString(src) {
560 t.Error("chrome.css does not size the rail account's panel to the rail's foot")
561 }
562 // The trigger is in the page flow: square, like every bar trigger.
563 if regexp.MustCompile(`\[estilo-account~="rail"\] > \[estilo-bar-trigger\] \{[^}]*border-radius: (?:[1-9]|var)`).MatchString(src) {
564 t.Error("the rail trigger is rounded; an in-flow highlight is square")
565 }
566}
567