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.getProjectsLocationsStudies({ 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
28const 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
39test.provider(
40 "getProjectsLocationsStudies on a missing study 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.getProjectsLocationsStudies({
48 name: `projects/${project}/locations/us-central1/studies/1234567890123456789`,
49 }),
50 );
51 expect(error._tag).toEqual("NotFound");
52 const page = yield* aiplatform.listProjectsLocationsStudies({
53 parent: `projects/${project}/locations/us-central1`,
54 pageSize: 10,
55 });
56 expect((page.studies ?? []).map((item) => item.name)).not.toContain(
57 `projects/${project}/locations/us-central1/studies/1234567890123456789`,
58 );
59
60 yield* stack.destroy();
61 }).pipe(logLevel),
62 {
63 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
64 timeout: 90_000,
65 },
66);
67
68test.provider(
69 "create and delete a vizier study",
70 (stack) =>
71 Effect.gen(function* () {
72 yield* stack.destroy();
73
74 const created = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* GCP.AIPlatform.Study("Tune", {
77 location: "us-central1",
78 displayName: "accuracy_search",
79 studySpec,
80 });
81 }),
82 );
83
84 expect(created.name).toContain("/studies/");
85 expect(created.displayName).toEqual("accuracy_search");
86 expect(created.studySpec?.metrics?.[0]?.metricId).toEqual("accuracy");
87
88 const fetched = yield* aiplatform.getProjectsLocationsStudies({
89 name: created.name,
90 });
91 expect(fetched.name).toEqual(created.name);
92 expect(fetched.displayName).toContain("accuracy_search_alchemy_");
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.AIPlatform.Study("Tune", {
97 location: "us-central1",
98 displayName: "accuracy_search",
99 studySpec,
100 });
101 }),
102 );
103 expect(updated.name).toEqual(created.name);
104
105 yield* stack.destroy();
106 const gone = yield* waitUntilGone(created.name);
107 expect(gone).toEqual("gone");
108 }).pipe(logLevel),
109 {
110 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
111 timeout: 90_000,
112 },
113);