|
1 | | -import { ThreadId } from "@t3tools/contracts"; |
| 1 | +import { |
| 2 | + CommandId, |
| 3 | + CorrelationId, |
| 4 | + EventId, |
| 5 | + type OrchestrationEvent, |
| 6 | + ThreadId, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { it as effectIt } from "@effect/vitest"; |
2 | 9 | import * as Cause from "effect/Cause"; |
| 10 | +import * as Deferred from "effect/Deferred"; |
3 | 11 | import * as Effect from "effect/Effect"; |
4 | 12 | import * as Exit from "effect/Exit"; |
| 13 | +import * as Fiber from "effect/Fiber"; |
| 14 | +import * as Layer from "effect/Layer"; |
| 15 | +import * as Ref from "effect/Ref"; |
| 16 | +import * as Stream from "effect/Stream"; |
5 | 17 | import { describe, expect, it } from "vite-plus/test"; |
6 | 18 |
|
7 | | -import { logCleanupCauseUnlessInterrupted } from "./ThreadDeletionReactor.ts"; |
| 19 | +import { |
| 20 | + ProviderService, |
| 21 | + type ProviderServiceShape, |
| 22 | +} from "../../provider/Services/ProviderService.ts"; |
| 23 | +import * as TerminalManager from "../../terminal/Manager.ts"; |
| 24 | +import { |
| 25 | + OrchestrationEngineService, |
| 26 | + type OrchestrationEngineShape, |
| 27 | +} from "../Services/OrchestrationEngine.ts"; |
| 28 | +import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts"; |
| 29 | +import { |
| 30 | + logCleanupCauseUnlessInterrupted, |
| 31 | + ThreadDeletionReactorLive, |
| 32 | +} from "./ThreadDeletionReactor.ts"; |
8 | 33 |
|
9 | 34 | describe("logCleanupCauseUnlessInterrupted", () => { |
10 | 35 | const threadId = ThreadId.make("thread-deletion-reactor-test"); |
@@ -36,3 +61,79 @@ describe("logCleanupCauseUnlessInterrupted", () => { |
36 | 61 | } |
37 | 62 | }); |
38 | 63 | }); |
| 64 | + |
| 65 | +describe("ThreadDeletionReactor drain", () => { |
| 66 | + const now = "2026-01-01T00:00:00.000Z"; |
| 67 | + const threadId = ThreadId.make("thread-deletion-reactor-drain"); |
| 68 | + const deletedEvent = (sequence: number): OrchestrationEvent => ({ |
| 69 | + sequence, |
| 70 | + eventId: EventId.make(`evt-deleted-${sequence}`), |
| 71 | + aggregateKind: "thread", |
| 72 | + aggregateId: threadId, |
| 73 | + type: "thread.deleted", |
| 74 | + occurredAt: now, |
| 75 | + commandId: CommandId.make(`cmd-deleted-${sequence}`), |
| 76 | + causationEventId: null, |
| 77 | + correlationId: CorrelationId.make(`cmd-deleted-${sequence}`), |
| 78 | + metadata: {}, |
| 79 | + payload: { threadId, deletedAt: now }, |
| 80 | + }); |
| 81 | + |
| 82 | + effectIt.effect("waits for a published deletion the subscriber has not consumed yet", () => |
| 83 | + Effect.gen(function* () { |
| 84 | + const stops: Array<number> = []; |
| 85 | + const firstCleanupDone = yield* Deferred.make<void>(); |
| 86 | + // The engine has already committed and published sequence 2, but the |
| 87 | + // subscriber has not received it yet: the stream releases it on demand. |
| 88 | + const releaseSecondEvent = yield* Deferred.make<void>(); |
| 89 | + const latestSequence = yield* Ref.make(0); |
| 90 | + const engine = { |
| 91 | + latestSequence: Ref.get(latestSequence), |
| 92 | + streamDomainEvents: Stream.concat( |
| 93 | + Stream.make(deletedEvent(1)), |
| 94 | + Stream.fromEffect(Deferred.await(releaseSecondEvent)).pipe( |
| 95 | + Stream.map(() => deletedEvent(2)), |
| 96 | + ), |
| 97 | + ), |
| 98 | + } as unknown as OrchestrationEngineShape; |
| 99 | + const providerService = { |
| 100 | + stopSession: () => |
| 101 | + Effect.gen(function* () { |
| 102 | + stops.push(stops.length + 1); |
| 103 | + if (stops.length === 1) { |
| 104 | + yield* Deferred.succeed(firstCleanupDone, undefined); |
| 105 | + } |
| 106 | + }), |
| 107 | + } as unknown as ProviderServiceShape; |
| 108 | + const terminalManager = { |
| 109 | + close: () => Effect.void, |
| 110 | + } as unknown as TerminalManager.TerminalManager["Service"]; |
| 111 | + const layer = ThreadDeletionReactorLive.pipe( |
| 112 | + Layer.provide(Layer.succeed(ProviderService, providerService)), |
| 113 | + Layer.provide(Layer.succeed(TerminalManager.TerminalManager, terminalManager)), |
| 114 | + Layer.provide(Layer.succeed(OrchestrationEngineService, engine)), |
| 115 | + ); |
| 116 | + |
| 117 | + yield* Effect.scoped( |
| 118 | + Effect.gen(function* () { |
| 119 | + const reactor = yield* ThreadDeletionReactor; |
| 120 | + yield* reactor.start(); |
| 121 | + yield* Deferred.await(firstCleanupDone); |
| 122 | + |
| 123 | + // Sequence 1 is fully cleaned and the worker queue is idle. Sequence |
| 124 | + // 2 is committed and published but still in flight to the subscriber. |
| 125 | + yield* Ref.set(latestSequence, 2); |
| 126 | + const drained = yield* Effect.forkChild(reactor.drain); |
| 127 | + yield* Effect.yieldNow; |
| 128 | + yield* Effect.yieldNow; |
| 129 | + expect(stops).toEqual([1]); |
| 130 | + expect(drained.pollUnsafe()).toBeUndefined(); |
| 131 | + |
| 132 | + yield* Deferred.succeed(releaseSecondEvent, undefined); |
| 133 | + yield* Fiber.join(drained); |
| 134 | + expect(stops).toEqual([1, 2]); |
| 135 | + }), |
| 136 | + ).pipe(Effect.provide(layer)); |
| 137 | + }), |
| 138 | + ); |
| 139 | +}); |
0 commit comments