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