Open History to see the last commit
1import { beforeEach, vi } from "vite-plus/test";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import type { EnvironmentId } from "@t3tools/contracts";
5import { ManagedRelay } from "@t3tools/client-runtime/relay";
6import * as Layer from "effect/Layer";
7import { HttpClient } from "effect/http";
8
9import type { SavedRemoteConnection } from "../../lib/connection";
10import * as MobileStorage from "../../persistence/mobile-storage";
11import {
12 CloudEnvironmentLinkError,
13 linkEnvironmentToCloudWithPreference,
14} from "../cloud/linkEnvironment";
15import { setLiveActivityUpdatesEnabled } from "./liveActivityPreferences";
16import { updateAgentAwarenessRegistrationPreferences } from "./remoteRegistration";
17
18vi.mock("expo-secure-store", () => ({
19 deleteItemAsync: vi.fn(),
20 getItemAsync: vi.fn(),
21 setItemAsync: vi.fn(),
22}));
23
24vi.mock("react-native", () => ({
25 Platform: { OS: "ios" },
26}));
27
28vi.mock("expo-constants", () => ({
29 default: { expoConfig: {} },
30}));
31
32vi.mock("expo-device", () => ({}));
33
34vi.mock("../cloud/linkEnvironment", async (importOriginal) => ({
35 ...(await importOriginal<typeof import("../cloud/linkEnvironment")>()),
36 linkEnvironmentToCloudWithPreference: vi.fn(() => Effect.void),
37}));
38
39vi.mock("./remoteRegistration", () => ({
40 updateAgentAwarenessRegistrationPreferences: vi.fn(() => Effect.void),
41}));
42
43const connection: SavedRemoteConnection = {
44 environmentId: "env-1" as EnvironmentId,
45 environmentLabel: "Desktop",
46 pairingUrl: "https://desktop.example.test/",
47 displayUrl: "https://desktop.example.test/",
48 httpBaseUrl: "https://desktop.example.test/",
49 wsBaseUrl: "wss://desktop.example.test/ws",
50 bearerToken: "local-bearer",
51};
52
53const layerTest = Layer.mergeAll(
54 Layer.succeed(ManagedRelay.ManagedRelayClient, null as never),
55 Layer.succeed(
56 HttpClient.HttpClient,
57 HttpClient.make(() => Effect.die("unexpected HTTP request")),
58 ),
59 Layer.succeed(
60 MobileStorage.MobileStorage,
61 MobileStorage.MobileStorage.of({
62 loadSavedConnections: Effect.succeed([]),
63 saveConnection: () => Effect.void,
64 clearSavedConnection: () => Effect.void,
65 loadOrCreateAgentAwarenessDeviceId: Effect.succeed("device-1"),
66 loadAgentAwarenessDeviceId: Effect.succeed("device-1"),
67 loadAgentAwarenessRegistrationRecord: Effect.succeed(null),
68 saveAgentAwarenessRegistrationRecord: () => Effect.void,
69 clearAgentAwarenessRegistrationRecord: Effect.void,
70 loadRecentThreadShortcuts: Effect.succeed([]),
71 saveRecentThreadShortcuts: () => Effect.void,
72 }),
73 ),
74);
75
76describe("liveActivityPreferences", () => {
77 beforeEach(() => {
78 vi.clearAllMocks();
79 });
80
81 it.effect("restores relay preferences when an environment update fails", () => {
82 vi.mocked(linkEnvironmentToCloudWithPreference).mockImplementationOnce(() =>
83 Effect.fail(new CloudEnvironmentLinkError({ message: "environment update failed" })),
84 );
85
86 return Effect.gen(function* () {
87 const exit = yield* Effect.exit(
88 setLiveActivityUpdatesEnabled({
89 enabled: false,
90 previousEnabled: true,
91 clerkToken: "clerk-token",
92 connections: [connection],
93 canConfigureEnvironment: () => true,
94 }),
95 );
96
97 expect(exit._tag).toBe("Failure");
98 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenNthCalledWith(1, {
99 liveActivitiesEnabled: false,
100 });
101 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenNthCalledWith(2, {
102 liveActivitiesEnabled: true,
103 });
104 expect(linkEnvironmentToCloudWithPreference).toHaveBeenNthCalledWith(1, {
105 clerkToken: "clerk-token",
106 connection,
107 liveActivitiesEnabled: false,
108 });
109 expect(linkEnvironmentToCloudWithPreference).toHaveBeenNthCalledWith(2, {
110 clerkToken: "clerk-token",
111 connection,
112 liveActivitiesEnabled: true,
113 });
114 }).pipe(Effect.provide(layerTest));
115 });
116
117 it.effect("only re-links environments with relay access when enabling updates", () => {
118 const readOnlyConnection: SavedRemoteConnection = {
119 ...connection,
120 environmentId: "read-only" as EnvironmentId,
121 };
122
123 return Effect.gen(function* () {
124 yield* setLiveActivityUpdatesEnabled({
125 enabled: true,
126 previousEnabled: false,
127 clerkToken: "clerk-token",
128 connections: [readOnlyConnection, connection],
129 canConfigureEnvironment: (environmentId) => environmentId === connection.environmentId,
130 });
131
132 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenCalledWith({
133 liveActivitiesEnabled: true,
134 });
135 expect(linkEnvironmentToCloudWithPreference).toHaveBeenCalledExactlyOnceWith({
136 clerkToken: "clerk-token",
137 connection,
138 liveActivitiesEnabled: true,
139 });
140 }).pipe(Effect.provide(layerTest));
141 });
142
143 it.effect("checks current relay access after updating the device registration", () => {
144 let canConfigureEnvironment = true;
145 vi.mocked(updateAgentAwarenessRegistrationPreferences).mockImplementationOnce(() =>
146 Effect.sync(() => {
147 canConfigureEnvironment = false;
148 }),
149 );
150
151 return Effect.gen(function* () {
152 yield* setLiveActivityUpdatesEnabled({
153 enabled: false,
154 previousEnabled: true,
155 clerkToken: "clerk-token",
156 connections: [connection],
157 canConfigureEnvironment: () => canConfigureEnvironment,
158 });
159
160 expect(linkEnvironmentToCloudWithPreference).not.toHaveBeenCalled();
161 expect(updateAgentAwarenessRegistrationPreferences).toHaveBeenCalledTimes(1);
162 }).pipe(Effect.provide(layerTest));
163 });
164});