|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + CommandId, |
| 5 | + EventId, |
| 6 | + MessageId, |
| 7 | + ProjectId, |
| 8 | + ProviderInstanceId, |
| 9 | + ThreadId, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | + |
| 13 | +import { decideOrchestrationCommand } from "./decider.ts"; |
| 14 | +import { createEmptyReadModel, projectEvent } from "./projector.ts"; |
| 15 | + |
| 16 | +const createdAt = "2026-08-24T10:00:00.000Z"; |
| 17 | +const projectId = ProjectId.make("project-1"); |
| 18 | +const threadId = ThreadId.make("thread-bootstrap"); |
| 19 | +const messageId = MessageId.make("message-bootstrap"); |
| 20 | + |
| 21 | +const readModelWithThread = Effect.gen(function* () { |
| 22 | + const withProject = yield* projectEvent(createEmptyReadModel(createdAt), { |
| 23 | + sequence: 1, |
| 24 | + eventId: EventId.make("event-project-created"), |
| 25 | + aggregateKind: "project", |
| 26 | + aggregateId: projectId, |
| 27 | + type: "project.created", |
| 28 | + occurredAt: createdAt, |
| 29 | + commandId: CommandId.make("command-project-created"), |
| 30 | + causationEventId: null, |
| 31 | + correlationId: CommandId.make("command-project-created"), |
| 32 | + metadata: {}, |
| 33 | + payload: { |
| 34 | + projectId, |
| 35 | + title: "Project", |
| 36 | + workspaceRoot: "/tmp/project", |
| 37 | + defaultModelSelection: null, |
| 38 | + scripts: [], |
| 39 | + createdAt, |
| 40 | + updatedAt: createdAt, |
| 41 | + }, |
| 42 | + }); |
| 43 | + return yield* projectEvent(withProject, { |
| 44 | + sequence: 2, |
| 45 | + eventId: EventId.make("event-thread-created"), |
| 46 | + aggregateKind: "thread", |
| 47 | + aggregateId: threadId, |
| 48 | + type: "thread.created", |
| 49 | + occurredAt: createdAt, |
| 50 | + commandId: CommandId.make("command-thread-created"), |
| 51 | + causationEventId: null, |
| 52 | + correlationId: CommandId.make("command-thread-created"), |
| 53 | + metadata: {}, |
| 54 | + payload: { |
| 55 | + threadId, |
| 56 | + projectId, |
| 57 | + title: "Bootstrap thread", |
| 58 | + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" }, |
| 59 | + runtimeMode: "full-access", |
| 60 | + interactionMode: "default", |
| 61 | + branch: null, |
| 62 | + worktreePath: null, |
| 63 | + createdAt, |
| 64 | + updatedAt: createdAt, |
| 65 | + }, |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +const appendCommand = { |
| 70 | + type: "thread.message.user.append" as const, |
| 71 | + commandId: CommandId.make("command-append"), |
| 72 | + threadId, |
| 73 | + message: { messageId, text: "Build it", attachments: [] }, |
| 74 | + createdAt, |
| 75 | +}; |
| 76 | + |
| 77 | +const turnStartCommand = { |
| 78 | + type: "thread.turn.start" as const, |
| 79 | + commandId: CommandId.make("command-turn-start"), |
| 80 | + threadId, |
| 81 | + message: { messageId, role: "user" as const, text: "Build it", attachments: [] }, |
| 82 | + runtimeMode: "full-access" as const, |
| 83 | + interactionMode: "default" as const, |
| 84 | + createdAt, |
| 85 | +}; |
| 86 | + |
| 87 | +it.layer(NodeServices.layer)("thread.message.user.append", (it) => { |
| 88 | + it.effect("persists a user message without a turn, tagged as deferred", () => |
| 89 | + Effect.gen(function* () { |
| 90 | + const readModel = yield* readModelWithThread; |
| 91 | + const planned = yield* decideOrchestrationCommand({ command: appendCommand, readModel }); |
| 92 | + const events = Array.isArray(planned) ? planned : [planned]; |
| 93 | + expect(events.map((event) => event.type)).toEqual(["thread.message-sent"]); |
| 94 | + expect(events[0]?.metadata.deferredTurn).toBe(true); |
| 95 | + expect(events[0]?.payload).toMatchObject({ messageId, role: "user", turnId: null }); |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + it.effect("is idempotent for an already persisted message id", () => |
| 100 | + Effect.gen(function* () { |
| 101 | + const readModel = yield* readModelWithThread; |
| 102 | + const first = yield* decideOrchestrationCommand({ command: appendCommand, readModel }); |
| 103 | + const firstEvent = Array.isArray(first) ? first[0]! : first; |
| 104 | + const withMessage = yield* projectEvent(readModel, { ...firstEvent, sequence: 3 }); |
| 105 | + const again = yield* decideOrchestrationCommand({ |
| 106 | + command: appendCommand, |
| 107 | + readModel: withMessage, |
| 108 | + }); |
| 109 | + expect(again).toEqual([]); |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + it.effect("lets the following turn start reference the message instead of re-sending it", () => |
| 114 | + Effect.gen(function* () { |
| 115 | + const readModel = yield* readModelWithThread; |
| 116 | + const appended = yield* decideOrchestrationCommand({ command: appendCommand, readModel }); |
| 117 | + const appendedEvent = Array.isArray(appended) ? appended[0]! : appended; |
| 118 | + const withMessage = yield* projectEvent(readModel, { ...appendedEvent, sequence: 3 }); |
| 119 | + |
| 120 | + const planned = yield* decideOrchestrationCommand({ |
| 121 | + command: turnStartCommand, |
| 122 | + readModel: withMessage, |
| 123 | + }); |
| 124 | + const events = Array.isArray(planned) ? planned : [planned]; |
| 125 | + expect(events.map((event) => event.type)).toEqual(["thread.turn-start-requested"]); |
| 126 | + expect(events[0]?.payload).toMatchObject({ messageId }); |
| 127 | + |
| 128 | + // Without the append the turn start still carries the message itself. |
| 129 | + const direct = yield* decideOrchestrationCommand({ command: turnStartCommand, readModel }); |
| 130 | + const directEvents = Array.isArray(direct) ? direct : [direct]; |
| 131 | + expect(directEvents.map((event) => event.type)).toEqual([ |
| 132 | + "thread.message-sent", |
| 133 | + "thread.turn-start-requested", |
| 134 | + ]); |
| 135 | + }), |
| 136 | + ); |
| 137 | +}); |
0 commit comments