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 pullRequests: [],
53 branch: null,
54 worktreePath: null,
55 latestTurn: null,
56 createdAt: NOW,
57 updatedAt: NOW,
58 archivedAt: null,
59 settledOverride: null,
60 settledAt: null,
61 snoozedUntil: null,
62 snoozedAt: null,
63 pinnedAt: null,
64 deletedAt: null,
65 messages: [],
66 proposedPlans: [],
67 activities: [...activities],
68 checkpoints: [],
69 session: null,
70 },
71 ],
72 updatedAt: NOW,
73 };
74}
75
76const command = {
77 type: "thread.user-input.dismiss" as const,
78 commandId: CommandId.make("dismiss-1"),
79 threadId,
80 requestId,
81 createdAt: NOW,
82};
83
84it.layer(NodeServices.layer)("user input dismiss decider", (it) => {
85 it.effect("closes an async question without sending a message or starting a turn", () =>
86 Effect.gen(function* () {
87 const request = makeRequest("message");
88 const readModel = makeReadModel([request]);
89 const result = yield* decideOrchestrationCommand({
90 command,
91 readModel,
92 userInputActivity: request,
93 });
94 const events = Array.isArray(result) ? result : [result];
95 expect(events.map((event) => event.type)).toEqual(["thread.activity-appended"]);
96 expect(events[0]?.payload).toMatchObject({
97 threadId,
98 activity: {
99 kind: "user-input.resolved",
100 summary: "User input dismissed",
101 payload: { requestId, responseMode: "message" },
102 },
103 });
104 const projected = yield* projectEvent(readModel, { ...events[0]!, sequence: 1 });
105 expect(projected.threads[0]?.messages).toEqual([]);
106 expect(projected.threads[0]?.latestTurn).toBeNull();
107 }),
108 );
109
110 it.effect("rejects dismissing a native callback question", () =>
111 Effect.gen(function* () {
112 const request = makeRequest(undefined);
113 const result = yield* decideOrchestrationCommand({
114 command,
115 readModel: makeReadModel([request]),
116 userInputActivity: request,
117 }).pipe(Effect.flip);
118 expect(result).toMatchObject({
119 _tag: "OrchestrationCommandInvariantError",
120 detail: "This question needs an answer. Answer it or stop the turn.",
121 });
122 }),
123 );
124
125 it.effect("rejects dismissing a question that was already resolved", () =>
126 Effect.gen(function* () {
127 const resolved: OrchestrationThreadActivity = {
128 ...makeRequest("message"),
129 id: EventId.make("resolved"),
130 kind: "user-input.resolved",
131 };
132 const result = yield* decideOrchestrationCommand({
133 command,
134 readModel: makeReadModel([makeRequest("message"), resolved]),
135 userInputActivity: resolved,
136 }).pipe(Effect.flip);
137 expect(result).toMatchObject({
138 _tag: "OrchestrationCommandInvariantError",
139 detail: "This question has already been answered.",
140 });
141 }),
142 );
143});