//go:build contract package studenttests_test import ( "bytes" "crypto/sha256" "encoding/binary" "encoding/hex" "image" "image/color" "image/jpeg" "image/png" "io" "net/http" "testing" "time" "golang.org/x/image/webp" "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 := processedVariant(t, source.Bytes(), "image/png", "thumbnail") 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 TestVariantsEXIF(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:]...) for _, variant := range []string{"thumbnail", "optimized"} { t.Run(variant, func(t *testing.T) { result, width, height := processedVariant(t, data, "image/jpeg", variant) if width != 20 || height != 40 { t.Fatalf("orientation ignored: %dx%d", width, height) } decoded, _, err := image.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")) || bytes.Contains(result, []byte("EXIF")) { t.Fatal("EXIF retained") } }) } } func processedVariant(t *testing.T, source []byte, contentType, variant 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 string(v.Name) != variant { continue } resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content?variant="+variant, user.Token, nil) requireStatus(t, resp, http.StatusOK) defer resp.Body.Close() data, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } sum := sha256.Sum256(data) if len(data) != v.SizeBytes || hex.EncodeToString(sum[:]) != v.Sha256 || resp.Header.Get("Content-Type") != string(v.ContentType) { t.Fatal("variant content disagrees with metadata") } return data, v.Width, v.Height } t.Fatalf("ready attachment has no %s", variant) return nil, 0, 0 } func TestCrop(t *testing.T) { for _, tc := range []struct { name string width, height int }{ {"landscape", 384, 128}, {"portrait", 128, 384}, {"small", 24, 8}, } { t.Run(tc.name, func(t *testing.T) { src := image.NewNRGBA(image.Rect(0, 0, tc.width, tc.height)) // Only the central square is green; a stretched full image retains red edges. side := min(tc.width, tc.height) left, top := (tc.width-side)/2, (tc.height-side)/2 for y := 0; y < tc.height; y++ { for x := 0; x < tc.width; x++ { c := color.NRGBA{R: 255, A: 255} if x >= left && x < left+side && y >= top && y < top+side { c = color.NRGBA{G: 255, A: 255} } src.SetNRGBA(x, y, c) } } var source bytes.Buffer if err := png.Encode(&source, src); err != nil { t.Fatal(err) } data, width, height := processedVariant(t, source.Bytes(), "image/png", "crop") decoded, err := jpeg.Decode(bytes.NewReader(data)) if err != nil { t.Fatal(err) } if width != 128 || height != 128 || decoded.Bounds() != image.Rect(0, 0, 128, 128) { t.Fatalf("expected crop 128x128, metadata=%dx%d, JPEG=%v", width, height, decoded.Bounds()) } for _, point := range []image.Point{{8, 8}, {64, 64}, {119, 119}} { r, g, b, _ := decoded.At(point.X, point.Y).RGBA() if g < 50000 || r > 15000 || b > 15000 { t.Fatalf("crop is not centered at %v", point) } } }) } } func TestOptimized(t *testing.T) { for _, tc := range []struct { name string width, height, wantWidth, wantHeight int }{ {"landscape", 1600, 1000, 1280, 800}, {"portrait", 1000, 1600, 800, 1280}, {"small", 12, 7, 12, 7}, } { 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 := processedVariant(t, source.Bytes(), "image/png", "optimized") decoded, err := webp.Decode(bytes.NewReader(data)) if err != nil { t.Fatal(err) } if width != tc.wantWidth || height != tc.wantHeight || decoded.Bounds().Dx() != width || decoded.Bounds().Dy() != height { t.Fatalf("unexpected optimized dimensions: metadata=%dx%d, WebP=%v", width, height, decoded.Bounds()) } _, _, _, alpha := decoded.At(0, 0).RGBA() if alpha != 0 { t.Fatal("optimized image lost transparency") } }) } }