| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as healthcare from "@distilled.cloud/gcp/healthcare_v1"; |
| 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 waitUntilGone = (name: string) => |
| 18 | healthcare |
| 19 | .getProjectsLocationsDatasetsConsentStoresUserDataMappings({ name }) |
| 20 | .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 | "getProjectsLocationsDatasetsConsentStoresUserDataMappings on a missing mapping 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 | healthcare.getProjectsLocationsDatasetsConsentStoresUserDataMappings({ |
| 39 | name: `projects/${project}/locations/us-central1/datasets/missing/consentStores/missing/userDataMappings/alchemy-missing`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | }).pipe(logLevel), |
| 46 | { |
| 47 | tags: ["provider:gcp", "provider:gcp:healthcare", "live"], |
| 48 | timeout: 90_000, |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | test.provider( |
| 53 | "create, update, and delete a user data mapping", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const created = yield* stack.deploy( |
| 59 | Effect.gen(function* () { |
| 60 | const dataset = yield* GCP.Healthcare.Dataset("MappingClinic", { |
| 61 | location: "us-central1", |
| 62 | }); |
| 63 | const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", { |
| 64 | dataset: dataset.name, |
| 65 | labels: { env: "test" }, |
| 66 | }); |
| 67 | const mapping = |
| 68 | yield* GCP.Healthcare.DatasetsConsentStoresUserDataMapping( |
| 69 | "Chart", |
| 70 | { |
| 71 | consentStore: store.name, |
| 72 | userId: "user-123", |
| 73 | dataId: "Patient/abc", |
| 74 | }, |
| 75 | ); |
| 76 | return { dataset, store, mapping }; |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | expect(created.mapping.name).toContain("/userDataMappings/"); |
| 81 | expect(created.mapping.userId).toEqual("user-123"); |
| 82 | expect(created.mapping.dataId).toEqual("Patient/abc"); |
| 83 | |
| 84 | const fetched = |
| 85 | yield* healthcare.getProjectsLocationsDatasetsConsentStoresUserDataMappings( |
| 86 | { |
| 87 | name: created.mapping.name, |
| 88 | }, |
| 89 | ); |
| 90 | expect(fetched.name).toEqual(created.mapping.name); |
| 91 | expect(fetched.userId).toEqual("user-123"); |
| 92 | expect(fetched.dataId).toContain("Patient/abc"); |
| 93 | expect(fetched.dataId?.startsWith("alc-")).toEqual(true); |
| 94 | |
| 95 | const updated = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | const dataset = yield* GCP.Healthcare.Dataset("MappingClinic", { |
| 98 | datasetId: created.dataset.datasetId, |
| 99 | location: "us-central1", |
| 100 | }); |
| 101 | const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", { |
| 102 | dataset: dataset.name, |
| 103 | consentStoreId: created.store.consentStoreId, |
| 104 | labels: { env: "test" }, |
| 105 | }); |
| 106 | const mapping = |
| 107 | yield* GCP.Healthcare.DatasetsConsentStoresUserDataMapping( |
| 108 | "Chart", |
| 109 | { |
| 110 | consentStore: store.name, |
| 111 | userId: "user-123", |
| 112 | dataId: "Patient/xyz", |
| 113 | }, |
| 114 | ); |
| 115 | return { dataset, store, mapping }; |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(updated.mapping.name).toEqual(created.mapping.name); |
| 120 | expect(updated.mapping.dataId).toEqual("Patient/xyz"); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | const gone = yield* waitUntilGone(created.mapping.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel), |
| 126 | { |
| 127 | tags: ["provider:gcp", "provider:gcp:healthcare", "live"], |
| 128 | timeout: 120_000, |
| 129 | }, |
| 130 | ); |