|
| 1 | +import { |
| 2 | + Uri, |
| 3 | + UriResolutionContext, |
| 4 | +} from "@polywrap/core-js"; |
| 5 | +import { expectHistory } from "../helpers/expectHistory"; |
| 6 | +import { PolywrapCoreClient } from "@polywrap/core-client-js"; |
| 7 | +import { UriResolverAggregator } from "../../aggregator"; |
| 8 | + |
| 9 | +jest.setTimeout(20000); |
| 10 | + |
| 11 | +describe("UriResolverAggregator", () => { |
| 12 | + const client = new PolywrapCoreClient({ |
| 13 | + resolver: new UriResolverAggregator([ |
| 14 | + { |
| 15 | + from: Uri.from("test/1"), |
| 16 | + to: Uri.from("test/2") |
| 17 | + }, |
| 18 | + { |
| 19 | + from: Uri.from("test/2"), |
| 20 | + to: Uri.from("test/3") |
| 21 | + }, |
| 22 | + { |
| 23 | + from: Uri.from("test/3"), |
| 24 | + to: Uri.from("test/4") |
| 25 | + } |
| 26 | + ], "TestAggregator") |
| 27 | + }); |
| 28 | + |
| 29 | + it("can resolve using first resolver", async () => { |
| 30 | + const uri = new Uri("test/1"); |
| 31 | + |
| 32 | + let resolutionContext = new UriResolutionContext(); |
| 33 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 34 | + |
| 35 | + await expectHistory( |
| 36 | + resolutionContext.getHistory(), |
| 37 | + "aggregator-resolver", |
| 38 | + "can-resolve-using-first-resolver", |
| 39 | + ); |
| 40 | + |
| 41 | + if (!result.ok) { |
| 42 | + fail(result.error); |
| 43 | + } |
| 44 | + |
| 45 | + if (result.value.type !== "uri") { |
| 46 | + fail("Expected a uri, received: " + result.value.type); |
| 47 | + } |
| 48 | + |
| 49 | + expect(result.value.uri.uri).toEqual("wrap://test/2"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("can resolve using last resolver", async () => { |
| 53 | + const uri = new Uri("test/3"); |
| 54 | + |
| 55 | + let resolutionContext = new UriResolutionContext(); |
| 56 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 57 | + |
| 58 | + await expectHistory( |
| 59 | + resolutionContext.getHistory(), |
| 60 | + "aggregator-resolver", |
| 61 | + "can-resolve-using-last-resolver", |
| 62 | + ); |
| 63 | + |
| 64 | + if (!result.ok) { |
| 65 | + fail(result.error); |
| 66 | + } |
| 67 | + |
| 68 | + if (result.value.type !== "uri") { |
| 69 | + fail("Expected a uri, received: " + result.value.type); |
| 70 | + } |
| 71 | + |
| 72 | + expect(result.value.uri.uri).toEqual("wrap://test/4"); |
| 73 | + }); |
| 74 | + |
| 75 | + |
| 76 | + it("does not resolve a uri when not a match", async () => { |
| 77 | + const uri = new Uri("test/not-a-match"); |
| 78 | + |
| 79 | + let resolutionContext = new UriResolutionContext(); |
| 80 | + let result = await client.tryResolveUri({ uri, resolutionContext }); |
| 81 | + |
| 82 | + await expectHistory( |
| 83 | + resolutionContext.getHistory(), |
| 84 | + "aggregator-resolver", |
| 85 | + "not-a-match", |
| 86 | + ); |
| 87 | + |
| 88 | + if (!result.ok) { |
| 89 | + fail(result.error); |
| 90 | + } |
| 91 | + |
| 92 | + if (result.value.type !== "uri") { |
| 93 | + fail("Expected a uri, received: " + result.value.type); |
| 94 | + } |
| 95 | + |
| 96 | + expect(result.value.uri.uri).toEqual("wrap://test/not-a-match"); |
| 97 | + }); |
| 98 | +}); |
0 commit comments