Open History to see the last commit
1import * as Effect from "effect/Effect";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4const mocks = vi.hoisted(() => ({
5 appState: "background" as string | null,
6 nativeCheckSupported: true,
7 platform: "ios",
8 subscribed: Promise.withResolvers<void>(),
9 listeners: new Set<(state: string) => void>(),
10 isProtectedDataAvailable: vi.fn<() => Promise<boolean>>(),
11 readStorage: vi.fn<() => Promise<void>>(),
12}));
13
14vi.mock("expo", () => ({
15 requireOptionalNativeModule: () => ({
16 ...(mocks.nativeCheckSupported && {
17 isProtectedDataAvailable: mocks.isProtectedDataAvailable,
18 }),
19 }),
20}));
21vi.mock("react-native", () => ({
22 Platform: {
23 get OS() {
24 return mocks.platform;
25 },
26 },
27 AppState: {
28 get currentState() {
29 return mocks.appState;
30 },
31 addEventListener: (_event: string, listener: (state: string) => void) => {
32 mocks.listeners.add(listener);
33 mocks.subscribed.resolve();
34 return { remove: () => mocks.listeners.delete(listener) };
35 },
36 },
37}));
38
39vi.mock("../persistence/layer", async () => {
40 const Effect = await import("effect/Effect");
41 const Layer = await import("effect/Layer");
42 return { layer: Layer.effectDiscard(Effect.promise(mocks.readStorage)) };
43});
44vi.mock("../features/cloud/dpop", async () => ({
45 layer: (await import("effect/Layer")).empty,
46}));
47vi.mock("../features/cloud/managedRelayLayer", async () => {
48 const Layer = await import("effect/Layer");
49 return { layer: () => Layer.empty };
50});
51vi.mock("../features/cloud/publicConfig", () => ({
52 resolveCloudPublicConfig: () => ({ relay: { url: null } }),
53}));
54vi.mock("../features/observability/tracing", async () => ({
55 layer: (await import("effect/Layer")).empty,
56}));
57vi.mock("@t3tools/client-runtime/rpc", async () => {
58 const Layer = await import("effect/Layer");
59 return { layerRemoteHttpClient: () => Layer.empty };
60});
61
62let runtime: typeof import("./runtime").runtime | undefined;
63
64beforeEach(() => {
65 vi.resetModules();
66 mocks.appState = "background";
67 mocks.nativeCheckSupported = true;
68 mocks.platform = "ios";
69 mocks.subscribed = Promise.withResolvers<void>();
70 mocks.listeners.clear();
71 mocks.isProtectedDataAvailable.mockReset();
72 mocks.readStorage.mockReset().mockResolvedValue(undefined);
73});
74
75afterEach(async () => {
76 await runtime?.dispose();
77 runtime = undefined;
78});
79
80describe("protected storage startup", () => {
81 it.each([
82 { launch: "background", state: "background", checkRejects: false },
83 { launch: "inactive", state: "inactive", checkRejects: false },
84 { launch: "unknown", state: "unknown", checkRejects: false },
85 { launch: "unset", state: null, checkRejects: false },
86 { launch: "background with a failed native check", state: "background", checkRejects: true },
87 {
88 launch: "background without a native check",
89 state: "background",
90 nativeCheckSupported: false,
91 },
92 { launch: "unknown without a native check", state: "unknown", nativeCheckSupported: false },
93 { launch: "unset without a native check", state: null, nativeCheckSupported: false },
94 ])(
95 "keeps a locked $launch launch usable after activation without rebuilding its runtime",
96 async (scenario) => {
97 mocks.appState = scenario.state;
98 mocks.nativeCheckSupported = scenario.nativeCheckSupported ?? true;
99 mocks.isProtectedDataAvailable.mockImplementation(async () => {
100 if (scenario.checkRejects) throw new Error("Protected data check unavailable.");
101 return false;
102 });
103 mocks.readStorage.mockImplementation(async () => {
104 if (mocks.appState !== "active") throw new Error("User interaction is not allowed.");
105 });
106 ({ runtime } = await import("./runtime"));
107 const startup = runtime.runPromiseExit(Effect.succeed("ready"));
108
109 // A missing gate finishes startup with a cached storage failure instead.
110 await Promise.race([mocks.subscribed.promise, startup]);
111 expect(mocks.readStorage).not.toHaveBeenCalled();
112 mocks.appState = "inactive";
113 for (const listener of mocks.listeners) listener("inactive");
114 await Promise.resolve();
115 expect(mocks.readStorage).not.toHaveBeenCalled();
116
117 mocks.appState = "active";
118 for (const listener of mocks.listeners) listener("active");
119 expect(await startup).toMatchObject({ _tag: "Success", value: "ready" });
120 expect(await runtime.runPromise(Effect.succeed("still ready"))).toBe("still ready");
121 expect(mocks.readStorage).toHaveBeenCalledOnce();
122 expect(mocks.listeners.size).toBe(0);
123 mocks.appState = "background";
124 await (await import("./protectedData")).whenProtectedDataAvailable();
125 expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes(
126 mocks.nativeCheckSupported ? 1 : 0,
127 );
128 },
129 );
130
131 it.each([
132 {
133 launch: "unlocked background",
134 state: "background",
135 platform: "ios",
136 nativeCheckSupported: true,
137 },
138 { launch: "active foreground", state: "active", platform: "ios", nativeCheckSupported: true },
139 {
140 launch: "Android background",
141 state: "background",
142 platform: "android",
143 nativeCheckSupported: false,
144 },
145 {
146 launch: "Android with a native check",
147 state: "background",
148 platform: "android",
149 nativeCheckSupported: true,
150 },
151 ])("starts storage on $launch launch without waiting for activation", async (scenario) => {
152 mocks.appState = scenario.state;
153 mocks.platform = scenario.platform;
154 mocks.nativeCheckSupported = scenario.nativeCheckSupported;
155 mocks.isProtectedDataAvailable.mockResolvedValue(scenario.platform === "ios");
156 ({ runtime } = await import("./runtime"));
157
158 expect(await runtime.runPromise(Effect.succeed("ready"))).toBe("ready");
159 expect(mocks.isProtectedDataAvailable).toHaveBeenCalledTimes(
160 scenario.platform === "ios" && scenario.state === "background" ? 1 : 0,
161 );
162 expect(mocks.readStorage).toHaveBeenCalledOnce();
163 expect(mocks.listeners.size).toBe(0);
164 });
165});