@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

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