@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 273 lines 9.21 KB
Raw
1 import { beforeEach, vi } from "vite-plus/test";
2 import { describe, expect, it } from "@effect/vitest";
3 import * as Exit from "effect/Exit";
4 import * as Option from "effect/Option";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import type { EnvironmentId } from "@t3tools/contracts";
8 import { ManagedRelay } from "@t3tools/client-runtime/relay";
9
10 import type { SavedRemoteConnection } from "../../lib/connection";
11 import {
12 MobilePreferencesSaveError,
13 MobilePreferencesStore,
14 } from "../../persistence/mobile-preferences";
15 import { MobileStorage } from "../../persistence/mobile-storage";
16 import { CloudEnvironmentLinkError } from "../cloud/linkEnvironment";
17 import { setLiveActivityUpdatesEnabled } from "./liveActivityPreferences";
18 import { updateAgentAwarenessRegistrationPreferences } from "./remoteRegistration";
19
20 vi.mock("expo-secure-store", () => ({
21 deleteItemAsync: vi.fn(),
22 getItemAsync: vi.fn(),
23 setItemAsync: vi.fn(),
24 }));
25
26 vi.mock("react-native", () => ({
27 Platform: { OS: "ios" },
28 }));
29
30 vi.mock("expo-constants", () => ({
31 default: { expoConfig: {} },
32 }));
33
34 vi.mock("expo-device", () => ({}));
35
36 vi.mock("./remoteRegistration", () => ({
37 updateAgentAwarenessRegistrationPreferences: vi.fn(() => Effect.void),
38 }));
39
40 const updateEnvironment = vi.fn<
41 (
42 id: EnvironmentId,
43 enabled: boolean,
44 ) => Effect.Effect<
45 "updated" | "unlinked" | "unsupported" | "disconnected",
46 CloudEnvironmentLinkError
47 >
48 >(() => Effect.succeed("updated"));
49
50 const linkEnvironment = vi.fn<
51 (
52 id: EnvironmentId,
53 input: { readonly clerkToken: string; readonly liveActivitiesEnabled: boolean },
54 ) => Effect.Effect<void, CloudEnvironmentLinkError>
55 >(() => Effect.void);
56
57 const saveLocalPreference = vi.fn<
58 (enabled: boolean) => Effect.Effect<void, MobilePreferencesSaveError>
59 >(() => Effect.void);
60
61 const connection: SavedRemoteConnection = {
62 environmentId: "env-1" as EnvironmentId,
63 environmentLabel: "Desktop",
64 pairingUrl: "https://desktop.example.test/",
65 displayUrl: "https://desktop.example.test/",
66 httpBaseUrl: "https://desktop.example.test/",
67 wsBaseUrl: "wss://desktop.example.test/ws",
68 bearerToken: "local-bearer",
69 };
70
71 const withRelayClient = Effect.provide(
72 Layer.mergeAll(
73 Layer.succeed(ManagedRelay.ManagedRelayClient, null as never),
74 Layer.succeed(MobileStorage, null as never),
75 Layer.succeed(MobilePreferencesStore, null as never),
76 ),
77 );
78
79 describe("liveActivityPreferences", () => {
80 beforeEach(() => {
81 vi.clearAllMocks();
82 });
83
84 it.effect("restores only the device preference when an environment update fails", () => {
85 const otherConnection = { ...connection, environmentId: "env-2" as EnvironmentId };
86 updateEnvironment.mockImplementationOnce(() =>
87 Effect.fail(new CloudEnvironmentLinkError({ message: "environment update failed" })),
88 );
89
90 return Effect.gen(function* () {
91 const exit = yield* Effect.exit(
92 setLiveActivityUpdatesEnabled({
93 updateEnvironment,
94 linkEnvironment,
95 saveLocalPreference,
96 enabled: false,
97 previousEnabled: true,
98 clerkToken: "clerk-token",
99 connections: [connection, otherConnection],
100 canConfigureEnvironment: () => false,
101 canUpdateEnvironment: () => true,
102 }),
103 );
104
105 expect(Exit.findErrorOption(exit)).toEqual(
106 Option.some(
107 expect.objectContaining({ updatedEnvironments: [otherConnection.environmentLabel] }),
108 ),
109 );
110 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenNthCalledWith(1, {
111 liveActivitiesEnabled: false,
112 });
113 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenNthCalledWith(2, {
114 liveActivitiesEnabled: true,
115 });
116 expect(updateEnvironment).toHaveBeenCalledTimes(2);
117 expect(updateEnvironment).toHaveBeenCalledWith(connection.environmentId, false);
118 expect(updateEnvironment).toHaveBeenCalledWith(otherConnection.environmentId, false);
119 expect(linkEnvironment).not.toHaveBeenCalled();
120 }).pipe(withRelayClient);
121 });
122
123 it.effect("restores the device preference when the device cannot save the setting", () => {
124 saveLocalPreference.mockImplementationOnce(() =>
125 Effect.fail(new MobilePreferencesSaveError({ cause: new Error("storage full") })),
126 );
127
128 return Effect.gen(function* () {
129 const exit = yield* Effect.exit(
130 setLiveActivityUpdatesEnabled({
131 updateEnvironment,
132 linkEnvironment,
133 saveLocalPreference,
134 enabled: true,
135 previousEnabled: false,
136 clerkToken: "clerk-token",
137 connections: [connection],
138 canConfigureEnvironment: () => false,
139 canUpdateEnvironment: () => true,
140 }),
141 );
142
143 expect(Exit.isFailure(exit)).toBe(true);
144 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenLastCalledWith({
145 liveActivitiesEnabled: false,
146 });
147 }).pipe(withRelayClient);
148 });
149
150 it.effect("reports a relay preference committed before environment configuration fails", () => {
151 const failure = new CloudEnvironmentLinkError({
152 message: "environment configuration failed",
153 relayLinkCommitted: true,
154 });
155 updateEnvironment.mockReturnValueOnce(Effect.succeed("unlinked"));
156 linkEnvironment.mockReturnValueOnce(Effect.fail(failure));
157
158 return Effect.gen(function* () {
159 const exit = yield* Effect.exit(
160 setLiveActivityUpdatesEnabled({
161 updateEnvironment,
162 linkEnvironment,
163 saveLocalPreference,
164 enabled: true,
165 previousEnabled: false,
166 clerkToken: "clerk-token",
167 connections: [connection],
168 canConfigureEnvironment: () => true,
169 canUpdateEnvironment: () => true,
170 }),
171 );
172
173 expect(Exit.findErrorOption(exit)).toEqual(
174 Option.some(
175 expect.objectContaining({
176 updatedEnvironments: [connection.environmentLabel],
177 cause: failure,
178 }),
179 ),
180 );
181 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenLastCalledWith({
182 liveActivitiesEnabled: false,
183 });
184 }).pipe(withRelayClient);
185 });
186
187 it.effect("only creates missing links when the session can configure Connect", () => {
188 updateEnvironment.mockReturnValueOnce(Effect.succeed("unlinked" as const));
189 const readOnlyConnection: SavedRemoteConnection = {
190 ...connection,
191 environmentId: "read-only" as EnvironmentId,
192 };
193 // Current mobile pairings authenticate with DPoP and carry no bearer token.
194 const dpopConnection: SavedRemoteConnection = {
195 ...connection,
196 bearerToken: null,
197 authenticationMethod: "dpop",
198 };
199
200 return Effect.gen(function* () {
201 yield* setLiveActivityUpdatesEnabled({
202 updateEnvironment,
203 linkEnvironment,
204 saveLocalPreference,
205 enabled: true,
206 previousEnabled: false,
207 clerkToken: "clerk-token",
208 connections: [readOnlyConnection, dpopConnection],
209 canUpdateEnvironment: (environmentId) => environmentId === connection.environmentId,
210 canConfigureEnvironment: (environmentId) => environmentId === connection.environmentId,
211 });
212
213 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenCalledWith({
214 liveActivitiesEnabled: true,
215 });
216 expect(linkEnvironment).toHaveBeenCalledExactlyOnceWith(connection.environmentId, {
217 clerkToken: "clerk-token",
218 liveActivitiesEnabled: true,
219 });
220 }).pipe(withRelayClient);
221 });
222
223 it.effect("reports older environments whose preferences could not be updated", () =>
224 Effect.gen(function* () {
225 const dpopConnection: SavedRemoteConnection = {
226 ...connection,
227 bearerToken: null,
228 authenticationMethod: "dpop",
229 };
230 updateEnvironment.mockReturnValueOnce(Effect.succeed("unsupported"));
231 const partial = yield* setLiveActivityUpdatesEnabled({
232 updateEnvironment,
233 linkEnvironment,
234 saveLocalPreference,
235 enabled: false,
236 previousEnabled: true,
237 clerkToken: "clerk-token",
238 connections: [dpopConnection],
239 canConfigureEnvironment: () => false,
240 canUpdateEnvironment: () => true,
241 });
242 expect(partial).toBe(true);
243 expect(linkEnvironment).not.toHaveBeenCalled();
244 }).pipe(withRelayClient),
245 );
246
247 it.effect("checks current relay access after updating the device registration", () => {
248 let canConfigureEnvironment = true;
249 vi.mocked(updateAgentAwarenessRegistrationPreferences).mockImplementationOnce(() =>
250 Effect.sync(() => {
251 canConfigureEnvironment = false;
252 }),
253 );
254
255 return Effect.gen(function* () {
256 yield* setLiveActivityUpdatesEnabled({
257 updateEnvironment,
258 linkEnvironment,
259 saveLocalPreference,
260 enabled: false,
261 previousEnabled: true,
262 clerkToken: "clerk-token",
263 connections: [connection],
264 canConfigureEnvironment: () => canConfigureEnvironment,
265 canUpdateEnvironment: () => canConfigureEnvironment,
266 });
267
268 expect(linkEnvironment).not.toHaveBeenCalled();
269 expect(updateEnvironment).not.toHaveBeenCalled();
270 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenCalledTimes(1);
271 }).pipe(withRelayClient);
272 });
273 });