| 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 HL7 = [ |
| 18 | "MSH|^~\\&|ALCHEMY|TESTFAC|RECEIVER|RECVFAC|20240101120000||ADT^A01|MSG00001|P|2.5", |
| 19 | "PID|1||PAT001^^^MR||DOE^JOHN", |
| 20 | ].join("\r"); |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | healthcare.getProjectsLocationsDatasetsHl7V2StoresMessages({ name }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("1 second"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsDatasetsHl7V2StoresMessages on a missing message fails with a typed tag", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | healthcare.getProjectsLocationsDatasetsHl7V2StoresMessages({ |
| 42 | name: `projects/${project}/locations/us-central1/datasets/alchemy-missing/hl7V2Stores/alchemy-missing/messages/alchemy-missing-hl7`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:healthcare", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "create, update, and delete an HL7v2 message", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const dataset = yield* GCP.Healthcare.Dataset("Clinic", {}); |
| 64 | const store = yield* GCP.Healthcare.DatasetsHl7V2Store("AdtStore", { |
| 65 | dataset: dataset.name, |
| 66 | }); |
| 67 | const message = yield* GCP.Healthcare.DatasetsHl7V2StoresMessage( |
| 68 | "Adt", |
| 69 | { |
| 70 | parent: store.name, |
| 71 | data: HL7, |
| 72 | labels: { env: "test" }, |
| 73 | }, |
| 74 | ); |
| 75 | return { dataset, store, message }; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.message.messageId.length).toBeGreaterThan(0); |
| 80 | expect(created.message.parent).toEqual(created.store.name); |
| 81 | expect(created.message.name).toContain("/messages/"); |
| 82 | expect(created.message.data).toContain("ADT^A01"); |
| 83 | expect(created.message.labels).toMatchObject({ env: "test" }); |
| 84 | |
| 85 | const fetched = |
| 86 | yield* healthcare.getProjectsLocationsDatasetsHl7V2StoresMessages({ |
| 87 | name: created.message.name, |
| 88 | view: "FULL", |
| 89 | }); |
| 90 | expect(fetched.name).toEqual(created.message.name); |
| 91 | expect(fetched.labels?.env).toEqual("test"); |
| 92 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 93 | |
| 94 | const updated = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | const dataset = yield* GCP.Healthcare.Dataset("Clinic", { |
| 97 | datasetId: created.dataset.datasetId, |
| 98 | location: created.dataset.location, |
| 99 | }); |
| 100 | const store = yield* GCP.Healthcare.DatasetsHl7V2Store("AdtStore", { |
| 101 | dataset: dataset.name, |
| 102 | hl7V2StoreId: created.store.hl7V2StoreId, |
| 103 | }); |
| 104 | const message = yield* GCP.Healthcare.DatasetsHl7V2StoresMessage( |
| 105 | "Adt", |
| 106 | { |
| 107 | parent: store.name, |
| 108 | messageId: created.message.messageId, |
| 109 | data: HL7, |
| 110 | labels: { env: "prod", role: "adt" }, |
| 111 | }, |
| 112 | ); |
| 113 | return { dataset, store, message }; |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(updated.message.messageId).toEqual(created.message.messageId); |
| 118 | expect(updated.message.labels).toMatchObject({ |
| 119 | env: "prod", |
| 120 | role: "adt", |
| 121 | }); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const gone = yield* waitUntilGone(created.message.name); |
| 126 | expect(gone).toEqual("gone"); |
| 127 | }).pipe(logLevel), |
| 128 | { |
| 129 | tags: ["provider:gcp", "provider:gcp:healthcare", "live"], |
| 130 | timeout: 120_000, |
| 131 | }, |
| 132 | ); |