1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 aiplatform.getProjectsLocationsTrainingPipelines({ 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
28test.provider(
29 "getProjectsLocationsTrainingPipelines on a missing pipeline fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 aiplatform.getProjectsLocationsTrainingPipelines({
37 name: `projects/${project}/locations/us-central1/trainingPipelines/1234567890123456789`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41 const page = yield* aiplatform.listProjectsLocationsTrainingPipelines({
42 parent: `projects/${project}/locations/us-central1`,
43 pageSize: 10,
44 });
45 expect(
46 (page.trainingPipelines ?? []).map((item) => item.name),
47 ).not.toContain(
48 `projects/${project}/locations/us-central1/trainingPipelines/1234567890123456789`,
49 );
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 {
54 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
55 timeout: 90_000,
56 },
57);
58
59test.provider(
60 "create, refresh, and delete a training pipeline",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.AIPlatform.TrainingPipeline("Train", {
68 location: "us-central1",
69 displayName: "alchemy-test-pipeline",
70 trainingTaskDefinition:
71 "gs://google-cloud-aiplatform/schema/trainingjob/definition/custom_task_1.0.0.yaml",
72 trainingTaskInputs: {
73 workerPoolSpecs: [
74 {
75 machineSpec: { machineType: "n1-standard-4" },
76 replicaCount: "1",
77 containerSpec: {
78 imageUri:
79 "us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-12.py310:latest",
80 command: ["echo", "ok"],
81 },
82 },
83 ],
84 },
85 labels: { env: "test" },
86 });
87 }),
88 );
89
90 expect(created.name).toContain("/trainingPipelines/");
91 expect(created.trainingPipelineId).toEqual(expect.any(String));
92 expect(created.location).toEqual("us-central1");
93 expect(created.labels).toMatchObject({ env: "test" });
94
95 const fetched = yield* aiplatform.getProjectsLocationsTrainingPipelines({
96 name: created.name,
97 });
98 expect(fetched.name).toEqual(created.name);
99 expect(fetched.labels?.env).toEqual("test");
100
101 const updated = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* GCP.AIPlatform.TrainingPipeline("Train", {
104 trainingPipelineId: created.trainingPipelineId,
105 location: "us-central1",
106 displayName: "alchemy-test-pipeline",
107 trainingTaskDefinition: created.trainingTaskDefinition ?? "",
108 trainingTaskInputs: created.trainingTaskInputs,
109 labels: { env: "prod" },
110 });
111 }),
112 );
113
114 expect(updated.name).toEqual(created.name);
115 expect(updated.trainingPipelineId).toEqual(created.trainingPipelineId);
116
117 yield* stack.destroy();
118
119 const gone = yield* waitUntilGone(created.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 {
123 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
124 timeout: 120_000,
125 },
126);