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 Output from "alchemy/Output"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { withDataplexSlot } from "./quota.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const runLifecycle = !process.env.FAST; |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | dataplex.getProjectsLocationsDataScans({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("2 seconds"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsDataScans on a missing scan fails with a typed tag", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | dataplex.getProjectsLocationsDataScans({ |
| 38 | name: `projects/${project}/locations/us-central1/dataScans/alchemy-missing-scan`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel, withDataplexSlot), |
| 45 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 46 | ); |
| 47 | |
| 48 | test.provider.skipIf(!runLifecycle)( |
| 49 | "create, update, and delete a data scan", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const created = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const dataset = yield* GCP.BigQuery.Dataset("ScanData", { |
| 57 | location: "US-CENTRAL1", |
| 58 | forceDestroy: true, |
| 59 | }); |
| 60 | const table = yield* GCP.BigQuery.Table("Orders", { |
| 61 | datasetId: dataset.datasetId, |
| 62 | schema: [{ name: "id", type: "STRING" }], |
| 63 | }); |
| 64 | return yield* GCP.Dataplex.DataScan("OrdersProfile", { |
| 65 | location: "us-central1", |
| 66 | displayName: "orders profile", |
| 67 | description: "scan a", |
| 68 | labels: { env: "test" }, |
| 69 | data: { |
| 70 | resource: Output.interpolate`//bigquery.googleapis.com/projects/${table.project}/datasets/${table.datasetId}/tables/${table.tableId}`, |
| 71 | }, |
| 72 | dataProfileSpec: { samplingPercent: 100 }, |
| 73 | }); |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | expect(created.name).toContain("/dataScans/"); |
| 78 | expect(created.dataScanId).toEqual(expect.any(String)); |
| 79 | expect(created.location).toEqual("us-central1"); |
| 80 | expect(created.displayName).toEqual("orders profile"); |
| 81 | expect(created.labels).toMatchObject({ env: "test" }); |
| 82 | |
| 83 | const fetched = yield* dataplex.getProjectsLocationsDataScans({ |
| 84 | name: created.name, |
| 85 | view: "FULL", |
| 86 | }); |
| 87 | expect(fetched.name).toEqual(created.name); |
| 88 | expect(fetched.labels?.env).toEqual("test"); |
| 89 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 90 | true, |
| 91 | ); |
| 92 | |
| 93 | const updated = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | const dataset = yield* GCP.BigQuery.Dataset("ScanData", { |
| 96 | location: "US-CENTRAL1", |
| 97 | forceDestroy: true, |
| 98 | }); |
| 99 | const table = yield* GCP.BigQuery.Table("Orders", { |
| 100 | datasetId: dataset.datasetId, |
| 101 | schema: [{ name: "id", type: "STRING" }], |
| 102 | }); |
| 103 | return yield* GCP.Dataplex.DataScan("OrdersProfile", { |
| 104 | dataScanId: created.dataScanId, |
| 105 | location: "us-central1", |
| 106 | displayName: "orders profile prod", |
| 107 | description: "scan b", |
| 108 | labels: { env: "prod", team: "data" }, |
| 109 | data: { |
| 110 | resource: Output.interpolate`//bigquery.googleapis.com/projects/${table.project}/datasets/${table.datasetId}/tables/${table.tableId}`, |
| 111 | }, |
| 112 | dataProfileSpec: { samplingPercent: 50 }, |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(updated.name).toEqual(created.name); |
| 118 | expect(updated.displayName).toEqual("orders profile prod"); |
| 119 | expect(updated.labels).toMatchObject({ env: "prod", team: "data" }); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | const gone = yield* waitUntilGone(created.name); |
| 123 | expect(gone).toEqual("gone"); |
| 124 | }).pipe(logLevel, withDataplexSlot), |
| 125 | { tags: ["provider:gcp", "provider:gcp:dataplex", "live"], timeout: 900_000 }, |
| 126 | ); |