Open History to see the last commit
1import {
2 CommandId,
3 EventId,
4 ProjectId,
5 ProviderInstanceId,
6 ThreadId,
7 ApprovalRequestId,
8 type OrchestrationReadModel,
9 type OrchestrationThreadActivity,
10} from "@t3tools/contracts";
11import * as NodeServices from "@effect/platform-node/NodeServices";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14
15import { decideOrchestrationCommand } from "./decider.ts";
16import { projectEvent } from "./projector.ts";
17
18const NOW = "2026-01-01T00:00:00.000Z";
19const threadId = ThreadId.make("thread-1");
20const requestId = ApprovalRequestId.make("question-1");
21
22function makeRequest(responseMode: "message" | undefined): OrchestrationThreadActivity {
23 return {
24 id: EventId.make(requestId),
25 kind: "user-input.requested",
26 summary: "Question",
27 tone: "approval",
28 turnId: null,
29 createdAt: NOW,
30 payload: {
31 requestId,
32 ...(responseMode === undefined ? {} : { responseMode }),
33 questions: [{ id: "0", header: "Q", question: "Continue?", options: [] }],
34 },
35 };
36}
37
38function makeReadModel(
39 activities: ReadonlyArray<OrchestrationThreadActivity>,
40): OrchestrationReadModel {
41 return {
42 snapshotSequence: 0,
43 projects: [],
44 threads: [
45 {
46 id: threadId,
47 projectId: ProjectId.make("project-1"),
48 title: "Thread",
49 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
50 runtimeMode: "full-access",
51 interactionMode: "default",
52 branch: null,
53 worktreePath: null,
54 latestTurn: null,
55 createdAt: NOW,
56 updatedAt: NOW,
57 archivedAt: null,
58 settledOverride: null,
59 settledAt: null,
60 snoozedUntil: null,
61 snoozedAt: null,
62 pinnedAt: null,
63 deletedAt: null,
64 messages: [],
65 proposedPlans: [],
66 activities: [...activities],
67 checkpoints: [],
68 session: null,
69 },
70 ],
71 updatedAt: NOW,
72 };
73}
74
75const command = {
76 type: "thread.user-input.dismiss" as const,
77 commandId: CommandId.make("dismiss-1"),
78 threadId,
79 requestId,
80 createdAt: NOW,
81};
82
83it.layer(NodeServices.layer)("user input dismiss decider", (it) => {
84 it.effect("closes an async question without sending a message or starting a turn", () =>
85 Effect.gen(function* () {
86 const request = makeRequest("message");
87 const readModel = makeReadModel([request]);
88 const result = yield* decideOrchestrationCommand({
89 command,
90 readModel,
91 userInputActivity: request,
92 });
93 const events = Array.isArray(result) ? result : [result];
94 expect(events.map((event) => event.type)).toEqual(["thread.activity-appended"]);
95 expect(events[0]?.payload).toMatchObject({
96 threadId,
97 activity: {
98 kind: "user-input.resolved",
99 summary: "User input dismissed",
100 payload: { requestId, responseMode: "message" },
101 },
102 });
103 const projected = yield* projectEvent(readModel, { ...events[0]!, sequence: 1 });
104 expect(projected.threads[0]?.messages).toEqual([]);
105 expect(projected.threads[0]?.latestTurn).toBeNull();
106 }),
107 );
108
109 it.effect("rejects dismissing a native callback question", () =>
110 Effect.gen(function* () {
111 const request = makeRequest(undefined);
112 const result = yield* decideOrchestrationCommand({
113 command,
114 readModel: makeReadModel([request]),
115 userInputActivity: request,
116 }).pipe(Effect.flip);
117 expect(result).toMatchObject({
118 _tag: "OrchestrationCommandInvariantError",
119 detail: "This question needs an answer. Answer it or stop the turn.",
120 });
121 }),
122 );
123
124 it.effect("rejects dismissing a question that was already resolved", () =>
125 Effect.gen(function* () {
126 const resolved: OrchestrationThreadActivity = {
127 ...makeRequest("message"),
128 id: EventId.make("resolved"),
129 kind: "user-input.resolved",
130 };
131 const result = yield* decideOrchestrationCommand({
132 command,
133 readModel: makeReadModel([makeRequest("message"), resolved]),
134 userInputActivity: resolved,
135 }).pipe(Effect.flip);
136 expect(result).toMatchObject({
137 _tag: "OrchestrationCommandInvariantError",
138 detail: "This question has already been answered.",
139 });
140 }),
141 );
142});