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

AcpRegistryProbe.test.ts

423 lines13.4 KB

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 role: "reasoning",
233 },
234 ]);
235 });
236​
237 it.each(["config option", "session model state"] as const)(
238 "retains the current model beyond the catalog cap from %s",
239 (source) => {
240 const models = Array.from({ length: 257 }, (_, index) => ({
241 modelId: `model-${index}`,
242 name: `Model ${index}`,
243 }));
244 const result = acpRegistryProbeResult(instanceId, {
245 sessionId: "probe-session",
246 initializeResult: { protocolVersion: 1 },
247 sessionSetupResult: {
248 sessionId: "probe-session",
249 ...(source === "config option"
250 ? {
251 configOptions: [
252 {
253 id: "model",
254 name: "Model",
255 category: "model",
256 type: "select",
257 currentValue: "model-256",
258 options: models.map(({ modelId, name }) => ({ value: modelId, name })),
259 },
260 ],
261 }
262 : { models: { currentModelId: "model-256", availableModels: models } }),
263 },
264 modelConfigId: source === "config option" ? "model" : undefined,
265 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
266​
267 expect(result.models).toHaveLength(256);
268 expect(result.models[0]?.id).toBe("model-0");
269 expect(result.models.at(-1)?.id).toBe("model-256");
270 expect(result.currentModelId).toBe("model-256");
271 },
272 );
273​
274 it("bounds and de-duplicates advertised commands", () => {
275 const commands = normalizeAcpRegistryCommands([
276 {
277 name: "create_plan",
278 description: " Create a plan ",
279 input: { type: "text", hint: " topic " },
280 },
281 { name: "CREATE_PLAN", description: "duplicate" },
282 ...Array.from({ length: 140 }, (_, index) => ({
283 name: `command_${index}`,
284 description: "",
285 })),
286 ]);
287​
288 expect(commands.slashCommands).toHaveLength(128);
289 expect(commands.slashCommands[0]).toEqual({
290 name: "create_plan",
291 description: "Create a plan",
292 input: { hint: "topic" },
293 });
294 expect(
295 commands.slashCommands.filter((command) => command.name.toLowerCase() === "create_plan"),
296 ).toHaveLength(1);
297 expect(commands.skills).toEqual([]);
298 });
299​
300 it("routes dollar-prefixed ACP commands to the provider skill menu", () => {
301 expect(
302 normalizeAcpRegistryCommands([
303 { name: "$workspace-skill", description: "Run the workspace skill" },
304 { name: "$WORKSPACE-SKILL", description: "duplicate" },
305 { name: "$", description: "empty" },
306 { name: "review", description: "Review changes" },
307 ]),
308 ).toEqual({
309 slashCommands: [{ name: "review", description: "Review changes" }],
310 skills: [
311 {
312 name: "workspace-skill",
313 description: "Run the workspace skill",
314 path: "acp://skill/workspace-skill",
315 scope: "agent",
316 enabled: true,
317 },
318 ],
319 });
320 });
321​
322 it.effect("lists native sessions and logs out through generic ACP lifecycle methods", () => {
323 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
324 search: () => Effect.die("unused search"),
325 prepare: () => Effect.die("unused prepare"),
326 inspect: () => Effect.die("unused inspect"),
327 uninstallManagedBinary: () => Effect.die("unused uninstall"),
328 resolve: () =>
329 Effect.succeed({
330 agent: {
331 id: "mock-agent",
332 name: "Mock Agent",
333 version: "1.0.0",
334 description: "ACP session management test agent",
335 distribution: { npx: { package: "mock-agent@1.0.0" } },
336 },
337 distribution: "npx" as const,
338 spawn: {
339 command: "node",
340 args: [mockAgentPath],
341 env: {
342 ...process.env,
343 T3_ACP_SESSION_LIFECYCLE: "1",
344 T3_ACP_AUTH_METHOD_ID: "mock-login",
345 },
346 },
347 }),
348 });
349 const input = {
350 instanceId,
351 settings: decodeSettings({ agentId: "mock-agent" }),
352 cwd: process.cwd(),
353 environment: process.env,
354 };
355​
356 return Effect.gen(function* () {
357 const listed = yield* listAcpRegistrySessions(input);
358 expect(listed).toMatchObject({
359 canLoad: true,
360 canResume: true,
361 nextCursor: null,
362 sessions: [
363 {
364 sessionId: "mock-session-1",
365 cwd: process.cwd(),
366 importedThreadId: null,
367 },
368 ],
369 });
370 yield* logoutAcpRegistry(input);
371 }).pipe(
372 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
373 Effect.provide(NodeServices.layer),
374 Effect.scoped,
375 );
376 });
377​
378 it("distinguishes authentication failures from generic probe failures", () => {
379 const advertised = normalizeAcpRegistryAuthMethods([
380 { id: "grok-login", name: "Log in with Grok" },
381 ]);
382 const cause = new EffectAcpErrors.AcpRequestError({
383 code: -32000,
384 errorMessage: "login required",
385 });
386 const authFailure = acpRegistryProbeFailure(cause, advertised);
387 expect(authFailure.reason).toBe("authentication_failed");
388 expect(authFailure.message).toBe("The ACP agent could not complete authentication.");
389 expect(authFailure.cause).toBe(cause);
390 expect(authFailure.authMethods).toEqual([
391 {
392 id: "grok-login",
393 name: "Log in with Grok",
394 description: null,
395 type: "agent",
396 },
397 ]);
398 expect(
399 acpRegistryProbeFailure(
400 new EffectAcpErrors.AcpTransportError({
401 detail: "Authentication required",
402 cause: "credentials missing",
403 }),
404 ).reason,
405 ).toBe("authentication_failed");
406 const genericCause = new EffectAcpErrors.AcpTransportError({
407 detail: "connection closed",
408 cause: "closed",
409 });
410 const genericFailure = acpRegistryProbeFailure(genericCause);
411 expect(genericFailure.reason).toBe("probe_failed");
412 expect(genericFailure.message).toBe("The ACP agent could not create a test session.");
413 expect(genericFailure.cause).toBe(genericCause);
414 expect(
415 acpRegistryProbeFailure(
416 new EffectAcpErrors.AcpTransportError({
417 detail: "Failed while logging request",
418 cause: "logging failed",
419 }),
420 ).reason,
421 ).toBe("probe_failed");
422 });
423});