go-html/pipeline_test.go
Snider c525437ed6
Some checks failed
Security Scan / security (pull_request) Successful in 9s
Test / test (pull_request) Failing after 20s
refactor(module): migrate module path from forge.lthn.ai to dappco.re
Update module path from forge.lthn.ai/core/go-html to dappco.re/go/core/html.
Migrate all .go import paths, update dependency versions (core v0.5.0,
log v0.1.0, io v0.2.0), and add replace directives for local development.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 01:28:30 +00:00

130 lines
3.1 KiB
Go

//go:build !js
package html
import (
"testing"
i18n "dappco.re/go/core/i18n"
)
func TestStripTags_Simple(t *testing.T) {
got := StripTags(`<div>hello</div>`)
want := "hello"
if got != want {
t.Errorf("StripTags(<div>hello</div>) = %q, want %q", got, want)
}
}
func TestStripTags_Nested(t *testing.T) {
got := StripTags(`<header role="banner"><h1>Title</h1></header>`)
want := "Title"
if got != want {
t.Errorf("StripTags(nested) = %q, want %q", got, want)
}
}
func TestStripTags_MultipleRegions(t *testing.T) {
got := StripTags(`<header>Head</header><main>Body</main><footer>Foot</footer>`)
want := "Head Body Foot"
if got != want {
t.Errorf("StripTags(multi) = %q, want %q", got, want)
}
}
func TestStripTags_Empty(t *testing.T) {
got := StripTags("")
if got != "" {
t.Errorf("StripTags(\"\") = %q, want empty", got)
}
}
func TestStripTags_NoTags(t *testing.T) {
got := StripTags("plain text")
if got != "plain text" {
t.Errorf("StripTags(plain) = %q, want %q", got, "plain text")
}
}
func TestStripTags_Entities(t *testing.T) {
got := StripTags(`&lt;script&gt;`)
want := "&lt;script&gt;"
if got != want {
t.Errorf("StripTags should preserve entities, got %q, want %q", got, want)
}
}
func TestImprint_FromNode(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
ctx := NewContext()
page := NewLayout("HCF").
H(El("h1", Text("Building project"))).
C(El("p", Text("Files deleted successfully"))).
F(El("small", Text("Completed")))
imp := Imprint(page, ctx)
if imp.TokenCount == 0 {
t.Error("Imprint should produce non-zero token count")
}
if imp.UniqueVerbs == 0 {
t.Error("Imprint should find verbs in rendered content")
}
}
func TestImprint_SimilarPages(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
ctx := NewContext()
page1 := NewLayout("HCF").
H(El("h1", Text("Building project"))).
C(El("p", Text("Files deleted successfully")))
page2 := NewLayout("HCF").
H(El("h1", Text("Building system"))).
C(El("p", Text("Files removed successfully")))
different := NewLayout("HCF").
C(El("p", Raw("no grammar content here xyz abc")))
imp1 := Imprint(page1, ctx)
imp2 := Imprint(page2, ctx)
impDiff := Imprint(different, ctx)
sim := imp1.Similar(imp2)
diffSim := imp1.Similar(impDiff)
if sim <= diffSim {
t.Errorf("similar pages should score higher (%f) than different (%f)", sim, diffSim)
}
}
func TestCompareVariants(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
ctx := NewContext()
r := NewResponsive().
Variant("desktop", NewLayout("HLCRF").
H(El("h1", Text("Building project"))).
C(El("p", Text("Files deleted successfully"))).
F(El("small", Text("Completed")))).
Variant("mobile", NewLayout("HCF").
H(El("h1", Text("Building project"))).
C(El("p", Text("Files deleted successfully"))).
F(El("small", Text("Completed"))))
scores := CompareVariants(r, ctx)
key := "desktop:mobile"
sim, ok := scores[key]
if !ok {
t.Fatalf("CompareVariants missing key %q, got keys: %v", key, scores)
}
if sim < 0.8 {
t.Errorf("same content in different variants should score >= 0.8, got %f", sim)
}
}