|
| 1 | +import { |
| 2 | + IWrapPackage, |
| 3 | + Uri, |
| 4 | + UriResolutionContext, |
| 5 | + Wrapper, |
| 6 | +} from "@polywrap/core-js"; |
| 7 | +import { expectHistory } from "../helpers/expectHistory"; |
| 8 | +import { PolywrapCoreClient } from "@polywrap/core-client-js"; |
| 9 | +import { StaticResolver } from "../../static"; |
| 10 | + |
| 11 | +jest.setTimeout(20000); |
| 12 | + |
| 13 | +describe("StaticResolver", () => { |
| 14 | + it("can redirect a uri", async () => { |
| 15 | + const uri = new Uri("test/from"); |
| 16 | + |
| 17 | + const client = new PolywrapCoreClient({ |
| 18 | + resolver: StaticResolver.from([ |
| 19 | + { |
| 20 | + from: Uri.from("test/from"), |
| 21 | + to: Uri.from("test/to"), |
| 22 | + }, |
| 23 | + ]), |
| 24 | + }); |
| 25 | + |
| 26 | + let resolutionContext = new UriResolutionContext(); |
| 27 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 28 | + |
| 29 | + await expectHistory( |
| 30 | + resolutionContext.getHistory(), |
| 31 | + "static-resolver", |
| 32 | + "can-redirect-a-uri" |
| 33 | + ); |
| 34 | + |
| 35 | + if (!result.ok) { |
| 36 | + fail(result.error); |
| 37 | + } |
| 38 | + |
| 39 | + if (result.value.type !== "uri") { |
| 40 | + fail("Expected a uri, received: " + result.value.type); |
| 41 | + } |
| 42 | + |
| 43 | + expect(result.value.uri.uri).toEqual("wrap://test/to"); |
| 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 | + }); |
| 136 | +}); |
0 commit comments