| 1 | import * as GCP from "@/GCP"; |
| 2 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as documentai from "@distilled.cloud/gcp/documentai_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | const location = "us"; |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | documentai.getProjectsLocationsProcessors({ 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 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsProcessors on a missing processor 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.getProjectsLocationsProcessors({ |
| 39 | name: `${parent}/processors/alchemy-missing-processor`, |
| 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 | |
| 52 | test.provider.skipIf(!!process.env.FAST)( |
| 53 | "create, update, and delete a processor", |
| 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.Processor("Ocr", { |
| 63 | location, |
| 64 | type: "OCR_PROCESSOR", |
| 65 | displayName: "ocr", |
| 66 | }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(created.processorId).toEqual(expect.any(String)); |
| 71 | expect(created.name).toContain("/processors/"); |
| 72 | expect(created.location).toEqual(location); |
| 73 | expect(created.type).toEqual("OCR_PROCESSOR"); |
| 74 | expect(created.displayName).toEqual("ocr"); |
| 75 | expect(created.state).toEqual("ENABLED"); |
| 76 | |
| 77 | const fetched = yield* documentai.getProjectsLocationsProcessors({ |
| 78 | name: created.name, |
| 79 | }); |
| 80 | expect(fetched.name).toEqual(created.name); |
| 81 | expect(fetched.type).toEqual("OCR_PROCESSOR"); |
| 82 | expect(fetched.displayName).toContain("ocr"); |
| 83 | expect(fetched.displayName).toMatch(/\[alc |\[alchemy /); |
| 84 | |
| 85 | const updated = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | return yield* GCP.DocumentAI.Processor("Ocr", { |
| 88 | processorId: created.processorId, |
| 89 | location, |
| 90 | type: "OCR_PROCESSOR", |
| 91 | displayName: "ocr", |
| 92 | enabled: false, |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(updated.name).toEqual(created.name); |
| 98 | expect(updated.state).toEqual("DISABLED"); |
| 99 | |
| 100 | const disabled = yield* documentai.getProjectsLocationsProcessors({ |
| 101 | name: created.name, |
| 102 | }); |
| 103 | expect(disabled.state).toEqual("DISABLED"); |
| 104 | |
| 105 | yield* stack.destroy(); |
| 106 | const gone = yield* waitUntilGone(created.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:gcp", "provider:gcp:documentai", "live"], |
| 111 | timeout: 120_000, |
| 112 | }, |
| 113 | ); |