package chrome import ( "bytes" "html/template" "regexp" "strings" "testing" "amadan.net/oficina/estilo.git/sky" ) // standInState and standInFederated stand in for rastrillo auth's // SigninState and FederatedDoor: estilo-signin-screen is duck-typed, so // any value with the fields it and the partial read will do. type standInState struct { Unavailable bool Federated *standInFederated } type standInFederated struct { Href, Label string Icon template.HTML } // renderScreen renders name with testdata/signin-stand-in.html parsed // after estilo's templates, as an app parses its rastrillo's ui.Templates() // alongside them. func renderScreen(t *testing.T, name string, data any) string { t.Helper() view, err := parse(t).ParseFiles("testdata/signin-stand-in.html") if err != nil { t.Fatal(err) } var out bytes.Buffer if err := view.ExecuteTemplate(&out, name, data); err != nil { t.Fatal(err) } return out.String() } func docsScreen(st any) SignInScreen { return SignInScreen{Icon: "/static/estilo/brand/docs.svg", App: "Docs", Sky: sky.For("docs.oficina.works"), State: st, L: "stand-in-localizer"} } // The smart door is rastrillo's screen inside the Oficina door: the sky, // then the card, its head the app's mark and name, then the partial. func TestTheSmartDoorDrawsTheScreenInTheDoor(t *testing.T) { st := standInState{Federated: &standInFederated{Href: "/signin/oficina?return_to=%2Fd%2Fabc", Label: "Oficina ID", Icon: OficinaGlyph}} html := renderScreen(t, "estilo-signin-screen", docsScreen(st)) order := []string{ `
`, `estilo-sky-layer=`, `
`, `

Docs

`, `
`, `
`, `

Sign in to Docs

`, `` + string(OficinaGlyph) + `Continue with Oficina ID`, "
\n
\n
\n
", } at := 0 for _, want := range order { i := strings.Index(html[at:], want) if i < 0 { t.Fatalf("the smart door lacks %q after byte %d:\n%s", want, at, html) } at += i + len(want) } // What estilo hands the partial: the app's name as Brand.Name, no Mark // (the door's head is the brand), the localizer, Preview, and the ask // not to draw its brand. for _, want := range []string{`data-stand-in-l="stand-in-localizer"`, `data-stand-in-preview="false"`, `data-stand-in-hide-brand="true"`} { if !strings.Contains(html, want) { t.Errorf("the partial was not handed %q:\n%s", want, html) } } if strings.Contains(html, "rst-signin-mark") { t.Error("estilo passed the partial a Mark: the card would show the app's mark twice") } 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"`) { t.Error("Preview was not handed on", h) } // A pointer to the state reads the same. if h := renderScreen(t, "estilo-signin-screen", docsScreen(&st)); !strings.Contains(h, `
section > :first-child:not(:only-child):not(:has(h1)) { display: none; }", "[estilo-signin] [estilo-signin-screen] h1 { margin: 0; font-size: 1.75rem;", "[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; }", } { if !strings.Contains(src, want) { t.Errorf("chrome.css lacks %q", want) } } html := renderScreen(t, "estilo-signin-screen", docsScreen(standInState{})) if n := strings.Count(html, "

"); n != 1 { t.Errorf("the door drew its head %d times", n) } // The structure the hiding rule relies on: the wrapper's one child is // the partial's card, whose first child is its brand column. if !regexp.MustCompile(`

\s*
]*>\s*
`).MatchString(html) { t.Errorf("the partial is not where chrome.css looks for it:\n%s", html) } } // Under a base path, or with no state, there is no sign-in here: nothing // is drawn, the door included, and the app answers as it did before. func TestTheSmartDoorDrawsNothingWhenUnavailable(t *testing.T) { for name, st := range map[string]any{ "unavailable": standInState{Unavailable: true}, "unavailable pointer": &standInState{Unavailable: true}, "nil": nil, "nil pointer": (*standInState)(nil), } { if h := renderScreen(t, "estilo-signin-screen", docsScreen(st)); strings.TrimSpace(h) != "" { t.Errorf("%s: drew %q", name, h) } if h := renderScreen(t, "estilo-signin-screen-title", docsScreen(st)); h != "" { t.Errorf("%s: a title %q", name, h) } } } // The tab title is rastrillo's, for the same state and name as the heading. func TestTheSmartDoorTitleIsThePartials(t *testing.T) { if got := renderScreen(t, "estilo-signin-screen-title", docsScreen(standInState{})); got != "Sign in to Docs" { t.Errorf("title %q", got) } } // The Oficina ID glyph is the mark's two fanned cards alone, one colour // (currentColor), hidden from assistive technology, and inline. func TestOficinaGlyph(t *testing.T) { g := string(OficinaGlyph) for _, want := range []string{`Sign in

` if !strings.Contains(html, want) { t.Errorf("signed-out lacks the Forget form:\n%s\nwant:\n%s", html, want) } // No return, no return_to; no action, no form; not asked, not drawn. if h := render(t, "estilo-signed-out", n.Forgetting("/auth/forget", "")); strings.Contains(h, "return_to") { t.Error("an empty return posted return_to", h) } for _, h := range []string{ render(t, "estilo-signed-out", n.Forgetting("", "/d/abc")), render(t, "estilo-signed-out", SignInNotice{Icon: "/i.svg", App: "Docs"}.SignedOut("/signin")), } { if strings.Contains(h, "estilo-signin-forget") { t.Error("a Forget form nobody asked for", h) } } // Its look is the door's quiet control, and keys only on its own hook. src := string(CSS()) 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;") { t.Error("chrome.css does not draw Forget as a quiet text button") } } // Every card the door draws has pill buttons (Paul, 2026-11-21, "yes to // pill buttons"): the main action a full-width pill, secondary controls // quiet text buttons. chrome.css reaches them by estilo's hooks only, so // estilo's own cards must carry them, and the smart door's partial, which // rastrillo already draws as pills, must carry none. func TestTheDoorsButtonsArePills(t *testing.T) { src := string(CSS()) for _, want := range []string{ `[estilo-signin] :is([estilo-signin-form] [type="submit"], [estilo-signin-action]:not([estilo-signin-action~="quiet"])):not([hidden]) {`, "min-block-size: 44px; padding-inline: var(--rst-sp-4); border-radius: 999px; font-weight: 600;", "inset 0 1px 0 rgb(255 255 255 / 0.22)", `[estilo-signin] [estilo-signin-action~="danger"] {`, "--estilo-signin-glow: light-dark(color-mix(in srgb, var(--rst-danger-fill", } { if !strings.Contains(src, want) { t.Errorf("chrome.css lacks %q", want) } } // The classic form's one submit is the form's main action. form := render(t, "estilo-signin", SignIn{Icon: "/i.svg", Action: "/signin"}) if !regexp.MustCompile(`(?s)
Sign in.*
`).MatchString(form) { t.Errorf("the sign-in form's submit is not where the pill rule looks for it:\n%s", form) } if n := strings.Count(form, `type="submit"`); n != 1 { t.Errorf("the sign-in form has %d submits; the pill rule makes each one the main action", n) } // Signed out: Sign in is the main action, Forget the quiet one. out := render(t, "estilo-signed-out", SignInNotice{Icon: "/i.svg", App: "Docs"}.SignedOut("/signin").Forgetting("/auth/forget", "")) for _, want := range []string{``, "
`) { t.Errorf("sent's way back is not a quiet button:\n%s", sent) } // Quiet controls keep the touch target; pressing a pill drops its glow, // so rastrillo's pressed state still sinks. 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) {`} { if !strings.Contains(src, want) { t.Errorf("chrome.css lacks %q", want) } } // The smart door's buttons are rastrillo's: no estilo hook reaches them. screen := renderScreen(t, "estilo-signin-screen", docsScreen(standInState{})) for _, hook := range []string{"estilo-signin-action", "estilo-signin-form", "estilo-signin-forget"} { if strings.Contains(screen, hook) { t.Errorf("the smart door carries %s, so estilo's pill rules would restyle rastrillo's buttons:\n%s", hook, screen) } } } // A copy set that leaves Forget empty still draws a named button: the // label falls back to English's at render time. func TestForgetIsNeverUnnamed(t *testing.T) { c := English c.Forget = "" n := SignInNotice{Icon: "/i.svg", App: "Docs", Copy: &c}.SignedOut("/signin").Forgetting("/auth/forget", "") if h := render(t, "estilo-signed-out", n); !strings.Contains(h, ``) { t.Error("an empty Forget drew an unnamed button", h) } n.Forget = &ForgetForm{Action: "/auth/forget"} if h := render(t, "estilo-signed-out", n); !strings.Contains(h, ">Forget me on this device") { t.Error("a ForgetForm with no Label drew an unnamed button", h) } }