Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

remote.test.ts

159 lines5.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 RemoteBackendUrlInvalidError,
5 RemoteBackendUrlMissingError,
6 RemotePairingTokenMissingError,
7 RemotePairingUrlInvalidError,
8 resolveRemotePairingTarget,
9} from "./remote.ts";
10​
11describe("remote", () => {
12 it("derives backend urls and token from a pairing url", () => {
13 expect(
14 resolveRemotePairingTarget({
15 pairingUrl: "https://remote.example.com/pair#token=pairing-token",
16 }),
17 ).toEqual({
18 credential: "pairing-token",
19 httpBaseUrl: "https://remote.example.com/",
20 wsBaseUrl: "wss://remote.example.com/",
21 });
22 });
23​
24 it("accepts pairing urls that still use a query token", () => {
25 expect(
26 resolveRemotePairingTarget({
27 pairingUrl: "https://remote.example.com/pair?token=pairing-token",
28 }),
29 ).toEqual({
30 credential: "pairing-token",
31 httpBaseUrl: "https://remote.example.com/",
32 wsBaseUrl: "wss://remote.example.com/",
33 });
34 });
35​
36 it("derives backend urls from hosted app pairing links", () => {
37 expect(
38 resolveRemotePairingTarget({
39 pairingUrl:
40 "https://app.v12.codes/pair?host=https%3A%2F%2Fdesktop.tailnet.ts.net%3A44342%2F#token=pairing-token",
41 }),
42 ).toEqual({
43 credential: "pairing-token",
44 httpBaseUrl: "https://desktop.tailnet.ts.net:44342/",
45 wsBaseUrl: "wss://desktop.tailnet.ts.net:44342/",
46 });
47 });
48​
49 it("derives backend urls from a host and pairing code", () => {
50 expect(
51 resolveRemotePairingTarget({
52 host: "https://remote.example.com",
53 pairingCode: "pairing-token",
54 }),
55 ).toEqual({
56 credential: "pairing-token",
57 httpBaseUrl: "https://remote.example.com/",
58 wsBaseUrl: "wss://remote.example.com/",
59 });
60 });
61​
62 it("preserves host ports when normalizing a bare host input", () => {
63 expect(
64 resolveRemotePairingTarget({
65 host: "myserver.com:3000",
66 pairingCode: "pairing-token",
67 }),
68 ).toEqual({
69 credential: "pairing-token",
70 httpBaseUrl: "https://myserver.com:3000/",
71 wsBaseUrl: "wss://myserver.com:3000/",
72 });
73 });
74​
75 it("rejects unsupported direct pairing URL protocols", () => {
76 let pairingUrlError: unknown;
77 try {
78 resolveRemotePairingTarget({
79 pairingUrl: "ftp://remote.example.com/pair#token=pairing-token",
80 });
81 } catch (cause) {
82 pairingUrlError = cause;
83 }
84​
85 expect(pairingUrlError).toBeInstanceOf(RemotePairingUrlInvalidError);
86 expect(pairingUrlError).toMatchObject({ protocol: "ftp:" });
87 expect((pairingUrlError as RemotePairingUrlInvalidError).cause).toBeUndefined();
88 });
89​
90 it("rejects unsupported hosted pairing backend protocols", () => {
91 let hostError: unknown;
92 try {
93 resolveRemotePairingTarget({
94 pairingUrl:
95 "https://app.v12.codes/pair?host=ftp%3A%2F%2Fremote.example.com#token=pairing-token",
96 });
97 } catch (cause) {
98 hostError = cause;
99 }
100​
101 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
102 expect(hostError).toMatchObject({ source: "hosted-pairing-host", protocol: "ftp:" });
103 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeUndefined();
104 });
105​
106 it("rejects unsupported direct host protocols", () => {
107 let hostError: unknown;
108 try {
109 resolveRemotePairingTarget({
110 host: "ftp://remote.example.com",
111 pairingCode: "pairing-token",
112 });
113 } catch (cause) {
114 hostError = cause;
115 }
116​
117 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
118 expect(hostError).toMatchObject({ source: "direct-host", protocol: "ftp:" });
119 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeUndefined();
120 });
121​
122 it("uses distinct structural errors for missing pairing inputs", () => {
123 expect(() => resolveRemotePairingTarget({})).toThrowError(RemoteBackendUrlMissingError);
124 expect(() =>
125 resolveRemotePairingTarget({ pairingUrl: "https://remote.example.com/pair" }),
126 ).toThrowError(RemotePairingTokenMissingError);
127 expect(() =>
128 resolveRemotePairingTarget({
129 host: "https://user:secret@remote.example.com/path?token=sensitive#fragment",
130 }),
131 ).toThrowError(
132 expect.objectContaining({
133 _tag: "RemotePairingCodeMissingError",
134 host: "remote.example.com",
135 }),
136 );
137 });
138​
139 it("preserves URL parsing causes with their input source", () => {
140 let pairingUrlError: unknown;
141 try {
142 resolveRemotePairingTarget({ pairingUrl: "not a url" });
143 } catch (cause) {
144 pairingUrlError = cause;
145 }
146 expect(pairingUrlError).toBeInstanceOf(RemotePairingUrlInvalidError);
147 expect((pairingUrlError as RemotePairingUrlInvalidError).cause).toBeInstanceOf(TypeError);
148​
149 let hostError: unknown;
150 try {
151 resolveRemotePairingTarget({ host: "https://[invalid", pairingCode: "pairing-token" });
152 } catch (cause) {
153 hostError = cause;
154 }
155 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
156 expect(hostError).toMatchObject({ source: "direct-host" });
157 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeInstanceOf(TypeError);
158 });
159});