package classes import ( "slices" "strings" "testing" "git.golem15.com/golem15/summercms/modules/album" ) func TestExtractMediaShortIDs(t *testing.T) { doc := `

Intro

` + `Lion` + `Legacy` + `Full URL` + `Duplicate` + `Invalid short id` + `CDN` 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 := `Old` in := `

Hello

` + legacy + `
Hero
Cap
` out := EnrichMediaHTML(in, items, nil) if !strings.Contains(out, legacy) { t.Fatalf("legacy img mutated:\ngot: %s\nwant: %s", out, legacy) } wantSrcset := `srcset="/media/abc123456789/hero-480w.webp 480w, /media/abc123456789/hero-960w.webp 960w"` if !strings.Contains(out, wantSrcset) { t.Fatalf("missing srcset in %s", out) } if !strings.Contains(out, `sizes="(max-width: 768px) 100vw, 800px"`) { t.Fatalf("missing sizes in %s", out) } if !strings.Contains(out, `width="1600" height="900"`) { t.Fatalf("missing width/height in %s", out) } }) t.Run("does not overwrite existing sizes width height or srcset", func(t *testing.T) { in := `` 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 := `

A

` if got := EnrichMediaHTML(in, nil, nil); got != in { t.Fatalf("got %q, want %q", got, in) } }) }