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") } }