package imageprocessor import ( "bytes" "errors" "io" "testing" "sneak.berlin/go/pixa/internal/imagerequest" ) // TestImageProcessor_LoadsEachSourceFormat verifies that an image in each of // the six source formats, saved by the processor itself, is loaded and saved // again in its own format (orig). func TestImageProcessor_LoadsEachSourceFormat(t *testing.T) { t.Parallel() proc := New(Params{MaxInputBytes: 50 << 20, MaxConcurrentProcessing: 1}) for _, format := range []imagerequest.Format{ imagerequest.FormatJPEG, imagerequest.FormatPNG, imagerequest.FormatGIF, imagerequest.FormatWebP, imagerequest.FormatAVIF, imagerequest.FormatJXL, } { saved, err := proc.Process(t.Context(), bytes.NewReader(createTestPNG(t, 20, 20)), imagerequest.FormatPNG, &imagerequest.Transform{Format: format}) if err != nil { t.Fatalf("saving the test image as %s: %v", format, err) } source, err := io.ReadAll(saved.Content) if err != nil { t.Fatalf("reading the %s image: %v", format, err) } result, err := proc.Process(t.Context(), bytes.NewReader(source), format, &imagerequest.Transform{Format: imagerequest.FormatOriginal}) if err != nil { t.Errorf("orig of the %s image: %v", format, err) continue } t.Logf("orig of the %s image: %s", format, result.ContentType) if result.ContentType != format.MIMEType() { t.Errorf("orig of the %s image is %s, want %s", format, result.ContentType, format.MIMEType()) } } } // TestImageProcessor_NotASourceFormatRefused verifies that a source format // other than the six, such as orig, auto or none, is refused with // ErrUnsupportedInputFormat. The input is text, which libvips reads as no // format. func TestImageProcessor_NotASourceFormatRefused(t *testing.T) { t.Parallel() proc := New(Params{MaxInputBytes: 50 << 20, MaxConcurrentProcessing: 1}) for _, sourceFormat := range []imagerequest.Format{ imagerequest.FormatOriginal, imagerequest.FormatAuto, "", } { result, err := proc.Process(t.Context(), bytes.NewReader([]byte("this is text, not an image")), sourceFormat, &imagerequest.Transform{Format: imagerequest.FormatJPEG}) t.Logf("source format %q: error = %v", sourceFormat, err) if result != nil || !errors.Is(err, ErrUnsupportedInputFormat) { t.Errorf("Process() with source format %q = %v, error %v; want %v", sourceFormat, result, err, ErrUnsupportedInputFormat) } } }