1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as registry from "@distilled.cloud/gcp/agentregistry_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// Agent Registry is not enabled in the test project (ServiceDisabled, see
18// the probe below). Set GCP_TEST_AGENT_REGISTRY=1 in a project that has it.
19const runLifecycle = !!process.env.GCP_TEST_AGENT_REGISTRY && !process.env.FAST;
20
21const location = "us-central1";
22const currentParent = GcpEnvironment.current.pipe(
23 Effect.map(({ project }) => `projects/${project}/locations/${location}`),
24);
25
26const waitUntilGone = (name: string) =>
27 registry.getProjectsLocationsBindings({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37const waitForOp = (operation: registry.Operation) =>
38 registry.getProjectsLocationsOperations({ name: operation.name ?? "" }).pipe(
39 Effect.repeat({
40 schedule: Schedule.spaced("2 seconds"),
41 until: (op) => op.done === true,
42 times: 30,
43 }),
44 );
45
46const getService = (name: string) =>
47 registry
48 .getProjectsLocationsServices({ name })
49 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
50
51const ensureService = (input: {
52 serviceId: string;
53 displayName: string;
54 body: registry.Service;
55}) =>
56 Effect.gen(function* () {
57 const parent = yield* currentParent;
58 const name = `${parent}/services/${input.serviceId}`;
59 const existing = yield* getService(name);
60 if (existing !== undefined) return existing;
61 const created = yield* registry
62 .createProjectsLocationsServices({
63 parent,
64 serviceId: input.serviceId,
65 body: {
66 displayName: input.displayName,
67 ...input.body,
68 },
69 })
70 .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined)));
71 if (created !== undefined) {
72 yield* waitForOp(created);
73 }
74 return yield* registry.getProjectsLocationsServices({ name });
75 });
76
77const deleteService = (name: string) =>
78 Effect.gen(function* () {
79 const deleted = yield* registry
80 .deleteProjectsLocationsServices({ name })
81 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
82 if (deleted !== undefined) {
83 yield* waitForOp(deleted);
84 }
85 });
86
87const identifierOf = (service: registry.Service) =>
88 Effect.gen(function* () {
89 const resourceName = service.registryResource;
90 if (resourceName === undefined || resourceName.length === 0) {
91 return undefined;
92 }
93 if (resourceName.includes("/agents/")) {
94 const agent = yield* registry
95 .getProjectsLocationsAgents({ name: resourceName })
96 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
97 return agent?.agentId;
98 }
99 if (resourceName.includes("/endpoints/")) {
100 const endpoint = yield* registry
101 .getProjectsLocationsEndpoints({ name: resourceName })
102 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
103 return endpoint?.endpointId;
104 }
105 if (resourceName.includes("/mcpServers/")) {
106 const mcp = yield* registry
107 .getProjectsLocationsMcpServers({ name: resourceName })
108 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
109 return mcp?.mcpServerId;
110 }
111 return undefined;
112 });
113
114const waitForIdentifier = (serviceName: string) =>
115 getService(serviceName).pipe(
116 Effect.flatMap((service) =>
117 service === undefined ? Effect.succeed(undefined) : identifierOf(service),
118 ),
119 Effect.repeat({
120 schedule: Schedule.spaced("1 second"),
121 until: (id) => typeof id === "string" && id.length > 0,
122 times: 10,
123 }),
124 );
125
126const ensureFixtures = () =>
127 Effect.gen(function* () {
128 const parent = yield* currentParent;
129 const sourceService = yield* ensureService({
130 serviceId: "alchagregsrc",
131 displayName: "alchemy-agentregistry-source",
132 body: {
133 agentSpec: { type: "NO_SPEC" },
134 interfaces: [
135 {
136 url: "https://example.com/agent",
137 protocolBinding: "HTTP_JSON",
138 },
139 ],
140 },
141 });
142 const targetService = yield* ensureService({
143 serviceId: "alchagregtgt",
144 displayName: "alchemy-agentregistry-target",
145 body: {
146 endpointSpec: { type: "NO_SPEC" },
147 interfaces: [
148 {
149 url: "https://example.com/endpoint",
150 protocolBinding: "HTTP_JSON",
151 },
152 ],
153 },
154 });
155 const sourceIdentifier = yield* waitForIdentifier(
156 sourceService.name ?? `${parent}/services/alchagregsrc`,
157 );
158 const targetIdentifier = yield* waitForIdentifier(
159 targetService.name ?? `${parent}/services/alchagregtgt`,
160 );
161 return {
162 sourceService,
163 targetService,
164 sourceIdentifier,
165 targetIdentifier,
166 };
167 });
168
169test.provider.skipIf(runLifecycle)(
170 "getProjectsLocationsBindings without Agent Registry enabled fails with ServiceDisabled",
171 (stack) =>
172 Effect.gen(function* () {
173 const parent = yield* currentParent;
174 yield* stack.destroy();
175
176 const error = yield* Effect.flip(
177 registry.getProjectsLocationsBindings({
178 name: `${parent}/bindings/alchemy-missing-binding`,
179 }),
180 );
181 expect(error._tag).toEqual("ServiceDisabled");
182
183 yield* stack.destroy();
184 }).pipe(logLevel),
185 {
186 tags: ["provider:gcp", "provider:gcp:agentregistry", "live"],
187 timeout: 90_000,
188 },
189);
190
191test.provider.skipIf(!runLifecycle)(
192 "create, update, and delete a binding",
193 (stack) =>
194 Effect.gen(function* () {
195 const parent = yield* currentParent;
196 yield* stack.destroy();
197
198 const {
199 sourceService,
200 targetService,
201 sourceIdentifier,
202 targetIdentifier,
203 } = yield* ensureFixtures();
204 expect(sourceIdentifier).toEqual(expect.any(String));
205 expect(targetIdentifier).toEqual(expect.any(String));
206
207 const created = yield* stack.deploy(
208 Effect.gen(function* () {
209 return yield* GCP.AgentRegistry.Binding("Orchestrator", {
210 location,
211 displayName: "orchestrator tools",
212 description: "routes the orchestrator",
213 sourceIdentifier: sourceIdentifier!,
214 targetIdentifier: targetIdentifier!,
215 });
216 }),
217 );
218
219 expect(created.bindingId).toEqual(expect.any(String));
220 expect(created.name).toEqual(`${parent}/bindings/${created.bindingId}`);
221 expect(created.location).toEqual(location);
222 expect(created.sourceIdentifier).toEqual(sourceIdentifier);
223 expect(created.targetIdentifier).toEqual(targetIdentifier);
224 expect(created.displayName).toEqual("orchestrator tools");
225 expect(created.description).toEqual("routes the orchestrator");
226
227 const fetched = yield* registry.getProjectsLocationsBindings({
228 name: created.name,
229 });
230 expect(fetched.name).toEqual(created.name);
231 expect(fetched.source?.identifier).toEqual(sourceIdentifier);
232 expect(fetched.target?.identifier).toEqual(targetIdentifier);
233 expect(fetched.description).toContain("alchemy-id=");
234 expect(fetched.description).toContain("routes the orchestrator");
235
236 const updated = yield* stack.deploy(
237 Effect.gen(function* () {
238 return yield* GCP.AgentRegistry.Binding("Orchestrator", {
239 location,
240 displayName: "orchestrator tools v2",
241 description: "updated routing",
242 sourceIdentifier: sourceIdentifier!,
243 targetIdentifier: targetIdentifier!,
244 });
245 }),
246 );
247
248 expect(updated.name).toEqual(created.name);
249 expect(updated.bindingId).toEqual(created.bindingId);
250 expect(updated.displayName).toEqual("orchestrator tools v2");
251 expect(updated.description).toEqual("updated routing");
252
253 yield* stack.destroy();
254 const gone = yield* waitUntilGone(created.name);
255 expect(gone).toEqual("gone");
256
257 yield* deleteService(sourceService.name ?? "");
258 yield* deleteService(targetService.name ?? "");
259 }).pipe(logLevel),
260 {
261 tags: ["provider:gcp", "provider:gcp:agentregistry", "live"],
262 timeout: 90_000,
263 },
264);