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
19 .getProjectsLocationsDatasetsConsentStoresAttributeDefinitions({ 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
30test.provider(
31 "getProjectsLocationsDatasetsConsentStoresAttributeDefinitions on a missing definition 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.getProjectsLocationsDatasetsConsentStoresAttributeDefinitions(
39 {
40 name: `projects/${project}/locations/us-central1/datasets/missing/consentStores/missing/attributeDefinitions/alchemy_missing`,
41 },
42 ),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 {
49 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
50 timeout: 90_000,
51 },
52);
53
54test.provider(
55 "create, update, and delete an attribute definition",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const dataset = yield* GCP.Healthcare.Dataset("AttrClinic", {
63 location: "us-central1",
64 });
65 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
66 dataset: dataset.name,
67 labels: { env: "test" },
68 });
69 const attr =
70 yield* GCP.Healthcare.DatasetsConsentStoresAttributeDefinition(
71 "DataType",
72 {
73 consentStore: store.name,
74 category: "RESOURCE",
75 allowedValues: ["fhir", "dicom"],
76 description: "data modality",
77 },
78 );
79 return { dataset, store, attr };
80 }),
81 );
82
83 expect(created.attr.name).toContain("/attributeDefinitions/");
84 expect(created.attr.category).toEqual("RESOURCE");
85 expect(created.attr.allowedValues).toEqual(["fhir", "dicom"]);
86 expect(created.attr.description).toEqual("data modality");
87
88 const fetched =
89 yield* healthcare.getProjectsLocationsDatasetsConsentStoresAttributeDefinitions(
90 {
91 name: created.attr.name,
92 },
93 );
94 expect(fetched.name).toEqual(created.attr.name);
95 expect(fetched.description).toContain("[alchemy ");
96 expect(fetched.allowedValues).toEqual(["fhir", "dicom"]);
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 const dataset = yield* GCP.Healthcare.Dataset("AttrClinic", {
101 datasetId: created.dataset.datasetId,
102 location: "us-central1",
103 });
104 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
105 dataset: dataset.name,
106 consentStoreId: created.store.consentStoreId,
107 labels: { env: "test" },
108 });
109 const attr =
110 yield* GCP.Healthcare.DatasetsConsentStoresAttributeDefinition(
111 "DataType",
112 {
113 consentStore: store.name,
114 attributeDefinitionId: created.attr.attributeDefinitionId,
115 category: "RESOURCE",
116 allowedValues: ["fhir", "dicom", "hl7"],
117 description: "data modality expanded",
118 },
119 );
120 return { dataset, store, attr };
121 }),
122 );
123
124 expect(updated.attr.name).toEqual(created.attr.name);
125 expect(updated.attr.allowedValues).toEqual(["fhir", "dicom", "hl7"]);
126 expect(updated.attr.description).toEqual("data modality expanded");
127
128 yield* stack.destroy();
129 const gone = yield* waitUntilGone(created.attr.name);
130 expect(gone).toEqual("gone");
131 }).pipe(logLevel),
132 {
133 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
134 timeout: 120_000,
135 },
136);