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{
`
",
}
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{`