1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package media
import (
"image"
"image/png"
"os"
"path/filepath"
"testing"
"github.com/davidbyttow/govips/v2/vips"
)
func TestMain(m *testing.M) {
vips.Startup(nil)
code := m.Run()
vips.Shutdown()
os.Exit(code)
}
// testPNG writes a 100×60 PNG to a temp file and returns its path.
func testPNG(t *testing.T) string {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, 100, 60))
path := filepath.Join(t.TempDir(), "src.png")
f, err := os.Create(path)
if err != nil {
t.Fatalf("create test png: %v", err)
}
if err := png.Encode(f, img); err != nil {
t.Fatalf("encode test png: %v", err)
}
f.Close()
return path
}
func TestConvertToWebP(t *testing.T) {
out, err := ConvertAndResize(testPNG(t), 0, "webp")
if err != nil {
t.Fatalf("ConvertAndResize: %v", err)
}
if len(out) == 0 {
t.Fatal("expected non-empty output")
}
}
func TestConvertToJPEG(t *testing.T) {
out, err := ConvertAndResize(testPNG(t), 0, "jpeg")
if err != nil {
t.Fatalf("ConvertAndResize: %v", err)
}
if len(out) == 0 {
t.Fatal("expected non-empty output")
}
}
func TestResizeAndConvert(t *testing.T) {
out, err := ConvertAndResize(testPNG(t), 50, "webp")
if err != nil {
t.Fatalf("ConvertAndResize resize: %v", err)
}
if len(out) == 0 {
t.Fatal("expected non-empty output")
}
}
func TestConvertUnsupportedFormat(t *testing.T) {
_, err := ConvertAndResize(testPNG(t), 0, "avif")
if err == nil {
t.Fatal("expected error for unsupported format")
}
}
func TestConvertMissingFile(t *testing.T) {
_, err := ConvertAndResize("/nonexistent/file.png", 0, "webp")
if err == nil {
t.Fatal("expected error for missing file")
}
}
|