| 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.getProjectsLocationsAnalysisRules({ 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 | test.provider( |
| 29 | "getProjectsLocationsAnalysisRules on a missing rule fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | cci.getProjectsLocationsAnalysisRules({ |
| 37 | name: `projects/${project}/locations/us-central1/analysisRules/alchemy-missing-rule`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, and delete an analysis rule", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.ContactCenterInsights.AnalysisRule("Draft", { |
| 59 | displayName: "draft", |
| 60 | active: false, |
| 61 | analysisPercentage: 0, |
| 62 | annotatorSelector: { runSentimentAnnotator: true }, |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.analysisRuleId).toEqual(expect.any(String)); |
| 68 | expect(created.name).toContain("/analysisRules/"); |
| 69 | expect(created.location).toEqual("us-central1"); |
| 70 | expect(created.displayName).toEqual("draft"); |
| 71 | expect(created.active).toEqual(false); |
| 72 | expect(created.annotatorSelector?.runSentimentAnnotator).toEqual(true); |
| 73 | |
| 74 | const fetched = yield* cci.getProjectsLocationsAnalysisRules({ |
| 75 | name: created.name, |
| 76 | }); |
| 77 | expect(fetched.name).toEqual(created.name); |
| 78 | expect(fetched.displayName).toContain("alchemy-id="); |
| 79 | expect(fetched.displayName).toContain("draft"); |
| 80 | expect(fetched.active ?? false).toEqual(false); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.ContactCenterInsights.AnalysisRule("Draft", { |
| 85 | analysisRuleId: created.analysisRuleId, |
| 86 | location: "us-central1", |
| 87 | displayName: "draft-2", |
| 88 | conversationFilter: 'agent_id="alchemy-agent"', |
| 89 | active: false, |
| 90 | analysisPercentage: 0, |
| 91 | annotatorSelector: { runSilenceAnnotator: true }, |
| 92 | }); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(updated.name).toEqual(created.name); |
| 97 | expect(updated.displayName).toEqual("draft-2"); |
| 98 | expect(updated.conversationFilter).toEqual('agent_id="alchemy-agent"'); |
| 99 | expect(updated.annotatorSelector?.runSilenceAnnotator).toEqual(true); |
| 100 | |
| 101 | const fetchedUpdate = yield* cci.getProjectsLocationsAnalysisRules({ |
| 102 | name: updated.name, |
| 103 | }); |
| 104 | expect(fetchedUpdate.conversationFilter).toEqual( |
| 105 | 'agent_id="alchemy-agent"', |
| 106 | ); |
| 107 | expect(fetchedUpdate.annotatorSelector?.runSilenceAnnotator).toEqual( |
| 108 | true, |
| 109 | ); |
| 110 | |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | const gone = yield* waitUntilGone(created.name); |
| 114 | expect(gone).toEqual("gone"); |
| 115 | }).pipe(logLevel), |
| 116 | { |
| 117 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 118 | timeout: 90_000, |
| 119 | }, |
| 120 | ); |