| 1 | import * as GCP from "@/GCP"; |
| 2 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 3 | import * as Output from "@/Output"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | aiplatform.getProjectsLocationsBatchPredictionJobs({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("2 seconds"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsBatchPredictionJobs on a missing job fails with a typed tag", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | const parent = `projects/${project}/locations/us-central1`; |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | aiplatform.getProjectsLocationsBatchPredictionJobs({ |
| 39 | name: `${parent}/batchPredictionJobs/1234567890123456789`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | const page = yield* aiplatform.listProjectsLocationsBatchPredictionJobs({ |
| 44 | parent, |
| 45 | pageSize: 10, |
| 46 | }); |
| 47 | expect( |
| 48 | (page.batchPredictionJobs ?? []).map((item) => item.name), |
| 49 | ).not.toContain(`${parent}/batchPredictionJobs/1234567890123456789`); |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | }).pipe(logLevel), |
| 53 | { |
| 54 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 55 | timeout: 90_000, |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | test.provider( |
| 60 | "create and delete a batch prediction job", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | const { project } = yield* GcpEnvironment.current; |
| 64 | const parent = `projects/${project}/locations/us-central1`; |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const created = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const bucket = yield* GCP.Storage.Bucket("BatchOut", { |
| 70 | location: "US-CENTRAL1", |
| 71 | forceDestroy: true, |
| 72 | }); |
| 73 | return yield* GCP.AIPlatform.BatchPredictionJob("Nightly", { |
| 74 | location: "us-central1", |
| 75 | displayName: "alchemy-batch", |
| 76 | model: `${parent}/publishers/google/models/gemini-2.5-flash`, |
| 77 | inputConfig: { |
| 78 | instancesFormat: "jsonl", |
| 79 | gcsSource: { |
| 80 | uris: ["gs://cloud-samples-data/vertex-ai/batch.jsonl"], |
| 81 | }, |
| 82 | }, |
| 83 | outputConfig: { |
| 84 | predictionsFormat: "jsonl", |
| 85 | gcsDestination: { |
| 86 | outputUriPrefix: Output.interpolate`gs://${bucket.bucketName}/out/`, |
| 87 | }, |
| 88 | }, |
| 89 | labels: { env: "test" }, |
| 90 | }); |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | expect(created.name).toContain("/batchPredictionJobs/"); |
| 95 | expect(created.labels).toMatchObject({ env: "test" }); |
| 96 | |
| 97 | const fetched = yield* aiplatform.getProjectsLocationsBatchPredictionJobs( |
| 98 | { |
| 99 | name: created.name, |
| 100 | }, |
| 101 | ); |
| 102 | expect(fetched.name).toEqual(created.name); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(created.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 111 | timeout: 120_000, |
| 112 | }, |
| 113 | ); |