|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 3 | + |
| 4 | +module Cardano.DbSync.OffChain.Vote.TypesTest (tests) where |
| 5 | + |
| 6 | +import Cardano.DbSync.OffChain.Vote.Types |
| 7 | +import Cardano.Prelude |
| 8 | +import qualified Data.Aeson as Aeson |
| 9 | +import qualified Data.ByteString.Lazy as LBS |
| 10 | +import qualified Data.Text as Text |
| 11 | +import Hedgehog |
| 12 | +import qualified Hedgehog as H |
| 13 | +import Prelude () |
| 14 | + |
| 15 | +tests :: IO Bool |
| 16 | +tests = |
| 17 | + checkParallel $ |
| 18 | + Group |
| 19 | + "Cardano.DbSync.OffChain.Vote.Types" |
| 20 | + [ ("Image preserves data URI prefix", prop_image_preserves_data_uri_prefix) |
| 21 | + , ("Image handles URL with hash", prop_image_handles_url_with_hash) |
| 22 | + , ("Image preserves JPEG data URI", prop_image_preserves_jpeg_data_uri) |
| 23 | + ] |
| 24 | + |
| 25 | +prop_image_preserves_data_uri_prefix :: Property |
| 26 | +prop_image_preserves_data_uri_prefix = property $ do |
| 27 | + let jsonWithDataUri = |
| 28 | + LBS.fromStrict $ |
| 29 | + encodeUtf8 |
| 30 | + "{ \"contentUrl\": \"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA\" }" |
| 31 | + |
| 32 | + case Aeson.eitherDecode jsonWithDataUri of |
| 33 | + Left err -> do |
| 34 | + H.footnote $ "Parse failed: " <> err |
| 35 | + H.failure |
| 36 | + Right (img :: Image) -> do |
| 37 | + let imgContent = textValue $ content img |
| 38 | + H.assert $ "data:" `Text.isPrefixOf` imgContent |
| 39 | + H.assert $ "image/png" `Text.isInfixOf` imgContent |
| 40 | + H.assert $ "base64" `Text.isInfixOf` imgContent |
| 41 | + imgContent === "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" |
| 42 | + |
| 43 | +prop_image_handles_url_with_hash :: Property |
| 44 | +prop_image_handles_url_with_hash = property $ do |
| 45 | + let jsonWithUrl = |
| 46 | + LBS.fromStrict $ |
| 47 | + encodeUtf8 |
| 48 | + "{ \"contentUrl\": \"https://example.com/image.png\", \"sha256\": \"abc123\" }" |
| 49 | + |
| 50 | + case Aeson.eitherDecode jsonWithUrl of |
| 51 | + Left err -> do |
| 52 | + H.footnote $ "Parse failed: " <> err |
| 53 | + H.failure |
| 54 | + Right (img :: Image) -> do |
| 55 | + textValue (content img) === "https://example.com/image.png" |
| 56 | + (textValue <$> msha256 img) === Just "abc123" |
| 57 | + |
| 58 | +prop_image_preserves_jpeg_data_uri :: Property |
| 59 | +prop_image_preserves_jpeg_data_uri = property $ do |
| 60 | + let jsonWithJpeg = |
| 61 | + LBS.fromStrict $ |
| 62 | + encodeUtf8 |
| 63 | + "{ \"contentUrl\": \"data:image/jpeg;base64,/9j/4AAQSkZJRg\" }" |
| 64 | + |
| 65 | + case Aeson.eitherDecode jsonWithJpeg of |
| 66 | + Left err -> do |
| 67 | + H.footnote $ "Parse failed: " <> err |
| 68 | + H.failure |
| 69 | + Right (img :: Image) -> do |
| 70 | + let imgContent = textValue $ content img |
| 71 | + imgContent === "data:image/jpeg;base64,/9j/4AAQSkZJRg" |
0 commit comments