package classes import ( "slices" "strings" "testing" "git.golem15.com/golem15/summercms/modules/album" ) func TestExtractMediaShortIDs(t *testing.T) { doc := `
Intro
` + `
` +
`
` +
`
` +
`
` +
`
` +
`
`
got := ExtractMediaShortIDs(doc)
want := []string{"abc123456789", "xyz987654321", "qwe123456789"}
if !slices.Equal(got, want) {
t.Fatalf("ExtractMediaShortIDs() = %v, want %v", got, want)
}
}
func TestEnrichMediaHTML(t *testing.T) {
w, h := 1600, 900
items := map[string]album.Item{
"abc123456789": {
ShortID: "abc123456789",
BlobKey: "media/abc123456789/hero.jpg",
Width: &w,
Height: &h,
Variants: album.Variants{
{Width: 480, Height: 270, Key: "media/abc123456789/hero-480w.webp", URL: "/media/abc123456789/hero-480w.webp"},
{Width: 960, Height: 540, Key: "media/abc123456789/hero-960w.webp", URL: "/media/abc123456789/hero-960w.webp"},
},
},
}
t.Run("enriches matching media img and preserves legacy uploads byte-identically", func(t *testing.T) {
legacy := `
`
in := `Hello
` + legacy + `
`
out := EnrichMediaHTML(in, items, nil)
if strings.Count(out, "sizes=") != 1 || !strings.Contains(out, `sizes="50vw"`) {
t.Fatalf("existing sizes overwritten: %s", out)
}
if strings.Count(out, "width=") != 1 || !strings.Contains(out, `width="600"`) {
t.Fatalf("existing width overwritten: %s", out)
}
if !strings.Contains(out, "srcset=") {
t.Fatalf("expected srcset added: %s", out)
}
if !strings.HasSuffix(out, "/>") {
t.Fatalf("expected self-closing syntax preserved: %s", out)
}
})
t.Run("empty items returns input byte-identical", func(t *testing.T) {
in := `