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