1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as ces from "@distilled.cloud/gcp/ces_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 ces.getProjectsLocationsAppsGuardrails({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsAppsGuardrails on a missing guardrail fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 ces.getProjectsLocationsAppsGuardrails({
38 name: `projects/${project}/locations/us/apps/missing/guardrails/missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
46);
47
48test.provider(
49 "create, update, and delete a guardrail",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 const app = yield* GCP.CES.App("Support", {
57 location: "us",
58 displayName: "support-guardrail",
59 });
60 const guardrail = yield* GCP.CES.AppsGuardrail("Safety", {
61 app: app.name,
62 displayName: "safety",
63 description: "ban phrases",
64 contentFilter: {
65 matchType: "SIMPLE_STRING_MATCH",
66 bannedContents: ["forbidden"],
67 },
68 });
69 return { app, guardrail };
70 }),
71 );
72
73 expect(created.guardrail.name).toContain("/guardrails/");
74 expect(created.guardrail.app).toEqual(created.app.name);
75 expect(created.guardrail.displayName).toEqual("safety");
76 expect(created.guardrail.description).toEqual("ban phrases");
77 expect(created.guardrail.contentFilter?.bannedContents).toContain(
78 "forbidden",
79 );
80
81 const fetched = yield* ces.getProjectsLocationsAppsGuardrails({
82 name: created.guardrail.name,
83 });
84 expect(fetched.name).toEqual(created.guardrail.name);
85 expect(fetched.description).toContain("alchemy-id=");
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 const app = yield* GCP.CES.App("Support", {
90 appId: created.app.appId,
91 location: created.app.location,
92 displayName: "support-guardrail",
93 });
94 const guardrail = yield* GCP.CES.AppsGuardrail("Safety", {
95 app: app.name,
96 guardrailId: created.guardrail.guardrailId,
97 displayName: "safety",
98 description: "ban more phrases",
99 contentFilter: {
100 matchType: "SIMPLE_STRING_MATCH",
101 bannedContents: ["forbidden", "blocked"],
102 },
103 });
104 return { app, guardrail };
105 }),
106 );
107
108 expect(updated.guardrail.name).toEqual(created.guardrail.name);
109 expect(updated.guardrail.description).toEqual("ban more phrases");
110 expect(updated.guardrail.contentFilter?.bannedContents).toContain(
111 "blocked",
112 );
113
114 yield* stack.destroy();
115 const gone = yield* waitUntilGone(created.guardrail.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
119);