|
1 | | -import { EnvironmentId, WS_METHODS } from "@t3tools/contracts"; |
| 1 | +import { |
| 2 | + AVAILABLE_CONNECTION_STATE, |
| 3 | + type ConnectionCatalogEntry, |
| 4 | + EnvironmentRegistry, |
| 5 | + EnvironmentSupervisor, |
| 6 | + type NetworkStatus, |
| 7 | + type PreparedConnection, |
| 8 | + RelayConnectionTarget, |
| 9 | + type SupervisorConnectionState, |
| 10 | +} from "@t3tools/client-runtime/connection"; |
| 11 | +import type { RpcSession, WsRpcProtocolClient } from "@t3tools/client-runtime/rpc"; |
| 12 | +import { type ClientActivityReportInput, EnvironmentId, WS_METHODS } from "@t3tools/contracts"; |
2 | 13 | import { describe, expect, it } from "@effect/vitest"; |
| 14 | +import * as Clock from "effect/Clock"; |
| 15 | +import * as Duration from "effect/Duration"; |
3 | 16 | import * as Effect from "effect/Effect"; |
| 17 | +import * as Fiber from "effect/Fiber"; |
| 18 | +import * as Layer from "effect/Layer"; |
| 19 | +import * as Option from "effect/Option"; |
| 20 | +import * as Queue from "effect/Queue"; |
| 21 | +import * as Stream from "effect/Stream"; |
| 22 | +import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 23 | +import * as TestClock from "effect/testing/TestClock"; |
| 24 | +import { vi } from "vite-plus/test"; |
4 | 25 |
|
| 26 | +import { MobileStorage } from "../persistence/mobile-storage"; |
| 27 | +import { mobileBackgroundActivityReporterLayer } from "./background-activity"; |
5 | 28 | import { |
6 | 29 | onRetainedMobileBackgroundScopesChange, |
7 | 30 | observeMobileBackgroundActivitySubscription, |
8 | 31 | retainedMobileBackgroundScopes, |
9 | 32 | } from "./background-activity-scopes"; |
10 | 33 |
|
| 34 | +vi.mock("react-native", () => ({ |
| 35 | + AppState: { currentState: "active", addEventListener: () => ({ remove: () => {} }) }, |
| 36 | +})); |
| 37 | +vi.mock("expo-secure-store", () => ({})); |
| 38 | + |
11 | 39 | describe("mobile background activity", () => { |
| 40 | + it.effect("reports activity after reconnect when the initial report had no session", () => |
| 41 | + Effect.gen(function* () { |
| 42 | + const environmentId = EnvironmentId.make("reconnecting-environment"); |
| 43 | + const target = new RelayConnectionTarget({ environmentId, label: "Test environment" }); |
| 44 | + const reports = yield* Queue.unbounded<ClientActivityReportInput>(); |
| 45 | + const attempts = yield* Queue.unbounded<void>(); |
| 46 | + const debounceArmed = yield* Queue.sliding<void>(1); |
| 47 | + const clock = yield* Clock.Clock; |
| 48 | + const reporterClock = { |
| 49 | + ...clock, |
| 50 | + sleep: Effect.fn("TestMobileActivity.sleep")(function* (duration: Duration.Duration) { |
| 51 | + if (Duration.toMillis(duration) !== 250) return yield* clock.sleep(duration); |
| 52 | + const timer = yield* clock |
| 53 | + .sleep(duration) |
| 54 | + .pipe(Effect.forkChild({ startImmediately: true })); |
| 55 | + yield* Queue.offer(debounceArmed, undefined); |
| 56 | + yield* Fiber.join(timer); |
| 57 | + }), |
| 58 | + }; |
| 59 | + const state = yield* SubscriptionRef.make<SupervisorConnectionState>({ |
| 60 | + ...AVAILABLE_CONNECTION_STATE, |
| 61 | + desired: true, |
| 62 | + network: "online", |
| 63 | + phase: "connecting", |
| 64 | + }); |
| 65 | + const session = yield* SubscriptionRef.make<Option.Option<RpcSession>>(Option.none()); |
| 66 | + const supervisor = EnvironmentSupervisor.of({ |
| 67 | + target, |
| 68 | + state, |
| 69 | + session, |
| 70 | + prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none()), |
| 71 | + connect: Effect.void, |
| 72 | + disconnect: Effect.void, |
| 73 | + retryNow: Effect.void, |
| 74 | + }); |
| 75 | + const registryLayer = Layer.mock(EnvironmentRegistry, { |
| 76 | + entries: yield* SubscriptionRef.make<ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>>( |
| 77 | + new Map([[environmentId, { target, profile: Option.none() }]]), |
| 78 | + ), |
| 79 | + networkStatus: yield* SubscriptionRef.make<NetworkStatus>("online"), |
| 80 | + run: (_environmentId, effect) => |
| 81 | + Effect.provideService(effect, EnvironmentSupervisor, supervisor).pipe( |
| 82 | + Effect.ensuring(Queue.offer(attempts, undefined)), |
| 83 | + ), |
| 84 | + followStream: (_environmentId, stream) => |
| 85 | + Stream.provideService(stream, EnvironmentSupervisor, supervisor), |
| 86 | + }); |
| 87 | + yield* Layer.build( |
| 88 | + mobileBackgroundActivityReporterLayer.pipe( |
| 89 | + Layer.provide(registryLayer), |
| 90 | + Layer.provide( |
| 91 | + Layer.mock(MobileStorage, { |
| 92 | + loadOrCreateAgentAwarenessDeviceId: Effect.succeed("test-device"), |
| 93 | + }), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ).pipe(Effect.provideService(Clock.Clock, reporterClock)); |
| 97 | + |
| 98 | + yield* Queue.take(debounceArmed); |
| 99 | + yield* TestClock.adjust("250 millis"); |
| 100 | + yield* Queue.take(attempts); |
| 101 | + yield* Queue.clear(debounceArmed); |
| 102 | + expect(yield* Queue.size(reports)).toBe(0); |
| 103 | + |
| 104 | + const client = { |
| 105 | + [WS_METHODS.serverReportClientActivity]: (input: ClientActivityReportInput) => |
| 106 | + Queue.offer(reports, input).pipe(Effect.asVoid), |
| 107 | + } satisfies Pick<WsRpcProtocolClient, typeof WS_METHODS.serverReportClientActivity>; |
| 108 | + yield* SubscriptionRef.set( |
| 109 | + session, |
| 110 | + Option.some({ |
| 111 | + client: client as unknown as WsRpcProtocolClient, |
| 112 | + initialConfig: Effect.die("Activity reports do not read server config."), |
| 113 | + subscribeServerConfig: () => Stream.empty, |
| 114 | + ready: Effect.void, |
| 115 | + probe: Effect.void, |
| 116 | + closed: Effect.never, |
| 117 | + }), |
| 118 | + ); |
| 119 | + yield* SubscriptionRef.update(state, (current) => ({ |
| 120 | + ...current, |
| 121 | + phase: "connected" as const, |
| 122 | + generation: 1, |
| 123 | + })); |
| 124 | + yield* Queue.take(debounceArmed); |
| 125 | + yield* TestClock.adjust("250 millis"); |
| 126 | + expect(yield* Queue.take(reports)).toMatchObject({ |
| 127 | + environmentId, |
| 128 | + clientId: "mobile-test-device", |
| 129 | + visible: true, |
| 130 | + focused: true, |
| 131 | + appState: "active", |
| 132 | + scopes: [{ type: "provider-status" }], |
| 133 | + }); |
| 134 | + }).pipe(Effect.provide(TestClock.layer())), |
| 135 | + ); |
| 136 | + |
12 | 137 | it.effect("retains VCS demand only while the mobile subscription is active", () => |
13 | 138 | Effect.gen(function* () { |
14 | 139 | const environmentId = EnvironmentId.make("mobile-environment"); |
|
0 commit comments