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.getProjectsLocationsDatasetsConsentStoresConsents({ 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 "getProjectsLocationsDatasetsConsentStoresConsents on a missing consent 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.getProjectsLocationsDatasetsConsentStoresConsents({
37 name: `projects/${project}/locations/us-central1/datasets/missing/consentStores/missing/consents/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",
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("ConsentClinic", {
59 location: "us-central1",
60 });
61 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
62 dataset: dataset.name,
63 labels: { env: "test" },
64 });
65 const artifact =
66 yield* GCP.Healthcare.DatasetsConsentStoresConsentArtifact(
67 "Proof",
68 {
69 consentStore: store.name,
70 userId: "user-123",
71 consentContentVersion: "v1",
72 },
73 );
74 const consent = yield* GCP.Healthcare.DatasetsConsentStoresConsent(
75 "Grant",
76 {
77 consentStore: store.name,
78 userId: "user-123",
79 consentArtifact: artifact.name,
80 state: "DRAFT",
81 metadata: { source: "app" },
82 },
83 );
84 return { dataset, store, artifact, consent };
85 }),
86 );
87
88 expect(created.consent.name).toContain("/consents/");
89 expect(created.consent.userId).toEqual("user-123");
90 expect(created.consent.state).toEqual("DRAFT");
91 expect(created.consent.metadata).toMatchObject({ source: "app" });
92
93 const fetched =
94 yield* healthcare.getProjectsLocationsDatasetsConsentStoresConsents({
95 name: created.consent.name,
96 });
97 expect(fetched.name).toEqual(created.consent.name);
98 expect(fetched.state).toEqual("DRAFT");
99 expect(fetched.metadata?.source).toEqual("app");
100 expect(fetched.metadata?.["alchemy-id"]).toEqual(expect.any(String));
101
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 const dataset = yield* GCP.Healthcare.Dataset("ConsentClinic", {
105 datasetId: created.dataset.datasetId,
106 location: "us-central1",
107 });
108 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
109 dataset: dataset.name,
110 consentStoreId: created.store.consentStoreId,
111 labels: { env: "test" },
112 });
113 const artifact =
114 yield* GCP.Healthcare.DatasetsConsentStoresConsentArtifact(
115 "Proof",
116 {
117 consentStore: store.name,
118 userId: "user-123",
119 consentContentVersion: "v1",
120 },
121 );
122 const consent = yield* GCP.Healthcare.DatasetsConsentStoresConsent(
123 "Grant",
124 {
125 consentStore: store.name,
126 userId: "user-123",
127 consentArtifact: artifact.name,
128 state: "DRAFT",
129 metadata: { source: "portal" },
130 },
131 );
132 return { dataset, store, artifact, consent };
133 }),
134 );
135
136 expect(updated.consent.name).toEqual(created.consent.name);
137 expect(updated.consent.metadata).toMatchObject({ source: "portal" });
138
139 yield* stack.destroy();
140 const gone = yield* waitUntilGone(created.consent.name);
141 expect(gone).toEqual("gone");
142 }).pipe(logLevel),
143 {
144 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
145 timeout: 120_000,
146 },
147);