|
1 | 1 | import { |
| 2 | + IWrapPackage, |
2 | 3 | Uri, |
3 | 4 | UriResolutionContext, |
| 5 | + Wrapper, |
4 | 6 | } from "@polywrap/core-js"; |
5 | 7 | import { expectHistory } from "../helpers/expectHistory"; |
6 | 8 | import { PolywrapCoreClient } from "@polywrap/core-client-js"; |
@@ -40,4 +42,95 @@ describe("StaticResolver", () => { |
40 | 42 |
|
41 | 43 | expect(result.value.uri.uri).toEqual("wrap://test/to"); |
42 | 44 | }); |
| 45 | + |
| 46 | + it("can resolve a package", async () => { |
| 47 | + const uri = new Uri("test/package"); |
| 48 | + |
| 49 | + const client = new PolywrapCoreClient({ |
| 50 | + resolver: StaticResolver.from([ |
| 51 | + { |
| 52 | + uri: Uri.from("test/package"), |
| 53 | + package: {} as IWrapPackage, |
| 54 | + }, |
| 55 | + ]), |
| 56 | + }); |
| 57 | + |
| 58 | + let resolutionContext = new UriResolutionContext(); |
| 59 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 60 | + |
| 61 | + await expectHistory( |
| 62 | + resolutionContext.getHistory(), |
| 63 | + "static-resolver", |
| 64 | + "can-resolve-a-package" |
| 65 | + ); |
| 66 | + |
| 67 | + if (!result.ok) { |
| 68 | + fail(result.error); |
| 69 | + } |
| 70 | + |
| 71 | + if (result.value.type !== "package") { |
| 72 | + fail("Expected a package, received: " + result.value.type); |
| 73 | + } |
| 74 | + |
| 75 | + expect(result.value.uri.uri).toEqual("wrap://test/package"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("can resolve a wrapper", async () => { |
| 79 | + const uri = new Uri("test/wrapper"); |
| 80 | + |
| 81 | + const client = new PolywrapCoreClient({ |
| 82 | + resolver: StaticResolver.from([ |
| 83 | + { |
| 84 | + uri: Uri.from("test/wrapper"), |
| 85 | + wrapper: {} as Wrapper, |
| 86 | + }, |
| 87 | + ]), |
| 88 | + }); |
| 89 | + |
| 90 | + let resolutionContext = new UriResolutionContext(); |
| 91 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 92 | + |
| 93 | + await expectHistory( |
| 94 | + resolutionContext.getHistory(), |
| 95 | + "static-resolver", |
| 96 | + "can-resolve-a-wrapper" |
| 97 | + ); |
| 98 | + |
| 99 | + if (!result.ok) { |
| 100 | + fail(result.error); |
| 101 | + } |
| 102 | + |
| 103 | + if (result.value.type !== "wrapper") { |
| 104 | + fail("Expected a wrapper, received: " + result.value.type); |
| 105 | + } |
| 106 | + |
| 107 | + expect(result.value.uri.uri).toEqual("wrap://test/wrapper"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("can not resolve unregistered uri", async () => { |
| 111 | + const uri = new Uri("test/not-a-match"); |
| 112 | + |
| 113 | + const client = new PolywrapCoreClient({ |
| 114 | + resolver: StaticResolver.from([]), |
| 115 | + }); |
| 116 | + |
| 117 | + let resolutionContext = new UriResolutionContext(); |
| 118 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 119 | + |
| 120 | + await expectHistory( |
| 121 | + resolutionContext.getHistory(), |
| 122 | + "static-resolver", |
| 123 | + "not-a-match" |
| 124 | + ); |
| 125 | + |
| 126 | + if (!result.ok) { |
| 127 | + fail(result.error); |
| 128 | + } |
| 129 | + |
| 130 | + if (result.value.type !== "uri") { |
| 131 | + fail("Expected a uri, received: " + result.value.type); |
| 132 | + } |
| 133 | + |
| 134 | + expect(result.value.uri.uri).toEqual("wrap://test/not-a-match"); |
| 135 | + }); |
43 | 136 | }); |
0 commit comments