| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | classifyHostedHttpsCompatibility, |
| 5 | createAdvertisedEndpoint, |
| 6 | deriveWsBaseUrl, |
| 7 | environmentEndpointUrl, |
| 8 | environmentWebSocketUrl, |
| 9 | normalizeHttpBaseUrl, |
| 10 | } from "./endpoint.ts"; |
| 11 | |
| 12 | const coreProvider = { |
| 13 | id: "desktop-core", |
| 14 | label: "Desktop", |
| 15 | kind: "core", |
| 16 | isAddon: false, |
| 17 | } as const; |
| 18 | |
| 19 | describe("advertised endpoint helpers", () => { |
| 20 | it("normalizes HTTP and WebSocket base URLs", () => { |
| 21 | expect(normalizeHttpBaseUrl("https://example.com/path?x=1#hash")).toBe( |
| 22 | "https://example.com/path/", |
| 23 | ); |
| 24 | expect(normalizeHttpBaseUrl("wss://example.com/socket")).toBe("https://example.com/socket/"); |
| 25 | expect(deriveWsBaseUrl("https://example.com/api")).toBe("wss://example.com/api/"); |
| 26 | expect(deriveWsBaseUrl("http://127.0.0.1:3773")).toBe("ws://127.0.0.1:3773/"); |
| 27 | }); |
| 28 | |
| 29 | it.each(["https", "http", "wss", "ws"])("preserves the base path for %s routes", (scheme) => { |
| 30 | const base = `${scheme}://hub.example/prefix/environments/one///?old=1#fragment`; |
| 31 | expect(environmentEndpointUrl(base, "/api/assets/a%2Fb?signature=abc#preview")).toBe( |
| 32 | `${scheme}://hub.example/prefix/environments/one/api/assets/a%2Fb?signature=abc#preview`, |
| 33 | ); |
| 34 | expect(environmentEndpointUrl(base, ".well-known/t3/environment")).toBe( |
| 35 | `${scheme}://hub.example/prefix/environments/one/.well-known/t3/environment`, |
| 36 | ); |
| 37 | }); |
| 38 | |
| 39 | it("distinguishes a canonical WebSocket base from an existing socket endpoint", () => { |
| 40 | expect(environmentWebSocketUrl("wss://hub.example/prefix/environments/one/")).toBe( |
| 41 | "wss://hub.example/prefix/environments/one/ws", |
| 42 | ); |
| 43 | expect(environmentWebSocketUrl("wss://remote.example/ws?transport=1")).toBe( |
| 44 | "wss://remote.example/ws?transport=1", |
| 45 | ); |
| 46 | }); |
| 47 | |
| 48 | it("marks HTTP endpoints as blocked from hosted HTTPS apps", () => { |
| 49 | expect(classifyHostedHttpsCompatibility("http://192.168.1.44:3773")).toBe( |
| 50 | "mixed-content-blocked", |
| 51 | ); |
| 52 | expect(classifyHostedHttpsCompatibility("https://desktop.example.com", "compatible")).toBe( |
| 53 | "compatible", |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | it("creates provider-neutral endpoint records", () => { |
| 58 | expect( |
| 59 | createAdvertisedEndpoint({ |
| 60 | id: "lan:http://192.168.1.44:3773", |
| 61 | label: "LAN", |
| 62 | provider: coreProvider, |
| 63 | httpBaseUrl: "http://192.168.1.44:3773", |
| 64 | reachability: "lan", |
| 65 | source: "desktop-core", |
| 66 | isDefault: true, |
| 67 | }), |
| 68 | ).toEqual({ |
| 69 | id: "lan:http://192.168.1.44:3773", |
| 70 | label: "LAN", |
| 71 | provider: coreProvider, |
| 72 | httpBaseUrl: "http://192.168.1.44:3773/", |
| 73 | wsBaseUrl: "ws://192.168.1.44:3773/", |
| 74 | reachability: "lan", |
| 75 | compatibility: { |
| 76 | hostedHttpsApp: "mixed-content-blocked", |
| 77 | desktopApp: "compatible", |
| 78 | }, |
| 79 | source: "desktop-core", |
| 80 | status: "available", |
| 81 | isDefault: true, |
| 82 | }); |
| 83 | }); |
| 84 | }); |