| 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 | const waitUntilGone = (name: string) => |
| 18 | aiplatform.getProjectsLocationsStudiesTrials({ 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 | const studySpec = { |
| 29 | metrics: [{ metricId: "accuracy", goal: "MAXIMIZE" as const }], |
| 30 | parameters: [ |
| 31 | { |
| 32 | parameterId: "learning_rate", |
| 33 | doubleValueSpec: { minValue: 0.001, maxValue: 0.1 }, |
| 34 | }, |
| 35 | ], |
| 36 | algorithm: "RANDOM_SEARCH" as const, |
| 37 | }; |
| 38 | |
| 39 | test.provider( |
| 40 | "getProjectsLocationsStudiesTrials on a missing trial fails with a typed tag", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | const { project } = yield* GcpEnvironment.current; |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const error = yield* Effect.flip( |
| 47 | aiplatform.getProjectsLocationsStudiesTrials({ |
| 48 | name: `projects/${project}/locations/us-central1/studies/1234567890123456789/trials/1`, |
| 49 | }), |
| 50 | ); |
| 51 | expect(error._tag).toEqual("NotFound"); |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | }).pipe(logLevel), |
| 55 | { |
| 56 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 57 | timeout: 90_000, |
| 58 | }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "create and delete a user-provided vizier trial", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const created = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const study = yield* GCP.AIPlatform.Study("Tune", { |
| 70 | location: "us-central1", |
| 71 | displayName: "trial_search", |
| 72 | studySpec, |
| 73 | }); |
| 74 | return yield* GCP.AIPlatform.StudiesTrial("Seed", { |
| 75 | parent: study.name, |
| 76 | parameters: [{ parameterId: "learning_rate", value: 0.01 }], |
| 77 | }); |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(created.name).toContain("/trials/"); |
| 82 | expect(created.parameters[0]?.parameterId).toEqual("learning_rate"); |
| 83 | |
| 84 | const fetched = yield* aiplatform.getProjectsLocationsStudiesTrials({ |
| 85 | name: created.name, |
| 86 | }); |
| 87 | expect(fetched.name).toEqual(created.name); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | const gone = yield* waitUntilGone(created.name); |
| 91 | expect(gone).toEqual("gone"); |
| 92 | }).pipe(logLevel), |
| 93 | { |
| 94 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 95 | timeout: 90_000, |
| 96 | }, |
| 97 | ); |