Open History to see the last commit
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.t3.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("treats a protocol-relative host as https", () => {
63 expect(
64 resolveRemotePairingTarget({
65 host: "//remote.example.com",
66 pairingCode: "pairing-token",
67 }),
68 ).toEqual({
69 credential: "pairing-token",
70 httpBaseUrl: "https://remote.example.com/",
71 wsBaseUrl: "wss://remote.example.com/",
72 });
73 });
74
75 it("preserves the port when normalizing a protocol-relative host", () => {
76 expect(
77 resolveRemotePairingTarget({
78 host: "//remote.example.com:3000",
79 pairingCode: "pairing-token",
80 }),
81 ).toEqual({
82 credential: "pairing-token",
83 httpBaseUrl: "https://remote.example.com:3000/",
84 wsBaseUrl: "wss://remote.example.com:3000/",
85 });
86 });
87
88 it("normalizes a protocol-relative host from a hosted pairing link", () => {
89 expect(
90 resolveRemotePairingTarget({
91 pairingUrl: "https://app.t3.codes/pair?host=%2F%2Fremote.example.com#token=pairing-token",
92 }),
93 ).toEqual({
94 credential: "pairing-token",
95 httpBaseUrl: "https://remote.example.com/",
96 wsBaseUrl: "wss://remote.example.com/",
97 });
98 });
99
100 it("collapses extra leading slashes instead of producing an empty host", () => {
101 expect(
102 resolveRemotePairingTarget({
103 host: "///example.com",
104 pairingCode: "pairing-token",
105 }),
106 ).toEqual({
107 credential: "pairing-token",
108 httpBaseUrl: "https://example.com/",
109 wsBaseUrl: "wss://example.com/",
110 });
111 });
112
113 it("does not double-prepend https when the host already carries a scheme", () => {
114 expect(
115 resolveRemotePairingTarget({
116 host: "//https://example.com",
117 pairingCode: "pairing-token",
118 }),
119 ).toEqual({
120 credential: "pairing-token",
121 httpBaseUrl: "https://example.com/",
122 wsBaseUrl: "wss://example.com/",
123 });
124 });
125
126 it("preserves host ports when normalizing a bare host input", () => {
127 expect(
128 resolveRemotePairingTarget({
129 host: "myserver.com:3000",
130 pairingCode: "pairing-token",
131 }),
132 ).toEqual({
133 credential: "pairing-token",
134 httpBaseUrl: "https://myserver.com:3000/",
135 wsBaseUrl: "wss://myserver.com:3000/",
136 });
137 });
138
139 it("rejects unsupported direct pairing URL protocols", () => {
140 let pairingUrlError: unknown;
141 try {
142 resolveRemotePairingTarget({
143 pairingUrl: "ftp://remote.example.com/pair#token=pairing-token",
144 });
145 } catch (cause) {
146 pairingUrlError = cause;
147 }
148
149 expect(pairingUrlError).toBeInstanceOf(RemotePairingUrlInvalidError);
150 expect(pairingUrlError).toMatchObject({ protocol: "ftp:" });
151 expect((pairingUrlError as RemotePairingUrlInvalidError).cause).toBeUndefined();
152 });
153
154 it("rejects unsupported hosted pairing backend protocols", () => {
155 let hostError: unknown;
156 try {
157 resolveRemotePairingTarget({
158 pairingUrl:
159 "https://app.t3.codes/pair?host=ftp%3A%2F%2Fremote.example.com#token=pairing-token",
160 });
161 } catch (cause) {
162 hostError = cause;
163 }
164
165 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
166 expect(hostError).toMatchObject({ source: "hosted-pairing-host", protocol: "ftp:" });
167 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeUndefined();
168 });
169
170 it("rejects unsupported direct host protocols", () => {
171 let hostError: unknown;
172 try {
173 resolveRemotePairingTarget({
174 host: "ftp://remote.example.com",
175 pairingCode: "pairing-token",
176 });
177 } catch (cause) {
178 hostError = cause;
179 }
180
181 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
182 expect(hostError).toMatchObject({ source: "direct-host", protocol: "ftp:" });
183 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeUndefined();
184 });
185
186 it("uses distinct structural errors for missing pairing inputs", () => {
187 expect(() => resolveRemotePairingTarget({})).toThrowError(RemoteBackendUrlMissingError);
188 expect(() =>
189 resolveRemotePairingTarget({ pairingUrl: "https://remote.example.com/pair" }),
190 ).toThrowError(RemotePairingTokenMissingError);
191 expect(() =>
192 resolveRemotePairingTarget({
193 host: "https://user:secret@remote.example.com/path?token=sensitive#fragment",
194 }),
195 ).toThrowError(
196 expect.objectContaining({
197 _tag: "RemotePairingCodeMissingError",
198 host: "remote.example.com",
199 }),
200 );
201 });
202
203 it("preserves URL parsing causes with their input source", () => {
204 let pairingUrlError: unknown;
205 try {
206 resolveRemotePairingTarget({ pairingUrl: "not a url" });
207 } catch (cause) {
208 pairingUrlError = cause;
209 }
210 expect(pairingUrlError).toBeInstanceOf(RemotePairingUrlInvalidError);
211 expect((pairingUrlError as RemotePairingUrlInvalidError).cause).toBeInstanceOf(TypeError);
212
213 let hostError: unknown;
214 try {
215 resolveRemotePairingTarget({ host: "https://[invalid", pairingCode: "pairing-token" });
216 } catch (cause) {
217 hostError = cause;
218 }
219 expect(hostError).toBeInstanceOf(RemoteBackendUrlInvalidError);
220 expect(hostError).toMatchObject({ source: "direct-host" });
221 expect((hostError as RemoteBackendUrlInvalidError).cause).toBeInstanceOf(TypeError);
222 });
223});