Files
mtusi2026lab5/student-tests/thumbnail_test.go
T

136 lines
3.3 KiB
Go

//go:build contract
package studenttests_test
import (
"bytes"
"encoding/binary"
"image"
"image/color"
"image/jpeg"
"image/png"
"io"
"net/http"
"testing"
"time"
"mtusi2026-messenger/internal/dto"
)
func TestThumbnail(t *testing.T) {
for _, tc := range []struct {
name string
width, height, wantWidth, wantHeight int
}{
{"landscape", 320, 200, 256, 160}, {"portrait", 201, 321, 160, 256},
{"small", 12, 7, 12, 7}, {"thin", 1, 1000, 1, 256},
} {
t.Run(tc.name, func(t *testing.T) {
var source bytes.Buffer
if err := png.Encode(&source, image.NewNRGBA(image.Rect(0, 0, tc.width, tc.height))); err != nil {
t.Fatal(err)
}
data, width, height := processedThumbnail(t, source.Bytes(), "image/png")
if width != tc.wantWidth || height != tc.wantHeight {
t.Fatalf("got %dx%d", width, height)
}
decoded, err := jpeg.Decode(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if decoded.Bounds().Dx() != width || decoded.Bounds().Dy() != height {
t.Fatal("JPEG dimensions disagree")
}
r, g, b, _ := decoded.At(0, 0).RGBA()
if r < 65000 || g < 65000 || b < 65000 {
t.Fatal("transparent source was not composited on white")
}
})
}
}
func TestThumbnailEXIF(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 40, 20))
for y := 0; y < 20; y++ {
for x := 0; x < 40; x++ {
c := color.RGBA{255, 0, 0, 255}
if x >= 20 {
c = color.RGBA{0, 0, 255, 255}
}
src.Set(x, y, c)
}
}
var original bytes.Buffer
if err := jpeg.Encode(&original, src, nil); err != nil {
t.Fatal(err)
}
// Little-endian TIFF, orientation=6 (90 degrees clockwise).
exif := []byte{
'E', 'x', 'i', 'f', 0, 0, 'I', 'I',
42, 0, 8, 0, 0, 0, 1, 0,
0x12, 1, 3, 0, 1, 0, 0, 0,
6, 0, 0, 0, 0, 0, 0, 0,
}
segment := []byte{0xff, 0xe1, 0, 0}
binary.BigEndian.PutUint16(segment[2:], uint16(len(exif)+2))
data := append([]byte{}, original.Bytes()[:2]...)
data = append(data, segment...)
data = append(data, exif...)
data = append(data, original.Bytes()[2:]...)
result, width, height := processedThumbnail(t, data, "image/jpeg")
if width != 20 || height != 40 {
t.Fatalf("orientation ignored: %dx%d", width, height)
}
decoded, err := jpeg.Decode(bytes.NewReader(result))
if err != nil {
t.Fatal(err)
}
r, _, b, _ := decoded.At(10, 5).RGBA()
if r <= b {
t.Fatal("incorrect EXIF rotation")
}
if bytes.Contains(result, []byte("Exif")) {
t.Fatal("EXIF retained")
}
}
func processedThumbnail(t *testing.T, source []byte, contentType string) ([]byte, int, int) {
t.Helper()
user := createUser(t)
chat := createChat(t, user, user.ID)
resp := uploadAttachment(t, user, chat.ID, "image", contentType, source)
requireStatus(t, resp, http.StatusAccepted)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
att = waitAttachment(t, user, att.ID, "ready", time.Minute)
for _, v := range att.Variants {
if v.Name != "thumbnail" {
continue
}
resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content?variant=thumbnail", user.Token, nil)
requireStatus(t, resp, http.StatusOK)
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
return data, v.Width, v.Height
}
t.Fatal("ready attachment has no thumbnail")
return nil, 0, 0
}