| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as ces from "@distilled.cloud/gcp/ces_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 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( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | ces.getProjectsLocationsAppsAgents({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsAppsAgents on a missing agent fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | ces.getProjectsLocationsAppsAgents({ |
| 38 | name: `projects/${project}/locations/us/apps/missing/agents/missing`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "create, update, and delete an agent", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const created = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const app = yield* GCP.CES.App("Support", { |
| 57 | location: "us", |
| 58 | displayName: "support-agent", |
| 59 | }); |
| 60 | const agent = yield* GCP.CES.AppsAgent("Greeter", { |
| 61 | app: app.name, |
| 62 | displayName: "greeter", |
| 63 | description: "greets callers", |
| 64 | instruction: "Greet the user.", |
| 65 | }); |
| 66 | return { app, agent }; |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(created.agent.name).toContain("/agents/"); |
| 71 | expect(created.agent.app).toEqual(created.app.name); |
| 72 | expect(created.agent.displayName).toEqual("greeter"); |
| 73 | expect(created.agent.description).toEqual("greets callers"); |
| 74 | expect(created.agent.instruction).toEqual("Greet the user."); |
| 75 | |
| 76 | const fetched = yield* ces.getProjectsLocationsAppsAgents({ |
| 77 | name: created.agent.name, |
| 78 | }); |
| 79 | expect(fetched.name).toEqual(created.agent.name); |
| 80 | // The description reaches the model: no ownership marker. |
| 81 | expect(fetched.description).toEqual("greets callers"); |
| 82 | |
| 83 | const updated = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | const app = yield* GCP.CES.App("Support", { |
| 86 | appId: created.app.appId, |
| 87 | location: created.app.location, |
| 88 | displayName: "support-agent", |
| 89 | }); |
| 90 | const agent = yield* GCP.CES.AppsAgent("Greeter", { |
| 91 | app: app.name, |
| 92 | agentId: created.agent.agentId, |
| 93 | displayName: "greeter", |
| 94 | description: "greets and routes", |
| 95 | instruction: "Greet the user and offer help.", |
| 96 | }); |
| 97 | return { app, agent }; |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(updated.agent.name).toEqual(created.agent.name); |
| 102 | expect(updated.agent.description).toEqual("greets and routes"); |
| 103 | expect(updated.agent.instruction).toEqual( |
| 104 | "Greet the user and offer help.", |
| 105 | ); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | const gone = yield* waitUntilGone(created.agent.name); |
| 109 | expect(gone).toEqual("gone"); |
| 110 | }).pipe(logLevel), |
| 111 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 112 | ); |