Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

connection.test.ts

128 lines3.8 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId } from "@t3tools/contracts";
3​
4import {
5 isRelayManagedConnection,
6 redactPairingCredential,
7 toStableSavedRemoteConnection,
8} from "./connection";
9import { authClientMetadata } from "./authClientMetadata";
10​
11const mobilePlatform = vi.hoisted(() => ({ OS: "ios" as "ios" | "android" }));
12const mobileDevice = vi.hoisted(() => ({
13 deviceType: 1,
14 DeviceType: {
15 UNKNOWN: 0,
16 PHONE: 1,
17 TABLET: 2,
18 DESKTOP: 3,
19 TV: 4,
20 },
21 osVersion: "18.4.1",
22 modelName: "iPhone 15 Pro",
23}));
24​
25vi.mock("./runtime", () => ({
26 runtime: {
27 runPromise: vi.fn(),
28 },
29}));
30​
31vi.mock("react-native", () => ({
32 Platform: mobilePlatform,
33}));
34​
35vi.mock("expo-device", () => mobileDevice);
36​
37describe("mobile remote connection records", () => {
38 afterEach(() => {
39 mobilePlatform.OS = "ios";
40 mobileDevice.deviceType = mobileDevice.DeviceType.PHONE;
41 mobileDevice.osVersion = "18.4.1";
42 mobileDevice.modelName = "iPhone 15 Pro";
43 });
44​
45 it("identifies mobile token exchanges for authorized-client presentation", () => {
46 expect(authClientMetadata()).toEqual({
47 label: "T3 Code Mobile",
48 deviceType: "mobile",
49 os: "iOS",
50 osMajorVersion: 18,
51 deviceModel: "iPhone 15 Pro",
52 surface: "mobile",
53 });
54 });
55​
56 it("includes only the Android major version and hardware model", () => {
57 mobilePlatform.OS = "android";
58 mobileDevice.osVersion = "15.2.1";
59 mobileDevice.modelName = "Pixel 9";
60​
61 expect(authClientMetadata()).toMatchObject({
62 os: "Android",
63 osMajorVersion: 15,
64 deviceModel: "Pixel 9",
65 });
66 });
67​
68 it("identifies native tablets separately from phones", () => {
69 mobileDevice.deviceType = mobileDevice.DeviceType.TABLET;
70 mobileDevice.modelName = "iPad Pro 13-inch";
71​
72 expect(authClientMetadata()).toMatchObject({
73 deviceType: "tablet",
74 os: "iOS",
75 deviceModel: "iPad Pro 13-inch",
76 });
77 });
78​
79 it("includes the mobile app version when the client provides it", () => {
80 expect(authClientMetadata("1.2.3")).toMatchObject({
81 surface: "mobile",
82 appVersion: "1.2.3",
83 });
84 });
85​
86 it("removes one-time bootstrap credentials before persisting pairing URLs", () => {
87 expect(redactPairingCredential("https://desktop.example/#token=bootstrap-token")).toBe(
88 "https://desktop.example/",
89 );
90 expect(redactPairingCredential("https://desktop.example/?token=bootstrap-token")).toBe(
91 "https://desktop.example/",
92 );
93 });
94​
95 it("removes hosted pairing credentials while keeping the advertised host", () => {
96 expect(
97 redactPairingCredential(
98 "https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.example&token=bootstrap-token&label=Desktop",
99 ),
100 ).toBe("https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.example&label=Desktop");
101 });
102​
103 it("recognizes explicitly managed relay connections", () => {
104 expect(isRelayManagedConnection({ relayManaged: true })).toBe(true);
105 });
106​
107 it("keeps existing DPoP tunnel records read-only after upgrading", () => {
108 expect(isRelayManagedConnection({ authenticationMethod: "dpop" })).toBe(true);
109 expect(isRelayManagedConnection({ authenticationMethod: "bearer" })).toBe(false);
110 });
111​
112 it("drops short-lived managed environment credentials from stable records", () => {
113 const connection = {
114 environmentId: EnvironmentId.make("environment-1"),
115 environmentLabel: "Desktop",
116 pairingUrl: "https://desktop.example/",
117 displayUrl: "https://desktop.example/",
118 httpBaseUrl: "https://desktop.example/",
119 wsBaseUrl: "wss://desktop.example/",
120 bearerToken: null,
121 authenticationMethod: "dpop",
122 dpopAccessToken: "short-lived-token",
123 relayManaged: true,
124 } as const;
125​
126 expect(toStableSavedRemoteConnection(connection)).not.toHaveProperty("dpopAccessToken");
127 });
128});