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
| 1 | import type { |
| 2 | RelayAgentActivityState, |
| 3 | RelayDeviceRegistrationRequest, |
| 4 | } from "@t3tools/contracts/relay"; |
| 5 | import type { SignedApnsDeliveryJob } from "./apnsDeliveryJobs.ts"; |
| 6 | import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; |
| 7 | import { describe, expect, it } from "@effect/vitest"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Logger from "effect/Logger"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { FetchHttpClient } from "effect/http"; |
| 13 | |
| 14 | import * as Devices from "./Devices.ts"; |
| 15 | import * as AgentActivityRows from "./AgentActivityRows.ts"; |
| 16 | import * as DeliveryAttempts from "./DeliveryAttempts.ts"; |
| 17 | import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts"; |
| 18 | import * as LiveActivities from "./LiveActivities.ts"; |
| 19 | import * as RelayConfiguration from "../Config.ts"; |
| 20 | import * as AgentActivityPublisher from "./AgentActivityPublisher.ts"; |
| 21 | import * as FcmDeliveries from "./FcmDeliveries.ts"; |
| 22 | |
| 23 | const layerPublisher = AgentActivityPublisher.layer.pipe( |
| 24 | Layer.provide( |
| 25 | Layer.succeed(FcmDeliveries.FcmDeliveries, { |
| 26 | enqueue: () => Effect.succeed(null), |
| 27 | process: () => Effect.void, |
| 28 | }), |
| 29 | ), |
| 30 | ); |
| 31 | import * as ApnsDeliveries from "./ApnsDeliveries.ts"; |
| 32 | import * as ApnsClient from "./ApnsClient.ts"; |
| 33 | import * as ApnsProviderTokens from "./ApnsProviderTokens.ts"; |
| 34 | import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts"; |
| 35 | import * as MobileRegistrations from "./MobileRegistrations.ts"; |
| 36 | |
| 37 | const device: RelayDeviceRegistrationRequest = { |
| 38 | deviceId: "device-1" as RelayDeviceRegistrationRequest["deviceId"], |
| 39 | label: "Julius's iPhone", |
| 40 | platform: "ios", |
| 41 | iosMajorVersion: 18, |
| 42 | appVersion: "1.0.0" as RelayDeviceRegistrationRequest["appVersion"], |
| 43 | preferences: { |
| 44 | liveActivitiesEnabled: true, |
| 45 | notificationsEnabled: true, |
| 46 | notifyOnApproval: true, |
| 47 | notifyOnInput: true, |
| 48 | notifyOnCompletion: true, |
| 49 | notifyOnFailure: true, |
| 50 | }, |
| 51 | }; |
| 52 | |
| 53 | function makeDevices( |
| 54 | overrides: Partial<Devices.Devices["Service"]> = {}, |
| 55 | ): Devices.Devices["Service"] { |
| 56 | return { |
| 57 | register: () => Effect.void, |
| 58 | unregister: () => Effect.void, |
| 59 | listForUser: () => Effect.succeed([]), |
| 60 | ...overrides, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | function makeLiveActivities( |
| 65 | overrides: Partial<LiveActivities.LiveActivities["Service"]> = {}, |
| 66 | ): LiveActivities.LiveActivities["Service"] { |
| 67 | return { |
| 68 | register: () => Effect.void, |
| 69 | listTargets: () => Effect.succeed([]), |
| 70 | listIdleArmedTargets: () => Effect.succeed([]), |
| 71 | markDelivery: () => Effect.void, |
| 72 | |
| 73 | invalidateDeliveryToken: () => Effect.void, |
| 74 | ...overrides, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | function makeAgentActivityRows( |
| 79 | overrides: Partial<AgentActivityRows.AgentActivityRows["Service"]> = {}, |
| 80 | ): AgentActivityRows.AgentActivityRows["Service"] { |
| 81 | return { |
| 82 | upsert: () => Effect.void, |
| 83 | remove: () => Effect.void, |
| 84 | pruneTerminal: () => Effect.void, |
| 85 | listForUser: () => { |
| 86 | const activeState: RelayAgentActivityState = { |
| 87 | environmentId: "env-1" as RelayAgentActivityState["environmentId"], |
| 88 | threadId: "thread-1" as RelayAgentActivityState["threadId"], |
| 89 | projectTitle: "Project", |
| 90 | threadTitle: "Implement APNs", |
| 91 | modelTitle: "gpt-5.4", |
| 92 | phase: "running", |
| 93 | headline: "Working", |
| 94 | updatedAt: "1970-01-01T00:00:10.000Z", |
| 95 | deepLink: "/env-1/thread-1", |
| 96 | }; |
| 97 | return Effect.succeed([activeState]); |
| 98 | }, |
| 99 | getForUserThread: () => Effect.succeed(null), |
| 100 | ...overrides, |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | function makeEnvironmentLinks( |
| 105 | overrides: Partial<EnvironmentLinks.EnvironmentLinks["Service"]> = {}, |
| 106 | ): EnvironmentLinks.EnvironmentLinks["Service"] { |
| 107 | return { |
| 108 | upsert: () => Effect.void, |
| 109 | listDeliveryUsersForEnvironment: () => |
| 110 | Effect.succeed([ |
| 111 | { |
| 112 | userId: "dev:julius", |
| 113 | notificationsEnabled: true, |
| 114 | liveActivitiesEnabled: true, |
| 115 | }, |
| 116 | ]), |
| 117 | listForUser: () => Effect.succeed([]), |
| 118 | getForUser: () => Effect.succeed(null), |
| 119 | findActiveManagedForEnvironment: () => Effect.succeed([]), |
| 120 | updateLiveActivityPreference: () => Effect.die("unexpected Live Activity preference update"), |
| 121 | setHoldWebhooksWhileOffline: () => Effect.void, |
| 122 | revokeForUser: () => Effect.succeed(false), |
| 123 | ...overrides, |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | function makeDeliveryAttempts( |
| 128 | overrides: Partial<DeliveryAttempts.DeliveryAttempts["Service"]> = {}, |
| 129 | ): DeliveryAttempts.DeliveryAttempts["Service"] { |
| 130 | return { |
| 131 | claimSourceJob: () => Effect.succeed("claimed"), |
| 132 | completeSourceJob: () => Effect.void, |
| 133 | pruneExpired: Effect.succeed(0), |
| 134 | ...overrides, |
| 135 | }; |
| 136 | } |
| 137 | |
| 138 | const config = RelayConfiguration.RelayConfiguration.of({ |
| 139 | relayIssuer: "https://relay.example.test", |
| 140 | apns: { |
| 141 | environment: "sandbox", |
| 142 | teamId: "team-id", |
| 143 | keyId: "key-id", |
| 144 | bundleId: "codes.t3.mobile", |
| 145 | privateKey: Redacted.make("apns-private-key"), |
| 146 | }, |
| 147 | clerkSecretKey: Redacted.make("clerk-secret"), |
| 148 | clerkPublishableKey: "pk_test_test", |
| 149 | clerkJwtAudience: "t3-code-relay", |
| 150 | apnsDeliveryJobSigningSecret: Redacted.make("apns-job-secret"), |
| 151 | cloudMintPrivateKey: Redacted.make("cloud-private-key"), |
| 152 | cloudMintPublicKey: "cloud-public-key", |
| 153 | managedEndpointBaseDomain: undefined, |
| 154 | managedEndpointNamespace: undefined, |
| 155 | }); |
| 156 | |
| 157 | function layerRegistrationReplay(input: { |
| 158 | readonly devices: Devices.Devices["Service"]; |
| 159 | readonly liveActivities: LiveActivities.LiveActivities["Service"]; |
| 160 | readonly queuedJobs: Array<SignedApnsDeliveryJob>; |
| 161 | }) { |
| 162 | return MobileRegistrations.layer.pipe( |
| 163 | Layer.provide(layerPublisher), |
| 164 | Layer.provide( |
| 165 | ApnsDeliveries.layer.pipe( |
| 166 | Layer.provide(ApnsClient.layer.pipe(Layer.provide(ApnsProviderTokens.layer))), |
| 167 | ), |
| 168 | ), |
| 169 | Layer.provide(ApnsDeliveryQueue.layer.pipe(Layer.provide(NodeCryptoLayer.layer))), |
| 170 | Layer.provide(NodeCryptoLayer.layerMac), |
| 171 | Layer.provide( |
| 172 | Layer.mergeAll( |
| 173 | Layer.succeed(Devices.Devices, input.devices), |
| 174 | Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()), |
| 175 | Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()), |
| 176 | Layer.succeed(LiveActivities.LiveActivities, input.liveActivities), |
| 177 | Layer.succeed(DeliveryAttempts.DeliveryAttempts, makeDeliveryAttempts()), |
| 178 | RelayConfiguration.layer(config), |
| 179 | Layer.succeed(ApnsDeliveryQueue.ApnsDeliveryQueueSender, { |
| 180 | send: (body) => |
| 181 | Effect.sync(() => { |
| 182 | input.queuedJobs.push(body); |
| 183 | }), |
| 184 | }), |
| 185 | ), |
| 186 | ), |
| 187 | Layer.provide(FetchHttpClient.layer), |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | function makeAgentActivityPublisher( |
| 192 | overrides: Partial<AgentActivityPublisher.AgentActivityPublisher["Service"]> = {}, |
| 193 | ): AgentActivityPublisher.AgentActivityPublisher["Service"] { |
| 194 | return { |
| 195 | publish: () => Effect.succeed({ ok: true, deliveries: [] }), |
| 196 | replayForLiveActivityRegistration: () => Effect.succeed(null), |
| 197 | endIdleLiveActivities: Effect.void, |
| 198 | ...overrides, |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | describe("MobileRegistrations", () => { |
| 203 | it.effect("keeps device registration successful when activity replay fails", () => { |
| 204 | const messages: unknown[] = []; |
| 205 | const logger = Logger.make(({ message }) => { |
| 206 | messages.push(message); |
| 207 | }); |
| 208 | |
| 209 | return Effect.gen(function* () { |
| 210 | const result = yield* Effect.gen(function* () { |
| 211 | const registrations = yield* MobileRegistrations.MobileRegistrations; |
| 212 | return yield* registrations.registerDevice({ userId: "dev:julius", payload: device }); |
| 213 | }).pipe( |
| 214 | Effect.provide( |
| 215 | MobileRegistrations.layer.pipe( |
| 216 | Layer.provide( |
| 217 | Layer.mergeAll( |
| 218 | Layer.succeed(Devices.Devices, makeDevices()), |
| 219 | Layer.succeed(LiveActivities.LiveActivities, makeLiveActivities()), |
| 220 | Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()), |
| 221 | Layer.succeed( |
| 222 | AgentActivityPublisher.AgentActivityPublisher, |
| 223 | makeAgentActivityPublisher({ |
| 224 | replayForLiveActivityRegistration: () => |
| 225 | Effect.fail( |
| 226 | new AgentActivityRows.AgentActivityRowListPersistenceError({ |
| 227 | userId: "dev:julius", |
| 228 | cause: "sensitive device replay detail", |
| 229 | }), |
| 230 | ), |
| 231 | }), |
| 232 | ), |
| 233 | ), |
| 234 | ), |
| 235 | ), |
| 236 | ), |
| 237 | ); |
| 238 | |
| 239 | expect(result).toEqual({ ok: true }); |
| 240 | expect(messages).toContainEqual([ |
| 241 | "device registration activity replay failed", |
| 242 | { errorTag: "AgentActivityRowListPersistenceError" }, |
| 243 | ]); |
| 244 | }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))); |
| 245 | }); |
| 246 | |
| 247 | it.effect( |
| 248 | "does not remotely start a Live Activity when a device registers after work is already active", |
| 249 | () => { |
| 250 | const queuedJobs: Array<SignedApnsDeliveryJob> = []; |
| 251 | const registeredDevices: Array<Parameters<Devices.Devices["Service"]["register"]>[0]> = []; |
| 252 | const devices = makeDevices({ |
| 253 | register: (input) => |
| 254 | Effect.sync(() => { |
| 255 | registeredDevices.push(input); |
| 256 | }), |
| 257 | }); |
| 258 | const liveActivities = makeLiveActivities({ |
| 259 | listTargets: () => |
| 260 | Effect.succeed([ |
| 261 | { |
| 262 | user_id: "dev:julius", |
| 263 | device_id: "device-1", |
| 264 | platform: "ios", |
| 265 | ios_major_version: 18, |
| 266 | app_version: "1.0.0", |
| 267 | bundle_id: null, |
| 268 | aps_environment: null, |
| 269 | push_token: "apns-device-token", |
| 270 | push_to_start_token: "push-to-start-token", |
| 271 | preferences_json: JSON.stringify(device.preferences), |
| 272 | activity_push_token: null, |
| 273 | remote_start_queued_at: null, |
| 274 | remote_started_at: null, |
| 275 | ended_at: null, |
| 276 | last_aggregate_json: null, |
| 277 | last_live_activity_delivery_at: null, |
| 278 | }, |
| 279 | ]), |
| 280 | }); |
| 281 | |
| 282 | return Effect.gen(function* () { |
| 283 | const registrations = yield* MobileRegistrations.MobileRegistrations; |
| 284 | const result = yield* registrations.registerDevice({ |
| 285 | userId: "dev:julius", |
| 286 | payload: { |
| 287 | ...device, |
| 288 | pushToken: "apns-device-token", |
| 289 | pushToStartToken: "push-to-start-token", |
| 290 | }, |
| 291 | }); |
| 292 | |
| 293 | expect(result).toEqual({ ok: true }); |
| 294 | expect(registeredDevices).toHaveLength(1); |
| 295 | expect(queuedJobs).toEqual([]); |
| 296 | }).pipe(Effect.provide(layerRegistrationReplay({ devices, liveActivities, queuedJobs }))); |
| 297 | }, |
| 298 | ); |
| 299 | }); |