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

connection.test.ts

82 lines2.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId } from "@t3tools/contracts";
3​
4import {
5 isRelayManagedConnection,
6 authClientMetadata,
7 redactPairingCredential,
8 toStableSavedRemoteConnection,
9} from "./connection";
10​
11vi.mock("./runtime", () => ({
12 runtime: {
13 runPromise: vi.fn(),
14 },
15}));
16​
17vi.mock("react-native", () => ({
18 Platform: {
19 OS: "ios",
20 },
21}));
22​
23describe("mobile remote connection records", () => {
24 it("identifies mobile token exchanges for authorized-client presentation", () => {
25 expect(authClientMetadata()).toEqual({
26 label: "T3 Code Mobile",
27 deviceType: "mobile",
28 os: "iOS",
29 surface: "mobile",
30 });
31 });
32​
33 it("includes the mobile app version when the client provides it", () => {
34 expect(authClientMetadata("1.2.3")).toMatchObject({
35 surface: "mobile",
36 appVersion: "1.2.3",
37 });
38 });
39​
40 it("removes one-time bootstrap credentials before persisting pairing URLs", () => {
41 expect(redactPairingCredential("https://desktop.example/#token=bootstrap-token")).toBe(
42 "https://desktop.example/",
43 );
44 expect(redactPairingCredential("https://desktop.example/?token=bootstrap-token")).toBe(
45 "https://desktop.example/",
46 );
47 });
48​
49 it("removes hosted pairing credentials while keeping the advertised host", () => {
50 expect(
51 redactPairingCredential(
52 "https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.example&token=bootstrap-token&label=Desktop",
53 ),
54 ).toBe("https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.example&label=Desktop");
55 });
56​
57 it("recognizes explicitly managed relay connections", () => {
58 expect(isRelayManagedConnection({ relayManaged: true })).toBe(true);
59 });
60​
61 it("keeps existing DPoP tunnel records read-only after upgrading", () => {
62 expect(isRelayManagedConnection({ authenticationMethod: "dpop" })).toBe(true);
63 expect(isRelayManagedConnection({ authenticationMethod: "bearer" })).toBe(false);
64 });
65​
66 it("drops short-lived managed environment credentials from stable records", () => {
67 const connection = {
68 environmentId: EnvironmentId.make("environment-1"),
69 environmentLabel: "Desktop",
70 pairingUrl: "https://desktop.example/",
71 displayUrl: "https://desktop.example/",
72 httpBaseUrl: "https://desktop.example/",
73 wsBaseUrl: "wss://desktop.example/",
74 bearerToken: null,
75 authenticationMethod: "dpop",
76 dpopAccessToken: "short-lived-token",
77 relayManaged: true,
78 } as const;
79​
80 expect(toStableSavedRemoteConnection(connection)).not.toHaveProperty("dpopAccessToken");
81 });
82});