Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3import * as NodeURL from "node:url";
4
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { ProviderInstanceId } from "@t3tools/contracts";
7import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings";
8import { describe, expect, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import * as Schema from "effect/Schema";
11import * as EffectAcpErrors from "effect-acp/errors";
12
13import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
14import {
15 acpRegistryProbeFailure,
16 acpRegistryProbeResult,
17 listAcpRegistrySessions,
18 logoutAcpRegistry,
19 normalizeAcpRegistryAuthMethods,
20 normalizeAcpRegistryCommands,
21} from "@t3tools/provider-acp-registry/testing";
22import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
23
24const instanceId = ProviderInstanceId.make("acpRegistry_codex");
25const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
26const mockAgentPath = NodePath.join(__dirname, "../../scripts/acp-mock-agent.ts");
27const decodeSettings = Schema.decodeSync(AcpRegistrySettings);
28
29describe("ACP Registry probe", () => {
30 it("returns advertised auth methods and de-duplicated models", () => {
31 const result = acpRegistryProbeResult(instanceId, {
32 sessionId: "probe-session",
33 initializeResult: {
34 protocolVersion: 1,
35 agentCapabilities: {
36 loadSession: true,
37 auth: { logout: {} },
38 sessionCapabilities: { list: {}, resume: {} },
39 },
40 authMethods: [
41 { id: "chatgpt", name: "ChatGPT" },
42 {
43 id: "api-key",
44 name: "API key",
45 type: "env_var",
46 vars: [{ name: "OPENAI_API_KEY", label: "OpenAI API key" }],
47 },
48 { id: "login", name: "Terminal login", type: "terminal" },
49 ],
50 },
51 sessionSetupResult: {
52 sessionId: "probe-session",
53 configOptions: [
54 {
55 id: "model",
56 name: "Model",
57 category: "model",
58 type: "select",
59 currentValue: "gpt-5.4",
60 options: [
61 { value: "gpt-5.4", name: "GPT-5.4" },
62 { value: "gpt-5.4", name: "Duplicate" },
63 ],
64 },
65 ],
66 },
67 modelConfigId: "model",
68 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
69
70 expect(result).toEqual({
71 instanceId,
72 ready: true,
73 icon: null,
74 authMethods: [
75 { id: "chatgpt", name: "ChatGPT", description: null, type: "agent" },
76 {
77 id: "api-key",
78 name: "API key",
79 description: null,
80 type: "env_var",
81 envVarNames: ["OPENAI_API_KEY"],
82 },
83 { id: "login", name: "Terminal login", description: null, type: "terminal" },
84 ],
85 models: [{ id: "gpt-5.4", name: "GPT-5.4", description: null }],
86 currentModelId: "gpt-5.4",
87 configOptions: [],
88 sessionManagement: {
89 canList: true,
90 canLoad: true,
91 canResume: true,
92 canLogout: true,
93 canDelete: false,
94 canConfigureProviders: false,
95 },
96 });
97 });
98
99 it("composes runnable terminal auth commands from the resolved spawn recipe", () => {
100 const methods = normalizeAcpRegistryAuthMethods(
101 [
102 {
103 id: "login",
104 name: "Terminal login",
105 type: "terminal",
106 args: ["auth", "log in"],
107 env: { FORCE_TTY: "1" },
108 },
109 ],
110 { command: "/opt/agents/devin", args: ["--acp"] },
111 );
112
113 expect(methods).toEqual([
114 {
115 id: "login",
116 name: "Terminal login",
117 description: null,
118 type: "terminal",
119 command: "FORCE_TTY=1 /opt/agents/devin --acp auth 'log in'",
120 },
121 ]);
122 });
123
124 it("omits unsafe truncated auth commands instead of publishing broken shell", () => {
125 const methods = normalizeAcpRegistryAuthMethods(
126 [
127 {
128 id: "login",
129 name: "Terminal login",
130 type: "terminal",
131 args: ["x".repeat(2_048)],
132 },
133 ],
134 { command: "/opt/agents/devin", args: ["--acp"] },
135 );
136
137 expect(methods[0]).not.toHaveProperty("command");
138 });
139
140 it("omits overlong opaque model ids instead of publishing mutated ids", () => {
141 const result = acpRegistryProbeResult(instanceId, {
142 sessionId: "probe-session",
143 initializeResult: { protocolVersion: 1 },
144 sessionSetupResult: {
145 sessionId: "probe-session",
146 configOptions: [
147 {
148 id: "model",
149 name: "Model",
150 category: "model",
151 type: "select",
152 currentValue: "x".repeat(129),
153 options: [
154 { value: "x".repeat(129), name: "Too long" },
155 { value: "valid", name: "Valid" },
156 ],
157 },
158 ],
159 },
160 modelConfigId: "model",
161 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
162
163 expect(result.models).toEqual([{ id: "valid", name: "Valid", description: null }]);
164 expect(result.currentModelId).toBeNull();
165 });
166
167 it("preserves grouped model values and advertises reasoning choices", () => {
168 const codexModel = '["codex","gpt-6.1-sol"]';
169 const goModel = '["opencode-go","deepseek-v4.1-flash"]';
170 const result = acpRegistryProbeResult(instanceId, {
171 sessionId: "probe-session",
172 initializeResult: { protocolVersion: 1 },
173 sessionSetupResult: {
174 sessionId: "probe-session",
175 configOptions: [
176 {
177 id: "model",
178 name: "Model",
179 category: "model",
180 type: "select",
181 currentValue: goModel,
182 options: [
183 {
184 groupId: "codex",
185 name: "Codex",
186 options: [{ value: codexModel, name: "GPT-6.1 Sol" }],
187 },
188 {
189 groupId: "opencode-go",
190 name: "OpenCode Go",
191 options: [{ value: goModel, name: "DeepSeek V4.1 Flash" }],
192 },
193 ],
194 },
195 {
196 id: "reasoning",
197 name: "Reasoning",
198 category: "thought_level",
199 type: "select",
200 currentValue: "high",
201 options: [
202 {
203 groupId: "effort",
204 name: "Effort",
205 options: [
206 { value: "medium", name: "Medium" },
207 { value: "high", name: "High" },
208 ],
209 },
210 ],
211 },
212 ],
213 },
214 modelConfigId: "model",
215 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
216
217 expect(result.models).toEqual([
218 { id: codexModel, name: "GPT-6.1 Sol", description: null },
219 { id: goModel, name: "DeepSeek V4.1 Flash", description: null },
220 ]);
221 expect(result.currentModelId).toBe(goModel);
222 expect(result.configOptions).toEqual([
223 {
224 id: "reasoning",
225 label: "Reasoning",
226 type: "select",
227 currentValue: "high",
228 options: [
229 { id: "medium", label: "Medium" },
230 { id: "high", label: "High" },
231 ],
232 },
233 ]);
234 });
235
236 it.each(["config option", "session model state"] as const)(
237 "retains the current model beyond the catalog cap from %s",
238 (source) => {
239 const models = Array.from({ length: 257 }, (_, index) => ({
240 modelId: `model-${index}`,
241 name: `Model ${index}`,
242 }));
243 const result = acpRegistryProbeResult(instanceId, {
244 sessionId: "probe-session",
245 initializeResult: { protocolVersion: 1 },
246 sessionSetupResult: {
247 sessionId: "probe-session",
248 ...(source === "config option"
249 ? {
250 configOptions: [
251 {
252 id: "model",
253 name: "Model",
254 category: "model",
255 type: "select",
256 currentValue: "model-256",
257 options: models.map(({ modelId, name }) => ({ value: modelId, name })),
258 },
259 ],
260 }
261 : { models: { currentModelId: "model-256", availableModels: models } }),
262 },
263 modelConfigId: source === "config option" ? "model" : undefined,
264 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
265
266 expect(result.models).toHaveLength(256);
267 expect(result.models[0]?.id).toBe("model-0");
268 expect(result.models.at(-1)?.id).toBe("model-256");
269 expect(result.currentModelId).toBe("model-256");
270 },
271 );
272
273 it("bounds and de-duplicates advertised commands", () => {
274 const commands = normalizeAcpRegistryCommands([
275 {
276 name: "create_plan",
277 description: " Create a plan ",
278 input: { type: "text", hint: " topic " },
279 },
280 { name: "CREATE_PLAN", description: "duplicate" },
281 ...Array.from({ length: 140 }, (_, index) => ({
282 name: `command_${index}`,
283 description: "",
284 })),
285 ]);
286
287 expect(commands.slashCommands).toHaveLength(128);
288 expect(commands.slashCommands[0]).toEqual({
289 name: "create_plan",
290 description: "Create a plan",
291 input: { hint: "topic" },
292 });
293 expect(
294 commands.slashCommands.filter((command) => command.name.toLowerCase() === "create_plan"),
295 ).toHaveLength(1);
296 expect(commands.skills).toEqual([]);
297 });
298
299 it("routes dollar-prefixed ACP commands to the provider skill menu", () => {
300 expect(
301 normalizeAcpRegistryCommands([
302 { name: "$workspace-skill", description: "Run the workspace skill" },
303 { name: "$WORKSPACE-SKILL", description: "duplicate" },
304 { name: "$", description: "empty" },
305 { name: "review", description: "Review changes" },
306 ]),
307 ).toEqual({
308 slashCommands: [{ name: "review", description: "Review changes" }],
309 skills: [
310 {
311 name: "workspace-skill",
312 description: "Run the workspace skill",
313 path: "acp://skill/workspace-skill",
314 scope: "agent",
315 enabled: true,
316 },
317 ],
318 });
319 });
320
321 it.effect("lists native sessions and logs out through generic ACP lifecycle methods", () => {
322 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
323 search: () => Effect.die("unused search"),
324 prepare: () => Effect.die("unused prepare"),
325 inspect: () => Effect.die("unused inspect"),
326 uninstallManagedBinary: () => Effect.die("unused uninstall"),
327 resolve: () =>
328 Effect.succeed({
329 agent: {
330 id: "mock-agent",
331 name: "Mock Agent",
332 version: "1.0.0",
333 description: "ACP session management test agent",
334 distribution: { npx: { package: "mock-agent@1.0.0" } },
335 },
336 distribution: "npx" as const,
337 spawn: {
338 command: "node",
339 args: [mockAgentPath],
340 env: {
341 ...process.env,
342 T3_ACP_SESSION_LIFECYCLE: "1",
343 T3_ACP_AUTH_METHOD_ID: "mock-login",
344 },
345 },
346 }),
347 });
348 const input = {
349 instanceId,
350 settings: decodeSettings({ agentId: "mock-agent" }),
351 cwd: process.cwd(),
352 environment: process.env,
353 };
354
355 return Effect.gen(function* () {
356 const listed = yield* listAcpRegistrySessions(input);
357 expect(listed).toMatchObject({
358 canLoad: true,
359 canResume: true,
360 nextCursor: null,
361 sessions: [
362 {
363 sessionId: "mock-session-1",
364 cwd: process.cwd(),
365 importedThreadId: null,
366 },
367 ],
368 });
369 yield* logoutAcpRegistry(input);
370 }).pipe(
371 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
372 Effect.provide(NodeServices.layer),
373 Effect.scoped,
374 );
375 });
376
377 it("distinguishes authentication failures from generic probe failures", () => {
378 const advertised = normalizeAcpRegistryAuthMethods([
379 { id: "grok-login", name: "Log in with Grok" },
380 ]);
381 const cause = new EffectAcpErrors.AcpRequestError({
382 code: -32000,
383 errorMessage: "login required",
384 });
385 const authFailure = acpRegistryProbeFailure(cause, advertised);
386 expect(authFailure.reason).toBe("authentication_failed");
387 expect(authFailure.message).toBe("The ACP agent could not complete authentication.");
388 expect(authFailure.cause).toBe(cause);
389 expect(authFailure.authMethods).toEqual([
390 {
391 id: "grok-login",
392 name: "Log in with Grok",
393 description: null,
394 type: "agent",
395 },
396 ]);
397 expect(
398 acpRegistryProbeFailure(
399 new EffectAcpErrors.AcpTransportError({
400 detail: "Authentication required",
401 cause: "credentials missing",
402 }),
403 ).reason,
404 ).toBe("authentication_failed");
405 const genericCause = new EffectAcpErrors.AcpTransportError({
406 detail: "connection closed",
407 cause: "closed",
408 });
409 const genericFailure = acpRegistryProbeFailure(genericCause);
410 expect(genericFailure.reason).toBe("probe_failed");
411 expect(genericFailure.message).toBe("The ACP agent could not create a test session.");
412 expect(genericFailure.cause).toBe(genericCause);
413 expect(
414 acpRegistryProbeFailure(
415 new EffectAcpErrors.AcpTransportError({
416 detail: "Failed while logging request",
417 cause: "logging failed",
418 }),
419 ).reason,
420 ).toBe("probe_failed");
421 });
422});