The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | type RelayClientInstallProgressEvent, |
| 4 | type ServerConfig, |
| 5 | type ServerLifecycleStreamEvent, |
| 6 | WS_METHODS, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { describe, expect, it } from "@effect/vitest"; |
| 9 | import * as Cause from "effect/Cause"; |
| 10 | import * as Clock from "effect/Clock"; |
| 11 | import * as Deferred from "effect/Deferred"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Fiber from "effect/Fiber"; |
| 14 | import * as Option from "effect/Option"; |
| 15 | import * as Queue from "effect/Queue"; |
| 16 | import * as Ref from "effect/Ref"; |
| 17 | import * as Schema from "effect/Schema"; |
| 18 | import * as Stream from "effect/Stream"; |
| 19 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 20 | import * as TestClock from "effect/testing/TestClock"; |
| 21 | import { RpcClientError } from "effect/rpc"; |
| 22 | |
| 23 | import { |
| 24 | AVAILABLE_CONNECTION_STATE, |
| 25 | PrimaryConnectionTarget, |
| 26 | type PreparedConnection, |
| 27 | type SupervisorConnectionState, |
| 28 | } from "../connection/model.ts"; |
| 29 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 30 | import * as RpcSession from "../rpc/session.ts"; |
| 31 | import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
| 32 | import { runStream, subscribe, subscribeDynamicWithSession, refreshVcsBranch } from "./client.ts"; |
| 33 | |
| 34 | const TARGET = new PrimaryConnectionTarget({ |
| 35 | environmentId: EnvironmentId.make("environment-1"), |
| 36 | label: "Test environment", |
| 37 | httpBaseUrl: "https://environment.example.test", |
| 38 | wsBaseUrl: "wss://environment.example.test", |
| 39 | }); |
| 40 | |
| 41 | const INSTALL_CHECKING: RelayClientInstallProgressEvent = { |
| 42 | type: "progress", |
| 43 | stage: "checking", |
| 44 | }; |
| 45 | const INSTALL_DOWNLOADING: RelayClientInstallProgressEvent = { |
| 46 | type: "progress", |
| 47 | stage: "downloading", |
| 48 | }; |
| 49 | |
| 50 | const decodeNumber = Schema.decodeUnknownEffect(Schema.Number); |
| 51 | |
| 52 | function session(client: WsRpcProtocolClient): RpcSession.RpcSession { |
| 53 | return { |
| 54 | client, |
| 55 | initialConfig: Effect.never, |
| 56 | subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input), |
| 57 | ready: Effect.void, |
| 58 | probe: Effect.void, |
| 59 | inboundFrames: Effect.succeed(0), |
| 60 | closed: Effect.never, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () { |
| 65 | const state = yield* SubscriptionRef.make<SupervisorConnectionState>(AVAILABLE_CONNECTION_STATE); |
| 66 | const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>( |
| 67 | Option.none(), |
| 68 | ); |
| 69 | const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none()); |
| 70 | const retryCount = yield* Ref.make(0); |
| 71 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 72 | target: TARGET, |
| 73 | state, |
| 74 | session: activeSession, |
| 75 | prepared, |
| 76 | connect: Effect.void, |
| 77 | disconnect: Effect.void, |
| 78 | retryNow: Ref.update(retryCount, (count) => count + 1), |
| 79 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 80 | return { |
| 81 | activeSession, |
| 82 | retryCount, |
| 83 | supervisor, |
| 84 | }; |
| 85 | }); |
| 86 | |
| 87 | describe("environment RPC", () => { |
| 88 | it.effect("binds finite streaming commands to one active session", () => |
| 89 | Effect.gen(function* () { |
| 90 | const firstEvents = yield* Queue.unbounded<RelayClientInstallProgressEvent>(); |
| 91 | const secondEvents = yield* Queue.unbounded<RelayClientInstallProgressEvent>(); |
| 92 | const firstClient = { |
| 93 | [WS_METHODS.cloudInstallRelayClient]: () => Stream.fromQueue(firstEvents), |
| 94 | } as unknown as WsRpcProtocolClient; |
| 95 | const secondClient = { |
| 96 | [WS_METHODS.cloudInstallRelayClient]: () => Stream.fromQueue(secondEvents), |
| 97 | } as unknown as WsRpcProtocolClient; |
| 98 | const { activeSession, supervisor } = yield* makeHarness(); |
| 99 | |
| 100 | yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 101 | const resultFiber = yield* runStream(WS_METHODS.cloudInstallRelayClient, {}).pipe( |
| 102 | Stream.take(2), |
| 103 | Stream.runCollect, |
| 104 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 105 | Effect.forkChild, |
| 106 | ); |
| 107 | yield* Effect.yieldNow; |
| 108 | |
| 109 | yield* Queue.offer(firstEvents, INSTALL_CHECKING); |
| 110 | yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 111 | yield* Queue.offer(secondEvents, INSTALL_DOWNLOADING); |
| 112 | yield* Queue.offer(firstEvents, INSTALL_DOWNLOADING); |
| 113 | |
| 114 | expect(yield* Fiber.join(resultFiber)).toEqual([INSTALL_CHECKING, INSTALL_DOWNLOADING]); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | it.effect("switches durable subscriptions when the supervisor replaces the session", () => |
| 119 | Effect.gen(function* () { |
| 120 | const subscriptions: string[] = []; |
| 121 | const subscriptionChanges = yield* Queue.make<void>(); |
| 122 | const firstClient = { |
| 123 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 124 | subscriptions.push("first"); |
| 125 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 126 | return Stream.never; |
| 127 | }, |
| 128 | } as unknown as WsRpcProtocolClient; |
| 129 | const secondClient = { |
| 130 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 131 | subscriptions.push("second"); |
| 132 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 133 | return Stream.never; |
| 134 | }, |
| 135 | } as unknown as WsRpcProtocolClient; |
| 136 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 137 | const awaitSubscriptions = Effect.fn("TestEnvironmentRpc.awaitSubscriptions")(function* ( |
| 138 | count: number, |
| 139 | ) { |
| 140 | for (;;) { |
| 141 | if (subscriptions.length >= count) { |
| 142 | return; |
| 143 | } |
| 144 | yield* Queue.take(subscriptionChanges); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | const subscriptionFiber = yield* subscribe(WS_METHODS.subscribeTerminalMetadata, {}).pipe( |
| 149 | Stream.runDrain, |
| 150 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 151 | Effect.forkChild, |
| 152 | ); |
| 153 | yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 154 | yield* awaitSubscriptions(1); |
| 155 | yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 156 | yield* awaitSubscriptions(2); |
| 157 | yield* Fiber.interrupt(subscriptionFiber); |
| 158 | |
| 159 | expect(subscriptions).toEqual(["first", "second"]); |
| 160 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 161 | }), |
| 162 | ); |
| 163 | |
| 164 | it.effect("keeps the producer session on an old value buffered across a session switch", () => |
| 165 | Effect.gen(function* () { |
| 166 | const firstSubscribed = yield* Deferred.make<void>(); |
| 167 | const secondSubscribed = yield* Deferred.make<void>(); |
| 168 | const firstValueBlocked = yield* Deferred.make<void>(); |
| 169 | const releaseFirstValue = yield* Deferred.make<void>(); |
| 170 | const firstValue = { source: "first", index: 1 } as unknown as ServerLifecycleStreamEvent; |
| 171 | const bufferedFirstValue = { |
| 172 | source: "first", |
| 173 | index: 2, |
| 174 | } as unknown as ServerLifecycleStreamEvent; |
| 175 | const secondValue = { source: "second", index: 1 } as unknown as ServerLifecycleStreamEvent; |
| 176 | const firstClient = { |
| 177 | [WS_METHODS.subscribeServerLifecycle]: () => |
| 178 | Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe( |
| 179 | Stream.drain, |
| 180 | Stream.concat(Stream.fromIterable([firstValue, bufferedFirstValue])), |
| 181 | Stream.concat(Stream.never), |
| 182 | ), |
| 183 | } as unknown as WsRpcProtocolClient; |
| 184 | const secondClient = { |
| 185 | [WS_METHODS.subscribeServerLifecycle]: () => |
| 186 | Stream.fromEffect(Deferred.succeed(secondSubscribed, undefined)).pipe( |
| 187 | Stream.drain, |
| 188 | Stream.concat(Stream.make(secondValue)), |
| 189 | Stream.concat(Stream.never), |
| 190 | ), |
| 191 | } as unknown as WsRpcProtocolClient; |
| 192 | const firstSession = session(firstClient); |
| 193 | const secondSession = session(secondClient); |
| 194 | const { activeSession, supervisor } = yield* makeHarness(); |
| 195 | |
| 196 | const resultFiber = yield* subscribeDynamicWithSession( |
| 197 | WS_METHODS.subscribeServerLifecycle, |
| 198 | () => Effect.succeed({}), |
| 199 | ).pipe( |
| 200 | Stream.mapEffect(([producerSession, value]) => |
| 201 | value === firstValue |
| 202 | ? Deferred.succeed(firstValueBlocked, undefined).pipe( |
| 203 | Effect.andThen(Deferred.await(releaseFirstValue)), |
| 204 | Effect.as([producerSession, value] as const), |
| 205 | ) |
| 206 | : Effect.succeed([producerSession, value] as const), |
| 207 | ), |
| 208 | Stream.take(3), |
| 209 | Stream.runCollect, |
| 210 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 211 | Effect.forkChild, |
| 212 | ); |
| 213 | |
| 214 | yield* SubscriptionRef.set(activeSession, Option.some(firstSession)); |
| 215 | yield* Deferred.await(firstSubscribed); |
| 216 | yield* Deferred.await(firstValueBlocked); |
| 217 | yield* SubscriptionRef.set(activeSession, Option.some(secondSession)); |
| 218 | yield* Deferred.await(secondSubscribed); |
| 219 | yield* Deferred.succeed(releaseFirstValue, undefined); |
| 220 | |
| 221 | const result = yield* Fiber.join(resultFiber); |
| 222 | expect(result).toEqual([ |
| 223 | [firstSession, firstValue], |
| 224 | [firstSession, bufferedFirstValue], |
| 225 | [secondSession, secondValue], |
| 226 | ]); |
| 227 | }), |
| 228 | ); |
| 229 | |
| 230 | it.effect("keeps durable subscriptions alive across a transport failure and new session", () => |
| 231 | Effect.gen(function* () { |
| 232 | const subscriptions: string[] = []; |
| 233 | const subscriptionChanges = yield* Queue.make<void>(); |
| 234 | const firstClient = { |
| 235 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 236 | subscriptions.push("first"); |
| 237 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 238 | return Stream.fail( |
| 239 | new RpcClientError.RpcClientError({ |
| 240 | reason: new RpcClientError.RpcClientDefect({ |
| 241 | message: "socket closed", |
| 242 | cause: new Error("socket closed"), |
| 243 | }), |
| 244 | }), |
| 245 | ); |
| 246 | }, |
| 247 | } as unknown as WsRpcProtocolClient; |
| 248 | const secondClient = { |
| 249 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 250 | subscriptions.push("second"); |
| 251 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 252 | return Stream.never; |
| 253 | }, |
| 254 | } as unknown as WsRpcProtocolClient; |
| 255 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 256 | |
| 257 | const subscriptionFiber = yield* subscribe(WS_METHODS.subscribeTerminalMetadata, {}).pipe( |
| 258 | Stream.runDrain, |
| 259 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 260 | Effect.forkChild, |
| 261 | ); |
| 262 | yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 263 | while (subscriptions.length < 1) { |
| 264 | yield* Queue.take(subscriptionChanges); |
| 265 | } |
| 266 | yield* SubscriptionRef.set(activeSession, Option.none()); |
| 267 | yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 268 | |
| 269 | while (subscriptions.length < 2) { |
| 270 | yield* Queue.take(subscriptionChanges); |
| 271 | } |
| 272 | // The pending same-session retry must not resubscribe the replaced session. |
| 273 | yield* TestClock.adjust("1 minute"); |
| 274 | yield* Fiber.interrupt(subscriptionFiber); |
| 275 | |
| 276 | expect(subscriptions).toEqual(["first", "second"]); |
| 277 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 278 | }), |
| 279 | ); |
| 280 | |
| 281 | // The session stays healthy (`closed` never resolves), as when one stream chunk fails to decode, |
| 282 | // so the supervisor will not replace it and only resubscribing restores delivery. |
| 283 | const resumesOnTheSameSession = Effect.fn("TestEnvironmentRpc.resumesOnTheSameSession")( |
| 284 | function* (streamFailure: Stream.Stream<never, unknown>) { |
| 285 | const resumedValue = { source: "resumed" } as unknown as ServerLifecycleStreamEvent; |
| 286 | const firstSubscribed = yield* Deferred.make<void>(); |
| 287 | let subscriptions = 0; |
| 288 | const client = { |
| 289 | [WS_METHODS.subscribeServerLifecycle]: () => { |
| 290 | subscriptions += 1; |
| 291 | return subscriptions === 1 |
| 292 | ? Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe( |
| 293 | Stream.drain, |
| 294 | Stream.concat(streamFailure), |
| 295 | ) |
| 296 | : Stream.make(resumedValue).pipe(Stream.concat(Stream.never)); |
| 297 | }, |
| 298 | } as unknown as WsRpcProtocolClient; |
| 299 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 300 | |
| 301 | yield* SubscriptionRef.set(activeSession, Option.some(session(client))); |
| 302 | const resultFiber = yield* subscribe(WS_METHODS.subscribeServerLifecycle, {}).pipe( |
| 303 | Stream.runHead, |
| 304 | Effect.timeoutOption("2 seconds"), |
| 305 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 306 | Effect.forkChild, |
| 307 | ); |
| 308 | yield* Deferred.await(firstSubscribed); |
| 309 | yield* TestClock.adjust("2 seconds"); |
| 310 | |
| 311 | expect( |
| 312 | yield* Fiber.join(resultFiber), |
| 313 | "durable subscription stopped delivering after its stream ended on a healthy session", |
| 314 | ).toEqual(Option.some(Option.some(resumedValue))); |
| 315 | expect(subscriptions).toBe(2); |
| 316 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 317 | }, |
| 318 | ); |
| 319 | |
| 320 | it.effect("resubscribes on the same session after a transport failure", () => |
| 321 | resumesOnTheSameSession( |
| 322 | Stream.fail( |
| 323 | new RpcClientError.RpcClientError({ |
| 324 | reason: new RpcClientError.RpcClientDefect({ |
| 325 | message: "stream failed", |
| 326 | cause: new Error("stream failed"), |
| 327 | }), |
| 328 | }), |
| 329 | ), |
| 330 | ), |
| 331 | ); |
| 332 | |
| 333 | it.effect("resubscribes on the same session after a stream chunk fails to decode", () => |
| 334 | // Mirrors RpcClient, which decodes each chunk with `Effect.orDie`. |
| 335 | resumesOnTheSameSession( |
| 336 | Stream.fromEffect(decodeNumber("not a number").pipe(Effect.orDie)).pipe(Stream.drain), |
| 337 | ), |
| 338 | ); |
| 339 | |
| 340 | it.effect( |
| 341 | "backs off a same-session stream that keeps failing to decode after its first value", |
| 342 | () => |
| 343 | Effect.gen(function* () { |
| 344 | // Like the shell stream: a valid value (metadata) arrives, then a chunk fails to decode. |
| 345 | const value = { source: "metadata" } as unknown as ServerLifecycleStreamEvent; |
| 346 | const decodeDefect = decodeNumber("not a number").pipe(Effect.orDie); |
| 347 | const subscriptions = yield* Queue.unbounded<{ |
| 348 | readonly session: "first" | "second"; |
| 349 | readonly at: number; |
| 350 | }>(); |
| 351 | let secondSubscriptions = 0; |
| 352 | const failingClient = (name: "first" | "second") => |
| 353 | ({ |
| 354 | [WS_METHODS.subscribeServerLifecycle]: () => |
| 355 | Stream.unwrap( |
| 356 | Effect.gen(function* () { |
| 357 | yield* Queue.offer(subscriptions, { |
| 358 | session: name, |
| 359 | at: yield* Clock.currentTimeMillis, |
| 360 | }); |
| 361 | if (name === "second" && ++secondSubscriptions > 2) return Stream.never; |
| 362 | return Stream.make(value).pipe( |
| 363 | Stream.concat(Stream.fromEffect(decodeDefect).pipe(Stream.drain)), |
| 364 | ); |
| 365 | }), |
| 366 | ), |
| 367 | }) as unknown as WsRpcProtocolClient; |
| 368 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 369 | |
| 370 | yield* SubscriptionRef.set(activeSession, Option.some(session(failingClient("first")))); |
| 371 | const subscriptionFiber = yield* subscribe( |
| 372 | WS_METHODS.subscribeServerLifecycle, |
| 373 | {}, |
| 374 | { |
| 375 | onExpectedFailure: () => Effect.void, |
| 376 | retryExpectedFailureAfter: "250 millis", |
| 377 | }, |
| 378 | ).pipe( |
| 379 | Stream.runDrain, |
| 380 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 381 | Effect.forkChild, |
| 382 | ); |
| 383 | |
| 384 | const expectedFirstSessionDelays = [250, 500, 1_000, 2_000, 4_000, 8_000, 16_000, 30_000]; |
| 385 | const firstSessionDelays: number[] = []; |
| 386 | let previous = yield* Queue.take(subscriptions); |
| 387 | for (const delay of expectedFirstSessionDelays) { |
| 388 | yield* TestClock.adjust(delay); |
| 389 | const next = yield* Queue.take(subscriptions); |
| 390 | firstSessionDelays.push(next.at - previous.at); |
| 391 | previous = next; |
| 392 | } |
| 393 | |
| 394 | // A replacement session starts a fresh backoff. |
| 395 | yield* SubscriptionRef.set(activeSession, Option.some(session(failingClient("second")))); |
| 396 | const replaced = yield* Queue.take(subscriptions); |
| 397 | yield* TestClock.adjust(250); |
| 398 | const retried = yield* Queue.take(subscriptions); |
| 399 | yield* Fiber.interrupt(subscriptionFiber); |
| 400 | |
| 401 | expect( |
| 402 | firstSessionDelays, |
| 403 | "same-session decode retries reset to the base delay after every first value", |
| 404 | ).toEqual(expectedFirstSessionDelays); |
| 405 | expect([replaced.session, retried.session]).toEqual(["second", "second"]); |
| 406 | expect(retried.at - replaced.at).toBe(250); |
| 407 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 408 | }), |
| 409 | ); |
| 410 | |
| 411 | it.effect("surfaces domain subscription failures without reconnecting", () => |
| 412 | Effect.gen(function* () { |
| 413 | const domainError = new Error("terminal subscription rejected"); |
| 414 | const client = { |
| 415 | [WS_METHODS.subscribeTerminalMetadata]: () => Stream.fail(domainError), |
| 416 | } as unknown as WsRpcProtocolClient; |
| 417 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 418 | |
| 419 | yield* SubscriptionRef.set(activeSession, Option.some(session(client))); |
| 420 | const error = yield* subscribe(WS_METHODS.subscribeTerminalMetadata, {}).pipe( |
| 421 | Stream.runDrain, |
| 422 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 423 | Effect.flip, |
| 424 | ); |
| 425 | |
| 426 | expect(error).toBe(domainError); |
| 427 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 428 | }), |
| 429 | ); |
| 430 | |
| 431 | it.effect("keeps handled domain failures dormant until a replacement session arrives", () => |
| 432 | Effect.gen(function* () { |
| 433 | const domainError = new Error("terminal subscription rejected"); |
| 434 | const subscriptions: string[] = []; |
| 435 | const subscriptionChanges = yield* Queue.make<void>(); |
| 436 | const observedFailures: Error[] = []; |
| 437 | const failureObserved = yield* Deferred.make<void>(); |
| 438 | const firstClient = { |
| 439 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 440 | subscriptions.push("first"); |
| 441 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 442 | return Stream.fail(domainError); |
| 443 | }, |
| 444 | } as unknown as WsRpcProtocolClient; |
| 445 | const secondClient = { |
| 446 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 447 | subscriptions.push("second"); |
| 448 | Queue.offerUnsafe(subscriptionChanges, undefined); |
| 449 | return Stream.never; |
| 450 | }, |
| 451 | } as unknown as WsRpcProtocolClient; |
| 452 | const { activeSession, retryCount, supervisor } = yield* makeHarness(); |
| 453 | |
| 454 | yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 455 | const subscriptionFiber = yield* subscribe( |
| 456 | WS_METHODS.subscribeTerminalMetadata, |
| 457 | {}, |
| 458 | { |
| 459 | onExpectedFailure: (cause) => |
| 460 | Effect.sync(() => { |
| 461 | observedFailures.push(Cause.squash(cause) as Error); |
| 462 | Deferred.doneUnsafe(failureObserved, Effect.void); |
| 463 | }), |
| 464 | }, |
| 465 | ).pipe( |
| 466 | Stream.runDrain, |
| 467 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 468 | Effect.forkChild, |
| 469 | ); |
| 470 | yield* Deferred.await(failureObserved); |
| 471 | |
| 472 | expect(subscriptions).toEqual(["first"]); |
| 473 | expect(observedFailures).toEqual([domainError]); |
| 474 | |
| 475 | yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 476 | while (subscriptions.length < 2) { |
| 477 | yield* Queue.take(subscriptionChanges); |
| 478 | } |
| 479 | yield* Fiber.interrupt(subscriptionFiber); |
| 480 | |
| 481 | expect(subscriptions).toEqual(["first", "second"]); |
| 482 | expect(yield* Ref.get(retryCount)).toBe(0); |
| 483 | }), |
| 484 | ); |
| 485 | |
| 486 | it.effect.each(["input", "stream"] as const)( |
| 487 | "reports %s subscription defects and resubscribes on the next session", |
| 488 | (where) => |
| 489 | Effect.gen(function* () { |
| 490 | const defect = new Error("subscription invariant failed"); |
| 491 | let expectedFailureCount = 0; |
| 492 | let inputs = 0; |
| 493 | let streams = 0; |
| 494 | const observedDefects: unknown[] = []; |
| 495 | const defectObserved = yield* Deferred.make<void>(); |
| 496 | const resubscribed = yield* Deferred.make<void>(); |
| 497 | const firstClient = { |
| 498 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 499 | streams += 1; |
| 500 | return where === "stream" ? Stream.die(defect) : Stream.never; |
| 501 | }, |
| 502 | } as unknown as WsRpcProtocolClient; |
| 503 | const secondClient = { |
| 504 | [WS_METHODS.subscribeTerminalMetadata]: () => { |
| 505 | Deferred.doneUnsafe(resubscribed, Effect.void); |
| 506 | return Stream.never; |
| 507 | }, |
| 508 | } as unknown as WsRpcProtocolClient; |
| 509 | const { activeSession, supervisor } = yield* makeHarness(); |
| 510 | |
| 511 | yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 512 | const subscriptionFiber = yield* subscribeDynamicWithSession( |
| 513 | WS_METHODS.subscribeTerminalMetadata, |
| 514 | () => |
| 515 | Effect.suspend(() => { |
| 516 | inputs += 1; |
| 517 | return where === "input" && inputs === 1 ? Effect.die(defect) : Effect.succeed({}); |
| 518 | }), |
| 519 | { |
| 520 | onDefect: (cause) => |
| 521 | Effect.sync(() => { |
| 522 | observedDefects.push(Cause.squash(cause)); |
| 523 | Deferred.doneUnsafe(defectObserved, Effect.void); |
| 524 | }), |
| 525 | onExpectedFailure: () => |
| 526 | Effect.sync(() => { |
| 527 | expectedFailureCount += 1; |
| 528 | }), |
| 529 | retryExpectedFailureAfter: "250 millis", |
| 530 | }, |
| 531 | ).pipe( |
| 532 | Stream.runDrain, |
| 533 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 534 | Effect.forkChild, |
| 535 | ); |
| 536 | yield* Deferred.await(defectObserved); |
| 537 | |
| 538 | expect(inputs).toBe(1); |
| 539 | expect(streams).toBe(where === "input" ? 0 : 1); |
| 540 | expect(expectedFailureCount).toBe(0); |
| 541 | expect(observedDefects).toEqual([defect]); |
| 542 | |
| 543 | // Let the defect settle before the next session arrives, as it does on a real device. |
| 544 | yield* TestClock.adjust("1 second"); |
| 545 | expect(subscriptionFiber.pollUnsafe()).toBeUndefined(); |
| 546 | yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 547 | yield* Deferred.await(resubscribed); |
| 548 | yield* Fiber.interrupt(subscriptionFiber); |
| 549 | |
| 550 | expect(inputs).toBe(2); |
| 551 | }), |
| 552 | ); |
| 553 | }); |
| 554 | |
| 555 | it.effect.each([true, false])( |
| 556 | "refreshes branches using the connected server capability (%s)", |
| 557 | (supported) => |
| 558 | Effect.gen(function* () { |
| 559 | const { activeSession, supervisor } = yield* makeHarness(); |
| 560 | const inputs: unknown[] = []; |
| 561 | const client = { |
| 562 | [supported ? WS_METHODS.vcsRefreshBranch : WS_METHODS.vcsRefreshStatus]: ( |
| 563 | input: unknown, |
| 564 | ) => { |
| 565 | inputs.push(input); |
| 566 | return Effect.void; |
| 567 | }, |
| 568 | } as unknown as WsRpcProtocolClient; |
| 569 | yield* SubscriptionRef.set( |
| 570 | activeSession, |
| 571 | Option.some({ |
| 572 | ...session(client), |
| 573 | initialConfig: Effect.succeed({ |
| 574 | environment: { capabilities: supported ? { vcsBranchStatus: true } : {} }, |
| 575 | } as ServerConfig), |
| 576 | }), |
| 577 | ); |
| 578 | expect( |
| 579 | yield* refreshVcsBranch({ cwd: "/repo" }).pipe( |
| 580 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 581 | ), |
| 582 | ).toBeUndefined(); |
| 583 | expect(inputs).toEqual(supported ? [{ cwd: "/repo" }] : []); |
| 584 | }), |
| 585 | ); |
| 586 | |
| 587 | it.effect("binds branch capability selection and execution to one session", () => |
| 588 | Effect.gen(function* () { |
| 589 | const { activeSession, supervisor } = yield* makeHarness(); |
| 590 | const configRequested = yield* Deferred.make<void>(); |
| 591 | const config = yield* Deferred.make<ServerConfig>(); |
| 592 | const executed = yield* Deferred.make<void>(); |
| 593 | const original = { |
| 594 | [WS_METHODS.vcsRefreshBranch]: () => |
| 595 | Deferred.succeed(executed, undefined).pipe(Effect.asVoid), |
| 596 | } as unknown as WsRpcProtocolClient; |
| 597 | const replacement = { |
| 598 | [WS_METHODS.vcsRefreshStatus]: () => Effect.succeed({ refName: "replacement" }), |
| 599 | } as unknown as WsRpcProtocolClient; |
| 600 | yield* SubscriptionRef.set( |
| 601 | activeSession, |
| 602 | Option.some({ |
| 603 | ...session(original), |
| 604 | initialConfig: Deferred.succeed(configRequested, undefined).pipe( |
| 605 | Effect.andThen(Deferred.await(config)), |
| 606 | ), |
| 607 | }), |
| 608 | ); |
| 609 | const refresh = yield* refreshVcsBranch({ cwd: "/repo" }).pipe( |
| 610 | Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 611 | Effect.forkChild, |
| 612 | ); |
| 613 | yield* Deferred.await(configRequested); |
| 614 | yield* SubscriptionRef.set(activeSession, Option.some(session(replacement))); |
| 615 | yield* Deferred.succeed(config, { |
| 616 | environment: { capabilities: { vcsBranchStatus: true } }, |
| 617 | } as ServerConfig); |
| 618 | expect(yield* Fiber.join(refresh)).toBeUndefined(); |
| 619 | yield* Deferred.await(executed); |
| 620 | }), |
| 621 | ); |