github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/avatar/initials_convert_test.go (about)

     1  package avatar
     2  
     3  import (
     4  	"bytes"
     5  	"image/png"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_Initials_PNG(t *testing.T) {
    13  	if testing.Short() {
    14  		t.Skipf("this test require the \"convert\" binary, skip it due to the \"--short\" flag")
    15  	}
    16  
    17  	client := NewPNGInitials("convert")
    18  	rawRes, err := client.Generate("JD", "#FF7F1B")
    19  	require.NoError(t, err)
    20  
    21  	rawExpected, err := os.ReadFile("./testdata/initials-convert.png")
    22  	require.NoError(t, err)
    23  
    24  	// Due to the compression algorithm we can't compare the bytes
    25  	// as they change for each generation. The only solution is to decode
    26  	// the image and check pixel by pixel.
    27  	// This also allow to ensure that the end result is exactly the same.
    28  	resImg, err := png.Decode(bytes.NewReader(rawRes))
    29  	require.NoError(t, err)
    30  
    31  	expectImg, err := png.Decode(bytes.NewReader(rawExpected))
    32  	require.NoError(t, err)
    33  
    34  	require.Equal(t, expectImg.Bounds(), resImg.Bounds(), "images doesn't have the same size")
    35  }