Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

decider.userMessageAppend.test.ts

137 lines5.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import {
4 CommandId,
5 EventId,
6 MessageId,
7 ProjectId,
8 ProviderInstanceId,
9 ThreadId,
10} from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12​
13import { decideOrchestrationCommand } from "./decider.ts";
14import { createEmptyReadModel, projectEvent } from "./projector.ts";
15​
16const createdAt = "2026-08-24T10:00:00.000Z";
17const projectId = ProjectId.make("project-1");
18const threadId = ThreadId.make("thread-bootstrap");
19const messageId = MessageId.make("message-bootstrap");
20​
21const 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​
69const 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​
77const 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​
87it.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("rejects a message id that already exists on the thread", () =>
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 error = yield* Effect.flip(
106 decideOrchestrationCommand({ command: appendCommand, readModel: withMessage }),
107 );
108 expect(error._tag).toBe("OrchestrationCommandInvariantError");
109 expect(error.message).toContain("already exists");
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});