66 lines · 2.2 KB · TypeScript
RawDownload
import { afterEach, describe, expect, it, vi } from "vitest"; import {  buildHostedPairingUrl,  hasHostedPairingRequest,  isHostedStaticApp,  readHostedPairingRequest,} from "./hostedPairing"; describe("hostedPairing", () => {  afterEach(() => {    vi.unstubAllEnvs();  });   it("reads hosted pairing host and query token parameters", () => {    const url = new URL("https://app.t3.codes/pair?host=100.64.1.2:3773&token=ABCD1234");     expect(readHostedPairingRequest(url)).toEqual({      host: "100.64.1.2:3773",      token: "ABCD1234",      label: "",    });    expect(hasHostedPairingRequest(url)).toBe(true);  });   it("prefers hash tokens so generated hosted links do not put credentials in search params", () => {    vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");     const url = new URL(      buildHostedPairingUrl({        host: "https://backend.example.com:3773",        token: "pairing-token",        label: "Workstation",      }),    );     expect(url.origin).toBe("https://preview.t3.codes");    expect(url.pathname).toBe("/pair");    expect(url.searchParams.get("host")).toBe("https://backend.example.com:3773");    expect(url.searchParams.get("label")).toBe("Workstation");    expect(url.searchParams.has("token")).toBe(false);    expect(url.hash).toBe("#token=pairing-token");  });   it("ignores incomplete hosted pairing requests", () => {    expect(      hasHostedPairingRequest(new URL("https://app.t3.codes/pair?host=backend.example.com")),    ).toBe(false);    expect(hasHostedPairingRequest(new URL("https://app.t3.codes/pair?token=ABCD1234"))).toBe(      false,    );  });   it("detects the hosted static app only when no backend URL is configured", () => {    vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");    vi.stubEnv("VITE_HTTP_URL", "");    vi.stubEnv("VITE_WS_URL", "");     expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(true);    expect(isHostedStaticApp(new URL("https://preview.t3.codes/pair"))).toBe(true);    expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);     vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");    expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(false);  });});