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

AcpRegistrySupport.test.ts

263 lines8.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import { AcpRegistrySettings } from "@t3tools/contracts";
4import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Schema from "effect/Schema";
9import { HttpClient, HttpClientResponse } from "effect/unstable/http";
10​
11import {
12 makeAcpRegistryResolver,
13 resolveAcpRegistryDistribution,
14 resolveAcpRegistryPlatformTarget,
15 type AcpRegistryAgent,
16} from "./AcpRegistrySupport.ts";
17​
18const registryUrl = "https://registry.test/registry.json";
19const archiveUrl = "https://registry.test/example-agent.bin";
20const decodeAcpRegistrySettings = Schema.decodeSync(AcpRegistrySettings);
21​
22function makeAgent(distribution: AcpRegistryAgent["distribution"]): AcpRegistryAgent {
23 return {
24 id: "example-agent",
25 name: "Example Agent",
26 version: "1.2.3",
27 description: "ACP Registry test agent",
28 distribution,
29 };
31​
32function makeRegistry(agent: AcpRegistryAgent): string {
33 return JSON.stringify({ version: "1.0.0", agents: [agent] });
35​
36function settings(input: Partial<AcpRegistrySettings> = {}): AcpRegistrySettings {
37 return decodeAcpRegistrySettings({
38 agentId: "example-agent",
39 ...input,
40 });
42​
43function resolverLayer(execute: Parameters<typeof HttpClient.make>[0]) {
44 return Layer.mergeAll(
45 NodeServices.layer,
46 Layer.succeed(HostProcessPlatform, "linux"),
47 Layer.succeed(HostProcessArchitecture, "x64"),
48 Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute)),
49 );
51​
52describe("AcpRegistrySupport", () => {
53 it("maps supported Node platforms to ACP Registry target keys", () => {
54 expect(resolveAcpRegistryPlatformTarget("darwin", "arm64")).toBe("darwin-aarch64");
55 expect(resolveAcpRegistryPlatformTarget("linux", "x64")).toBe("linux-x86_64");
56 expect(resolveAcpRegistryPlatformTarget("win32", "arm64")).toBe("windows-aarch64");
57 expect(resolveAcpRegistryPlatformTarget("freebsd", "x64")).toBeUndefined();
58 expect(resolveAcpRegistryPlatformTarget("linux", "ia32")).toBeUndefined();
59 });
60​
61 it("selects the preferred compatible distribution", () => {
62 const agent = makeAgent({
63 binary: {
64 "linux-x86_64": {
65 archive: archiveUrl,
66 cmd: "./bin/example-agent",
67 args: ["acp"],
68 },
69 },
70 npx: {
71 package: "@example/acp@1.2.3",
72 args: ["--stdio"],
73 },
74 });
75​
76 expect(
77 resolveAcpRegistryDistribution({
78 agent,
79 preference: "auto",
80 platformTarget: "linux-x86_64",
81 }),
82 ).toMatchObject({ kind: "binary", args: ["acp"] });
83 expect(
84 resolveAcpRegistryDistribution({
85 agent,
86 preference: "npx",
87 platformTarget: "linux-x86_64",
88 }),
89 ).toEqual({
90 kind: "npx",
91 packageName: "@example/acp@1.2.3",
92 args: ["--stdio"],
93 env: {},
94 });
95 expect(
96 resolveAcpRegistryDistribution({
97 agent,
98 preference: "binary",
99 platformTarget: "darwin-aarch64",
100 }),
101 ).toBeUndefined();
102 });
103​
104 it.effect("resolves command overrides while preserving registry args and environment", () => {
105 const agent = makeAgent({
106 binary: {
107 "linux-x86_64": {
108 archive: archiveUrl,
109 cmd: "./bin/example-agent",
110 args: ["acp", "--stdio"],
111 env: { REGISTRY_VALUE: "registry", OVERRIDE_ME: "registry" },
112 },
113 },
114 });
115 const requests: Array<string> = [];
116 return Effect.gen(function* () {
117 const fileSystem = yield* FileSystem.FileSystem;
118 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
119 prefix: "t3-acp-registry-override-",
120 });
121 const resolver = yield* makeAcpRegistryResolver({ cacheDir, registryUrl });
122 const resolved = yield* resolver.resolve(
123 settings({ commandPath: "/opt/example-agent" }),
124 "/workspace",
125 { HOST_VALUE: "host", OVERRIDE_ME: "host" },
126 );
127​
128 expect(resolved.distribution).toBe("binary");
129 expect(resolved.spawn).toEqual({
130 command: "/opt/example-agent",
131 args: ["acp", "--stdio"],
132 cwd: "/workspace",
133 env: {
134 HOST_VALUE: "host",
135 OVERRIDE_ME: "registry",
136 REGISTRY_VALUE: "registry",
137 },
138 });
139 expect(requests).toEqual([registryUrl]);
140 }).pipe(
141 Effect.scoped,
142 Effect.provide(
143 resolverLayer((request) => {
144 requests.push(request.url);
145 return Effect.succeed(
146 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
147 );
148 }),
149 ),
150 );
151 });
152​
153 it.effect("installs and reuses a registry binary in the managed cache", () => {
154 const agent = makeAgent({
155 binary: {
156 "linux-x86_64": {
157 archive: archiveUrl,
158 cmd: "./bin/example-agent",
159 args: ["acp"],
160 },
161 },
162 });
163 const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
164 const requests: Array<string> = [];
165 return Effect.gen(function* () {
166 const fileSystem = yield* FileSystem.FileSystem;
167 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
168 prefix: "t3-acp-registry-install-",
169 });
170 const resolver = yield* makeAcpRegistryResolver({ cacheDir, registryUrl });
171 const first = yield* resolver.resolve(settings(), "/workspace");
172 const second = yield* resolver.resolve(settings(), "/workspace");
173​
174 expect(first.spawn.command).toBe(second.spawn.command);
175 expect(first.spawn.command).toContain(
176 "/acp-registry/agents/example-agent/1.2.3/linux-x86_64/bin/example-agent",
177 );
178 expect(yield* fileSystem.readFileString(first.spawn.command)).toBe(
179 "#!/bin/sh\necho example\n",
180 );
181 expect(requests).toEqual([registryUrl, archiveUrl]);
182 }).pipe(
183 Effect.scoped,
184 Effect.provide(
185 resolverLayer((request) => {
186 requests.push(request.url);
187 const response =
188 request.url === registryUrl
189 ? new Response(makeRegistry(agent))
190 : new Response(binaryBytes.buffer as ArrayBuffer);
191 return Effect.succeed(HttpClientResponse.fromWeb(request, response));
192 }),
193 ),
194 );
195 });
196​
197 it.effect("falls back to a valid cached registry index when refresh fails", () => {
198 const agent = makeAgent({
199 npx: {
200 package: "@example/acp@1.2.3",
201 args: ["--stdio"],
202 },
203 });
204 return Effect.gen(function* () {
205 const fileSystem = yield* FileSystem.FileSystem;
206 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
207 prefix: "t3-acp-registry-cache-",
208 });
209 const registryDirectory = `${cacheDir}/acp-registry`;
210 yield* fileSystem.makeDirectory(registryDirectory, { recursive: true });
211 yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent));
212 const resolver = yield* makeAcpRegistryResolver({ cacheDir, registryUrl });
213 const resolved = yield* resolver.resolve(settings(), "/workspace");
214​
215 expect(resolved.spawn).toMatchObject({
216 command: "npx",
217 args: ["--yes", "@example/acp@1.2.3", "--stdio"],
218 });
219 }).pipe(
220 Effect.scoped,
221 Effect.provide(
222 resolverLayer((request) =>
223 Effect.succeed(
224 HttpClientResponse.fromWeb(request, new Response("unavailable", { status: 503 })),
225 ),
226 ),
227 ),
228 );
229 });
230​
231 it.effect("rejects unsafe command paths before downloading an archive", () => {
232 const agent = makeAgent({
233 binary: {
234 "linux-x86_64": {
235 archive: archiveUrl,
236 cmd: "../outside",
237 },
238 },
239 });
240 const requests: Array<string> = [];
241 return Effect.gen(function* () {
242 const fileSystem = yield* FileSystem.FileSystem;
243 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
244 prefix: "t3-acp-registry-invalid-",
245 });
246 const resolver = yield* makeAcpRegistryResolver({ cacheDir, registryUrl });
247 const error = yield* resolver.resolve(settings(), "/workspace").pipe(Effect.flip);
248​
249 expect(error.reason).toBe("archive_invalid");
250 expect(requests).toEqual([registryUrl]);
251 }).pipe(
252 Effect.scoped,
253 Effect.provide(
254 resolverLayer((request) => {
255 requests.push(request.url);
256 return Effect.succeed(
257 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
258 );
259 }),
260 ),
261 );
262 });
263});