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.getProjectsLocationsCustomJobs({ 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 "getProjectsLocationsCustomJobs on a missing job 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.getProjectsLocationsCustomJobs({
38 name: `${parent}/customJobs/1234567890123456789`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42 const page = yield* aiplatform.listProjectsLocationsCustomJobs({
43 parent,
44 pageSize: 10,
45 });
46 expect((page.customJobs ?? []).map((item) => item.name)).not.toContain(
47 `${parent}/customJobs/1234567890123456789`,
48 );
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 {
53 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
54 timeout: 90_000,
55 },
56);
57
58test.provider(
59 "create and delete a custom job",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.AIPlatform.CustomJob("Train", {
67 location: "us-central1",
68 displayName: "alchemy-echo",
69 jobSpec: {
70 workerPoolSpecs: [
71 {
72 machineSpec: { machineType: "n1-standard-4" },
73 replicaCount: "1",
74 containerSpec: {
75 imageUri: "gcr.io/google-samples/hello-app:1.0",
76 command: ["echo"],
77 args: ["ok"],
78 },
79 },
80 ],
81 scheduling: { timeout: "600s" },
82 },
83 labels: { env: "test" },
84 });
85 }),
86 );
87
88 expect(created.name).toContain("/customJobs/");
89 expect(created.labels).toMatchObject({ env: "test" });
90
91 const fetched = yield* aiplatform.getProjectsLocationsCustomJobs({
92 name: created.name,
93 });
94 expect(fetched.name).toEqual(created.name);
95
96 yield* stack.destroy();
97
98 const gone = yield* waitUntilGone(created.name);
99 expect(gone).toEqual("gone");
100 }).pipe(logLevel),
101 {
102 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
103 timeout: 120_000,
104 },
105);