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 .getProjectsLocationsDatasetsConsentStoresConsentArtifacts({ 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 "getProjectsLocationsDatasetsConsentStoresConsentArtifacts on a missing artifact 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.getProjectsLocationsDatasetsConsentStoresConsentArtifacts({
39 name: `projects/${project}/locations/us-central1/datasets/missing/consentStores/missing/consentArtifacts/alchemy-missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
48 timeout: 90_000,
49 },
50);
51
52test.provider(
53 "create and delete a consent artifact",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const dataset = yield* GCP.Healthcare.Dataset("ArtifactClinic", {
61 location: "us-central1",
62 });
63 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
64 dataset: dataset.name,
65 labels: { env: "test" },
66 });
67 const artifact =
68 yield* GCP.Healthcare.DatasetsConsentStoresConsentArtifact(
69 "Proof",
70 {
71 consentStore: store.name,
72 userId: "user-123",
73 consentContentVersion: "v1",
74 metadata: { locale: "en" },
75 },
76 );
77 return { dataset, store, artifact };
78 }),
79 );
80
81 expect(created.artifact.name).toContain("/consentArtifacts/");
82 expect(created.artifact.userId).toEqual("user-123");
83 expect(created.artifact.consentContentVersion).toEqual("v1");
84 expect(created.artifact.metadata).toMatchObject({ locale: "en" });
85
86 const fetched =
87 yield* healthcare.getProjectsLocationsDatasetsConsentStoresConsentArtifacts(
88 {
89 name: created.artifact.name,
90 },
91 );
92 expect(fetched.name).toEqual(created.artifact.name);
93 expect(fetched.userId).toEqual("user-123");
94 expect(fetched.metadata?.locale).toEqual("en");
95 expect(fetched.metadata?.["alchemy-id"]).toEqual(expect.any(String));
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 const dataset = yield* GCP.Healthcare.Dataset("ArtifactClinic", {
100 datasetId: created.dataset.datasetId,
101 location: "us-central1",
102 });
103 const store = yield* GCP.Healthcare.DatasetsConsentStore("Consents", {
104 dataset: dataset.name,
105 consentStoreId: created.store.consentStoreId,
106 labels: { env: "test" },
107 });
108 const artifact =
109 yield* GCP.Healthcare.DatasetsConsentStoresConsentArtifact(
110 "Proof",
111 {
112 consentStore: store.name,
113 userId: "user-123",
114 consentContentVersion: "v1",
115 metadata: { locale: "en" },
116 },
117 );
118 return { dataset, store, artifact };
119 }),
120 );
121
122 expect(updated.artifact.name).toEqual(created.artifact.name);
123 expect(updated.artifact.userId).toEqual("user-123");
124
125 yield* stack.destroy();
126 const gone = yield* waitUntilGone(created.artifact.name);
127 expect(gone).toEqual("gone");
128 }).pipe(logLevel),
129 {
130 tags: ["provider:gcp", "provider:gcp:healthcare", "live"],
131 timeout: 120_000,
132 },
133);