Files
summercms/lagoon/attach/thumb_test.go
Jakub Zych 2d84ae4565 test(05-04): add failing tests for attach File, Thumb, and bucket
- ThumbFilename/PartitionDirectory/File.Thumb behavior tests
- OpenBucket fail-loud and memblob tests
- Migrate empty plugin list must create system_files
2026-09-18 19:53:00 +02:00

85 lines
2.1 KiB
Go

package attach
import (
"bytes"
"image"
"image/color"
"image/jpeg"
"testing"
"gocloud.dev/blob"
"gocloud.dev/blob/memblob"
)
func TestThumbFilename(t *testing.T) {
got := ThumbFilename(42, 200, 200, 0, 0, "crop", "jpg")
const want = "thumb_42_200_200_0_0_crop.jpg"
if got != want {
t.Fatalf("ThumbFilename = %q, want %q", got, want)
}
}
func TestPartitionDirectory(t *testing.T) {
got := PartitionDirectory("abc123xyz.jpg")
const want = "abc/123/xyz/"
if got != want {
t.Fatalf("PartitionDirectory = %q, want %q", got, want)
}
}
func TestFileThumbResizesOnce(t *testing.T) {
ctx := t.Context()
bucket := memblob.OpenBucket(nil)
t.Cleanup(func() { _ = bucket.Close() })
f := &File{ID: 42, DiskName: "abc123xyz.jpg", FileName: "cover.jpg"}
origKey := PartitionDirectory(f.DiskName) + f.DiskName
if origKey == "abc123xyz.jpg" || origKey == "" {
// PartitionDirectory still stubbed — write under the Winter key the
// GREEN implementation will look up so this test stays the behavior spec.
origKey = "abc/123/xyz/" + f.DiskName
}
if err := bucket.WriteAll(ctx, origKey, testJPEG(t), &blob.WriterOptions{ContentType: "image/jpeg"}); err != nil {
t.Fatal(err)
}
var n int
orig := resizeImage
resizeImage = func(src image.Image, w, h int, mode string) image.Image {
n++
return orig(src, w, h, mode)
}
t.Cleanup(func() { resizeImage = orig })
url1, err := f.Thumb(ctx, bucket, 200, 200, "crop")
if err != nil {
t.Fatalf("first Thumb: %v", err)
}
url2, err := f.Thumb(ctx, bucket, 200, 200, "crop")
if err != nil {
t.Fatalf("second Thumb: %v", err)
}
if n != 1 {
t.Fatalf("resize calls = %d, want 1", n)
}
want := "/storage/uploads/abc/123/xyz/thumb_42_200_200_0_0_crop.jpg"
if url1 != want || url2 != want {
t.Fatalf("urls = %q %q, want %q", url1, url2, want)
}
}
func testJPEG(t *testing.T) []byte {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, 8, 8))
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
img.Set(x, y, color.RGBA{R: 200, A: 255})
}
}
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90}); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}