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 redact = (infoTypes: string[]) => ({
18 infoTypeTransformations: {
19 transformations: [
20 {
21 infoTypes: infoTypes.map((name) => ({ name })),
22 primitiveTransformation: { replaceWithInfoTypeConfig: {} },
23 },
24 ],
25 },
26});
27
28const waitUntilGone = (name: string) =>
29 dlp.getProjectsLocationsDeidentifyTemplates({ name }).pipe(
30 Effect.as("found" as const),
31 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
32 Effect.repeat({
33 schedule: Schedule.spaced("1 second"),
34 until: (status) => status === "gone",
35 times: 10,
36 }),
37 );
38
39test.provider(
40 "getProjectsLocationsDeidentifyTemplates on a missing template fails with a typed tag",
41 (stack) =>
42 Effect.gen(function* () {
43 const { project } = yield* GcpEnvironment.current;
44 yield* stack.destroy();
45
46 const error = yield* Effect.flip(
47 dlp.getProjectsLocationsDeidentifyTemplates({
48 name: `projects/${project}/locations/global/deidentifyTemplates/alchemy-missing`,
49 }),
50 );
51 expect(error._tag).toEqual("NotFound");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
56);
57
58test.provider(
59 "create, update, and delete a location deidentify template",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63 yield* stack.destroy();
64
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.DLP.LocationsDeidentifyTemplate("Emails", {
68 location: "global",
69 displayName: "redact emails",
70 description: "replace email findings",
71 deidentifyConfig: redact(["EMAIL_ADDRESS"]),
72 });
73 }),
74 );
75
76 expect(created.templateId).toEqual(expect.any(String));
77 expect(created.location).toEqual("global");
78 expect(created.name).toEqual(
79 `projects/${project}/locations/global/deidentifyTemplates/${created.templateId}`,
80 );
81 expect(created.displayName).toEqual("redact emails");
82 expect(created.description).toEqual("replace email findings");
83
84 const fetched = yield* dlp.getProjectsLocationsDeidentifyTemplates({
85 name: created.name,
86 });
87 expect(fetched.name).toEqual(created.name);
88 expect(fetched.description).toContain("alchemy-id=");
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.DLP.LocationsDeidentifyTemplate("Emails", {
93 templateId: created.templateId,
94 location: "global",
95 displayName: "redact emails and phones",
96 description: "replace email and phone findings",
97 deidentifyConfig: redact(["EMAIL_ADDRESS", "PHONE_NUMBER"]),
98 });
99 }),
100 );
101
102 expect(updated.name).toEqual(created.name);
103 expect(updated.displayName).toEqual("redact emails and phones");
104 expect(updated.description).toEqual("replace email and phone findings");
105
106 yield* stack.destroy();
107
108 const gone = yield* waitUntilGone(created.name);
109 expect(gone).toEqual("gone");
110 }).pipe(logLevel),
111 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
112);