feat(media): unify uploads onto modules/album, add batched srcset enrichment and Media Library picker
This commit is contained in:
85
classes/enrich_media_test.go
Normal file
85
classes/enrich_media_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/album"
|
||||
)
|
||||
|
||||
func TestExtractMediaShortIDs(t *testing.T) {
|
||||
doc := `<p>Intro</p>` +
|
||||
`<img src="/media/abc123456789/lion-mane.jpg" alt="Lion">` +
|
||||
`<img src="/uploads/public/012/345/678/legacy.png" alt="Legacy">` +
|
||||
`<img src="https://example.com/media/xyz987654321/chart.png?v=1" alt="Full URL">` +
|
||||
`<img src="/media/abc123456789/lion-mane.jpg" alt="Duplicate">` +
|
||||
`<img src="/media/short/too-short.jpg" alt="Invalid short id">` +
|
||||
`<img src="https://cdn.example.com/assets/media/qwe123456789/diagram.webp" alt="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 := `<img src="/uploads/public/1/2/3/old.jpg" alt="Old" class="keep" />`
|
||||
in := `<p>Hello</p>` + legacy + `<figure><img src="/media/abc123456789/hero.jpg" alt="Hero"><figcaption>Cap</figcaption></figure>`
|
||||
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 := `<img src="/media/abc123456789/hero.jpg" sizes="50vw" width="600" height="338" />`
|
||||
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 := `<p><img src="/media/abc123456789/hero.jpg" alt="A"></p>`
|
||||
if got := EnrichMediaHTML(in, nil, nil); got != in {
|
||||
t.Fatalf("got %q, want %q", got, in)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user