|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import type { |
| 3 | + OrchestrationV2DomainEvent, |
| 4 | + OrchestrationV2Run, |
| 5 | + OrchestrationV2ServerCommand, |
| 6 | + OrchestrationV2ThreadProjection, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import * as DateTime from "effect/DateTime"; |
| 9 | +import * as Effect from "effect/Effect"; |
| 10 | +import * as Layer from "effect/Layer"; |
| 11 | +import * as Queue from "effect/Queue"; |
| 12 | +import * as Stream from "effect/Stream"; |
| 13 | + |
| 14 | +import * as ServerSettings from "../serverSettings.ts"; |
| 15 | +import * as IdleCompactionService from "./IdleCompactionService.ts"; |
| 16 | +import * as Orchestrator from "./Orchestrator.ts"; |
| 17 | +import * as ProjectionStore from "./ProjectionStore.ts"; |
| 18 | + |
| 19 | +const at = (minute: number) => DateTime.makeUnsafe(Date.UTC(2026, 9, 19, 12, minute)); |
| 20 | + |
| 21 | +function compactedThread(threadId: string, lastVisitedAt: DateTime.Utc | null) { |
| 22 | + const run = (ordinal: number, userMessageId: string, completedAt: DateTime.Utc) => |
| 23 | + ({ |
| 24 | + id: `${threadId}:run-${ordinal}`, |
| 25 | + threadId, |
| 26 | + ordinal, |
| 27 | + userMessageId, |
| 28 | + status: "completed", |
| 29 | + completedAt, |
| 30 | + }) as unknown as OrchestrationV2Run; |
| 31 | + const previous = run(1, "message", at(0)); |
| 32 | + const compaction = run(2, `idle-compact:${threadId}:${previous.id}`, at(58)); |
| 33 | + return { |
| 34 | + records: { |
| 35 | + thread: { id: threadId, lastVisitedAt }, |
| 36 | + runs: [previous, compaction], |
| 37 | + } as unknown as OrchestrationV2ThreadProjection, |
| 38 | + completed: { |
| 39 | + type: "run.updated", |
| 40 | + threadId, |
| 41 | + payload: compaction, |
| 42 | + } as unknown as OrchestrationV2DomainEvent, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +it.effect("marks a caught-up thread read when its automatic compaction completes", () => |
| 47 | + Effect.gen(function* () { |
| 48 | + const threads = [ |
| 49 | + compactedThread("unseen", null), |
| 50 | + compactedThread("behind", at(-1)), |
| 51 | + compactedThread("caught-up", at(1)), |
| 52 | + ]; |
| 53 | + const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 54 | + const commands = yield* Queue.unbounded<OrchestrationV2ServerCommand>(); |
| 55 | + const service = yield* IdleCompactionService.make.pipe( |
| 56 | + Effect.provide( |
| 57 | + Layer.mergeAll( |
| 58 | + Layer.mock(Orchestrator.OrchestratorV2)({ |
| 59 | + streamDomainEvents: Stream.fromQueue(events), |
| 60 | + dispatch: (command) => |
| 61 | + Queue.offer(commands, command).pipe(Effect.as({ sequence: 1, storedEvents: [] })), |
| 62 | + }), |
| 63 | + Layer.mock(ProjectionStore.ProjectionStoreV2)({ |
| 64 | + getThreadRecords: (threadId) => |
| 65 | + Effect.succeed(threads.find((t) => t.records.thread.id === threadId)!.records), |
| 66 | + }), |
| 67 | + ServerSettings.layerTest(), |
| 68 | + ), |
| 69 | + ), |
| 70 | + ); |
| 71 | + yield* service.start(); |
| 72 | + |
| 73 | + yield* Queue.offerAll( |
| 74 | + events, |
| 75 | + threads.map((thread) => thread.completed), |
| 76 | + ); |
| 77 | + |
| 78 | + expect(yield* Queue.take(commands)).toMatchObject({ |
| 79 | + type: "thread.visit", |
| 80 | + threadId: "caught-up", |
| 81 | + visitedAt: DateTime.formatIso(at(58)), |
| 82 | + opened: false, |
| 83 | + }); |
| 84 | + }).pipe(Effect.scoped), |
| 85 | +); |
0 commit comments