1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as aiplatform from "@distilled.cloud/gcp/aiplatform_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// AgentService is v1beta1-only; the aiplatform_v1 SDK gets BadRequest
18// "This API version is not supported by AgentService. Please use the
19// v1beta1 version." Set GCP_TEST_AIPLATFORM_AGENTS=1 once the v1 API serves agents.
20const runLifecycle =
21 !process.env.FAST && !!process.env.GCP_TEST_AIPLATFORM_AGENTS;
22
23const waitUntilGone = (name: string) =>
24 aiplatform.getProjectsLocationsAgents({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider(
35 "getProjectsLocationsAgents on a missing agent fails with a typed tag",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39 const parent = `projects/${project}/locations/us-central1`;
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 aiplatform.getProjectsLocationsAgents({
44 name: `${parent}/agents/1234567890123456789`,
45 }),
46 );
47 expect(error._tag).toEqual("AgentServiceV1Unsupported");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider.skipIf(runLifecycle)(
58 "createProjectsLocationsAgents is rejected on the v1 API",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63 const error = yield* Effect.flip(
64 aiplatform.createProjectsLocationsAgents({
65 parent: `projects/${project}/locations/us-central1`,
66 body: {},
67 }),
68 );
69 expect(error._tag).toEqual("AgentServiceV1Unsupported");
70 }).pipe(logLevel),
71 {
72 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
73 timeout: 90_000,
74 },
75);
76
77test.provider.skipIf(!runLifecycle)(
78 "create, update, and delete a vertex agent",
79 (stack) =>
80 Effect.gen(function* () {
81 yield* stack.destroy();
82
83 const created = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* GCP.AIPlatform.Agent("Support", {
86 location: "us-central1",
87 systemInstruction: "Answer briefly.",
88 metadata: { env: "test" },
89 });
90 }),
91 );
92
93 expect(created.name).toContain("/agents/");
94 expect(created.location).toEqual("us-central1");
95 expect(created.metadata).toMatchObject({ env: "test" });
96
97 const fetched = yield* aiplatform.getProjectsLocationsAgents({
98 name: created.name,
99 });
100 expect(fetched.name).toEqual(created.name);
101
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 return yield* GCP.AIPlatform.Agent("Support", {
105 agentId: created.agentId,
106 location: "us-central1",
107 systemInstruction: "Answer in one sentence.",
108 metadata: { env: "prod" },
109 });
110 }),
111 );
112
113 expect(updated.name).toEqual(created.name);
114 expect(updated.systemInstruction).toEqual("Answer in one sentence.");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 {
122 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
123 timeout: 120_000,
124 },
125);