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