1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as Core from "@/Test/Core";
5import * as crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6import * as documentai from "@distilled.cloud/gcp/documentai_v1";
7import { describe, expect } from "alchemy-test";
8import * as Effect from "effect/Effect";
9import { dockerAvailable, expectProbe } from "../bindingHost.ts";
10import DocumentAIBindingsHost, {
11 Invoice,
12 InvoiceV1,
13 OcrBind,
14} from "./fixtures/bindings-host.ts";
15
16const testOptions = { providers: GCP.providers() };
17const { test, beforeAll, afterAll } = Test.make(testOptions);
18const sharedStack = Core.scratchStack(testOptions, "DocumentAIBindings");
19
20let baseUrl: string;
21let hostAccount: string;
22let processorName: string;
23let schemaName: string;
24let versionName: string;
25
26/**
27 * Document AI has no resource-level IAM, so its bindings grant on the
28 * project: `documentai.viewer` (GetProcessor / GetSchema / GetSchemaVersion)
29 * and `documentai.apiUser` (Process).
30 */
31const expectProjectGrants = Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 const policy = yield* crm.getIamPolicyProjects({
34 resource: `projects/${project}`,
35 body: { options: { requestedPolicyVersion: 3 } },
36 });
37 const grants = (policy.bindings ?? [])
38 .filter((binding) =>
39 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
40 )
41 .map((binding) => ({
42 role: binding.role,
43 condition: binding.condition?.expression,
44 }))
45 .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? ""));
46 expect(grants).toEqual([
47 { role: "roles/documentai.apiUser", condition: undefined },
48 { role: "roles/documentai.viewer", condition: undefined },
49 ]);
50});
51
52describe.skipIf(!dockerAvailable || !!process.env.FAST)(
53 "DocumentAI Bindings",
54 {
55 tags: [
56 "provider:gcp",
57 "provider:gcp:documentai",
58 "provider:gcp:run",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* sharedStack.destroy();
66 const out = yield* sharedStack.deploy(
67 Effect.gen(function* () {
68 const host = yield* DocumentAIBindingsHost;
69 const processor = yield* OcrBind;
70 const schema = yield* Invoice;
71 const version = yield* InvoiceV1;
72 return {
73 uri: host.uri,
74 serviceAccount: host.serviceAccount,
75 processor: processor.name,
76 schema: schema.name,
77 version: version.name,
78 };
79 }),
80 );
81 baseUrl = out.uri!;
82 hostAccount = out.serviceAccount!;
83 processorName = out.processor;
84 schemaName = out.schema;
85 versionName = out.version;
86 }),
87 { timeout: 900_000 },
88 );
89
90 afterAll(sharedStack.destroy(), { timeout: 600_000 });
91
92 describe("GetProcessor", () => {
93 test.provider(
94 "reads the processor as the host's service account",
95 (_stack) =>
96 Effect.gen(function* () {
97 const processor =
98 yield* expectProbe<documentai.GoogleCloudDocumentaiV1Processor>(
99 baseUrl,
100 "getProcessor",
101 );
102 const live = yield* documentai.getProjectsLocationsProcessors({
103 name: processorName,
104 });
105 expect(processor.name).toEqual(processorName);
106 expect(processor.type).toEqual("OCR_PROCESSOR");
107 expect(processor.displayName).toEqual(live.displayName);
108 yield* expectProjectGrants;
109 }),
110 {
111 tags: ["provider:gcp", "provider:gcp:documentai", "live"],
112 timeout: 600_000,
113 },
114 );
115 });
116
117 describe("Process", () => {
118 test.provider(
119 "OCRs a PDF as the host's service account",
120 (_stack) =>
121 Effect.gen(function* () {
122 const processed = yield* expectProbe<{
123 text?: string;
124 pages?: number;
125 }>(baseUrl, "process");
126 expect(processed.pages).toEqual(1);
127 expect(processed.text).toContain("Hello");
128 yield* expectProjectGrants;
129 }),
130 {
131 tags: ["provider:gcp", "provider:gcp:documentai", "live"],
132 timeout: 600_000,
133 },
134 );
135 });
136
137 describe("GetSchema", () => {
138 test.provider(
139 "reads the schema as the host's service account",
140 (_stack) =>
141 Effect.gen(function* () {
142 const schema =
143 yield* expectProbe<documentai.GoogleCloudDocumentaiV1NextSchema>(
144 baseUrl,
145 "getSchema",
146 );
147 const live = yield* documentai.getProjectsLocationsSchemas({
148 name: schemaName,
149 });
150 expect(schema.name).toEqual(schemaName);
151 expect(schema.displayName).toEqual("invoice-bind");
152 expect(schema.labels).toEqual(live.labels);
153 yield* expectProjectGrants;
154 }),
155 {
156 tags: ["provider:gcp", "provider:gcp:documentai", "live"],
157 timeout: 600_000,
158 },
159 );
160 });
161
162 describe("GetSchemaVersion", () => {
163 test.provider(
164 "reads the schema version as the host's service account",
165 (_stack) =>
166 Effect.gen(function* () {
167 const version =
168 yield* expectProbe<documentai.GoogleCloudDocumentaiV1SchemaVersion>(
169 baseUrl,
170 "getSchemaVersion",
171 );
172 const live =
173 yield* documentai.getProjectsLocationsSchemasSchemaVersions({
174 name: versionName,
175 });
176 expect(version.name).toEqual(versionName);
177 expect(version.displayName).toEqual("v1");
178 expect(
179 version.schema?.entityTypes?.[0]?.properties?.map(
180 (property) => property.name,
181 ),
182 ).toEqual(
183 live.schema?.entityTypes?.[0]?.properties?.map(
184 (property) => property.name,
185 ),
186 );
187 yield* expectProjectGrants;
188 }),
189 {
190 tags: ["provider:gcp", "provider:gcp:documentai", "live"],
191 timeout: 600_000,
192 },
193 );
194 });
195 },
196);