1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as ml from "@distilled.cloud/gcp/ml_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { logLevel, currentProject, region, runLifecycle } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 ml.getProjectsModels({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("1 second"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22test.provider(
23 "getProjectsModels on a missing model fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 const project = yield* currentProject;
27
28 yield* stack.destroy();
29
30 const error = yield* Effect.flip(
31 ml.getProjectsModels({
32 name: `projects/${project}/models/alchemy-missing-model`,
33 }),
34 );
35 expect(error._tag).toEqual("NotFound");
36
37 yield* stack.destroy();
38 }).pipe(logLevel),
39 { tags: ["provider:gcp", "provider:gcp:ml", "live"], timeout: 90_000 },
40);
41
42test.provider.skipIf(runLifecycle)(
43 "createProjectsModels is rejected with AiPlatformDeprecated",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 stack.deploy(
50 Effect.gen(function* () {
51 return yield* GCP.ML.Model("Classifier", {
52 description: "probe",
53 labels: { env: "test" },
54 regions: [region],
55 });
56 }),
57 ),
58 );
59 expect(error._tag).toEqual("AiPlatformDeprecated");
60
61 yield* stack.destroy();
62 }).pipe(logLevel),
63 { tags: ["provider:gcp", "provider:gcp:ml", "live"], timeout: 90_000 },
64);
65
66test.provider.skipIf(!runLifecycle)(
67 "create, update, and delete a model",
68 (stack) =>
69 Effect.gen(function* () {
70 const project = yield* currentProject;
71
72 yield* stack.destroy();
73
74 const created = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* GCP.ML.Model("Classifier", {
77 description: "image classifier",
78 labels: { env: "test" },
79 regions: [region],
80 });
81 }),
82 );
83
84 expect(created.name.startsWith(`projects/${project}/models/`)).toEqual(
85 true,
86 );
87 expect(created.modelId.length).toBeGreaterThan(0);
88 expect(created.description).toEqual("image classifier");
89 expect(created.labels).toMatchObject({ env: "test" });
90 expect(created.regions).toEqual([region]);
91
92 const fetched = yield* ml.getProjectsModels({ name: created.name });
93 expect(fetched.name).toEqual(created.name);
94 expect(fetched.description).toContain("[alchemy ");
95 expect(fetched.description).toContain("image classifier");
96 expect(fetched.labels?.env).toEqual("test");
97 expect(
98 Object.keys(fetched.labels ?? {}).some((key) =>
99 key.startsWith("alchemy-"),
100 ),
101 ).toEqual(true);
102
103 const updated = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.ML.Model("Classifier", {
106 modelId: created.modelId,
107 description: "image classifier v2",
108 labels: { env: "prod" },
109 regions: [region],
110 });
111 }),
112 );
113
114 expect(updated.name).toEqual(created.name);
115 expect(updated.description).toEqual("image classifier v2");
116
117 const fetchedUpdate = yield* ml.getProjectsModels({
118 name: created.name,
119 });
120 expect(fetchedUpdate.description).toContain("image classifier v2");
121 expect(fetchedUpdate.description).toContain("alchemy-id=");
122
123 yield* stack.destroy();
124 const gone = yield* waitUntilGone(created.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 { tags: ["provider:gcp", "provider:gcp:ml", "live"], timeout: 90_000 },
128);