| 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 { ChatTranscript } from "./transcript.ts"; |
| 9 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | cci.getProjectsLocationsConversations({ name, view: "BASIC" }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsConversations on a missing conversation fails with a typed tag", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | cci.getProjectsLocationsConversations({ |
| 38 | name: `projects/${project}/locations/us-central1/conversations/alchemy-missing-conv`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel), |
| 45 | { |
| 46 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 47 | timeout: 90_000, |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "create, update, and delete a conversation", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const created = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* GCP.ContactCenterInsights.Conversation("Chat", { |
| 60 | dataSource: yield* ChatTranscript, |
| 61 | medium: "CHAT", |
| 62 | languageCode: "en-US", |
| 63 | agentId: "agent-1", |
| 64 | labels: { env: "test" }, |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.conversationId).toEqual(expect.any(String)); |
| 70 | expect(created.name).toContain("/conversations/"); |
| 71 | expect(created.location).toEqual("us-central1"); |
| 72 | expect(created.medium).toEqual("CHAT"); |
| 73 | expect(created.agentId).toEqual("agent-1"); |
| 74 | expect(created.languageCode).toEqual("en-US"); |
| 75 | expect(created.labels).toMatchObject({ env: "test" }); |
| 76 | |
| 77 | const fetched = yield* cci.getProjectsLocationsConversations({ |
| 78 | name: created.name, |
| 79 | view: "BASIC", |
| 80 | }); |
| 81 | expect(fetched.name).toEqual(created.name); |
| 82 | expect(fetched.agentId).toEqual("agent-1"); |
| 83 | expect(fetched.labels?.env).toEqual("test"); |
| 84 | expect( |
| 85 | Object.keys(fetched.labels ?? {}).some((key) => |
| 86 | key.startsWith("alchemy-"), |
| 87 | ), |
| 88 | ).toEqual(true); |
| 89 | |
| 90 | const updated = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | return yield* GCP.ContactCenterInsights.Conversation("Chat", { |
| 93 | dataSource: yield* ChatTranscript, |
| 94 | conversationId: created.conversationId, |
| 95 | location: "us-central1", |
| 96 | medium: "CHAT", |
| 97 | languageCode: "en-GB", |
| 98 | agentId: "agent-2", |
| 99 | labels: { env: "prod", role: "chat" }, |
| 100 | }); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | expect(updated.name).toEqual(created.name); |
| 105 | expect(updated.agentId).toEqual("agent-2"); |
| 106 | expect(updated.languageCode).toEqual("en-GB"); |
| 107 | expect(updated.labels).toMatchObject({ env: "prod", role: "chat" }); |
| 108 | |
| 109 | const fetchedUpdate = yield* cci.getProjectsLocationsConversations({ |
| 110 | name: updated.name, |
| 111 | view: "BASIC", |
| 112 | }); |
| 113 | expect(fetchedUpdate.agentId).toEqual("agent-2"); |
| 114 | expect(fetchedUpdate.languageCode).toEqual("en-GB"); |
| 115 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 116 | |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | const gone = yield* waitUntilGone(created.name); |
| 120 | expect(gone).toEqual("gone"); |
| 121 | }).pipe(logLevel), |
| 122 | { |
| 123 | tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"], |
| 124 | timeout: 90_000, |
| 125 | }, |
| 126 | ); |