1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 buildHostedPairingUrl,
5 hasHostedPairingRequest,
6 readHostedPairingRequest,
7} from "./hostedPairing";
8
9describe("hostedPairing", () => {
10 afterEach(() => {
11 vi.unstubAllEnvs();
12 });
13
14 it("reads hosted pairing host and query token parameters", () => {
15 const url = new URL("https://app.t3.codes/pair?host=100.64.1.2:3773&token=ABCD1234");
16
17 expect(readHostedPairingRequest(url)).toEqual({
18 host: "100.64.1.2:3773",
19 token: "ABCD1234",
20 label: "",
21 });
22 expect(hasHostedPairingRequest(url)).toBe(true);
23 });
24
25 it("prefers hash tokens so generated hosted links do not put credentials in search params", () => {
26 vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");
27
28 const url = new URL(
29 buildHostedPairingUrl({
30 host: "https://backend.example.com:3773",
31 token: "pairing-token",
32 label: "Workstation",
33 }),
34 );
35
36 expect(url.origin).toBe("https://preview.t3.codes");
37 expect(url.pathname).toBe("/pair");
38 expect(url.searchParams.get("host")).toBe("https://backend.example.com:3773");
39 expect(url.searchParams.get("label")).toBe("Workstation");
40 expect(url.searchParams.has("token")).toBe(false);
41 expect(url.hash).toBe("#token=pairing-token");
42 });
43});