Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vitest";
2
3import {
4 buildHostedPairingUrl,
5 hasHostedPairingRequest,
6 isHostedStaticApp,
7 readHostedPairingRequest,
8} from "./hostedPairing";
9
10describe("hostedPairing", () => {
11 afterEach(() => {
12 vi.unstubAllEnvs();
13 });
14
15 it("reads hosted pairing host and query token parameters", () => {
16 const url = new URL("https://app.t3.codes/pair?host=100.64.1.2:3773&token=ABCD1234");
17
18 expect(readHostedPairingRequest(url)).toEqual({
19 host: "100.64.1.2:3773",
20 token: "ABCD1234",
21 label: "",
22 });
23 expect(hasHostedPairingRequest(url)).toBe(true);
24 });
25
26 it("prefers hash tokens so generated hosted links do not put credentials in search params", () => {
27 vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");
28
29 const url = new URL(
30 buildHostedPairingUrl({
31 host: "https://backend.example.com:3773",
32 token: "pairing-token",
33 label: "Workstation",
34 }),
35 );
36
37 expect(url.origin).toBe("https://preview.t3.codes");
38 expect(url.pathname).toBe("/pair");
39 expect(url.searchParams.get("host")).toBe("https://backend.example.com:3773");
40 expect(url.searchParams.get("label")).toBe("Workstation");
41 expect(url.searchParams.has("token")).toBe(false);
42 expect(url.hash).toBe("#token=pairing-token");
43 });
44
45 it("ignores incomplete hosted pairing requests", () => {
46 expect(
47 hasHostedPairingRequest(new URL("https://app.t3.codes/pair?host=backend.example.com")),
48 ).toBe(false);
49 expect(hasHostedPairingRequest(new URL("https://app.t3.codes/pair?token=ABCD1234"))).toBe(
50 false,
51 );
52 });
53
54 it("detects the hosted static app only when no backend URL is configured", () => {
55 vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");
56 vi.stubEnv("VITE_HTTP_URL", "");
57 vi.stubEnv("VITE_WS_URL", "");
58
59 expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(true);
60 expect(isHostedStaticApp(new URL("https://preview.t3.codes/pair"))).toBe(true);
61 expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);
62
63 vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");
64 expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(false);
65 });
66});