From cd32b9ec964fcaa5247bfd7df8373ef89ee129c4 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:33:07 +0000 Subject: [PATCH] tests: image tests of the chat flow on a small image, unit test of the crop (#7980) * tests: profile image from file on a small image, unit test of the crop * tests: image preview in the chat flow on a small image --- tests/ChatTests/Files.hs | 8 ++++---- tests/ChatTests/Profiles.hs | 2 +- tests/ImageTests.hs | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/ChatTests/Files.hs b/tests/ChatTests/Files.hs index aa8a9b5682..0b36a4b1cf 100644 --- a/tests/ChatTests/Files.hs +++ b/tests/ChatTests/Files.hs @@ -181,12 +181,12 @@ testSendImagePreview = testChat2 aliceProfile bobProfile $ \alice bob -> withXFTPServer alice $ do connectUsers alice bob - alice ##> "/img @bob ./tests/fixtures/test.jpg" - alice <# "/f @bob ./tests/fixtures/test.jpg" + alice ##> "/img @bob ./tests/fixtures/logo.jpg" + alice <# "/f @bob ./tests/fixtures/logo.jpg" alice <## "use /fc 1 to cancel sending" - bob <# "alice> sends file test.jpg (136.5 KiB / 139737 bytes)" + bob <# "alice> sends file logo.jpg (31.3 KiB / 32080 bytes)" bob <## "use /fr 1 [/ | ] to receive it" - alice <## "completed uploading file 1 (test.jpg) for bob" + alice <## "completed uploading file 1 (logo.jpg) for bob" preview <- lastImagePreview bob 2 T.length preview `shouldSatisfy` (<= 14000) preview `shouldSatisfy` ("data:image/jpg;base64," `T.isPrefixOf`) diff --git a/tests/ChatTests/Profiles.hs b/tests/ChatTests/Profiles.hs index aa7c281f02..7f274402c0 100644 --- a/tests/ChatTests/Profiles.hs +++ b/tests/ChatTests/Profiles.hs @@ -1218,7 +1218,7 @@ testSetProfileImageFromFile ps = testChat aliceProfile test ps emptyPath = tmp <> "/empty.png" test alice = do -- set profile image from a .jpg file - alice ##> "/set profile image file ./tests/fixtures/test.jpg" + alice ##> "/set profile image file ./tests/fixtures/logo.jpg" alice <## "profile image updated" Profile {image = Just (ImageData img)} <- withCCUser alice $ \User {profile} -> pure $ fromLocalProfile profile T.length img `shouldSatisfy` (<= 12500) diff --git a/tests/ImageTests.hs b/tests/ImageTests.hs index 4bfbd4d5fa..dad1b5722e 100644 --- a/tests/ImageTests.hs +++ b/tests/ImageTests.hs @@ -59,6 +59,7 @@ imageTests = do it "should scale and sharpen as the research code" testScaleResearch it "should keep an image of the same size" testScaleSameSize it "should scale transparency in premultiplied alpha" testScaleTransparency + it "should crop to the centre square" testCropToSquare describe "colour profiles" $ do it "should write the Display P3 profile for Display P3 JPEG" testJpegProfile it "should write the Display P3 profile for Display P3 PNG" testPngProfile @@ -333,6 +334,24 @@ testScaleSameSize = do imageData (scaleRGB8 rgb (64, 48)) `shouldBe` imageData rgb imageData (scaleRGBA8 rgba (64, 48)) `shouldBe` imageData rgba +testCropToSquare :: IO () +testCropToSquare = do + let pixel :: Int -> Int -> PixelRGB8 + pixel x y = PixelRGB8 (fromIntegral x) (fromIntegral y) 128 + rgbaPixel :: Int -> Int -> PixelRGBA8 + rgbaPixel x y = PixelRGBA8 (fromIntegral x) (fromIntegral y) 128 200 + ImageRGB8 wide <- pure $ cropToSquare $ ImageRGB8 $ generateImage pixel 7 4 + imageSize (ImageRGB8 wide) `shouldBe` (4, 4) + imageData wide `shouldBe` imageData (generateImage (\x y -> pixel (x + 1) y) 4 4) + ImageRGB8 tall <- pure $ cropToSquare $ ImageRGB8 $ generateImage pixel 4 7 + imageSize (ImageRGB8 tall) `shouldBe` (4, 4) + imageData tall `shouldBe` imageData (generateImage (\x y -> pixel x (y + 1)) 4 4) + ImageRGBA8 rgba <- pure $ cropToSquare $ ImageRGBA8 $ generateImage rgbaPixel 6 3 + imageSize (ImageRGBA8 rgba) `shouldBe` (3, 3) + imageData rgba `shouldBe` imageData (generateImage (\x y -> rgbaPixel (x + 1) y) 3 3) + ImageRGB8 square <- pure $ cropToSquare $ ImageRGB8 $ generateImage pixel 4 4 + imageData square `shouldBe` imageData (generateImage pixel 4 4) + testScaleTransparency :: IO () testScaleTransparency = do let scaled = scaleRGBA8 (generateImage pixel 64 64) (40, 40)