53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package cabana
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/album"
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
)
|
|
|
|
// TestMediaItemViewRecomputesVariantURLs guards WR-02: variant and thumbnail
|
|
// URLs must be recomputed from v.Key against current config rather than
|
|
// serving stale persisted upload-time URLs.
|
|
func TestMediaItemViewRecomputesVariantURLs(t *testing.T) {
|
|
cfg, err := compass.Open(compass.Options{Dir: t.TempDir()})
|
|
if err != nil {
|
|
t.Fatalf("compass.Open: %v", err)
|
|
}
|
|
if err := cfg.Set(album.ConfigPublicPathPrefix, "/cdn-media"); err != nil {
|
|
t.Fatalf("cfg.Set: %v", err)
|
|
}
|
|
app := backpack.New(cfg)
|
|
svc := &service{app: app}
|
|
|
|
item := album.Item{
|
|
ID: 42,
|
|
ShortID: "abc123456789",
|
|
BlobKey: "media/abc123456789/hero.png",
|
|
Slug: "hero",
|
|
FileName: "hero.png",
|
|
ContentType: "image/png",
|
|
Variants: album.Variants{
|
|
{
|
|
Width: 480,
|
|
Height: 320,
|
|
Key: "media/abc123456789/hero-480w.webp",
|
|
URL: "/media/abc123456789/hero-480w.webp", // stale persisted URL
|
|
Size: 1234,
|
|
ContentType: "image/webp",
|
|
},
|
|
},
|
|
}
|
|
|
|
view := svc.mediaItemView(item)
|
|
wantVarURL := "/cdn-media/abc123456789/hero-480w.webp"
|
|
if view.ThumbURL != wantVarURL {
|
|
t.Fatalf("ThumbURL = %q, want %q", view.ThumbURL, wantVarURL)
|
|
}
|
|
if len(view.Variants) != 1 || view.Variants[0].URL != wantVarURL {
|
|
t.Fatalf("Variants[0].URL = %q, want %q", view.Variants[0].URL, wantVarURL)
|
|
}
|
|
}
|