Skip to content

Commit 3ae3816

Browse files
committed
chore: static resolver works as expected
1 parent 52a84d4 commit 3ae3816

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

packages/polywrap-client/tests/test_client.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
import pytest
12
from pathlib import Path
23

34
from polywrap_client import PolywrapClient
4-
from polywrap_core import Uri, InvokerOptions, InterfaceImplementations, Env
5-
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader
6-
5+
from polywrap_core import Uri, InvokerOptions, InterfaceImplementations, Env, UriWrapper
6+
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader, StaticResolver
7+
from polywrap_result import Result, Ok, Err
78
from polywrap_client.client import PolywrapClientConfig
8-
9-
10-
async def test_invoke():
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,27 @@ async def test_invoke():
2051

2152
assert result.unwrap() == args["arg"]
2253

54+
wrapper = WasmWrapper(
55+
file_reader=simple_file_reader,
56+
wasm_module=simple_wrap_module,
57+
manifest=simple_wrap_manifest
58+
)
59+
uri_wrapper = UriWrapper(uri=Uri("ens/wrapper.eth"), wrapper=wrapper)
60+
resolver = StaticResolver._from([uri_wrapper])
61+
62+
config = PolywrapClientConfig(resolver=resolver)
63+
client = PolywrapClient(config=config)
64+
65+
args = {"arg": "hello polywrap"}
66+
options = InvokerOptions(
67+
uri=Uri("ens/wrapper.eth"),
68+
method="simpleMethod",
69+
args=args,
70+
encode_result=False
71+
)
72+
result = await client.invoke(options)
73+
74+
assert result.unwrap() == args["arg"]
2375

2476
async def test_subinvoke():
2577
uri_resolver = BaseUriResolver(

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/static_resolver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ async def try_resolve_uri(self, uri: Uri, client: Client, resolution_context: IU
3939

4040
if package_or_wrapper:
4141
if hasattr(package_or_wrapper, "package"):
42-
result = UriResolutionResult.ok(uri, cast(IWrapPackage, package_or_wrapper))
42+
result = UriResolutionResult.ok(uri, cast(IWrapPackage, package_or_wrapper.package))
4343
description = f"Static - Package ({uri.uri})"
4444
elif hasattr(package_or_wrapper, "wrapper"):
45-
result = UriResolutionResult.ok(uri, None, cast(Wrapper, package_or_wrapper))
45+
result = UriResolutionResult.ok(uri, None, cast(Wrapper, package_or_wrapper.wrapper))
4646
description = f"Static - Wrapper ({uri.uri})"
4747

4848
step = IUriResolutionStep(source_uri=uri, result=result, description=description)
4949
resolution_context.track_step(step)
50-
5150
return result

packages/polywrap-uri-resolvers/tests/test_static_resolver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from polywrap_result import Ok, Err, Result
55
from polywrap_wasm import WRAP_MANIFEST_PATH, WRAP_MODULE_PATH, IFileReader, WasmPackage, WasmWrapper
6-
from polywrap_core import UriPackage, Uri, UriResolutionContext, UriWrapper
6+
from polywrap_core import UriPackage, Uri, UriResolutionContext, UriWrapper, IWrapPackage
77
from polywrap_client import PolywrapClient
88
from polywrap_uri_resolvers import StaticResolver
99
from polywrap_manifest import deserialize_wrap_manifest
@@ -56,15 +56,15 @@ async def test_static_resolver(
5656
result = await resolver.try_resolve_uri(Uri("ens/package.eth"), PolywrapClient(), resolution_context)
5757

5858
assert result.is_ok
59-
assert isinstance((result.unwrap()).package, UriPackage)
59+
assert isinstance((result.unwrap()).package, IWrapPackage)
6060

6161
result = await resolver.try_resolve_uri(Uri("ens/wrapper.eth"), PolywrapClient(), resolution_context)
6262

6363
assert result.is_ok
64-
assert isinstance((result.unwrap()).wrapper, UriWrapper)
64+
assert isinstance((result.unwrap()).wrapper, WasmWrapper)
6565

6666
result = await resolver.try_resolve_uri(Uri("ens/nested-package.eth"), PolywrapClient(), resolution_context)
6767

6868
assert result.is_ok
69-
assert isinstance((result.unwrap()).package, UriPackage)
69+
assert isinstance((result.unwrap()).package, IWrapPackage)
7070

0 commit comments

Comments
 (0)