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 = "global";
18
19const hybridInspect = {
20 inspectConfig: {
21 infoTypes: [{ name: "EMAIL_ADDRESS" }],
22 includeQuote: true,
23 },
24 storageConfig: { hybridOptions: { description: "hybrid scan" } },
25};
26
27const waitUntilGone = (name: string) =>
28 dlp.getProjectsLocationsDlpJobs({ 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 "getProjectsLocationsDlpJobs on a missing job 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.getProjectsLocationsDlpJobs({
47 name: `projects/${project}/locations/${location}/dlpJobs/i-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, refresh, and delete a location dlp job",
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.LocationsDlpJob("Scan", {
67 location,
68 inspectJob: hybridInspect,
69 });
70 }),
71 );
72
73 expect(created.jobId.startsWith("i-")).toEqual(true);
74 expect(created.location).toEqual(location);
75 expect(created.name).toEqual(
76 `projects/${project}/locations/${location}/dlpJobs/${created.jobId}`,
77 );
78 expect(created.project).toEqual(project);
79 expect(created.type).toEqual("INSPECT_JOB");
80 expect(created.state).toEqual(expect.any(String));
81
82 const fetched = yield* dlp.getProjectsLocationsDlpJobs({
83 name: created.name,
84 });
85 expect(fetched.name).toEqual(created.name);
86 expect(fetched.type).toEqual("INSPECT_JOB");
87 const hybrid =
88 fetched.inspectDetails?.requestedOptions?.jobConfig?.storageConfig
89 ?.hybridOptions;
90 expect(
91 Object.keys(hybrid?.labels ?? {}).some((key) =>
92 key.startsWith("alchemy-"),
93 ),
94 ).toEqual(true);
95 expect(hybrid?.description).toContain("alchemy-id=");
96
97 const refreshed = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* GCP.DLP.LocationsDlpJob("Scan", {
100 jobId: created.jobId,
101 location,
102 inspectJob: hybridInspect,
103 });
104 }),
105 );
106
107 expect(refreshed.name).toEqual(created.name);
108 expect(refreshed.jobId).toEqual(created.jobId);
109
110 yield* stack.destroy();
111
112 const gone = yield* waitUntilGone(created.name);
113 expect(gone).toEqual("gone");
114 }).pipe(logLevel),
115 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
116);