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
17// Pool create + update + delete takes ~1.5 minutes.
18const runLifecycle = !process.env.FAST;
19
20const waitUntilGone = (name: string) =>
21 aiplatform.getProjectsLocationsDeploymentResourcePools({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsDeploymentResourcePools on a missing pool fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 const parent = `projects/${project}/locations/us-central1`;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 aiplatform.getProjectsLocationsDeploymentResourcePools({
41 name: `${parent}/deploymentResourcePools/alchemy-aiplatform-missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45 const page =
46 yield* aiplatform.listProjectsLocationsDeploymentResourcePools({
47 parent,
48 pageSize: 10,
49 });
50 expect(
51 (page.deploymentResourcePools ?? []).map((item) => item.name),
52 ).not.toContain(
53 `${parent}/deploymentResourcePools/alchemy-aiplatform-missing`,
54 );
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 {
59 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
60 timeout: 90_000,
61 },
62);
63
64test.provider.skipIf(!runLifecycle)(
65 "create, update, and delete a deployment resource pool",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 const created = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* GCP.AIPlatform.DeploymentResourcePool("Shared", {
73 location: "us-central1",
74 dedicatedResources: {
75 minReplicaCount: 1,
76 maxReplicaCount: 1,
77 machineSpec: { machineType: "n1-standard-2" },
78 },
79 });
80 }),
81 );
82
83 expect(created.name).toContain("/deploymentResourcePools/");
84 expect(created.deploymentResourcePoolId.startsWith("alch-")).toEqual(
85 true,
86 );
87 expect(created.dedicatedResources?.minReplicaCount).toEqual(1);
88
89 const fetched =
90 yield* aiplatform.getProjectsLocationsDeploymentResourcePools({
91 name: created.name,
92 });
93 expect(fetched.name).toEqual(created.name);
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.AIPlatform.DeploymentResourcePool("Shared", {
98 deploymentResourcePoolId: created.deploymentResourcePoolId,
99 location: "us-central1",
100 dedicatedResources: {
101 minReplicaCount: 1,
102 maxReplicaCount: 2,
103 machineSpec: { machineType: "n1-standard-2" },
104 },
105 });
106 }),
107 );
108
109 expect(updated.name).toEqual(created.name);
110 expect(updated.dedicatedResources?.maxReplicaCount).toEqual(2);
111
112 yield* stack.destroy();
113
114 const gone = yield* waitUntilGone(created.name);
115 expect(gone).toEqual("gone");
116 }).pipe(logLevel),
117 {
118 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
119 timeout: 120_000,
120 },
121);