Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import type * as EffectAcpSchema from "effect-acp/schema";
4
5import {
6 extractModelConfigId,
7 mergeToolCallState,
8 parsePermissionRequest,
9 parseSessionModeState,
10 parseSessionUpdateEvent,
11} from "./AcpRuntimeModel.ts";
12
13describe("AcpRuntimeModel", () => {
14 it("parses session mode state from typed ACP session setup responses", () => {
15 const modeState = parseSessionModeState({
16 sessionId: "session-1",
17 modes: {
18 currentModeId: " code ",
19 availableModes: [
20 { id: " ask ", name: " Ask ", description: " Request approval " },
21 { id: " code ", name: " Code " },
22 ],
23 },
24 configOptions: [],
25 } satisfies EffectAcpSchema.NewSessionResponse);
26
27 expect(modeState).toEqual({
28 currentModeId: "code",
29 availableModes: [
30 { id: "ask", name: "Ask", description: "Request approval" },
31 { id: "code", name: "Code" },
32 ],
33 });
34 });
35
36 it("extracts the model config id from typed ACP config options", () => {
37 const modelConfigId = extractModelConfigId({
38 sessionId: "session-1",
39 configOptions: [
40 {
41 id: "approval",
42 name: "Approval Mode",
43 category: "permission",
44 type: "select",
45 currentValue: "ask",
46 options: [{ value: "ask", name: "Ask" }],
47 },
48 {
49 id: "model",
50 name: "Model",
51 category: "model",
52 type: "select",
53 currentValue: "default",
54 options: [{ value: "default", name: "Auto" }],
55 },
56 ],
57 } satisfies EffectAcpSchema.NewSessionResponse);
58
59 expect(modelConfigId).toBe("model");
60 });
61
62 it("projects typed ACP tool call updates into runtime events", () => {
63 const created = parseSessionUpdateEvent({
64 sessionId: "session-1",
65 update: {
66 sessionUpdate: "tool_call",
67 toolCallId: "tool-1",
68 title: "Terminal",
69 kind: "execute",
70 status: "pending",
71 rawInput: {
72 executable: "bun",
73 args: ["run", "typecheck"],
74 },
75 content: [
76 {
77 type: "content",
78 content: {
79 type: "text",
80 text: "Running checks",
81 },
82 },
83 ],
84 },
85 } satisfies EffectAcpSchema.SessionNotification);
86
87 expect(created.events).toEqual([
88 {
89 _tag: "ToolCallUpdated",
90 toolCall: {
91 toolCallId: "tool-1",
92 kind: "execute",
93 title: "Ran command",
94 status: "pending",
95 command: "bun run typecheck",
96 detail: "bun run typecheck",
97 data: {
98 toolCallId: "tool-1",
99 kind: "execute",
100 command: "bun run typecheck",
101 rawInput: {
102 executable: "bun",
103 args: ["run", "typecheck"],
104 },
105 content: [
106 {
107 type: "content",
108 content: {
109 type: "text",
110 text: "Running checks",
111 },
112 },
113 ],
114 },
115 },
116 rawPayload: {
117 sessionId: "session-1",
118 update: {
119 sessionUpdate: "tool_call",
120 toolCallId: "tool-1",
121 title: "Terminal",
122 kind: "execute",
123 status: "pending",
124 rawInput: {
125 executable: "bun",
126 args: ["run", "typecheck"],
127 },
128 content: [
129 {
130 type: "content",
131 content: {
132 type: "text",
133 text: "Running checks",
134 },
135 },
136 ],
137 },
138 },
139 },
140 ]);
141
142 const updated = parseSessionUpdateEvent({
143 sessionId: "session-1",
144 update: {
145 sessionUpdate: "tool_call_update",
146 toolCallId: "tool-1",
147 status: "completed",
148 rawOutput: { exitCode: 0 },
149 },
150 } satisfies EffectAcpSchema.SessionNotification);
151
152 expect(updated.events).toHaveLength(1);
153 expect(updated.events[0]?._tag).toBe("ToolCallUpdated");
154 const createdEvent = created.events[0];
155 const updatedEvent = updated.events[0];
156 if (createdEvent?._tag === "ToolCallUpdated" && updatedEvent?._tag === "ToolCallUpdated") {
157 expect(mergeToolCallState(createdEvent.toolCall, updatedEvent.toolCall)).toMatchObject({
158 toolCallId: "tool-1",
159 status: "completed",
160 title: "Ran command",
161 detail: "bun run typecheck",
162 command: "bun run typecheck",
163 });
164 }
165 });
166
167 it("trims padded current mode updates before emitting a mode change", () => {
168 const result = parseSessionUpdateEvent({
169 sessionId: "session-1",
170 update: {
171 sessionUpdate: "current_mode_update",
172 currentModeId: " code ",
173 },
174 } satisfies EffectAcpSchema.SessionNotification);
175
176 expect(result.modeId).toBe("code");
177 expect(result.events).toEqual([
178 {
179 _tag: "ModeChanged",
180 modeId: "code",
181 },
182 ]);
183 });
184
185 it("projects typed ACP plan and content updates", () => {
186 const planResult = parseSessionUpdateEvent({
187 sessionId: "session-1",
188 update: {
189 sessionUpdate: "plan",
190 entries: [
191 { content: " Inspect state ", priority: "high", status: "completed" },
192 { content: "", priority: "medium", status: "in_progress" },
193 ],
194 },
195 } satisfies EffectAcpSchema.SessionNotification);
196
197 expect(planResult.events).toEqual([
198 {
199 _tag: "PlanUpdated",
200 payload: {
201 plan: [
202 { step: "Inspect state", status: "completed" },
203 { step: "Step 2", status: "inProgress" },
204 ],
205 },
206 rawPayload: {
207 sessionId: "session-1",
208 update: {
209 sessionUpdate: "plan",
210 entries: [
211 { content: " Inspect state ", priority: "high", status: "completed" },
212 { content: "", priority: "medium", status: "in_progress" },
213 ],
214 },
215 },
216 },
217 ]);
218
219 const contentResult = parseSessionUpdateEvent({
220 sessionId: "session-1",
221 update: {
222 sessionUpdate: "agent_message_chunk",
223 content: {
224 type: "text",
225 text: "hello from acp",
226 },
227 },
228 } satisfies EffectAcpSchema.SessionNotification);
229
230 expect(contentResult.events).toEqual([
231 {
232 _tag: "ContentDelta",
233 text: "hello from acp",
234 rawPayload: {
235 sessionId: "session-1",
236 update: {
237 sessionUpdate: "agent_message_chunk",
238 content: {
239 type: "text",
240 text: "hello from acp",
241 },
242 },
243 },
244 },
245 ]);
246 });
247
248 it("keeps permission request parsing compatible with loose extension payloads", () => {
249 const request = parsePermissionRequest({
250 sessionId: "session-1",
251 options: [
252 {
253 optionId: "allow-once",
254 name: "Allow once",
255 kind: "allow_once",
256 },
257 ],
258 toolCall: {
259 toolCallId: "tool-1",
260 title: "`cat package.json`",
261 kind: "execute",
262 status: "pending",
263 content: [
264 {
265 type: "content",
266 content: {
267 type: "text",
268 text: "Not in allowlist",
269 },
270 },
271 ],
272 },
273 });
274
275 expect(request).toMatchObject({
276 kind: "execute",
277 detail: "cat package.json",
278 toolCall: {
279 toolCallId: "tool-1",
280 kind: "execute",
281 status: "pending",
282 command: "cat package.json",
283 },
284 });
285 });
286});