Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpRuntimeModel.test.ts

377 lines10.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import type * as EffectAcpSchema from "effect-acp/schema";
4​
5import {
6 extractModelConfigId,
7 mergeToolCallState,
8 parsePermissionRequest,
9 parseSessionModeState,
10 parseSessionUpdateEvent,
11 sessionUpdateIsReplay,
12 syntheticLoadSessionResponseFromInitialize,
13} from "./AcpRuntimeModel.ts";
14​
15describe("AcpRuntimeModel", () => {
16 it("parses session mode state from typed ACP session setup responses", () => {
17 const modeState = parseSessionModeState({
18 sessionId: "session-1",
19 modes: {
20 currentModeId: " code ",
21 availableModes: [
22 { id: " ask ", name: " Ask ", description: " Request approval " },
23 { id: " code ", name: " Code " },
24 ],
25 },
26 configOptions: [],
27 } satisfies EffectAcpSchema.NewSessionResponse);
28​
29 expect(modeState).toEqual({
30 currentModeId: "code",
31 availableModes: [
32 { id: "ask", name: "Ask", description: "Request approval" },
33 { id: "code", name: "Code" },
34 ],
35 });
36 });
37​
38 it("extracts the model config id from typed ACP config options", () => {
39 const modelConfigId = extractModelConfigId({
40 sessionId: "session-1",
41 configOptions: [
42 {
43 id: "approval",
44 name: "Approval Mode",
45 category: "permission",
46 type: "select",
47 currentValue: "ask",
48 options: [{ value: "ask", name: "Ask" }],
49 },
50 {
51 id: "model",
52 name: "Model",
53 category: "model",
54 type: "select",
55 currentValue: "default",
56 options: [{ value: "default", name: "Auto" }],
57 },
58 ],
59 } satisfies EffectAcpSchema.NewSessionResponse);
60​
61 expect(modelConfigId).toBe("model");
62 });
63​
64 it("detects Grok session replay updates from _meta.isReplay", () => {
65 expect(
66 sessionUpdateIsReplay({
67 _meta: { isReplay: true },
68 sessionId: "session-1",
69 update: {
70 sessionUpdate: "agent_message_chunk",
71 content: { type: "text", text: "replayed" },
72 },
73 } satisfies EffectAcpSchema.SessionNotification),
74 ).toBe(true);
75 expect(
76 sessionUpdateIsReplay({
77 sessionId: "session-1",
78 update: {
79 sessionUpdate: "agent_message_chunk",
80 content: { type: "text", text: "live" },
81 },
82 } satisfies EffectAcpSchema.SessionNotification),
83 ).toBe(false);
84 });
85​
86 it("builds a synthetic load response from initialize model state", () => {
87 const response = syntheticLoadSessionResponseFromInitialize({
88 protocolVersion: 1,
89 _meta: {
90 modelState: {
91 currentModelId: "grok-build",
92 availableModels: [{ modelId: "grok-build", name: "Grok Build" }],
93 },
94 },
95 } satisfies EffectAcpSchema.InitializeResponse);
96​
97 expect(response.models?.currentModelId).toBe("grok-build");
98 expect(response._meta).toMatchObject({ t3SessionLoadReady: "replay_idle" });
99 });
100​
101 it("accepts initialize model descriptions with null", () => {
102 const response = syntheticLoadSessionResponseFromInitialize({
103 protocolVersion: 1,
104 _meta: {
105 modelState: {
106 currentModelId: "grok-build",
107 availableModels: [{ modelId: "grok-build", name: "Grok Build", description: null }],
108 },
109 },
110 } satisfies EffectAcpSchema.InitializeResponse);
111​
112 expect(response.models?.availableModels[0]?.description).toBeNull();
113 });
114​
115 it("ignores malformed initialize model state in synthetic load responses", () => {
116 const response = syntheticLoadSessionResponseFromInitialize({
117 protocolVersion: 1,
118 _meta: {
119 modelState: {
120 currentModelId: "grok-build",
121 availableModels: [null],
122 },
123 modeState: {
124 currentModeId: "code",
125 availableModes: [{ id: "code", name: 12 }],
126 },
127 },
128 } as EffectAcpSchema.InitializeResponse);
129​
130 expect(response.models).toBeUndefined();
131 expect(response.modes).toBeUndefined();
132 expect(response._meta).toMatchObject({ t3SessionLoadReady: "replay_idle" });
133 });
134​
135 it("builds a synthetic load response with initialize mode state", () => {
136 const response = syntheticLoadSessionResponseFromInitialize({
137 protocolVersion: 1,
138 _meta: {
139 modeState: {
140 currentModeId: "code",
141 availableModes: [
142 { id: "ask", name: "Ask" },
143 { id: "code", name: "Code" },
144 ],
145 },
146 },
147 } satisfies EffectAcpSchema.InitializeResponse);
148​
149 expect(response.modes?.currentModeId).toBe("code");
150 expect(response.modes?.availableModes).toHaveLength(2);
151 });
152​
153 it("projects typed ACP tool call updates into runtime events", () => {
154 const created = parseSessionUpdateEvent({
155 sessionId: "session-1",
156 update: {
157 sessionUpdate: "tool_call",
158 toolCallId: "tool-1",
159 title: "Terminal",
160 kind: "execute",
161 status: "pending",
162 rawInput: {
163 executable: "bun",
164 args: ["run", "typecheck"],
165 },
166 content: [
167 {
168 type: "content",
169 content: {
170 type: "text",
171 text: "Running checks",
172 },
173 },
174 ],
175 },
176 } satisfies EffectAcpSchema.SessionNotification);
177​
178 expect(created.events).toEqual([
179 {
180 _tag: "ToolCallUpdated",
181 toolCall: {
182 toolCallId: "tool-1",
183 kind: "execute",
184 title: "Ran command",
185 status: "pending",
186 command: "bun run typecheck",
187 detail: "bun run typecheck",
188 data: {
189 toolCallId: "tool-1",
190 kind: "execute",
191 command: "bun run typecheck",
192 rawInput: {
193 executable: "bun",
194 args: ["run", "typecheck"],
195 },
196 content: [
197 {
198 type: "content",
199 content: {
200 type: "text",
201 text: "Running checks",
202 },
203 },
204 ],
205 },
206 },
207 rawPayload: {
208 sessionId: "session-1",
209 update: {
210 sessionUpdate: "tool_call",
211 toolCallId: "tool-1",
212 title: "Terminal",
213 kind: "execute",
214 status: "pending",
215 rawInput: {
216 executable: "bun",
217 args: ["run", "typecheck"],
218 },
219 content: [
220 {
221 type: "content",
222 content: {
223 type: "text",
224 text: "Running checks",
225 },
226 },
227 ],
228 },
229 },
230 },
231 ]);
232​
233 const updated = parseSessionUpdateEvent({
234 sessionId: "session-1",
235 update: {
236 sessionUpdate: "tool_call_update",
237 toolCallId: "tool-1",
238 status: "completed",
239 rawOutput: { exitCode: 0 },
240 },
241 } satisfies EffectAcpSchema.SessionNotification);
242​
243 expect(updated.events).toHaveLength(1);
244 expect(updated.events[0]?._tag).toBe("ToolCallUpdated");
245 const createdEvent = created.events[0];
246 const updatedEvent = updated.events[0];
247 if (createdEvent?._tag === "ToolCallUpdated" && updatedEvent?._tag === "ToolCallUpdated") {
248 expect(mergeToolCallState(createdEvent.toolCall, updatedEvent.toolCall)).toMatchObject({
249 toolCallId: "tool-1",
250 status: "completed",
251 title: "Ran command",
252 detail: "bun run typecheck",
253 command: "bun run typecheck",
254 });
255 }
256 });
257​
258 it("trims padded current mode updates before emitting a mode change", () => {
259 const result = parseSessionUpdateEvent({
260 sessionId: "session-1",
261 update: {
262 sessionUpdate: "current_mode_update",
263 currentModeId: " code ",
264 },
265 } satisfies EffectAcpSchema.SessionNotification);
266​
267 expect(result.modeId).toBe("code");
268 expect(result.events).toEqual([
269 {
270 _tag: "ModeChanged",
271 modeId: "code",
272 },
273 ]);
274 });
275​
276 it("projects typed ACP plan and content updates", () => {
277 const planResult = parseSessionUpdateEvent({
278 sessionId: "session-1",
279 update: {
280 sessionUpdate: "plan",
281 entries: [
282 { content: " Inspect state ", priority: "high", status: "completed" },
283 { content: "", priority: "medium", status: "in_progress" },
284 ],
285 },
286 } satisfies EffectAcpSchema.SessionNotification);
287​
288 expect(planResult.events).toEqual([
289 {
290 _tag: "PlanUpdated",
291 payload: {
292 plan: [
293 { step: "Inspect state", status: "completed" },
294 { step: "Step 2", status: "inProgress" },
295 ],
296 },
297 rawPayload: {
298 sessionId: "session-1",
299 update: {
300 sessionUpdate: "plan",
301 entries: [
302 { content: " Inspect state ", priority: "high", status: "completed" },
303 { content: "", priority: "medium", status: "in_progress" },
304 ],
305 },
306 },
307 },
308 ]);
309​
310 const contentResult = parseSessionUpdateEvent({
311 sessionId: "session-1",
312 update: {
313 sessionUpdate: "agent_message_chunk",
314 content: {
315 type: "text",
316 text: "hello from acp",
317 },
318 },
319 } satisfies EffectAcpSchema.SessionNotification);
320​
321 expect(contentResult.events).toEqual([
322 {
323 _tag: "ContentDelta",
324 text: "hello from acp",
325 rawPayload: {
326 sessionId: "session-1",
327 update: {
328 sessionUpdate: "agent_message_chunk",
329 content: {
330 type: "text",
331 text: "hello from acp",
332 },
333 },
334 },
335 },
336 ]);
337 });
338​
339 it("keeps permission request parsing compatible with loose extension payloads", () => {
340 const request = parsePermissionRequest({
341 sessionId: "session-1",
342 options: [
343 {
344 optionId: "allow-once",
345 name: "Allow once",
346 kind: "allow_once",
347 },
348 ],
349 toolCall: {
350 toolCallId: "tool-1",
351 title: "`cat package.json`",
352 kind: "execute",
353 status: "pending",
354 content: [
355 {
356 type: "content",
357 content: {
358 type: "text",
359 text: "Not in allowlist",
360 },
361 },
362 ],
363 },
364 });
365​
366 expect(request).toMatchObject({
367 kind: "execute",
368 detail: "cat package.json",
369 toolCall: {
370 toolCallId: "tool-1",
371 kind: "execute",
372 status: "pending",
373 command: "cat package.json",
374 },
375 });
376 });
377});