| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as cw from "@distilled.cloud/gcp/contentwarehouse_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 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 | |
| 17 | // Document AI Warehouse is disabled on the testing project: calls fail with |
| 18 | // ServiceDisabled "Document AI Warehouse API has not been used in project ... |
| 19 | // before or it is disabled" (the service also needs per-project |
| 20 | // provisioning). Set GCP_TEST_CONTENTWAREHOUSE=1 on a provisioned project. |
| 21 | const runLifecycle = |
| 22 | !process.env.FAST && !!process.env.GCP_TEST_CONTENTWAREHOUSE; |
| 23 | |
| 24 | const location = "us"; |
| 25 | |
| 26 | const waitUntilGone = (name: string) => |
| 27 | cw.getProjectsLocationsRuleSets({ name }).pipe( |
| 28 | Effect.as("found" as const), |
| 29 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.spaced("1 second"), |
| 32 | until: (status) => status === "gone", |
| 33 | times: 10, |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | test.provider.skipIf(runLifecycle)( |
| 38 | "getProjectsLocationsRuleSets without Document AI Warehouse enabled fails with ServiceDisabled", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const { project } = yield* GcpEnvironment.current; |
| 42 | const parent = `projects/${project}/locations/${location}`; |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const error = yield* Effect.flip( |
| 46 | cw.getProjectsLocationsRuleSets({ |
| 47 | name: `${parent}/ruleSets/alchemy-missing-ruleset`, |
| 48 | }), |
| 49 | ); |
| 50 | expect(error._tag).toEqual("ServiceDisabled"); |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | }).pipe(logLevel), |
| 54 | { |
| 55 | tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"], |
| 56 | timeout: 90_000, |
| 57 | }, |
| 58 | ); |
| 59 | |
| 60 | test.provider.skipIf(!runLifecycle)( |
| 61 | "create, update, and delete a rule set", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | const { project } = yield* GcpEnvironment.current; |
| 65 | const parent = `projects/${project}/locations/${location}`; |
| 66 | yield* stack.destroy(); |
| 67 | |
| 68 | const created = yield* stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | return yield* GCP.ContentWarehouse.RuleSet("Checks", { |
| 71 | location, |
| 72 | description: "on create", |
| 73 | source: "alchemy", |
| 74 | rules: [ |
| 75 | { |
| 76 | description: "require title", |
| 77 | triggerType: "ON_CREATE", |
| 78 | condition: "true", |
| 79 | actions: [ |
| 80 | { |
| 81 | dataValidation: { |
| 82 | conditions: { display_name: "true" }, |
| 83 | }, |
| 84 | }, |
| 85 | ], |
| 86 | }, |
| 87 | ], |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(created.ruleSetId).toEqual(expect.any(String)); |
| 93 | expect(created.name).toContain("/ruleSets/"); |
| 94 | expect(created.description).toEqual("on create"); |
| 95 | expect(created.source).toEqual("alchemy"); |
| 96 | expect((created.rules ?? []).length).toBeGreaterThan(0); |
| 97 | |
| 98 | const fetched = yield* cw.getProjectsLocationsRuleSets({ |
| 99 | name: created.name, |
| 100 | }); |
| 101 | expect(fetched.name).toEqual(created.name); |
| 102 | expect(fetched.description).toContain("alchemy-id="); |
| 103 | expect(fetched.description).toContain("on create"); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.ContentWarehouse.RuleSet("Checks", { |
| 108 | ruleSetId: created.ruleSetId, |
| 109 | location, |
| 110 | description: "on update", |
| 111 | source: "alchemy-v2", |
| 112 | rules: [ |
| 113 | { |
| 114 | description: "require title", |
| 115 | triggerType: "ON_UPDATE", |
| 116 | condition: "true", |
| 117 | actions: [ |
| 118 | { |
| 119 | dataValidation: { |
| 120 | conditions: { display_name: "true" }, |
| 121 | }, |
| 122 | }, |
| 123 | ], |
| 124 | }, |
| 125 | ], |
| 126 | }); |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(updated.name).toEqual(created.name); |
| 131 | expect(updated.description).toEqual("on update"); |
| 132 | expect(updated.source).toEqual("alchemy-v2"); |
| 133 | expect(updated.rules?.[0]?.triggerType).toEqual("ON_UPDATE"); |
| 134 | |
| 135 | yield* stack.destroy(); |
| 136 | const gone = yield* waitUntilGone(created.name); |
| 137 | expect(gone).toEqual("gone"); |
| 138 | }).pipe(logLevel), |
| 139 | { |
| 140 | tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"], |
| 141 | timeout: 90_000, |
| 142 | }, |
| 143 | ); |