1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as healthcare from "@distilled.cloud/gcp/healthcare_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 healthcare.getProjectsLocationsDatasetsFhirStores({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsDatasetsFhirStores on a missing store fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 healthcare.getProjectsLocationsDatasetsFhirStores({
37 name: `projects/${project}/locations/us-central1/datasets/missing/fhirStores/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, and delete a FHIR store",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const dataset = yield* GCP.Healthcare.Dataset("Records", {
59 location: "us-central1",
60 });
61 const store = yield* GCP.Healthcare.DatasetsFhirStore("Fhir", {
62 dataset: dataset.name,
63 version: "R4",
64 labels: { env: "test" },
65 });
66 return { dataset, store };
67 }),
68 );
69
70 expect(created.store.name).toContain("/fhirStores/");
71 expect(created.store.version).toEqual("R4");
72 expect(created.store.enableUpdateCreate).toEqual(false);
73 expect(created.store.labels).toMatchObject({ env: "test" });
74
75 const fetched = yield* healthcare.getProjectsLocationsDatasetsFhirStores({
76 name: created.store.name,
77 });
78 expect(fetched.name).toEqual(created.store.name);
79 expect(fetched.version).toEqual("R4");
80 expect(fetched.labels?.env).toEqual("test");
81 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 const dataset = yield* GCP.Healthcare.Dataset("Records", {
86 datasetId: created.dataset.datasetId,
87 location: "us-central1",
88 });
89 const store = yield* GCP.Healthcare.DatasetsFhirStore("Fhir", {
90 dataset: dataset.name,
91 fhirStoreId: created.store.fhirStoreId,
92 version: "R4",
93 enableUpdateCreate: true,
94 labels: { env: "prod", role: "fhir" },
95 });
96 return { dataset, store };
97 }),
98 );
99
100 expect(updated.store.name).toEqual(created.store.name);
101 expect(updated.store.enableUpdateCreate).toEqual(true);
102 expect(updated.store.labels).toMatchObject({ env: "prod", role: "fhir" });
103
104 yield* stack.destroy();
105 const gone = yield* waitUntilGone(created.store.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 {
109 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
110 timeout: 120_000,
111 },
112);