|
| 1 | +import { EnvironmentId, ProjectId, WS_METHODS } from "@t3tools/contracts"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { vi } from "vite-plus/test"; |
| 4 | +import { RpcClientError } from "effect/rpc"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as Latch from "effect/Latch"; |
| 7 | +import * as Layer from "effect/Layer"; |
| 8 | +import * as Option from "effect/Option"; |
| 9 | +import * as Stream from "effect/Stream"; |
| 10 | +import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 11 | +import * as TestClock from "effect/testing/TestClock"; |
| 12 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 13 | +import type { PreparedConnection, SupervisorConnectionState } from "../connection/model.ts"; |
| 14 | +import type { ConnectionCatalogEntry } from "../connection/catalog.ts"; |
| 15 | +import { AVAILABLE_CONNECTION_STATE, PrimaryConnectionTarget } from "../connection/model.ts"; |
| 16 | +import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 17 | +import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 18 | +import type { RpcSession } from "../rpc/session.ts"; |
| 19 | +import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
| 20 | +import * as PullRequestDiffLoader from "./pullRequestDiffHttp.ts"; |
| 21 | +import { createPullRequestEnvironmentAtoms } from "./pullRequests.ts"; |
| 22 | + |
| 23 | +it.effect.each(["current", "unknown RPC", "missing method"] as const)( |
| 24 | + "isolates activity from turns and preserves reads on a %s server", |
| 25 | + (server) => |
| 26 | + Effect.scoped( |
| 27 | + Effect.gen(function* () { |
| 28 | + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] }); |
| 29 | + yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers())); |
| 30 | + const turns = yield* SubscriptionRef.make(0); |
| 31 | + const edits = yield* SubscriptionRef.make(0); |
| 32 | + let reads = 0; |
| 33 | + let detailReads = 0; |
| 34 | + let detailRefreshed = Latch.makeUnsafe(); |
| 35 | + const activityRead = Latch.makeUnsafe(); |
| 36 | + const client = { |
| 37 | + [WS_METHODS.pullRequestsSubscribeRefreshes]: () => |
| 38 | + SubscriptionRef.changes(turns).pipe(Stream.filter((revision) => revision > 0)), |
| 39 | + ...(server === "missing method" |
| 40 | + ? {} |
| 41 | + : { |
| 42 | + [WS_METHODS.pullRequestsSubscribeActivityRefreshesFork]: () => |
| 43 | + server === "unknown RPC" |
| 44 | + ? Stream.fail( |
| 45 | + new RpcClientError.RpcClientError({ |
| 46 | + reason: new RpcClientError.RpcClientDefect({ |
| 47 | + message: |
| 48 | + "Unknown request tag: pullRequests.subscribeActivityRefreshesFork", |
| 49 | + cause: new Error("Unknown request tag"), |
| 50 | + }), |
| 51 | + }), |
| 52 | + ) |
| 53 | + : SubscriptionRef.changes(edits).pipe( |
| 54 | + Stream.filter((revision) => revision > 0), |
| 55 | + ), |
| 56 | + }), |
| 57 | + [WS_METHODS.pullRequestsActivity]: () => |
| 58 | + Effect.sync(() => { |
| 59 | + reads++; |
| 60 | + if (reads === 2) activityRead.openUnsafe(); |
| 61 | + return { |
| 62 | + author: null, |
| 63 | + reviewers: [], |
| 64 | + comments: [], |
| 65 | + commentCount: reads, |
| 66 | + commentsTruncated: false, |
| 67 | + reviewThreads: [], |
| 68 | + commits: [], |
| 69 | + reactions: [], |
| 70 | + }; |
| 71 | + }), |
| 72 | + [WS_METHODS.pullRequestsDetail]: () => |
| 73 | + Effect.sync(() => { |
| 74 | + detailReads++; |
| 75 | + if (detailReads > 1) detailRefreshed.openUnsafe(); |
| 76 | + return { state: "open" }; |
| 77 | + }), |
| 78 | + } as unknown as WsRpcProtocolClient; |
| 79 | + const target = new PrimaryConnectionTarget({ |
| 80 | + environmentId: EnvironmentId.make("environment-1"), |
| 81 | + label: "Test", |
| 82 | + httpBaseUrl: "https://example.test", |
| 83 | + wsBaseUrl: "wss://example.test", |
| 84 | + }); |
| 85 | + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 86 | + target, |
| 87 | + state: yield* SubscriptionRef.make<SupervisorConnectionState>({ |
| 88 | + ...AVAILABLE_CONNECTION_STATE, |
| 89 | + desired: true, |
| 90 | + network: "online", |
| 91 | + phase: "connected", |
| 92 | + attempt: 1, |
| 93 | + generation: 1, |
| 94 | + }), |
| 95 | + session: yield* SubscriptionRef.make<Option.Option<RpcSession>>( |
| 96 | + Option.some({ |
| 97 | + client, |
| 98 | + inboundFrames: Effect.succeed(0), |
| 99 | + ready: Effect.void, |
| 100 | + probe: Effect.void, |
| 101 | + closed: Effect.never, |
| 102 | + initialConfig: Effect.never, |
| 103 | + subscribeServerConfig: () => Stream.empty, |
| 104 | + } satisfies RpcSession), |
| 105 | + ), |
| 106 | + prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 107 | + connect: Effect.void, |
| 108 | + disconnect: Effect.void, |
| 109 | + retryNow: Effect.void, |
| 110 | + }); |
| 111 | + const registryService = EnvironmentRegistry.EnvironmentRegistry.of({ |
| 112 | + entries: yield* SubscriptionRef.make<ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>>( |
| 113 | + new Map([[target.environmentId, { target, profile: Option.none(), enabled: true }]]), |
| 114 | + ), |
| 115 | + run: (_id, effect) => |
| 116 | + Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 117 | + runStream: (_id, stream) => |
| 118 | + Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 119 | + followStream: (_id, stream) => |
| 120 | + Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 121 | + } as EnvironmentRegistry.EnvironmentRegistry["Service"]); |
| 122 | + const runtime = Atom.runtime( |
| 123 | + Layer.mergeAll( |
| 124 | + Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registryService), |
| 125 | + Layer.mock(PullRequestDiffLoader.PullRequestDiffLoader)({ |
| 126 | + load: () => Effect.die("unused"), |
| 127 | + }), |
| 128 | + ), |
| 129 | + ); |
| 130 | + const atoms = createPullRequestEnvironmentAtoms(runtime); |
| 131 | + const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (value) => |
| 132 | + Effect.sync(() => value.dispose()), |
| 133 | + ); |
| 134 | + const ref = { |
| 135 | + environmentId: target.environmentId, |
| 136 | + input: { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 1 }, |
| 137 | + }; |
| 138 | + const activity = atoms.activity(ref); |
| 139 | + const detail = atoms.detail(ref); |
| 140 | + let unmountActivity = registry.mount(activity); |
| 141 | + yield* Effect.addFinalizer(() => Effect.sync(() => unmountActivity())); |
| 142 | + const unmountDetail = registry.mount(detail); |
| 143 | + yield* Effect.addFinalizer(() => Effect.sync(unmountDetail)); |
| 144 | + yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true }); |
| 145 | + yield* AtomRegistry.getResult(registry, detail, { suspendOnWaiting: true }); |
| 146 | + expect(reads).toBe(1); |
| 147 | + |
| 148 | + // Older than the server activity TTL: a turn must still cause no activity RPC. |
| 149 | + vi.advanceTimersByTime(61_000); |
| 150 | + yield* TestClock.adjust("61 seconds"); |
| 151 | + for (let turn = 1; turn <= 3; turn++) { |
| 152 | + detailRefreshed = Latch.makeUnsafe(); |
| 153 | + yield* SubscriptionRef.set(turns, turn); |
| 154 | + yield* detailRefreshed.await; |
| 155 | + expect(reads).toBe(1); |
| 156 | + } |
| 157 | + if (server === "current") yield* SubscriptionRef.set(edits, 1); |
| 158 | + else registry.refresh(activity); // Missing notifications never prevent explicit reads. |
| 159 | + yield* activityRead.await; |
| 160 | + yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true }); |
| 161 | + expect(reads).toBe(2); |
| 162 | + |
| 163 | + unmountActivity(); |
| 164 | + yield* Effect.yieldNow; |
| 165 | + vi.advanceTimersByTime(6 * 60_000); |
| 166 | + yield* TestClock.adjust("6 minutes"); |
| 167 | + unmountActivity = registry.mount(activity); |
| 168 | + yield* AtomRegistry.getResult(registry, activity, { suspendOnWaiting: true }); |
| 169 | + expect(reads).toBe(3); |
| 170 | + }), |
| 171 | + ), |
| 172 | +); |
0 commit comments