package calls
import (
"html/template"
"testing"
"amadan.net/rastrillo/rastrillo/ui"
"amadan.net/oficina/calls/internal/calls/icons"
)
// buildPage parses rastrillo's vendored ui partials and this app's own
// templates/partials/ into one template set, and html/template resolves
// a name by taking the last definition it parsed. So an app partial that
// happens to define a name the framework already defines does not
// collide, does not warn and does not fail to build: it silently
// replaces the vendored one for every page in the app, and the first
// anyone hears of it is a screen that looks wrong somewhere else
// entirely.
//
// The names are not obscure ones to reach for either — "field",
// "badge", "person", "stat", "callout" are all vendored, and all are
// exactly what someone would call a new partial of their own. This test
// is the warning that parse order will not give.
//
// It compares every defined name, not just the {{define}} blocks:
// ParseFS also defines a template per file under its base name, so
// naming an app partial field-text.html would shadow the vendored
// partial's file entry the same way.
func TestAppPartialsDoNotShadowAVendoredUIPartial(t *testing.T) {
funcs := func(tpl *template.Template) *template.Template {
return tpl.
Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))).
Funcs(template.FuncMap{"asset": assets.Path})
}
const root = "shadow-probe"
vendored := template.Must(funcs(template.New(root)).ParseFS(ui.Templates(), "*.html"))
taken := map[string]bool{}
for _, tpl := range vendored.Templates() {
if tpl.Name() != root {
taken[tpl.Name()] = true
}
}
if len(taken) == 0 {
t.Fatal("no vendored ui partials were parsed — this test would pass on anything")
}
own := template.Must(funcs(template.New(root)).ParseFS(appFS, "templates/partials/*.html"))
found := 0
for _, tpl := range own.Templates() {
name := tpl.Name()
if name == root {
continue
}
found++
if taken[name] {
t.Errorf("templates/partials/ defines %q, which rastrillo's ui already defines: "+
"buildPage parses ui first, so this app's definition silently wins for every page. Rename it.", name)
}
}
if found == 0 {
t.Fatal("no app partials were parsed — templates/partials/ moved, and this test now proves nothing")
}
}