go-html/path_test.go
Virgil 0fcffb029d
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
fix(html): preserve responsive block paths
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-03 18:09:46 +00:00

199 lines
5.3 KiB
Go

package html
import (
"strings"
"testing"
"slices"
)
func TestNestedLayout_PathChain(t *testing.T) {
inner := NewLayout("HCF").H(Raw("inner h")).C(Raw("inner c")).F(Raw("inner f"))
outer := NewLayout("HLCRF").
H(Raw("header")).L(inner).C(Raw("main")).R(Raw("right")).F(Raw("footer"))
got := outer.Render(NewContext())
// Inner layout paths must be prefixed with parent block ID
for _, want := range []string{`data-block="L-0-H-0"`, `data-block="L-0-C-0"`, `data-block="L-0-F-0"`} {
if !strings.Contains(got, want) {
t.Errorf("nested layout missing %q in:\n%s", want, got)
}
}
// Outer layout must still have root-level paths
for _, want := range []string{`data-block="H-0"`, `data-block="C-0"`, `data-block="F-0"`} {
if !strings.Contains(got, want) {
t.Errorf("outer layout missing %q in:\n%s", want, got)
}
}
}
func TestNestedLayout_DeepNesting(t *testing.T) {
deepest := NewLayout("C").C(Raw("deep"))
middle := NewLayout("C").C(deepest)
outer := NewLayout("C").C(middle)
got := outer.Render(NewContext())
for _, want := range []string{`data-block="C-0"`, `data-block="C-0-C-0"`, `data-block="C-0-C-0-C-0"`} {
if !strings.Contains(got, want) {
t.Errorf("deep nesting missing %q in:\n%s", want, got)
}
}
}
func TestNestedLayout_ThroughConditionalWrapper(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("wrapped"))
wrapped := If(func(*Context) bool { return true }, inner)
got := NewLayout("C").C(wrapped).Render(ctx)
if !strings.Contains(got, `data-block="C-0-C-0"`) {
t.Fatalf("conditional wrapper should preserve nested block path, got:\n%s", got)
}
}
func TestNestedLayout_ThroughEntitledWrapper(t *testing.T) {
ctx := NewContext()
ctx.Entitlements = func(feature string) bool { return feature == "feature" }
inner := NewLayout("C").C(Raw("entitled"))
wrapped := Entitled("feature", inner)
got := NewLayout("C").C(wrapped).Render(ctx)
if !strings.Contains(got, `data-block="C-0-C-0"`) {
t.Fatalf("entitled wrapper should preserve nested block path, got:\n%s", got)
}
}
func TestNestedLayout_ThroughSwitchWrapper(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("switch"))
wrapped := Switch(func(*Context) string { return "match" }, map[string]Node{
"match": inner,
})
got := NewLayout("C").C(wrapped).Render(ctx)
if !strings.Contains(got, `data-block="C-0-C-0"`) {
t.Fatalf("switch wrapper should preserve nested block path, got:\n%s", got)
}
}
func TestNestedLayout_ThroughEachWrapper(t *testing.T) {
ctx := NewContext()
items := []int{1, 2}
node := Each(items, func(i int) Node {
return NewLayout("C").C(Raw(strings.Repeat("x", i)))
})
got := NewLayout("C").C(node).Render(ctx)
if count := strings.Count(got, `data-block="C-0-C-0"`); count != 2 {
t.Fatalf("each wrapper should preserve nested block path twice, got %d in:\n%s", count, got)
}
}
func TestNestedLayout_ThroughEachSeqWrapper(t *testing.T) {
ctx := NewContext()
node := EachSeq(slices.Values([]string{"a", "b"}), func(s string) Node {
return NewLayout("C").C(Raw(s))
})
got := NewLayout("C").C(node).Render(ctx)
if count := strings.Count(got, `data-block="C-0-C-0"`); count != 2 {
t.Fatalf("eachseq wrapper should preserve nested block path twice, got %d in:\n%s", count, got)
}
}
func TestNestedLayout_ThroughElementWrapper(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("wrapped"))
wrapped := El("section", inner)
got := NewLayout("C").C(wrapped).Render(ctx)
if !strings.Contains(got, `data-block="C-0-C-0"`) {
t.Fatalf("element wrapper should preserve nested block path, got:\n%s", got)
}
}
func TestNestedLayout_ThroughResponsiveWrapper(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("wrapped"))
wrapped := NewResponsive().
Variant("desktop", inner)
got := NewLayout("C").C(wrapped).Render(ctx)
if !strings.Contains(got, `data-block="C-0-C-0"`) {
t.Fatalf("responsive wrapper should preserve nested block path, got:\n%s", got)
}
}
func TestNestedLayout_NilChild(t *testing.T) {
ctx := NewContext()
got := NewLayout("C").C(nil, Raw("leaf")).Render(ctx)
if !strings.Contains(got, "leaf") {
t.Fatalf("layout with nil child should still render leaf content, got:\n%s", got)
}
if strings.Contains(got, "<nil>") {
t.Fatalf("layout with nil child should not render placeholder text, got:\n%s", got)
}
}
func TestBlockID(t *testing.T) {
tests := []struct {
path string
slot byte
want string
}{
{"", 'H', "H-0"},
{"L-0-", 'C', "L-0-C-0"},
{"C-0-C-0-", 'C', "C-0-C-0-C-0"},
{"", 'F', "F-0"},
}
for _, tt := range tests {
l := &Layout{path: tt.path}
got := l.blockID(tt.slot)
if got != tt.want {
t.Errorf("blockID(%q, %c) = %q, want %q", tt.path, tt.slot, got, tt.want)
}
}
}
func TestParseBlockID(t *testing.T) {
tests := []struct {
id string
want []byte
}{
{"L-0-C-0", []byte{'L', 'C'}},
{"H-0", []byte{'H'}},
{"C-0-C-0-C-0", []byte{'C', 'C', 'C'}},
{"", nil},
{"L-1-C-0", nil},
{"L-0-C", nil},
{"LL-0", nil},
{"X-0", nil},
{"H-0-X-0", nil},
}
for _, tt := range tests {
got := ParseBlockID(tt.id)
if len(got) != len(tt.want) {
t.Errorf("ParseBlockID(%q) = %v, want %v", tt.id, got, tt.want)
continue
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("ParseBlockID(%q)[%d] = %c, want %c", tt.id, i, got[i], tt.want[i])
}
}
}
}