| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import type { RelayLinkProofRequest } from "@t3tools/contracts/relay"; |
| 3 | |
| 4 | import { |
| 5 | isSupportedLinkProviderKind, |
| 6 | linkProofScopes, |
| 7 | parseManagedEndpointLocalOrigin, |
| 8 | } from "./linkChecks.ts"; |
| 9 | |
| 10 | describe("parseManagedEndpointLocalOrigin", () => { |
| 11 | it.each([ |
| 12 | { |
| 13 | input: "http://127.0.0.1:80", |
| 14 | httpBaseUrl: "http://127.0.0.1", |
| 15 | wsBaseUrl: "ws://127.0.0.1", |
| 16 | port: 80, |
| 17 | }, |
| 18 | { |
| 19 | input: "https://127.0.0.1:443", |
| 20 | httpBaseUrl: "https://127.0.0.1", |
| 21 | wsBaseUrl: "wss://127.0.0.1", |
| 22 | port: 443, |
| 23 | }, |
| 24 | ])("accepts an explicit default port in $input", ({ input, httpBaseUrl, wsBaseUrl, port }) => { |
| 25 | expect(parseManagedEndpointLocalOrigin(input)).toEqual({ |
| 26 | httpBaseUrl, |
| 27 | wsBaseUrl, |
| 28 | origin: { localHttpHost: "127.0.0.1", localHttpPort: port }, |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | it.each([ |
| 33 | "ftp://127.0.0.1:3773", |
| 34 | "http://user:password@127.0.0.1:3773", |
| 35 | "http://127.0.0.1:3773/api", |
| 36 | "http://127.0.0.1:3773?mode=test", |
| 37 | "http://127.0.0.1:3773#fragment", |
| 38 | ])("rejects non-origin URL %s", (input) => { |
| 39 | expect(() => parseManagedEndpointLocalOrigin(input)).toThrow("Invalid local origin"); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | describe("link proof provider kinds", () => { |
| 44 | const proofRequest = ( |
| 45 | providerKind: RelayLinkProofRequest["endpoint"]["providerKind"], |
| 46 | ): RelayLinkProofRequest => ({ |
| 47 | challenge: "challenge", |
| 48 | relayIssuer: "https://relay.example.test", |
| 49 | endpoint: { |
| 50 | httpBaseUrl: "http://127.0.0.1:7331", |
| 51 | wsBaseUrl: "ws://127.0.0.1:7331", |
| 52 | providerKind, |
| 53 | }, |
| 54 | origin: { localHttpHost: "127.0.0.1", localHttpPort: 7331 }, |
| 55 | }); |
| 56 | |
| 57 | it("accepts managed and manual endpoints but not t3_relay", () => { |
| 58 | expect(isSupportedLinkProviderKind(proofRequest("cloudflare_tunnel"))).toBe(true); |
| 59 | expect(isSupportedLinkProviderKind(proofRequest("manual"))).toBe(true); |
| 60 | expect(isSupportedLinkProviderKind(proofRequest("t3_relay"))).toBe(false); |
| 61 | }); |
| 62 | |
| 63 | it("only claims the managed-tunnel scope for tunnel links", () => { |
| 64 | expect(linkProofScopes(proofRequest("cloudflare_tunnel"))).toEqual([ |
| 65 | "agent_activity_notifications", |
| 66 | "managed_tunnels", |
| 67 | ]); |
| 68 | expect(linkProofScopes(proofRequest("manual"))).toEqual(["agent_activity_notifications"]); |
| 69 | }); |
| 70 | }); |