| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as dlp from "@distilled.cloud/gcp/dlp_v2"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const redact = (infoTypes: string[]) => ({ |
| 18 | infoTypeTransformations: { |
| 19 | transformations: [ |
| 20 | { |
| 21 | infoTypes: infoTypes.map((name) => ({ name })), |
| 22 | primitiveTransformation: { replaceWithInfoTypeConfig: {} }, |
| 23 | }, |
| 24 | ], |
| 25 | }, |
| 26 | }); |
| 27 | |
| 28 | const waitUntilGone = (name: string) => |
| 29 | dlp.getProjectsDeidentifyTemplates({ 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 | |
| 39 | test.provider( |
| 40 | "getProjectsDeidentifyTemplates 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.getProjectsDeidentifyTemplates({ |
| 48 | name: `projects/${project}/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 | |
| 58 | test.provider( |
| 59 | "create, update, replace, and delete a 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.DeidentifyTemplate("Emails", { |
| 68 | displayName: "redact emails", |
| 69 | description: "replace email findings", |
| 70 | deidentifyConfig: redact(["EMAIL_ADDRESS"]), |
| 71 | }); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | expect(created.templateId).toEqual(expect.any(String)); |
| 76 | expect(created.name).toEqual( |
| 77 | `projects/${project}/deidentifyTemplates/${created.templateId}`, |
| 78 | ); |
| 79 | expect(created.project).toEqual(project); |
| 80 | expect(created.displayName).toEqual("redact emails"); |
| 81 | expect(created.description).toEqual("replace email findings"); |
| 82 | |
| 83 | const fetched = yield* dlp.getProjectsDeidentifyTemplates({ |
| 84 | name: created.name, |
| 85 | }); |
| 86 | expect(fetched.name).toEqual(created.name); |
| 87 | expect(fetched.description).toContain("alchemy-id="); |
| 88 | expect(fetched.description).toContain("replace email findings"); |
| 89 | |
| 90 | const updated = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | return yield* GCP.DLP.DeidentifyTemplate("Emails", { |
| 93 | templateId: created.templateId, |
| 94 | displayName: "redact emails and phones", |
| 95 | description: "replace email and phone findings", |
| 96 | deidentifyConfig: redact(["EMAIL_ADDRESS", "PHONE_NUMBER"]), |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(updated.name).toEqual(created.name); |
| 102 | expect(updated.displayName).toEqual("redact emails and phones"); |
| 103 | expect(updated.description).toEqual("replace email and phone findings"); |
| 104 | |
| 105 | const last = created.templateId.at(-1) ?? "a"; |
| 106 | const nextId = `${created.templateId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 107 | |
| 108 | const replaced = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | return yield* GCP.DLP.DeidentifyTemplate("Emails", { |
| 111 | templateId: nextId, |
| 112 | displayName: "replaced", |
| 113 | deidentifyConfig: redact(["EMAIL_ADDRESS"]), |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(replaced.templateId).not.toEqual(created.templateId); |
| 119 | |
| 120 | const previousGone = yield* waitUntilGone(created.name); |
| 121 | expect(previousGone).toEqual("gone"); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const gone = yield* waitUntilGone(replaced.name); |
| 126 | expect(gone).toEqual("gone"); |
| 127 | }).pipe(logLevel), |
| 128 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 129 | ); |