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.getProjectsLocationsNasJobs({ 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
28const nasJobSpec = {
29 searchSpaceSpec: "{}",
30 multiTrialAlgorithmSpec: {
31 metric: { metricId: "accuracy", goal: "MAXIMIZE" as const },
32 searchTrialSpec: {
33 maxTrialCount: 1,
34 maxParallelTrialCount: 1,
35 searchTrialJobSpec: {
36 workerPoolSpecs: [
37 {
38 machineSpec: { machineType: "n1-standard-4" },
39 replicaCount: "1",
40 containerSpec: {
41 imageUri:
42 "us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-12.py310:latest",
43 command: ["echo", "ok"],
44 },
45 },
46 ],
47 },
48 },
49 },
50};
51
52test.provider(
53 "getProjectsLocationsNasJobs on a missing job fails with a typed tag",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57 const parent = `projects/${project}/locations/us-central1`;
58 yield* stack.destroy();
59
60 const error = yield* Effect.flip(
61 aiplatform.getProjectsLocationsNasJobs({
62 name: `${parent}/nasJobs/1234567890123456789`,
63 }),
64 );
65 expect(error._tag).toEqual("NotFound");
66 const page = yield* aiplatform.listProjectsLocationsNasJobs({
67 parent,
68 pageSize: 10,
69 });
70 expect((page.nasJobs ?? []).map((item) => item.name)).not.toContain(
71 `${parent}/nasJobs/1234567890123456789`,
72 );
73
74 yield* stack.destroy();
75 }).pipe(logLevel),
76 {
77 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
78 timeout: 90_000,
79 },
80);
81
82test.provider(
83 "create and delete a nas job",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const created = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.AIPlatform.NasJob("Search", {
91 location: "us-central1",
92 displayName: "alchemy-nas",
93 nasJobSpec,
94 labels: { env: "test" },
95 });
96 }),
97 );
98
99 expect(created.name).toContain("/nasJobs/");
100 expect(created.location).toEqual("us-central1");
101 expect(created.labels).toMatchObject({ env: "test" });
102
103 const fetched = yield* aiplatform.getProjectsLocationsNasJobs({
104 name: created.name,
105 });
106 expect(fetched.name).toEqual(created.name);
107
108 yield* stack.destroy();
109
110 const gone = yield* waitUntilGone(created.name);
111 expect(gone).toEqual("gone");
112 }).pipe(logLevel),
113 {
114 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
115 timeout: 180_000,
116 },
117);