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.getProjectsLocationsEvaluationRuns({ 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 "getProjectsLocationsEvaluationRuns on a missing run 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.getProjectsLocationsEvaluationRuns({
38 name: `${parent}/evaluationRuns/1234567890123456789`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42 const page = yield* aiplatform.listProjectsLocationsEvaluationRuns({
43 parent,
44 pageSize: 10,
45 });
46 expect(
47 (page.evaluationRuns ?? []).map((item) => item.name),
48 ).not.toContain(`${parent}/evaluationRuns/1234567890123456789`);
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 {
53 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
54 timeout: 90_000,
55 },
56);
57
58test.provider(
59 "create and delete a vertex evaluation run",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const item = yield* GCP.AIPlatform.EvaluationItem("Prompt", {
67 location: "us-central1",
68 displayName: "alchemy-eval-run-item",
69 evaluationItemType: "REQUEST",
70 evaluationRequest: { prompt: { text: "What is 2+2?" } },
71 labels: { env: "test" },
72 });
73 const set = yield* GCP.AIPlatform.EvaluationSet("Prompts", {
74 location: "us-central1",
75 displayName: "alchemy-eval-run-set",
76 evaluationItems: [item.name],
77 });
78 return yield* GCP.AIPlatform.EvaluationRun("Quality", {
79 location: "us-central1",
80 displayName: "alchemy-eval-run",
81 dataSource: { evaluationSet: set.name },
82 evaluationConfig: {
83 metrics: [
84 {
85 metric: "instruction_following_v1",
86 predefinedMetricSpec: {
87 metricSpecName: "instruction_following_v1",
88 },
89 },
90 ],
91 },
92 labels: { env: "test" },
93 });
94 }),
95 );
96
97 expect(created.name).toContain("/evaluationRuns/");
98 expect(created.location).toEqual("us-central1");
99 expect(created.labels).toMatchObject({ env: "test" });
100
101 const fetched = yield* aiplatform.getProjectsLocationsEvaluationRuns({
102 name: created.name,
103 });
104 expect(fetched.name).toEqual(created.name);
105
106 yield* stack.destroy();
107
108 const gone = yield* waitUntilGone(created.name);
109 expect(gone).toEqual("gone");
110 }).pipe(logLevel),
111 {
112 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
113 timeout: 180_000,
114 },
115);