@@ -44,6 +44,7 @@ import Cardano.Slotting.Time qualified as Slotting
4444
4545import Control.Monad.Identity (Identity )
4646import Data.Bifunctor (first )
47+ import Data.ByteString qualified as BS
4748import Data.Foldable (toList )
4849import Data.Map.Strict qualified as Map
4950import Data.Maybe (fromMaybe )
@@ -54,8 +55,14 @@ import Data.Time qualified as Time
5455import Data.Time.Clock.POSIX qualified as Time
5556import Lens.Micro
5657
57- import Test.Gen.Cardano.Api.Experimental (genAnyScript )
58- import Test.Gen.Cardano.Api.Typed (genAddressInEra , genTx , genTxIn )
58+ import Test.Gen.Cardano.Api.Experimental (genAnyScript , genSimpleScriptInEra )
59+ import Test.Gen.Cardano.Api.Typed
60+ ( genAddressInEra
61+ , genPlutusScriptInEra
62+ , genSimpleScript
63+ , genTx
64+ , genTxIn
65+ )
5966
6067import Hedgehog (Gen , Property )
6168import Hedgehog qualified as H
@@ -95,6 +102,21 @@ tests =
95102 " Roundtrip serialiseToCBOR/deserialiseFromCBOR AnyScript"
96103 prop_roundtrip_cbor_any_script
97104 ]
105+ , testGroup
106+ " readAnyScriptBytes"
107+ [ testProperty
108+ " Roundtrip Plutus script text envelope"
109+ prop_roundtrip_plutus_script_text_envelope
110+ , testProperty
111+ " Roundtrip simple script text envelope"
112+ prop_roundtrip_simple_script_text_envelope
113+ , testProperty
114+ " Read legacy JSON simple script"
115+ prop_read_legacy_json_simple_script
116+ , testProperty
117+ " Roundtrip readFileAnyScript"
118+ prop_roundtrip_read_file_any_script
119+ ]
98120 , testGroup
99121 " makeUnsignedTx"
100122 [ testProperty
@@ -132,6 +154,49 @@ prop_roundtrip_cbor_any_script = H.property $ do
132154 script <- H. forAll genAnyScript
133155 H. tripping script Api. serialiseToCBOR (Api. deserialiseFromCBOR Exp. AsAnyScript )
134156
157+ prop_roundtrip_plutus_script_text_envelope :: Property
158+ prop_roundtrip_plutus_script_text_envelope = H. property $ do
159+ ps <- H. forAll genPlutusScriptInEra
160+ let envelopeJson = Api. serialiseToJSON $ Api. serialiseToTextEnvelope Nothing ps
161+ script <- H. evalEither $ Exp. readAnyScriptBytes Exp. ConwayEra envelopeJson
162+ script H. === Exp. AnyPlutusScript ps
163+
164+ -- | Simple scripts have no text envelope format of their own, so the envelope
165+ -- is constructed by hand using the \"SimpleScript\" type string written by the
166+ -- old API's 'Api.Script' text envelope instance.
167+ prop_roundtrip_simple_script_text_envelope :: Property
168+ prop_roundtrip_simple_script_text_envelope = H. property $ do
169+ ss <- H. forAll genSimpleScriptInEra
170+ let envelope =
171+ Api. TextEnvelope
172+ { Api. teType = Api. TextEnvelopeType " SimpleScript"
173+ , Api. teDescription = " "
174+ , Api. teRawCBOR = Api. serialiseToCBOR ss
175+ }
176+ script <- H. evalEither $ Exp. readAnyScriptBytes Exp. ConwayEra (Api. serialiseToJSON envelope)
177+ script H. === Exp. AnySimpleScript ss
178+
179+ prop_read_legacy_json_simple_script :: Property
180+ prop_read_legacy_json_simple_script = H. property $ do
181+ oldScript <- H. forAll genSimpleScript
182+ script <- H. evalEither $ Exp. readAnyScriptBytes Exp. ConwayEra (Api. serialiseToJSON oldScript)
183+ script H. === Exp. AnySimpleScript (Exp. SimpleScript (Api. toAllegraTimelock oldScript))
184+
185+ prop_roundtrip_read_file_any_script :: Property
186+ prop_roundtrip_read_file_any_script = H. propertyOnce . H. moduleWorkspace " any-script" $ \ ws -> do
187+ ss <- H. forAll genSimpleScriptInEra
188+ let envelope =
189+ Api. TextEnvelope
190+ { Api. teType = Api. TextEnvelopeType " SimpleScript"
191+ , Api. teDescription = " "
192+ , Api. teRawCBOR = Api. serialiseToCBOR ss
193+ }
194+ path = ws <> " /simple-script.json"
195+ H. evalIO $ BS. writeFile path (Api. serialiseToJSON envelope)
196+ result <- H. evalIO $ Exp. readFileAnyScript Exp. ConwayEra (Api. File path)
197+ script <- H. evalEither result
198+ script H. === Exp. AnySimpleScript ss
199+
135200prop_created_transaction_with_both_apis_are_the_same :: Property
136201prop_created_transaction_with_both_apis_are_the_same = H. propertyOnce $ do
137202 let era = Exp. ConwayEra
0 commit comments