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";
9import { UsersSource } from "./fixtures/features.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const waitUntilGone = (name: string) =>
19 aiplatform.getProjectsLocationsFeatureGroupsFeatures({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29const groupProps = {
30 location: "us-central1" as const,
31 description: "user features",
32 labels: { env: "test" },
33};
34
35test.provider(
36 "getProjectsLocationsFeatureGroupsFeatures on a missing feature fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 aiplatform.getProjectsLocationsFeatureGroupsFeatures({
44 name: `projects/${project}/locations/us-central1/featureGroups/alchemy_missing/features/alchemy_missing`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider(
58 "create, update, and delete a feature group feature",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 const inputUri = yield* UsersSource;
66 const group = yield* GCP.AIPlatform.FeatureGroup("Users", {
67 ...groupProps,
68 bigQuery: { inputUri, entityIdColumns: ["entity_id"] },
69 });
70 const feature = yield* GCP.AIPlatform.FeatureGroupsFeature("Age", {
71 featureGroup: group.name,
72 location: "us-central1",
73 description: "customer age",
74 versionColumnName: "age",
75 labels: { env: "test" },
76 });
77 return { group, feature };
78 }),
79 );
80
81 expect(created.feature.name).toContain("/features/");
82 expect(created.feature.featureGroup).toEqual(created.group.name);
83 expect(created.feature.labels).toMatchObject({ env: "test" });
84 expect(created.feature.description).toEqual("customer age");
85
86 const fetched =
87 yield* aiplatform.getProjectsLocationsFeatureGroupsFeatures({
88 name: created.feature.name,
89 });
90 expect(fetched.name).toEqual(created.feature.name);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 const inputUri = yield* UsersSource;
95 const group = yield* GCP.AIPlatform.FeatureGroup("Users", {
96 ...groupProps,
97 featureGroupId: created.group.featureGroupId,
98 bigQuery: { inputUri, entityIdColumns: ["entity_id"] },
99 });
100 const feature = yield* GCP.AIPlatform.FeatureGroupsFeature("Age", {
101 featureGroup: group.name,
102 featureId: created.feature.featureId,
103 location: "us-central1",
104 description: "customer age v2",
105 versionColumnName: "age",
106 labels: { env: "prod" },
107 });
108 return { group, feature };
109 }),
110 );
111
112 expect(updated.feature.name).toEqual(created.feature.name);
113 expect(updated.feature.description).toEqual("customer age v2");
114 expect(updated.feature.labels).toMatchObject({ env: "prod" });
115
116 yield* stack.destroy();
117 const gone = yield* waitUntilGone(created.feature.name);
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 {
121 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
122 timeout: 600_000,
123 },
124);