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";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11// Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
12// administer the organization (the testing service account does not).
13const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(
14 /^organizations\//,
15 "",
16);
17const organization = `organizations/${organizationId}`;
18
19const logLevel = Effect.provideService(
20 MinimumLogLevel,
21 process.env.DEBUG ? "Debug" : "Info",
22);
23
24const waitUntilGone = (name: string) =>
25 dlp.getOrganizationsInspectTemplates({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider.skipIf(!organizationId)(
36 "getOrganizationsInspectTemplates on a missing template fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 dlp.getOrganizationsInspectTemplates({
43 name: `${organization}/inspectTemplates/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
51);
52
53test.provider.skipIf(!organizationId)(
54 "create, update, and delete an organization inspect template",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.DLP.OrganizationsInspectTemplate("Emails", {
62 organization,
63 displayName: "emails",
64 description: "detect emails",
65 inspectConfig: {
66 infoTypes: [{ name: "EMAIL_ADDRESS" }],
67 includeQuote: true,
68 },
69 });
70 }),
71 );
72
73 expect(created.templateId).toEqual(expect.any(String));
74 expect(created.name).toEqual(
75 `${organization}/inspectTemplates/${created.templateId}`,
76 );
77 expect(created.description).toEqual("detect emails");
78
79 const fetched = yield* dlp.getOrganizationsInspectTemplates({
80 name: created.name,
81 });
82 expect(fetched.name).toEqual(created.name);
83 expect(fetched.description).toContain("alchemy-id=");
84 expect(fetched.inspectConfig?.includeQuote).toEqual(true);
85
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.DLP.OrganizationsInspectTemplate("Emails", {
89 organization,
90 templateId: created.templateId,
91 displayName: "emails-v2",
92 description: "detect emails and phones",
93 inspectConfig: {
94 infoTypes: [{ name: "EMAIL_ADDRESS" }, { name: "PHONE_NUMBER" }],
95 includeQuote: false,
96 },
97 });
98 }),
99 );
100
101 expect(updated.name).toEqual(created.name);
102 expect(updated.displayName).toEqual("emails-v2");
103 expect(updated.description).toEqual("detect emails and phones");
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.name);
108 expect(gone).toEqual("gone");
109 }).pipe(logLevel),
110 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
111);