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.getProjectsLocationsDatasetsConsentStores({ 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 "getProjectsLocationsDatasetsConsentStores 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.getProjectsLocationsDatasetsConsentStores({
37 name: `projects/${project}/locations/us-central1/datasets/missing/consentStores/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 consent 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("Clinic", {
59 location: "us-central1",
60 });
61 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
62 dataset: dataset.name,
63 labels: { env: "test" },
64 });
65 return { dataset, store };
66 }),
67 );
68
69 expect(created.store.name).toContain("/consentStores/");
70 expect(created.store.dataset).toEqual(created.dataset.name);
71 expect(created.store.labels).toMatchObject({ env: "test" });
72 expect(created.store.enableConsentCreateOnUpdate).toEqual(false);
73
74 const fetched =
75 yield* healthcare.getProjectsLocationsDatasetsConsentStores({
76 name: created.store.name,
77 });
78 expect(fetched.name).toEqual(created.store.name);
79 expect(fetched.labels?.env).toEqual("test");
80 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 const dataset = yield* GCP.Healthcare.Dataset("Clinic", {
85 datasetId: created.dataset.datasetId,
86 location: "us-central1",
87 });
88 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
89 dataset: dataset.name,
90 consentStoreId: created.store.consentStoreId,
91 labels: { env: "prod", role: "consent" },
92 enableConsentCreateOnUpdate: true,
93 });
94 return { dataset, store };
95 }),
96 );
97
98 expect(updated.store.name).toEqual(created.store.name);
99 expect(updated.store.enableConsentCreateOnUpdate).toEqual(true);
100 expect(updated.store.labels).toMatchObject({
101 env: "prod",
102 role: "consent",
103 });
104
105 yield* stack.destroy();
106 const gone = yield* waitUntilGone(created.store.name);
107 expect(gone).toEqual("gone");
108 }).pipe(logLevel),
109 {
110 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
111 timeout: 120_000,
112 },
113);