|
3 | 3 | EnvironmentId, |
4 | 4 | type TerminalAttachInput, |
5 | 5 | type TerminalAttachStreamEvent, |
| 6 | + type TerminalMetadataStreamEvent, |
| 7 | + type TerminalSummary, |
6 | 8 | WS_METHODS, |
7 | 9 | } from "@t3tools/contracts"; |
8 | 10 | import * as Cause from "effect/Cause"; |
@@ -388,6 +390,83 @@ describe("finite environment subscription lifecycle", () => { |
388 | 390 | }); |
389 | 391 |
|
390 | 392 | describe("environment query lifecycle", () => { |
| 393 | + it.effect( |
| 394 | + "terminal metadata waits for snapshots and retains known sessions across restarts", |
| 395 | + () => |
| 396 | + Effect.gen(function* () { |
| 397 | + const harness = yield* makeEnvironmentQueryHarness(Effect.void); |
| 398 | + const started = yield* Queue.unbounded<void>(); |
| 399 | + const events = yield* Queue.unbounded<TerminalMetadataStreamEvent>(); |
| 400 | + const receivedCheckout = yield* Deferred.make<void>(); |
| 401 | + const receivedEmpty = yield* Deferred.make<void>(); |
| 402 | + const checkout: TerminalSummary = { |
| 403 | + threadId: "thread-1", |
| 404 | + terminalId: "terminal-1", |
| 405 | + cwd: "/repo/root/packages/app", |
| 406 | + worktreePath: null, |
| 407 | + status: "running", |
| 408 | + pid: 123, |
| 409 | + exitCode: null, |
| 410 | + exitSignal: null, |
| 411 | + hasRunningSubprocess: false, |
| 412 | + label: "Terminal 1", |
| 413 | + updatedAt: "2026-04-01T00:00:00.000Z", |
| 414 | + }; |
| 415 | + const client = { |
| 416 | + [WS_METHODS.subscribeTerminalMetadata]: () => |
| 417 | + Stream.fromEffect(Queue.offer(started, undefined)).pipe( |
| 418 | + Stream.drain, |
| 419 | + Stream.concat(Stream.fromQueue(events)), |
| 420 | + ), |
| 421 | + } as unknown as RpcSession.RpcSession["client"]; |
| 422 | + const session = { ...QUERY_RPC_SESSION, client }; |
| 423 | + yield* SubscriptionRef.set(harness.supervisorSession, Option.some(session)); |
| 424 | + const atoms = createTerminalEnvironmentAtoms(harness.runtime); |
| 425 | + const atom = atoms.metadata({ |
| 426 | + environmentId: QUERY_ENVIRONMENT.environmentId, |
| 427 | + input: null, |
| 428 | + }); |
| 429 | + const registry = AtomRegistry.make(); |
| 430 | + const observed: Array<ReadonlyArray<TerminalSummary>> = []; |
| 431 | + const unsubscribe = registry.subscribe( |
| 432 | + atom, |
| 433 | + (result) => { |
| 434 | + if (!AsyncResult.isSuccess(result)) return; |
| 435 | + observed.push(result.value); |
| 436 | + if (result.value.length > 0) Deferred.doneUnsafe(receivedCheckout, Exit.void); |
| 437 | + else Deferred.doneUnsafe(receivedEmpty, Exit.void); |
| 438 | + }, |
| 439 | + { immediate: true }, |
| 440 | + ); |
| 441 | + yield* Effect.addFinalizer(() => |
| 442 | + Effect.sync(() => { |
| 443 | + unsubscribe(); |
| 444 | + registry.dispose(); |
| 445 | + }), |
| 446 | + ); |
| 447 | + |
| 448 | + yield* Queue.take(started); |
| 449 | + expect(selectEnvironmentQueryData(registry.get(atom))).toBeNull(); |
| 450 | + expect(observed).toEqual([]); |
| 451 | + |
| 452 | + yield* Queue.offer(events, { type: "snapshot", terminals: [checkout] }); |
| 453 | + yield* Deferred.await(receivedCheckout); |
| 454 | + expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]); |
| 455 | + |
| 456 | + yield* SubscriptionRef.set(harness.supervisorSession, Option.some({ ...session })); |
| 457 | + yield* Queue.take(started); |
| 458 | + expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]); |
| 459 | + |
| 460 | + yield* SubscriptionRef.update(harness.streamGeneration, (generation) => generation + 1); |
| 461 | + yield* Queue.take(started); |
| 462 | + expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]); |
| 463 | + |
| 464 | + yield* Queue.offer(events, { type: "snapshot", terminals: [] }); |
| 465 | + yield* Deferred.await(receivedEmpty); |
| 466 | + expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([]); |
| 467 | + }), |
| 468 | + ); |
| 469 | + |
391 | 470 | it.effect("terminal attach resubscribes with resume only after the stream replied", () => |
392 | 471 | Effect.gen(function* () { |
393 | 472 | const harness = yield* makeEnvironmentQueryHarness(Effect.void); |
|
0 commit comments