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

connection.test.ts

107 lines3.1 KB

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