| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as cci from "@distilled.cloud/gcp/contactcenterinsights_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 | const waitUntilGone = (name: string) => |
| 18 | cci.getProjectsLocationsPhraseMatchers({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | const refundGroups = [ |
| 29 | { |
| 30 | type: "ANY_OF" as const, |
| 31 | phraseMatchRules: [ |
| 32 | { |
| 33 | query: "refund", |
| 34 | config: { exactMatchConfig: { caseSensitive: false } }, |
| 35 | }, |
| 36 | ], |
| 37 | }, |
| 38 | ]; |
| 39 | |
| 40 | const cancelGroups = [ |
| 41 | { |
| 42 | type: "ANY_OF" as const, |
| 43 | phraseMatchRules: [ |
| 44 | { |
| 45 | query: "cancel", |
| 46 | config: { exactMatchConfig: { caseSensitive: false } }, |
| 47 | }, |
| 48 | ], |
| 49 | }, |
| 50 | ]; |
| 51 | |
| 52 | test.provider( |
| 53 | "getProjectsLocationsPhraseMatchers on a missing matcher fails with a typed tag", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | const { project } = yield* GcpEnvironment.current; |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const error = yield* Effect.flip( |
| 60 | cci.getProjectsLocationsPhraseMatchers({ |
| 61 | name: `projects/${project}/locations/us-central1/phraseMatchers/9876543210`, |
| 62 | }), |
| 63 | ); |
| 64 | expect(error._tag).toEqual("NotFound"); |
| 65 | |
| 66 | yield* stack.destroy(); |
| 67 | }).pipe(logLevel), |
| 68 | { |
| 69 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 70 | timeout: 90_000, |
| 71 | }, |
| 72 | ); |
| 73 | |
| 74 | test.provider.skipIf(!!process.env.FAST)( |
| 75 | "create, update, and delete a phrase matcher", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | yield* stack.destroy(); |
| 79 | |
| 80 | const created = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* GCP.ContactCenterInsights.PhraseMatcher("Refunds", { |
| 83 | location: "us-central1", |
| 84 | displayName: "refunds", |
| 85 | type: "ALL_OF", |
| 86 | active: false, |
| 87 | phraseMatchRuleGroups: refundGroups, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(created.name).toContain("/phraseMatchers/"); |
| 93 | expect(created.location).toEqual("us-central1"); |
| 94 | expect(created.displayName).toEqual("refunds"); |
| 95 | expect(created.type).toEqual("ALL_OF"); |
| 96 | expect(created.active).toEqual(false); |
| 97 | |
| 98 | const fetched = yield* cci.getProjectsLocationsPhraseMatchers({ |
| 99 | name: created.name, |
| 100 | }); |
| 101 | expect(fetched.name).toEqual(created.name); |
| 102 | expect(fetched.displayName).toContain("alchemy-id="); |
| 103 | expect(fetched.displayName).toContain("refunds"); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.ContactCenterInsights.PhraseMatcher("Refunds", { |
| 108 | phraseMatcherId: created.phraseMatcherId, |
| 109 | location: "us-central1", |
| 110 | displayName: "cancellations", |
| 111 | type: "ALL_OF", |
| 112 | active: false, |
| 113 | phraseMatchRuleGroups: cancelGroups, |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(updated.name).toEqual(created.name); |
| 119 | expect(updated.displayName).toEqual("cancellations"); |
| 120 | expect( |
| 121 | updated.phraseMatchRuleGroups?.[0]?.phraseMatchRules?.[0]?.query, |
| 122 | ).toEqual("cancel"); |
| 123 | |
| 124 | const fetchedUpdate = yield* cci.getProjectsLocationsPhraseMatchers({ |
| 125 | name: updated.name, |
| 126 | }); |
| 127 | expect(fetchedUpdate.displayName).toContain("cancellations"); |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | const gone = yield* waitUntilGone(created.name); |
| 131 | expect(gone).toEqual("gone"); |
| 132 | }).pipe(logLevel), |
| 133 | { |
| 134 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 135 | timeout: 90_000, |
| 136 | }, |
| 137 | ); |