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 pullRequests: [],
30 branch: null,
31 worktreePath: null,
32 latestTurn: null,
33 createdAt: UPDATED_AT,
34 updatedAt: UPDATED_AT,
35 archivedAt: null,
36 settledOverride: null,
37 settledAt: null,
38 snoozedUntil: null,
39 snoozedAt: null,
40 deletedAt: null,
41 messages: [],
42 proposedPlans: [],
43 activities: [],
44 checkpoints: [],
45 session: null,
46 },
47 ],
48 updatedAt: UPDATED_AT,
49};
50
51const requestId = ApprovalRequestId.make("question-request");
52const command = {
53 type: "thread.user-input.respond" as const,
54 commandId: CommandId.make("answer"),
55 threadId: ThreadId.make("thread-1"),
56 requestId,
57 answers: { q: "" },
58 createdAt: UPDATED_AT,
59 attachmentsByQuestionId: {
60 q: [
61 {
62 type: "file" as const,
63 id: "thread-1-00000000-0000-4000-8000-0000000000aa-txt",
64 name: "spec.txt",
65 mimeType: "text/plain",
66 sizeBytes: 4,
67 },
68 ],
69 },
70};
71const request = {
72 id: EventId.make("question"),
73 kind: "user-input.requested",
74 summary: "Question",
75 tone: "info" as const,
76 turnId: null,
77 createdAt: UPDATED_AT,
78 payload: {
79 requestId,
80 questions: [
81 { id: "q", header: "Spec", question: "Provide a spec", options: [], allowCustomAnswer: true },
82 ],
83 },
84};
85it.layer(NodeServices.layer)("question attachment answers", (it) => {
86 it.effect("persists the original answer with its attachment and emits a provider response", () =>
87 Effect.gen(function* () {
88 const result = yield* decideOrchestrationCommand({
89 readModel,
90 command,
91 userInputActivity: request,
92 });
93 const events = Array.isArray(result) ? result : [result];
94 expect(events.map((event) => event.type)).toEqual([
95 "thread.activity-appended",
96 "thread.user-input-response-requested",
97 ]);
98 expect(events[0]?.payload).toMatchObject({
99 activity: {
100 kind: "user-input.answer-submitted",
101 payload: { answers: { q: "" }, attachmentsByQuestionId: command.attachmentsByQuestionId },
102 },
103 });
104 expect(events[1]?.payload).toMatchObject({
105 answers: { q: "" },
106 attachmentsByQuestionId: command.attachmentsByQuestionId,
107 });
108 }),
109 );
110 it.effect("rejects attachments for a resolved or unknown request", () =>
111 Effect.gen(function* () {
112 const result = yield* decideOrchestrationCommand({ readModel, command }).pipe(Effect.result);
113 expect(result._tag).toBe("Failure");
114 }),
115 );
116 it.effect("rejects unknown question IDs and predefined-choice-only protocols", () =>
117 Effect.gen(function* () {
118 for (const question of [
119 { ...request.payload.questions[0], id: "other" },
120 { ...request.payload.questions[0], allowCustomAnswer: false },
121 ]) {
122 const result = yield* decideOrchestrationCommand({
123 readModel,
124 command,
125 userInputActivity: { ...request, payload: { ...request.payload, questions: [question] } },
126 }).pipe(Effect.result);
127 expect(result._tag).toBe("Failure");
128 }
129 }),
130 );
131});