1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 buildPairingUrl,
5 extractPairingUrlFromQrPayload,
6 PairingQrPayloadEmptyError,
7 parsePairingUrl,
8} from "./pairing";
9
10describe("buildPairingUrl", () => {
11 it("uses HTTP for a schemeless IP address", () => {
12 expect(buildPairingUrl("192.168.1.100:3773", "pairing-token")).toBe(
13 "http://192.168.1.100:3773/#token=pairing-token",
14 );
15 });
16
17 it("keeps HTTPS as the default for a schemeless hostname", () => {
18 expect(buildPairingUrl("remote.example.com", "pairing-token")).toBe(
19 "https://remote.example.com/#token=pairing-token",
20 );
21 });
22
23 it("preserves an explicit scheme for an IP address", () => {
24 expect(buildPairingUrl("https://192.168.1.100:3773", "pairing-token")).toBe(
25 "https://192.168.1.100:3773/#token=pairing-token",
26 );
27 });
28});
29
30describe("extractPairingUrlFromQrPayload", () => {
31 it("trims raw pairing urls from qr payloads", () => {
32 expect(
33 extractPairingUrlFromQrPayload(" https://remote.example.com/pair#token=pairing-token "),
34 ).toBe("https://remote.example.com/pair#token=pairing-token");
35 });
36
37 it("unwraps mobile deep links that carry an encoded pairing url", () => {
38 expect(
39 extractPairingUrlFromQrPayload(
40 "t3code://pair?pairingUrl=https%3A%2F%2Fremote.example.com%2Fpair%23token%3Dpairing-token",
41 ),
42 ).toBe("https://remote.example.com/pair#token=pairing-token");
43 });
44
45 it("rejects empty qr payloads", () => {
46 expect(() => extractPairingUrlFromQrPayload(" ")).toThrowError(PairingQrPayloadEmptyError);
47 expect(() => extractPairingUrlFromQrPayload(" ")).toThrowError(
48 "Scanned QR code did not contain a pairing URL.",
49 );
50 });
51});
52
53describe("parsePairingUrl", () => {
54 it.each(["", "pair", "pair/"])("preserves a proxied environment address before %s", (route) => {
55 expect(
56 parsePairingUrl(`https://hub.example/prefix/environments/one/${route}?old=1#token=code`),
57 ).toEqual({
58 host: "https://hub.example/prefix/environments/one",
59 code: "code",
60 });
61 });
62
63 it("reads hosted pairing links into backend host fields", () => {
64 expect(
65 parsePairingUrl(
66 "https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.tailnet.ts.net%2F#token=pairing-token",
67 ),
68 ).toEqual({
69 host: "https://desktop.tailnet.ts.net",
70 code: "pairing-token",
71 });
72 });
73});