-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathhostedPairing.test.ts
More file actions
94 lines (77 loc) · 3.22 KB
/
hostedPairing.test.ts
File metadata and controls
94 lines (77 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { afterEach, describe, expect, it, vi } from "vitest";
import {
buildHostedChannelSelectionUrl,
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("builds hosted channel selection URLs through the configured router origin", () => {
vi.stubEnv("VITE_HOSTED_APP_URL", "https://app.t3.codes");
const url = new URL(
buildHostedChannelSelectionUrl({
channel: "nightly",
}),
);
expect(url.origin).toBe("https://app.t3.codes");
expect(url.pathname).toBe("/__t3code/channel");
expect(url.searchParams.get("channel")).toBe("nightly");
expect(url.searchParams.has("next")).toBe(false);
});
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);
});
it("detects hosted channel aliases as static apps", () => {
vi.stubEnv("VITE_HOSTED_APP_URL", "https://app.t3.codes");
vi.stubEnv("VITE_HOSTED_APP_CHANNEL", "nightly");
vi.stubEnv("VITE_HTTP_URL", "");
vi.stubEnv("VITE_WS_URL", "");
expect(isHostedStaticApp(new URL("https://nightly.app.t3.codes/"))).toBe(true);
vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");
expect(isHostedStaticApp(new URL("https://nightly.app.t3.codes/"))).toBe(false);
});
});