@@ -2,8 +2,15 @@ module Test.Registry.Metadata (spec) where
22
33import Prelude
44
5+ import Codec.JSON.DecodeError as CJ.DecodeError
6+ import Data.Codec.JSON as CJ
7+ import Data.Either (Either (..))
8+ import Data.Maybe (Maybe (..))
9+ import Data.String as String
10+ import JSON as JSON
511import Registry.Metadata as Metadata
612import Registry.Test.Assert as Assert
13+ import Registry.Test.Utils as Utils
714import Test.Spec (Spec )
815import Test.Spec as Spec
916
@@ -14,6 +21,40 @@ spec = do
1421 [ { label: " record-studio" , value: recordStudio }
1522 ]
1623
24+ Spec .describe " PublishedMetadata ref field (deprecated)" do
25+ Spec .it " Decodes metadata without ref field" do
26+ let json = Utils .fromRight " Failed to parse JSON" $ JSON .parse publishedWithoutRef
27+ case CJ .decode Metadata .publishedMetadataCodec json of
28+ Left err -> Assert .fail $ " Failed to decode: " <> CJ.DecodeError .print err
29+ Right meta -> meta.ref `Assert.shouldEqual` Nothing
30+
31+ Spec .it " Decodes metadata with ref field" do
32+ let json = Utils .fromRight " Failed to parse JSON" $ JSON .parse publishedWithRef
33+ case CJ .decode Metadata .publishedMetadataCodec json of
34+ Left err -> Assert .fail $ " Failed to decode: " <> CJ.DecodeError .print err
35+ Right meta -> meta.ref `Assert.shouldEqual` (Just " v1.0.0" )
36+
37+ Spec .it " Encodes Nothing as missing field (no ref in output)" do
38+ let json = Utils .fromRight " Failed to parse JSON" $ JSON .parse publishedWithoutRef
39+ case CJ .decode Metadata .publishedMetadataCodec json of
40+ Left err -> Assert .fail $ " Failed to decode: " <> CJ.DecodeError .print err
41+ Right meta -> do
42+ let encoded = CJ .encode Metadata .publishedMetadataCodec meta
43+ let jsonStr = JSON .printIndented encoded
44+ -- Nothing should result in the field being omitted
45+ String .contains (String.Pattern " \" ref\" " ) jsonStr `Assert.shouldEqual` false
46+
47+ Spec .it " Encodes Just \"\" as present field" do
48+ let json = Utils .fromRight " Failed to parse JSON" $ JSON .parse publishedWithRef
49+ case CJ .decode Metadata .publishedMetadataCodec json of
50+ Left err -> Assert .fail $ " Failed to decode: " <> CJ.DecodeError .print err
51+ Right meta -> do
52+ let withEmptyRef = meta { ref = Just " " }
53+ let encoded = CJ .encode Metadata .publishedMetadataCodec withEmptyRef
54+ let jsonStr = JSON .printIndented encoded
55+ -- Just "" should result in the field being present (with "ref": "" pattern)
56+ String .contains (String.Pattern " \" ref\" : \"\" " ) jsonStr `Assert.shouldEqual` true
57+
1758recordStudio :: String
1859recordStudio =
1960 """
@@ -65,3 +106,26 @@ recordStudio =
65106 }
66107 }
67108}"""
109+
110+ -- PublishedMetadata without ref field (should decode with ref = Nothing)
111+ publishedWithoutRef :: String
112+ publishedWithoutRef =
113+ """
114+ {
115+ "bytes": 3438,
116+ "compilers": ["0.15.10"],
117+ "hash": "sha256-LPRUC8ozZc7VCeRhKa4CtSgAfNqgAoVs2lH+7mYEcTk=",
118+ "publishedTime": "2021-03-27T10:03:46.000Z"
119+ }"""
120+
121+ -- PublishedMetadata with ref field (should decode with ref = Just "v1.0.0")
122+ publishedWithRef :: String
123+ publishedWithRef =
124+ """
125+ {
126+ "bytes": 3438,
127+ "compilers": ["0.15.10"],
128+ "hash": "sha256-LPRUC8ozZc7VCeRhKa4CtSgAfNqgAoVs2lH+7mYEcTk=",
129+ "publishedTime": "2021-03-27T10:03:46.000Z",
130+ "ref": "v1.0.0"
131+ }"""
0 commit comments