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 dataplex from "@distilled.cloud/gcp/dataplex_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { withDataplexSlot } from "./quota.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Data domains are allowlisted: the testing project gets Forbidden |
| 16 | // "Permission 'dataplex.dataDomains.get' denied". Set |
| 17 | // GCP_TEST_DATAPLEX_DATA_DOMAIN=1 on an allowlisted project. |
| 18 | const runLifecycle = process.env.GCP_TEST_DATAPLEX_DATA_DOMAIN === "1"; |
| 19 | |
| 20 | const contactsA: dataplex.GoogleCloudDataplexV1Contacts = { |
| 21 | identities: [ |
| 22 | { |
| 23 | contactName: "steward", |
| 24 | contactRole: "steward", |
| 25 | contactId: "steward@example.com", |
| 26 | }, |
| 27 | ], |
| 28 | }; |
| 29 | |
| 30 | const contactsB: dataplex.GoogleCloudDataplexV1Contacts = { |
| 31 | identities: [ |
| 32 | { |
| 33 | contactName: "owner", |
| 34 | contactRole: "owner", |
| 35 | contactId: "owner@example.com", |
| 36 | }, |
| 37 | ], |
| 38 | }; |
| 39 | |
| 40 | const waitUntilGone = (name: string) => |
| 41 | dataplex.getProjectsLocationsDataDomains({ name }).pipe( |
| 42 | Effect.as("found" as const), |
| 43 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 44 | Effect.repeat({ |
| 45 | schedule: Schedule.spaced("1 second"), |
| 46 | until: (status) => status === "gone", |
| 47 | times: 10, |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "getProjectsLocationsDataDomains on a missing domain fails with a typed tag", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const { project } = yield* GcpEnvironment.current; |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const error = yield* Effect.flip( |
| 59 | dataplex.getProjectsLocationsDataDomains({ |
| 60 | name: `projects/${project}/locations/us-central1/dataDomains/alchemy-missing-domain`, |
| 61 | }), |
| 62 | ); |
| 63 | expect(error._tag).toEqual(runLifecycle ? "NotFound" : "Forbidden"); |
| 64 | |
| 65 | yield* stack.destroy(); |
| 66 | }).pipe(logLevel, withDataplexSlot), |
| 67 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 68 | ); |
| 69 | |
| 70 | test.provider.skipIf(!runLifecycle)( |
| 71 | "create, update, and delete a data domain", |
| 72 | (stack) => |
| 73 | Effect.gen(function* () { |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | const created = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.Dataplex.DataDomain("Finance", { |
| 79 | location: "us-central1", |
| 80 | displayName: "Finance", |
| 81 | description: "domain a", |
| 82 | labels: { env: "test" }, |
| 83 | contacts: contactsA, |
| 84 | }); |
| 85 | }), |
| 86 | ); |
| 87 | |
| 88 | expect(created.name).toContain("/dataDomains/"); |
| 89 | expect(created.dataDomainId).toEqual(expect.any(String)); |
| 90 | expect(created.location).toEqual("us-central1"); |
| 91 | expect(created.displayName).toEqual("Finance"); |
| 92 | expect(created.description).toEqual("domain a"); |
| 93 | expect(created.labels).toMatchObject({ env: "test" }); |
| 94 | |
| 95 | const fetched = yield* dataplex.getProjectsLocationsDataDomains({ |
| 96 | name: created.name, |
| 97 | }); |
| 98 | expect(fetched.name).toEqual(created.name); |
| 99 | expect(fetched.labels?.env).toEqual("test"); |
| 100 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 101 | true, |
| 102 | ); |
| 103 | |
| 104 | const updated = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | return yield* GCP.Dataplex.DataDomain("Finance", { |
| 107 | dataDomainId: created.dataDomainId, |
| 108 | location: "us-central1", |
| 109 | displayName: "Finance prod", |
| 110 | description: "domain b", |
| 111 | labels: { env: "prod", team: "data" }, |
| 112 | contacts: contactsB, |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(updated.name).toEqual(created.name); |
| 118 | expect(updated.displayName).toEqual("Finance prod"); |
| 119 | expect(updated.description).toEqual("domain b"); |
| 120 | expect(updated.labels).toMatchObject({ env: "prod", team: "data" }); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | const gone = yield* waitUntilGone(created.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel, withDataplexSlot), |
| 126 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 127 | ); |