| 1 | package chrome |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "html/template" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/estilo.git/sky" |
| 11 | ) |
| 12 | |
| 13 | // standInState and standInFederated stand in for rastrillo auth's |
| 14 | // SigninState and FederatedDoor: estilo-signin-screen is duck-typed, so |
| 15 | // any value with the fields it and the partial read will do. |
| 16 | type standInState struct { |
| 17 | Unavailable bool |
| 18 | Federated *standInFederated |
| 19 | } |
| 20 | |
| 21 | type standInFederated struct { |
| 22 | Href, Label string |
| 23 | Icon template.HTML |
| 24 | } |
| 25 | |
| 26 | // renderScreen renders name with testdata/signin-stand-in.html parsed |
| 27 | // after estilo's templates, as an app parses its rastrillo's ui.Templates() |
| 28 | // alongside them. |
| 29 | func renderScreen(t *testing.T, name string, data any) string { |
| 30 | t.Helper() |
| 31 | view, err := parse(t).ParseFiles("testdata/signin-stand-in.html") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var out bytes.Buffer |
| 36 | if err := view.ExecuteTemplate(&out, name, data); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | return out.String() |
| 40 | } |
| 41 | |
| 42 | func docsScreen(st any) SignInScreen { |
| 43 | return SignInScreen{Icon: "/static/estilo/brand/docs.svg", App: "Docs", Sky: sky.For("docs.oficina.works"), State: st, L: "stand-in-localizer"} |
| 44 | } |
| 45 | |
| 46 | // The smart door is rastrillo's screen inside the Oficina door: the sky, |
| 47 | // then the card, its head the app's mark and name, then the partial. |
| 48 | func TestTheSmartDoorDrawsTheScreenInTheDoor(t *testing.T) { |
| 49 | st := standInState{Federated: &standInFederated{Href: "/signin/oficina?return_to=%2Fd%2Fabc", Label: "Oficina ID", Icon: OficinaGlyph}} |
| 50 | html := renderScreen(t, "estilo-signin-screen", docsScreen(st)) |
| 51 | order := []string{ |
| 52 | `<div estilo-door estilo-sky-host>`, |
| 53 | `estilo-sky-layer=`, |
| 54 | `<section estilo-signin>`, |
| 55 | `<p estilo-brand><img estilo-brand-icon="listing" src="/static/estilo/brand/docs.svg" alt="" width="36" height="36"><span>Docs</span></p>`, |
| 56 | `<div estilo-signin-screen>`, |
| 57 | `<section rst-signin aria-labelledby="rst-signin-heading">`, |
| 58 | `<h1 id="rst-signin-heading">Sign in to Docs</h1>`, |
| 59 | `<a rst-btn="primary block" href="/signin/oficina?return_to=%2Fd%2Fabc">` + string(OficinaGlyph) + `<span>Continue with Oficina ID</span></a>`, |
| 60 | "</section>\n</div>\n</section>\n</div>", |
| 61 | } |
| 62 | at := 0 |
| 63 | for _, want := range order { |
| 64 | i := strings.Index(html[at:], want) |
| 65 | if i < 0 { |
| 66 | t.Fatalf("the smart door lacks %q after byte %d:\n%s", want, at, html) |
| 67 | } |
| 68 | at += i + len(want) |
| 69 | } |
| 70 | // What estilo hands the partial: the app's name as Brand.Name, no Mark |
| 71 | // (the door's head is the brand), the localizer, Preview, and the ask |
| 72 | // not to draw its brand. |
| 73 | for _, want := range []string{`data-stand-in-l="stand-in-localizer"`, `data-stand-in-preview="false"`, `data-stand-in-hide-brand="true"`} { |
| 74 | if !strings.Contains(html, want) { |
| 75 | t.Errorf("the partial was not handed %q:\n%s", want, html) |
| 76 | } |
| 77 | } |
| 78 | if strings.Contains(html, "rst-signin-mark") { |
| 79 | t.Error("estilo passed the partial a Mark: the card would show the app's mark twice") |
| 80 | } |
| 81 | if h := renderScreen(t, "estilo-signin-screen", SignInScreen{App: "Docs", Icon: "/i.svg", State: st, Preview: true}); !strings.Contains(h, `data-stand-in-preview="true"`) { |
| 82 | t.Error("Preview was not handed on", h) |
| 83 | } |
| 84 | // A pointer to the state reads the same. |
| 85 | if h := renderScreen(t, "estilo-signin-screen", docsScreen(&st)); !strings.Contains(h, `<section rst-signin`) { |
| 86 | t.Error("a *SigninState drew no screen", h) |
| 87 | } |
| 88 | // The door's own scripts are none: anything the screen loads is the |
| 89 | // partial's (its passkey module), never inline script from estilo. |
| 90 | if strings.Contains(html, "<script") { |
| 91 | t.Error("the door drew a script", html) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Exactly one brand on the card: the door's head. The partial still draws |
| 96 | // its brand column until rastrillo reads HideBrand, so chrome.css hides |
| 97 | // it (the first of the partial's two children, never an only child), and |
| 98 | // flattens the partial's own card into the door's. |
| 99 | func TestTheSmartDoorHasOneBrand(t *testing.T) { |
| 100 | src := string(CSS()) |
| 101 | for _, want := range []string{ |
| 102 | "[estilo-signin-screen] > section > :first-child:not(:only-child):not(:has(h1)) { display: none; }", |
| 103 | "[estilo-signin] [estilo-signin-screen] h1 { margin: 0; font-size: 1.75rem;", |
| 104 | "[estilo-signin-screen] > section { display: block; inline-size: auto; max-inline-size: none; padding: 0; background: none; border: 0; border-radius: 0; box-shadow: none; }", |
| 105 | } { |
| 106 | if !strings.Contains(src, want) { |
| 107 | t.Errorf("chrome.css lacks %q", want) |
| 108 | } |
| 109 | } |
| 110 | html := renderScreen(t, "estilo-signin-screen", docsScreen(standInState{})) |
| 111 | if n := strings.Count(html, "<p estilo-brand>"); n != 1 { |
| 112 | t.Errorf("the door drew its head %d times", n) |
| 113 | } |
| 114 | // The structure the hiding rule relies on: the wrapper's one child is |
| 115 | // the partial's card, whose first child is its brand column. |
| 116 | if !regexp.MustCompile(`<div estilo-signin-screen>\s*<section rst-signin[^>]*>\s*<div rst-signin-brand>`).MatchString(html) { |
| 117 | t.Errorf("the partial is not where chrome.css looks for it:\n%s", html) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Under a base path, or with no state, there is no sign-in here: nothing |
| 122 | // is drawn, the door included, and the app answers as it did before. |
| 123 | func TestTheSmartDoorDrawsNothingWhenUnavailable(t *testing.T) { |
| 124 | for name, st := range map[string]any{ |
| 125 | "unavailable": standInState{Unavailable: true}, |
| 126 | "unavailable pointer": &standInState{Unavailable: true}, |
| 127 | "nil": nil, |
| 128 | "nil pointer": (*standInState)(nil), |
| 129 | } { |
| 130 | if h := renderScreen(t, "estilo-signin-screen", docsScreen(st)); strings.TrimSpace(h) != "" { |
| 131 | t.Errorf("%s: drew %q", name, h) |
| 132 | } |
| 133 | if h := renderScreen(t, "estilo-signin-screen-title", docsScreen(st)); h != "" { |
| 134 | t.Errorf("%s: a title %q", name, h) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // The tab title is rastrillo's, for the same state and name as the heading. |
| 140 | func TestTheSmartDoorTitleIsThePartials(t *testing.T) { |
| 141 | if got := renderScreen(t, "estilo-signin-screen-title", docsScreen(standInState{})); got != "Sign in to Docs" { |
| 142 | t.Errorf("title %q", got) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // The Oficina ID glyph is the mark's two fanned cards alone, one colour |
| 147 | // (currentColor), hidden from assistive technology, and inline. |
| 148 | func TestOficinaGlyph(t *testing.T) { |
| 149 | g := string(OficinaGlyph) |
| 150 | for _, want := range []string{`<svg viewBox="18 20 62 60"`, `aria-hidden="true"`, `focusable="false"`} { |
| 151 | if !strings.Contains(g, want) { |
| 152 | t.Errorf("the glyph lacks %q", want) |
| 153 | } |
| 154 | } |
| 155 | if n := strings.Count(g, "<rect "); n != 2 { |
| 156 | t.Errorf("the glyph has %d cards, want 2", n) |
| 157 | } |
| 158 | for _, m := range regexp.MustCompile(`(fill|stroke)="([^"]*)"`).FindAllStringSubmatch(g, -1) { |
| 159 | if m[2] != "currentColor" { |
| 160 | t.Errorf("the glyph paints %s=%q: it must be one colour, currentColor", m[1], m[2]) |
| 161 | } |
| 162 | } |
| 163 | for _, banned := range []string{"<script", "href", "<image", "<use", "url(", "style="} { |
| 164 | if strings.Contains(g, banned) { |
| 165 | t.Errorf("the glyph carries %q: it must be self-contained", banned) |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Signed out: a quiet "Forget me on this device" under Sign in, a POST to |
| 171 | // rastrillo auth's Forget, for a shared computer. |
| 172 | func TestSignedOutOffersForget(t *testing.T) { |
| 173 | n := SignInNotice{Icon: "/i.svg", App: "Docs"}.SignedOut("/signin").Forgetting("/auth/forget", "/d/abc", Hidden{"csrf", "tok"}) |
| 174 | html := render(t, "estilo-signed-out", n) |
| 175 | want := ` <p><a rst-btn="primary" estilo-signin-action href="/signin">Sign in</a></p> |
| 176 | <form estilo-signin-forget method="post" action="/auth/forget"> |
| 177 | <input type="hidden" name="return_to" value="/d/abc"> |
| 178 | <input type="hidden" name="csrf" value="tok"> |
| 179 | <button rst-btn="ghost" type="submit">Forget me on this device</button> |
| 180 | </form> |
| 181 | </section>` |
| 182 | if !strings.Contains(html, want) { |
| 183 | t.Errorf("signed-out lacks the Forget form:\n%s\nwant:\n%s", html, want) |
| 184 | } |
| 185 | // No return, no return_to; no action, no form; not asked, not drawn. |
| 186 | if h := render(t, "estilo-signed-out", n.Forgetting("/auth/forget", "")); strings.Contains(h, "return_to") { |
| 187 | t.Error("an empty return posted return_to", h) |
| 188 | } |
| 189 | for _, h := range []string{ |
| 190 | render(t, "estilo-signed-out", n.Forgetting("", "/d/abc")), |
| 191 | render(t, "estilo-signed-out", SignInNotice{Icon: "/i.svg", App: "Docs"}.SignedOut("/signin")), |
| 192 | } { |
| 193 | if strings.Contains(h, "estilo-signin-forget") { |
| 194 | t.Error("a Forget form nobody asked for", h) |
| 195 | } |
| 196 | } |
| 197 | // Its look is the door's quiet control, and keys only on its own hook. |
| 198 | src := string(CSS()) |
| 199 | if !strings.Contains(src, "[estilo-signin] :is([estilo-signin-action~=\"quiet\"], [estilo-signin-forget] > button):not([hidden]) {") || !strings.Contains(src, "color: var(--rst-text-muted); font-size: calc(var(--rst-fs-base) * 0.9375); font-weight: 500;") { |
| 200 | t.Error("chrome.css does not draw Forget as a quiet text button") |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Every card the door draws has pill buttons (Paul, 2026-11-21, "yes to |
| 205 | // pill buttons"): the main action a full-width pill, secondary controls |
| 206 | // quiet text buttons. chrome.css reaches them by estilo's hooks only, so |
| 207 | // estilo's own cards must carry them, and the smart door's partial, which |
| 208 | // rastrillo already draws as pills, must carry none. |
| 209 | func TestTheDoorsButtonsArePills(t *testing.T) { |
| 210 | src := string(CSS()) |
| 211 | for _, want := range []string{ |
| 212 | `[estilo-signin] :is([estilo-signin-form] [type="submit"], [estilo-signin-action]:not([estilo-signin-action~="quiet"])):not([hidden]) {`, |
| 213 | "min-block-size: 44px; padding-inline: var(--rst-sp-4); border-radius: 999px; font-weight: 600;", |
| 214 | "inset 0 1px 0 rgb(255 255 255 / 0.22)", |
| 215 | `[estilo-signin] [estilo-signin-action~="danger"] {`, |
| 216 | "--estilo-signin-glow: light-dark(color-mix(in srgb, var(--rst-danger-fill", |
| 217 | } { |
| 218 | if !strings.Contains(src, want) { |
| 219 | t.Errorf("chrome.css lacks %q", want) |
| 220 | } |
| 221 | } |
| 222 | // The classic form's one submit is the form's main action. |
| 223 | form := render(t, "estilo-signin", SignIn{Icon: "/i.svg", Action: "/signin"}) |
| 224 | if !regexp.MustCompile(`(?s)<form rst-form estilo-signin-form .*<button rst-btn="primary" type="submit">Sign in</button>.*</form>`).MatchString(form) { |
| 225 | t.Errorf("the sign-in form's submit is not where the pill rule looks for it:\n%s", form) |
| 226 | } |
| 227 | if n := strings.Count(form, `type="submit"`); n != 1 { |
| 228 | t.Errorf("the sign-in form has %d submits; the pill rule makes each one the main action", n) |
| 229 | } |
| 230 | // Signed out: Sign in is the main action, Forget the quiet one. |
| 231 | out := render(t, "estilo-signed-out", SignInNotice{Icon: "/i.svg", App: "Docs"}.SignedOut("/signin").Forgetting("/auth/forget", "")) |
| 232 | for _, want := range []string{`<a rst-btn="primary" estilo-signin-action href="/signin">`, "<form estilo-signin-forget"} { |
| 233 | if !strings.Contains(out, want) { |
| 234 | t.Errorf("signed-out lacks %q:\n%s", want, out) |
| 235 | } |
| 236 | } |
| 237 | // Sent: Use a different address is the card's quiet secondary action. |
| 238 | sent := render(t, "estilo-signin-sent", SignInNotice{Icon: "/i.svg", App: "Docs"}.Sent("/signin")) |
| 239 | if !strings.Contains(sent, `<a rst-btn="ghost" estilo-signin-action="quiet" href="/signin">`) { |
| 240 | t.Errorf("sent's way back is not a quiet button:\n%s", sent) |
| 241 | } |
| 242 | // Quiet controls keep the touch target; pressing a pill drops its glow, |
| 243 | // so rastrillo's pressed state still sinks. |
| 244 | for _, want := range []string{"@media (pointer: coarse), (max-width: 40rem) {\n [estilo-signin] :is([estilo-signin-action~=\"quiet\"], [estilo-signin-forget] > button):not([hidden]) { min-block-size: 2.75rem; }", `:not([hidden]):not(:active) {`} { |
| 245 | if !strings.Contains(src, want) { |
| 246 | t.Errorf("chrome.css lacks %q", want) |
| 247 | } |
| 248 | } |
| 249 | // The smart door's buttons are rastrillo's: no estilo hook reaches them. |
| 250 | screen := renderScreen(t, "estilo-signin-screen", docsScreen(standInState{})) |
| 251 | for _, hook := range []string{"estilo-signin-action", "estilo-signin-form", "estilo-signin-forget"} { |
| 252 | if strings.Contains(screen, hook) { |
| 253 | t.Errorf("the smart door carries %s, so estilo's pill rules would restyle rastrillo's buttons:\n%s", hook, screen) |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // A copy set that leaves Forget empty still draws a named button: the |
| 259 | // label falls back to English's at render time. |
| 260 | func TestForgetIsNeverUnnamed(t *testing.T) { |
| 261 | c := English |
| 262 | c.Forget = "" |
| 263 | n := SignInNotice{Icon: "/i.svg", App: "Docs", Copy: &c}.SignedOut("/signin").Forgetting("/auth/forget", "") |
| 264 | if h := render(t, "estilo-signed-out", n); !strings.Contains(h, `<button rst-btn="ghost" type="submit">Forget me on this device</button>`) { |
| 265 | t.Error("an empty Forget drew an unnamed button", h) |
| 266 | } |
| 267 | n.Forget = &ForgetForm{Action: "/auth/forget"} |
| 268 | if h := render(t, "estilo-signed-out", n); !strings.Contains(h, ">Forget me on this device</button>") { |
| 269 | t.Error("a ForgetForm with no Label drew an unnamed button", h) |
| 270 | } |
| 271 | } |
| 272 | |