1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as dlp from "@distilled.cloud/gcp/dlp_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const location = "us-central1";
18
19const hybridInspect = (infoTypes: string[]) => ({
20 inspectConfig: {
21 infoTypes: infoTypes.map((name) => ({ name })),
22 includeQuote: true,
23 },
24 storageConfig: { hybridOptions: { description: "hybrid inbox" } },
25});
26
27const waitUntilGone = (name: string) =>
28 dlp.getProjectsLocationsJobTriggers({ name }).pipe(
29 Effect.as("found" as const),
30 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("1 second"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38test.provider(
39 "getProjectsLocationsJobTriggers on a missing trigger fails with a typed tag",
40 (stack) =>
41 Effect.gen(function* () {
42 const { project } = yield* GcpEnvironment.current;
43 yield* stack.destroy();
44
45 const error = yield* Effect.flip(
46 dlp.getProjectsLocationsJobTriggers({
47 name: `projects/${project}/locations/${location}/jobTriggers/alchemy-missing`,
48 }),
49 );
50 expect(error._tag).toEqual("NotFound");
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
55);
56
57test.provider(
58 "create, update, and delete a location job trigger",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.DLP.LocationsJobTrigger("Inbox", {
67 location,
68 displayName: "inbox scan",
69 description: "paused hybrid inspect",
70 status: "PAUSED",
71 inspectJob: hybridInspect(["EMAIL_ADDRESS"]),
72 triggers: [{ manual: {} }],
73 });
74 }),
75 );
76
77 expect(created.triggerId).toEqual(expect.any(String));
78 expect(created.location).toEqual(location);
79 expect(created.name).toEqual(
80 `projects/${project}/locations/${location}/jobTriggers/${created.triggerId}`,
81 );
82 expect(created.project).toEqual(project);
83 expect(created.displayName).toEqual("inbox scan");
84 expect(created.description).toEqual("paused hybrid inspect");
85 expect(created.status).toEqual("PAUSED");
86
87 const fetched = yield* dlp.getProjectsLocationsJobTriggers({
88 name: created.name,
89 });
90 expect(fetched.name).toEqual(created.name);
91 expect(fetched.description).toContain("alchemy-id=");
92 expect(fetched.description).toContain("paused hybrid inspect");
93 expect(fetched.status).toEqual("PAUSED");
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.DLP.LocationsJobTrigger("Inbox", {
98 triggerId: created.triggerId,
99 location,
100 displayName: "inbox scan v2",
101 description: "paused hybrid inspect v2",
102 status: "PAUSED",
103 inspectJob: hybridInspect(["EMAIL_ADDRESS", "PHONE_NUMBER"]),
104 triggers: [{ manual: {} }],
105 });
106 }),
107 );
108
109 expect(updated.name).toEqual(created.name);
110 expect(updated.displayName).toEqual("inbox scan v2");
111 expect(updated.description).toEqual("paused hybrid inspect v2");
112
113 yield* stack.destroy();
114
115 const gone = yield* waitUntilGone(created.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
119);