feat(html): canonicalise variant comparison keys
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:25:22 +00:00
parent de3a40000c
commit 0ecef2e72f
2 changed files with 24 additions and 1 deletions

View file

@ -79,7 +79,11 @@ func CompareVariants(r *Responsive, ctx *Context) map[string]float64 {
scores := make(map[string]float64)
for i := range len(imprints) {
for j := i + 1; j < len(imprints); j++ {
key := imprints[i].name + ":" + imprints[j].name
left, right := imprints[i].name, imprints[j].name
if right < left {
left, right = right, left
}
key := left + ":" + right
scores[key] = imprints[i].imp.Similar(imprints[j].imp)
}
}

View file

@ -142,6 +142,25 @@ func TestCompareVariants(t *testing.T) {
}
}
func TestCompareVariants_CanonicalKeyOrder(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
ctx := NewContext()
r := NewResponsive().
Variant("mobile", NewLayout("C").C(El("p", Text("Hello")))).
Variant("desktop", NewLayout("C").C(El("p", Text("Hello"))))
scores := CompareVariants(r, ctx)
if _, ok := scores["desktop:mobile"]; !ok {
t.Fatalf("CompareVariants should canonicalise pair keys, got %v", scores)
}
if _, ok := scores["mobile:desktop"]; ok {
t.Fatalf("CompareVariants should not emit reversed duplicate keys, got %v", scores)
}
}
func TestCompareVariants_NilResponsive(t *testing.T) {
scores := CompareVariants(nil, NewContext())
if len(scores) != 0 {