| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "html/template" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo/ui" |
| 8 | |
| 9 | "amadan.net/oficina/calls/internal/calls/icons" |
| 10 | ) |
| 11 | |
| 12 | // buildPage parses rastrillo's vendored ui partials and this app's own |
| 13 | // templates/partials/ into one template set, and html/template resolves |
| 14 | // a name by taking the last definition it parsed. So an app partial that |
| 15 | // happens to define a name the framework already defines does not |
| 16 | // collide, does not warn and does not fail to build: it silently |
| 17 | // replaces the vendored one for every page in the app, and the first |
| 18 | // anyone hears of it is a screen that looks wrong somewhere else |
| 19 | // entirely. |
| 20 | // |
| 21 | // The names are not obscure ones to reach for either — "field", |
| 22 | // "badge", "person", "stat", "callout" are all vendored, and all are |
| 23 | // exactly what someone would call a new partial of their own. This test |
| 24 | // is the warning that parse order will not give. |
| 25 | // |
| 26 | // It compares every defined name, not just the {{define}} blocks: |
| 27 | // ParseFS also defines a template per file under its base name, so |
| 28 | // naming an app partial field-text.html would shadow the vendored |
| 29 | // partial's file entry the same way. |
| 30 | func TestAppPartialsDoNotShadowAVendoredUIPartial(t *testing.T) { |
| 31 | funcs := func(tpl *template.Template) *template.Template { |
| 32 | return tpl. |
| 33 | Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))). |
| 34 | Funcs(template.FuncMap{"asset": assets.Path}) |
| 35 | } |
| 36 | |
| 37 | const root = "shadow-probe" |
| 38 | vendored := template.Must(funcs(template.New(root)).ParseFS(ui.Templates(), "*.html")) |
| 39 | taken := map[string]bool{} |
| 40 | for _, tpl := range vendored.Templates() { |
| 41 | if tpl.Name() != root { |
| 42 | taken[tpl.Name()] = true |
| 43 | } |
| 44 | } |
| 45 | if len(taken) == 0 { |
| 46 | t.Fatal("no vendored ui partials were parsed — this test would pass on anything") |
| 47 | } |
| 48 | |
| 49 | own := template.Must(funcs(template.New(root)).ParseFS(appFS, "templates/partials/*.html")) |
| 50 | found := 0 |
| 51 | for _, tpl := range own.Templates() { |
| 52 | name := tpl.Name() |
| 53 | if name == root { |
| 54 | continue |
| 55 | } |
| 56 | found++ |
| 57 | if taken[name] { |
| 58 | t.Errorf("templates/partials/ defines %q, which rastrillo's ui already defines: "+ |
| 59 | "buildPage parses ui first, so this app's definition silently wins for every page. Rename it.", name) |
| 60 | } |
| 61 | } |
| 62 | if found == 0 { |
| 63 | t.Fatal("no app partials were parsed — templates/partials/ moved, and this test now proves nothing") |
| 64 | } |
| 65 | } |
| 66 | |