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.getProjectsLocationsNotebookRuntimeTemplates({ 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 "getProjectsLocationsNotebookRuntimeTemplates on a missing template 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.getProjectsLocationsNotebookRuntimeTemplates({
38 name: `${parent}/notebookRuntimeTemplates/alchemy-missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42 const page =
43 yield* aiplatform.listProjectsLocationsNotebookRuntimeTemplates({
44 parent,
45 pageSize: 10,
46 });
47 expect(
48 (page.notebookRuntimeTemplates ?? []).map((item) => item.name),
49 ).not.toContain(`${parent}/notebookRuntimeTemplates/alchemy-missing`);
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, update, and delete a notebook runtime template",
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.NotebookRuntimeTemplate("Runtime", {
68 location: "us-central1",
69 displayName: "alchemy-colab",
70 description: "colab default",
71 machineSpec: { machineType: "e2-standard-4" },
72 networkSpec: { enableInternetAccess: true },
73 labels: { env: "test" },
74 });
75 }),
76 );
77
78 expect(created.name).toContain("/notebookRuntimeTemplates/");
79 expect(created.location).toEqual("us-central1");
80 expect(created.labels).toMatchObject({ env: "test" });
81 expect(created.description).toEqual("colab default");
82
83 const fetched =
84 yield* aiplatform.getProjectsLocationsNotebookRuntimeTemplates({
85 name: created.name,
86 });
87 expect(fetched.name).toEqual(created.name);
88 expect(fetched.labels?.env).toEqual("test");
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.AIPlatform.NotebookRuntimeTemplate("Runtime", {
93 notebookRuntimeTemplateId: created.notebookRuntimeTemplateId,
94 location: "us-central1",
95 displayName: "alchemy-colab-v2",
96 description: "colab default",
97 machineSpec: { machineType: "e2-standard-4" },
98 networkSpec: { enableInternetAccess: true },
99 labels: { env: "test" },
100 });
101 }),
102 );
103
104 expect(updated.name).toEqual(created.name);
105 expect(updated.displayName).toEqual("alchemy-colab-v2");
106 expect(updated.labels).toMatchObject({ env: "test" });
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);