1+ import pytest
12from pathlib import Path
2-
3- from polywrap_client import PolywrapClient
4- from polywrap_core import Uri , InvokerOptions , InterfaceImplementations , Env
5- from polywrap_uri_resolvers import BaseUriResolver , SimpleFileReader
6-
7- from polywrap_client .client import PolywrapClientConfig
8-
9-
10- async def test_invoke ():
3+ import pytest
4+ from polywrap_client import PolywrapClient , PolywrapClientConfig
5+ from polywrap_manifest import deserialize_wrap_manifest
6+ from polywrap_core import Uri , InvokerOptions , UriWrapper
7+ from polywrap_uri_resolvers import BaseUriResolver , SimpleFileReader , StaticResolver
8+ from polywrap_result import Result , Ok , Err
9+ from polywrap_wasm import WRAP_MANIFEST_PATH , WRAP_MODULE_PATH , IFileReader , WasmWrapper
10+
11+ @pytest .fixture
12+ def simple_wrap_module ():
13+ wrap_path = Path (__file__ ).parent / "cases" / "simple-invoke" / "wrap.wasm"
14+ with open (wrap_path , "rb" ) as f :
15+ yield f .read ()
16+
17+
18+ @pytest .fixture
19+ def simple_wrap_manifest ():
20+ wrap_path = Path (__file__ ).parent / "cases" / "simple-invoke" / "wrap.info"
21+ with open (wrap_path , "rb" ) as f :
22+ yield f .read ()
23+
24+ @pytest .fixture
25+ def simple_file_reader (simple_wrap_module : bytes , simple_wrap_manifest : bytes ):
26+ class FileReader (IFileReader ):
27+ async def read_file (self , file_path : str ) -> Result [bytes ]:
28+ if file_path == WRAP_MODULE_PATH :
29+ return Ok (simple_wrap_module )
30+ if file_path == WRAP_MANIFEST_PATH :
31+ return Ok (simple_wrap_manifest )
32+ return Err .from_str (f"FileNotFound: { file_path } " )
33+
34+ yield FileReader ()
35+
36+ @pytest .mark .asyncio
37+ async def test_invoke (
38+ simple_file_reader : IFileReader ,
39+ simple_wrap_module : bytes ,
40+ simple_wrap_manifest : bytes
41+ ):
1142 client = PolywrapClient ()
1243 uri = Uri (
1344 f'fs/{ Path (__file__ ).parent .joinpath ("cases" , "simple-invoke" ).absolute ()} '
@@ -20,6 +51,29 @@ async def test_invoke():
2051
2152 assert result .unwrap () == args ["arg" ]
2253
54+ manifest = deserialize_wrap_manifest (simple_wrap_manifest ).unwrap ()
55+
56+ wrapper = WasmWrapper (
57+ file_reader = simple_file_reader ,
58+ wasm_module = simple_wrap_module ,
59+ manifest = manifest
60+ )
61+ uri_wrapper = UriWrapper (uri = Uri ("ens/wrapper.eth" ), wrapper = wrapper )
62+ resolver = StaticResolver .from_list ([uri_wrapper ]).unwrap ()
63+
64+ config = PolywrapClientConfig (resolver = resolver )
65+ client = PolywrapClient (config = config )
66+
67+ args = {"arg" : "hello polywrap" }
68+ options = InvokerOptions (
69+ uri = Uri ("ens/wrapper.eth" ),
70+ method = "simpleMethod" ,
71+ args = args ,
72+ encode_result = False
73+ )
74+ result = await client .invoke (options )
75+
76+ assert result .unwrap () == args ["arg" ]
2377
2478async def test_subinvoke ():
2579 uri_resolver = BaseUriResolver (
@@ -31,7 +85,7 @@ async def test_subinvoke():
3185 },
3286 )
3387
34- client = PolywrapClient (config = PolywrapClientConfig (envs = [], resolver = uri_resolver ))
88+ client = PolywrapClient (config = PolywrapClientConfig (resolver = uri_resolver ))
3589 uri = Uri (
3690 f'fs/{ Path (__file__ ).parent .joinpath ("cases" , "simple-subinvoke" , "invoke" ).absolute ()} '
3791 )
@@ -48,19 +102,15 @@ async def test_interface_implementation():
48102 redirects = {},
49103 )
50104
105+ interface_uri = Uri ("ens/interface.eth" )
51106 impl_uri = Uri (
52107 f'fs/{ Path (__file__ ).parent .joinpath ("cases" , "simple-interface" , "implementation" ).absolute ()} '
53108 )
54109
55110 client = PolywrapClient (
56111 config = PolywrapClientConfig (
57- envs = [],
58112 resolver = uri_resolver ,
59- interfaces = [
60- InterfaceImplementations (
61- interface = Uri ("ens/interface.eth" ), implementations = [impl_uri ]
62- )
63- ],
113+ interfaces = {interface_uri : [impl_uri ]}
64114 )
65115 )
66116 uri = Uri (
@@ -71,10 +121,26 @@ async def test_interface_implementation():
71121 uri = uri , method = "moduleMethod" , args = args , encode_result = False
72122 )
73123 result = await client .invoke (options )
74-
124+ assert client . get_implementations ( interface_uri ) == Ok ([ impl_uri ])
75125 assert result .unwrap () == {"str" : "hello" , "uint8" : 2 }
76126
77127
128+ def test_get_env_by_uri ():
129+ uri_resolver = BaseUriResolver (
130+ file_reader = SimpleFileReader (),
131+ redirects = {},
132+ )
133+ uri = Uri (f'fs/{ Path (__file__ ).parent .joinpath ("cases" , "simple-env" ).absolute ()} ' )
134+ env = {"externalArray" : [1 , 2 , 3 ], "externalString" : "hello" }
135+
136+ client = PolywrapClient (
137+ config = PolywrapClientConfig (
138+ envs = {uri : env },
139+ resolver = uri_resolver ,
140+ )
141+ )
142+ assert client .get_env_by_uri (uri ) == env
143+
78144async def test_env ():
79145 uri_resolver = BaseUriResolver (
80146 file_reader = SimpleFileReader (),
@@ -86,13 +152,14 @@ async def test_env():
86152
87153 client = PolywrapClient (
88154 config = PolywrapClientConfig (
89- envs = [ Env ( uri = uri , env = env )] ,
155+ envs = { uri : env } ,
90156 resolver = uri_resolver ,
91157 )
92158 )
93159 options = InvokerOptions (
94- uri = uri , method = "externalEnvMethod" , args = {}, encode_result = False
160+ uri = uri , method = "externalEnvMethod" , args = {}, encode_result = False ,
95161 )
162+
96163 result = await client .invoke (options )
97164
98- assert result .unwrap () == env
165+ assert result .unwrap () == env
0 commit comments