| 1 | import * as GCP from "@/GCP"; |
| 2 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as aiplatform from "@distilled.cloud/gcp/aiplatform_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 | // Sandbox template provisioning takes 1-2 minutes. |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | aiplatform |
| 22 | .getProjectsLocationsReasoningEnginesSandboxEnvironmentTemplates({ name }) |
| 23 | .pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsReasoningEnginesSandboxEnvironmentTemplates on a missing template fails with a typed tag", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | aiplatform |
| 42 | .getProjectsLocationsReasoningEnginesSandboxEnvironmentTemplates({ |
| 43 | name: `projects/${project}/locations/us-central1/reasoningEngines/1234567890123456789/sandboxEnvironmentTemplates/1234567890123456789`, |
| 44 | }) |
| 45 | .pipe(Effect.timeout("15 seconds")), |
| 46 | ); |
| 47 | expect(error._tag).toEqual("NotFound"); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { |
| 52 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 53 | timeout: 30_000, |
| 54 | }, |
| 55 | ); |
| 56 | |
| 57 | test.provider.skipIf(!runLifecycle)( |
| 58 | "create and delete a sandbox environment template", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const created = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const engine = yield* GCP.AIPlatform.ReasoningEngine("Agent", { |
| 66 | location: "us-central1", |
| 67 | displayName: "alchemy-template-engine", |
| 68 | labels: { env: "test" }, |
| 69 | spec: { agentFramework: "custom" }, |
| 70 | }); |
| 71 | const template = |
| 72 | yield* GCP.AIPlatform.ReasoningEnginesSandboxEnvironmentTemplate( |
| 73 | "Browser", |
| 74 | { |
| 75 | reasoningEngine: engine.name, |
| 76 | displayName: "browser", |
| 77 | defaultContainerEnvironment: { |
| 78 | defaultContainerCategory: |
| 79 | "DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE", |
| 80 | }, |
| 81 | }, |
| 82 | ); |
| 83 | return { engine, template }; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.template.name).toContain("/sandboxEnvironmentTemplates/"); |
| 88 | expect(created.template.displayName).toEqual("browser"); |
| 89 | expect(created.template.reasoningEngine).toEqual(created.engine.name); |
| 90 | |
| 91 | const fetched = |
| 92 | yield* aiplatform.getProjectsLocationsReasoningEnginesSandboxEnvironmentTemplates( |
| 93 | { |
| 94 | name: created.template.name, |
| 95 | }, |
| 96 | ); |
| 97 | expect(fetched.name).toEqual(created.template.name); |
| 98 | expect(fetched.displayName).toContain("[alchemy "); |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const gone = yield* waitUntilGone(created.template.name); |
| 103 | expect(gone).toEqual("gone"); |
| 104 | }).pipe(logLevel), |
| 105 | { |
| 106 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 107 | timeout: 600_000, |
| 108 | }, |
| 109 | ); |