| 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 runLifecycle = !process.env.FAST; |
| 18 | |
| 19 | const waitUntilGone = (name: string) => |
| 20 | dlp.getProjectsDlpJobs({ name }).pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("1 second"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "getProjectsDlpJobs on a missing job fails with a typed tag", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | const { project } = yield* GcpEnvironment.current; |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | dlp.getProjectsDlpJobs({ |
| 39 | name: `projects/${project}/dlpJobs/i-alchemy-missing`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | }).pipe(logLevel), |
| 46 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 47 | ); |
| 48 | |
| 49 | test.provider.skipIf(!runLifecycle)( |
| 50 | "create and delete a hybrid inspect job", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | const { project } = yield* GcpEnvironment.current; |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.DLP.DlpJob("Scan", { |
| 59 | inspectJob: { |
| 60 | inspectConfig: { infoTypes: [{ name: "EMAIL_ADDRESS" }] }, |
| 61 | storageConfig: { |
| 62 | hybridOptions: { description: "hybrid scan" }, |
| 63 | }, |
| 64 | }, |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.jobId).toEqual(expect.any(String)); |
| 70 | expect(created.name).toEqual( |
| 71 | `projects/${project}/dlpJobs/${created.jobId}`, |
| 72 | ); |
| 73 | expect(created.project).toEqual(project); |
| 74 | expect(created.type).toEqual("INSPECT_JOB"); |
| 75 | expect(created.state).toEqual(expect.any(String)); |
| 76 | |
| 77 | const fetched = yield* dlp.getProjectsDlpJobs({ name: created.name }); |
| 78 | expect(fetched.name).toEqual(created.name); |
| 79 | expect(fetched.type).toEqual("INSPECT_JOB"); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const gone = yield* waitUntilGone(created.name); |
| 84 | expect(gone).toEqual("gone"); |
| 85 | }).pipe(logLevel), |
| 86 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 87 | ); |