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