Open History to see the last commit
1import {
2 ApprovalRequestId,
3 EventId,
4 CommandId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 type OrchestrationReadModel,
9} from "@t3tools/contracts";
10import * as NodeServices from "@effect/platform-node/NodeServices";
11import { expect, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13
14import { decideOrchestrationCommand } from "./decider.ts";
15
16const UPDATED_AT = "2026-01-01T00:00:00.000Z";
17
18const readModel: OrchestrationReadModel = {
19 snapshotSequence: 0,
20 projects: [],
21 threads: [
22 {
23 id: ThreadId.make("thread-1"),
24 projectId: ProjectId.make("project-1"),
25 title: "Manual title",
26 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
27 runtimeMode: "full-access",
28 interactionMode: "default",
29 branch: null,
30 worktreePath: null,
31 latestTurn: null,
32 createdAt: UPDATED_AT,
33 updatedAt: UPDATED_AT,
34 archivedAt: null,
35 settledOverride: null,
36 settledAt: null,
37 snoozedUntil: null,
38 snoozedAt: null,
39 deletedAt: null,
40 messages: [],
41 proposedPlans: [],
42 activities: [],
43 checkpoints: [],
44 session: null,
45 },
46 ],
47 updatedAt: UPDATED_AT,
48};
49
50const requestId = ApprovalRequestId.make("question-request");
51const command = {
52 type: "thread.user-input.respond" as const,
53 commandId: CommandId.make("answer"),
54 threadId: ThreadId.make("thread-1"),
55 requestId,
56 answers: { q: "" },
57 createdAt: UPDATED_AT,
58 attachmentsByQuestionId: {
59 q: [
60 {
61 type: "file" as const,
62 id: "thread-1-00000000-0000-4000-8000-0000000000aa-txt",
63 name: "spec.txt",
64 mimeType: "text/plain",
65 sizeBytes: 4,
66 },
67 ],
68 },
69};
70const request = {
71 id: EventId.make("question"),
72 kind: "user-input.requested",
73 summary: "Question",
74 tone: "info" as const,
75 turnId: null,
76 createdAt: UPDATED_AT,
77 payload: {
78 requestId,
79 questions: [
80 { id: "q", header: "Spec", question: "Provide a spec", options: [], allowCustomAnswer: true },
81 ],
82 },
83};
84it.layer(NodeServices.layer)("question attachment answers", (it) => {
85 it.effect("persists the original answer with its attachment and emits a provider response", () =>
86 Effect.gen(function* () {
87 const result = yield* decideOrchestrationCommand({
88 readModel,
89 command,
90 userInputActivity: request,
91 });
92 const events = Array.isArray(result) ? result : [result];
93 expect(events.map((event) => event.type)).toEqual([
94 "thread.activity-appended",
95 "thread.user-input-response-requested",
96 ]);
97 expect(events[0]?.payload).toMatchObject({
98 activity: {
99 kind: "user-input.answer-submitted",
100 payload: { answers: { q: "" }, attachmentsByQuestionId: command.attachmentsByQuestionId },
101 },
102 });
103 expect(events[1]?.payload).toMatchObject({
104 answers: { q: "" },
105 attachmentsByQuestionId: command.attachmentsByQuestionId,
106 });
107 }),
108 );
109 it.effect("rejects attachments for a resolved or unknown request", () =>
110 Effect.gen(function* () {
111 const result = yield* decideOrchestrationCommand({ readModel, command }).pipe(Effect.result);
112 expect(result._tag).toBe("Failure");
113 }),
114 );
115 it.effect("rejects unknown question IDs and predefined-choice-only protocols", () =>
116 Effect.gen(function* () {
117 for (const question of [
118 { ...request.payload.questions[0], id: "other" },
119 { ...request.payload.questions[0], allowCustomAnswer: false },
120 ]) {
121 const result = yield* decideOrchestrationCommand({
122 readModel,
123 command,
124 userInputActivity: { ...request, payload: { ...request.payload, questions: [question] } },
125 }).pipe(Effect.result);
126 expect(result._tag).toBe("Failure");
127 }
128 }),
129 );
130});