Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4const mocks = vi.hoisted(() => {
5 const values = new Map<string, string>();
6 return {
7 clear: () => values.clear(),
8 getItemAsync: vi.fn((key: string) => Promise.resolve(values.get(key) ?? null)),
9 setItemAsync: vi.fn((key: string, value: string) => {
10 values.set(key, value);
11 return Promise.resolve();
12 }),
13 };
14});
15
16vi.mock("expo-secure-store", () => ({
17 getItemAsync: mocks.getItemAsync,
18 setItemAsync: mocks.setItemAsync,
19}));
20
21vi.mock("react-native", () => ({
22 Platform: {
23 OS: "ios",
24 },
25}));
26
27vi.mock("./runtime", () => ({
28 runtime: {
29 runPromise: vi.fn(),
30 },
31}));
32
33import { loadSavedConnections, saveConnection } from "./storage";
34import { toStableSavedRemoteConnection } from "./connection";
35
36const managedConnection = {
37 environmentId: EnvironmentId.make("environment-1"),
38 environmentLabel: "Desktop",
39 pairingUrl: "https://desktop.example/",
40 displayUrl: "https://desktop.example/",
41 httpBaseUrl: "https://desktop.example/",
42 wsBaseUrl: "wss://desktop.example/",
43 bearerToken: null,
44 authenticationMethod: "dpop",
45 dpopAccessToken: "short-lived-token",
46 relayManaged: true,
47} as const;
48
49describe("mobile connection storage", () => {
50 beforeEach(() => {
51 mocks.clear();
52 vi.clearAllMocks();
53 });
54
55 it("persists relay-managed connections without their ephemeral access token", async () => {
56 await saveConnection(managedConnection);
57
58 const savedValue = mocks.setItemAsync.mock.calls[0]?.[1];
59 expect(savedValue).toBeDefined();
60 expect(JSON.parse(savedValue ?? "")).toEqual({
61 connections: [toStableSavedRemoteConnection(managedConnection)],
62 });
63 });
64
65 it("loads relay-managed connection metadata without a cached access token", async () => {
66 await saveConnection(managedConnection);
67
68 await expect(loadSavedConnections()).resolves.toEqual([
69 toStableSavedRemoteConnection(managedConnection),
70 ]);
71 });
72
73 it("preserves secure-storage read failures with operation and key context", async () => {
74 const cause = new Error("keychain unavailable");
75 mocks.getItemAsync.mockRejectedValueOnce(cause);
76
77 await expect(loadSavedConnections()).rejects.toMatchObject({
78 _tag: "MobileSecureStorageError",
79 operation: "read",
80 key: "t3code.connections",
81 cause,
82 message: "Mobile secure storage operation read failed for key t3code.connections.",
83 });
84 });
85
86 it("logs structured decode failures before using the empty fallback", async () => {
87 await mocks.setItemAsync("t3code.connections", "{");
88 const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
89
90 await expect(loadSavedConnections()).resolves.toEqual([]);
91 expect(warn).toHaveBeenCalledWith(
92 "[mobile-storage] ignored invalid JSON",
93 expect.objectContaining({
94 _tag: "MobileStorageDecodeError",
95 key: "t3code.connections",
96 cause: expect.any(SyntaxError),
97 message: "Failed to decode mobile storage value for key t3code.connections.",
98 }),
99 );
100
101 warn.mockRestore();
102 });
103});