|
1 | 1 | import { describe, expect, it, vi } from "@effect/vitest"; |
2 | 2 | import { |
3 | 3 | EnvironmentId, |
| 4 | + type PreviewEvent, |
4 | 5 | type TerminalAttachInput, |
5 | 6 | type TerminalAttachStreamEvent, |
6 | 7 | type TerminalMetadataStreamEvent, |
@@ -32,6 +33,7 @@ import * as EnvironmentRegistry from "../connection/registry.ts"; |
32 | 33 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
33 | 34 | import { EnvironmentRpcUnavailableError } from "../rpc/client.ts"; |
34 | 35 | import type * as RpcSession from "../rpc/session.ts"; |
| 36 | +import { createPreviewEnvironmentAtoms } from "./preview.ts"; |
35 | 37 | import type { QueryPersistence } from "./queryPersistence.ts"; |
36 | 38 | import { createTerminalEnvironmentAtoms } from "./terminal.ts"; |
37 | 39 | import { |
@@ -96,17 +98,21 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness" |
96 | 98 | idleTtlMs, |
97 | 99 | persistence, |
98 | 100 | initialState, |
| 101 | + rpcSession, |
99 | 102 | isFinal, |
100 | 103 | }: { |
101 | 104 | readonly refreshTrigger?: Atom.Atom<unknown>; |
102 | 105 | readonly isFinal?: (value: A) => boolean; |
103 | 106 | readonly idleTtlMs?: number; |
104 | 107 | readonly persistence?: QueryPersistence<A>; |
105 | 108 | readonly initialState?: SupervisorConnectionState; |
| 109 | + readonly rpcSession?: RpcSession.RpcSession; |
106 | 110 | } = {}, |
107 | 111 | ) { |
108 | 112 | const supervisorState = yield* SubscriptionRef.make(initialState ?? queryConnectionState()); |
109 | | - const supervisorSession = yield* SubscriptionRef.make(Option.some(QUERY_RPC_SESSION)); |
| 113 | + const supervisorSession = yield* SubscriptionRef.make( |
| 114 | + Option.some(rpcSession ?? QUERY_RPC_SESSION), |
| 115 | + ); |
110 | 116 | const streamGeneration = yield* SubscriptionRef.make(0); |
111 | 117 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
112 | 118 | target: QUERY_ENVIRONMENT, |
@@ -320,6 +326,82 @@ describe("environmentRpcKey", () => { |
320 | 326 | }); |
321 | 327 | }); |
322 | 328 |
|
| 329 | +describe("preview event subscriptions", () => { |
| 330 | + it.effect("delivers every open and close when RPC events arrive in batches", () => |
| 331 | + Effect.scoped( |
| 332 | + Effect.gen(function* () { |
| 333 | + const opened: PreviewEvent[] = ["thread-a", "thread-b"].map((threadId, index) => ({ |
| 334 | + type: "opened", |
| 335 | + threadId, |
| 336 | + tabId: `tab-${index}`, |
| 337 | + serverEpoch: "epoch-1", |
| 338 | + revision: index + 1, |
| 339 | + createdAt: "2026-10-02T00:00:00.000Z", |
| 340 | + snapshot: { |
| 341 | + threadId, |
| 342 | + tabId: `tab-${index}`, |
| 343 | + navStatus: { _tag: "Idle" }, |
| 344 | + canGoBack: false, |
| 345 | + canGoForward: false, |
| 346 | + updatedAt: "2026-10-02T00:00:00.000Z", |
| 347 | + }, |
| 348 | + })); |
| 349 | + const closed: PreviewEvent[] = opened.map((event, index) => ({ |
| 350 | + type: "closed", |
| 351 | + threadId: event.threadId, |
| 352 | + tabId: event.tabId, |
| 353 | + serverEpoch: event.serverEpoch, |
| 354 | + revision: opened.length + index + 1, |
| 355 | + createdAt: event.createdAt, |
| 356 | + })); |
| 357 | + const releaseEvents = yield* Deferred.make<void>(); |
| 358 | + // Each iterable is one RPC chunk; separate emissions would hide lost events. |
| 359 | + const stream = Stream.fromEffect(Deferred.await(releaseEvents)).pipe( |
| 360 | + Stream.flatMap(() => |
| 361 | + Stream.fromIterable(opened).pipe( |
| 362 | + Stream.concat(Stream.fromIterable(closed)), |
| 363 | + Stream.concat(Stream.never), |
| 364 | + ), |
| 365 | + ), |
| 366 | + ); |
| 367 | + const harness = yield* makeEnvironmentQueryHarness(Effect.void, { |
| 368 | + rpcSession: { |
| 369 | + client: { [WS_METHODS.subscribePreviewEvents]: () => stream }, |
| 370 | + } as unknown as RpcSession.RpcSession, |
| 371 | + }); |
| 372 | + const atom = createPreviewEnvironmentAtoms(harness.runtime).events({ |
| 373 | + environmentId: QUERY_ENVIRONMENT.environmentId, |
| 374 | + input: {}, |
| 375 | + }); |
| 376 | + const registry = AtomRegistry.make(); |
| 377 | + const observed: PreviewEvent[] = []; |
| 378 | + const sawLast = yield* Deferred.make<void>(); |
| 379 | + const unsubscribe = registry.subscribe( |
| 380 | + atom, |
| 381 | + (result) => { |
| 382 | + if (!AsyncResult.isSuccess(result)) return; |
| 383 | + observed.push(result.value); |
| 384 | + if (result.value === closed[closed.length - 1]) { |
| 385 | + Deferred.doneUnsafe(sawLast, Exit.void); |
| 386 | + } |
| 387 | + }, |
| 388 | + { immediate: true }, |
| 389 | + ); |
| 390 | + yield* Effect.addFinalizer(() => |
| 391 | + Effect.sync(() => { |
| 392 | + unsubscribe(); |
| 393 | + registry.dispose(); |
| 394 | + }), |
| 395 | + ); |
| 396 | + |
| 397 | + yield* Deferred.succeed(releaseEvents, undefined); |
| 398 | + yield* Deferred.await(sawLast); |
| 399 | + expect(observed).toEqual([...opened, ...closed]); |
| 400 | + }), |
| 401 | + ), |
| 402 | + ); |
| 403 | +}); |
| 404 | + |
323 | 405 | describe("finite environment subscription lifecycle", () => { |
324 | 406 | it.effect( |
325 | 407 | "exposes early values and settles on the terminal value without waiting for a reconnect", |
|
0 commit comments