| 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.getDatasetsDatasetVersions({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("2 seconds"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getDatasetsDatasetVersions on a missing version fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | const parent = `projects/${project}/locations/us-central1`; |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | aiplatform.getDatasetsDatasetVersions({ |
| 38 | name: `${parent}/datasets/1234567890123456789/datasetVersions/0`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { |
| 46 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 47 | timeout: 90_000, |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | const imageDataset = { |
| 52 | location: "us-central1", |
| 53 | displayName: "version-parent", |
| 54 | metadataSchemaUri: |
| 55 | "gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml", |
| 56 | metadata: {}, |
| 57 | labels: { env: "test" }, |
| 58 | savedQueries: [ |
| 59 | { |
| 60 | displayName: "default", |
| 61 | problemType: "IMAGE_CLASSIFICATION_SINGLE_LABEL", |
| 62 | }, |
| 63 | ], |
| 64 | }; |
| 65 | |
| 66 | test.provider( |
| 67 | "creating a version of a dataset without data items fails with GCP.OperationFailed", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | const error = yield* Effect.flip( |
| 73 | stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const dataset = yield* GCP.AIPlatform.Dataset( |
| 76 | "Samples", |
| 77 | imageDataset, |
| 78 | ); |
| 79 | const version = yield* GCP.AIPlatform.DatasetsDatasetVersion("V1", { |
| 80 | dataset: dataset.name, |
| 81 | displayName: "v1", |
| 82 | }); |
| 83 | return { dataset, version }; |
| 84 | }), |
| 85 | ), |
| 86 | ); |
| 87 | expect(error._tag).toEqual("GCP.OperationFailed"); |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | }).pipe(logLevel), |
| 91 | { |
| 92 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 93 | timeout: 600_000, |
| 94 | }, |
| 95 | ); |
| 96 | |
| 97 | // Set GCP_TEST_AIPLATFORM_DATASET_VERSION on a project where this succeeds to run the full lifecycle. |
| 98 | test.provider.skipIf(!process.env.GCP_TEST_AIPLATFORM_DATASET_VERSION)( |
| 99 | "create, update, and delete a dataset version", |
| 100 | (stack) => |
| 101 | Effect.gen(function* () { |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const created = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | const dataset = yield* GCP.AIPlatform.Dataset( |
| 107 | "Samples", |
| 108 | imageDataset, |
| 109 | ); |
| 110 | const version = yield* GCP.AIPlatform.DatasetsDatasetVersion("V1", { |
| 111 | dataset: dataset.name, |
| 112 | displayName: "v1", |
| 113 | }); |
| 114 | return { dataset, version }; |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(created.version.name).toContain("/datasetVersions/"); |
| 119 | expect(created.version.dataset).toEqual(created.dataset.name); |
| 120 | expect(created.version.displayName).toEqual("v1"); |
| 121 | |
| 122 | const fetched = yield* aiplatform.getDatasetsDatasetVersions({ |
| 123 | name: created.version.name, |
| 124 | }); |
| 125 | expect(fetched.name).toEqual(created.version.name); |
| 126 | |
| 127 | const updated = yield* stack.deploy( |
| 128 | Effect.gen(function* () { |
| 129 | const dataset = yield* GCP.AIPlatform.Dataset("Samples", { |
| 130 | ...imageDataset, |
| 131 | datasetId: created.dataset.datasetId, |
| 132 | }); |
| 133 | const version = yield* GCP.AIPlatform.DatasetsDatasetVersion("V1", { |
| 134 | dataset: dataset.name, |
| 135 | displayName: "v1-final", |
| 136 | }); |
| 137 | return { dataset, version }; |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | expect(updated.version.name).toEqual(created.version.name); |
| 142 | expect(updated.version.displayName).toEqual("v1-final"); |
| 143 | |
| 144 | yield* stack.destroy(); |
| 145 | |
| 146 | const gone = yield* waitUntilGone(created.version.name); |
| 147 | expect(gone).toEqual("gone"); |
| 148 | }).pipe(logLevel), |
| 149 | { |
| 150 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 151 | timeout: 120_000, |
| 152 | }, |
| 153 | ); |