The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as dlp from "@distilled.cloud/gcp/dlp_v2"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials |
| 13 | // administer the organization (the testing service account does not). |
| 14 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, ""); |
| 15 | const organization = `organizations/${organizationId}`; |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | const location = "us-central1"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | dlp.getOrganizationsLocationsJobTriggers({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("1 second"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider.skipIf(!organizationId)( |
| 33 | "getOrganizationsLocationsJobTriggers on a missing trigger fails with a typed tag", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const error = yield* Effect.flip( |
| 39 | dlp.getOrganizationsLocationsJobTriggers({ |
| 40 | name: `${organization}/locations/${location}/jobTriggers/alchemy-missing`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toEqual("NotFound"); |
| 44 | |
| 45 | yield* stack.destroy(); |
| 46 | }).pipe(logLevel), |
| 47 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 48 | ); |
| 49 | |
| 50 | test.provider.skipIf(!organizationId)( |
| 51 | "create, update, and delete an organization job trigger", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | const { project } = yield* GcpEnvironment.current; |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const inspectJob = { |
| 58 | inspectConfig: { infoTypes: [{ name: "EMAIL_ADDRESS" }] }, |
| 59 | storageConfig: { |
| 60 | cloudStorageOptions: { |
| 61 | fileSet: { url: `gs://${project}-dlp-noop/` }, |
| 62 | }, |
| 63 | }, |
| 64 | }; |
| 65 | |
| 66 | const parent = `${organization}/locations/${location}`; |
| 67 | |
| 68 | const created = yield* stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | return yield* GCP.DLP.OrganizationsLocationsJobTrigger("ScanBucket", { |
| 71 | organization, |
| 72 | location, |
| 73 | displayName: "scan-bucket", |
| 74 | description: "paused inspect", |
| 75 | status: "PAUSED", |
| 76 | triggers: [{ schedule: { recurrencePeriodDuration: "86400s" } }], |
| 77 | inspectJob, |
| 78 | }); |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | expect(created.location).toEqual(location); |
| 83 | expect(created.status).toEqual("PAUSED"); |
| 84 | expect(created.name).toEqual(`${parent}/jobTriggers/${created.triggerId}`); |
| 85 | |
| 86 | const fetched = yield* dlp.getOrganizationsLocationsJobTriggers({ |
| 87 | name: created.name, |
| 88 | }); |
| 89 | expect(fetched.description).toContain("alchemy-id="); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* GCP.DLP.OrganizationsLocationsJobTrigger("ScanBucket", { |
| 94 | organization, |
| 95 | location, |
| 96 | triggerId: created.triggerId, |
| 97 | displayName: "scan-bucket-v2", |
| 98 | description: "paused inspect v2", |
| 99 | status: "PAUSED", |
| 100 | triggers: [{ schedule: { recurrencePeriodDuration: "172800s" } }], |
| 101 | inspectJob, |
| 102 | }); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | expect(updated.name).toEqual(created.name); |
| 107 | expect(updated.displayName).toEqual("scan-bucket-v2"); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const gone = yield* waitUntilGone(created.name); |
| 112 | expect(gone).toEqual("gone"); |
| 113 | }).pipe(logLevel), |
| 114 | { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 }, |
| 115 | ); |