1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 cci.getProjectsLocationsQaScorecardsRevisionsQaQuestions({ 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
28test.provider(
29 "getProjectsLocationsQaScorecardsRevisionsQaQuestions on a missing question 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.getProjectsLocationsQaScorecardsRevisionsQaQuestions({
37 name: `projects/${project}/locations/us-central1/qaScorecards/missing/revisions/missing/qaQuestions/missing`,
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// Needs Contact Center AI Insights features the testing project does not
51// have enabled: the parent scorecard's PATCH returns 404 NOT_FOUND "Requested entity was not found."
52test.provider.skipIf(!process.env.GCP_TEST_CCAI_QUALITY || !!process.env.FAST)(
53 "create, update, and delete a qa question",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const card = yield* GCP.ContactCenterInsights.QaScorecard("Quality", {
61 location: "us-central1",
62 displayName: "quality",
63 description: "call quality",
64 });
65 const revision =
66 yield* GCP.ContactCenterInsights.QaScorecardsRevision("V1", {
67 parent: card.name,
68 });
69 return yield* GCP.ContactCenterInsights.QaScorecardsRevisionsQaQuestion(
70 "Greeting",
71 {
72 parent: revision.name,
73 abbreviation: "Greeting",
74 questionBody: "Did the agent greet the customer?",
75 answerInstructions: "Listen for a greeting in the first turn.",
76 answerChoices: [
77 { strValue: "Yes", score: 1 },
78 { strValue: "No", score: 0 },
79 ],
80 },
81 );
82 }),
83 );
84
85 expect(created.name).toContain("/qaQuestions/");
86 expect(created.abbreviation).toEqual("Greeting");
87 expect(created.questionBody).toEqual("Did the agent greet the customer?");
88 expect(created.answerInstructions).toEqual(
89 "Listen for a greeting in the first turn.",
90 );
91
92 const fetched =
93 yield* cci.getProjectsLocationsQaScorecardsRevisionsQaQuestions({
94 name: created.name,
95 });
96 expect(fetched.name).toEqual(created.name);
97 expect(fetched.answerInstructions).toContain("alchemy-id=");
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const card = yield* GCP.ContactCenterInsights.QaScorecard("Quality", {
102 location: "us-central1",
103 displayName: "quality",
104 description: "call quality",
105 });
106 const revision =
107 yield* GCP.ContactCenterInsights.QaScorecardsRevision("V1", {
108 parent: card.name,
109 });
110 return yield* GCP.ContactCenterInsights.QaScorecardsRevisionsQaQuestion(
111 "Greeting",
112 {
113 parent: revision.name,
114 qaQuestionId: created.qaQuestionId,
115 abbreviation: "Intro",
116 questionBody: "Did the agent introduce themselves?",
117 answerInstructions: "Listen for a name in the first turn.",
118 answerChoices: [
119 { strValue: "Yes", score: 1 },
120 { strValue: "No", score: 0 },
121 ],
122 },
123 );
124 }),
125 );
126
127 expect(updated.name).toEqual(created.name);
128 expect(updated.abbreviation).toEqual("Intro");
129 expect(updated.questionBody).toEqual(
130 "Did the agent introduce themselves?",
131 );
132
133 yield* stack.destroy();
134 const gone = yield* waitUntilGone(created.name);
135 expect(gone).toEqual("gone");
136 }).pipe(logLevel),
137 {
138 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
139 timeout: 90_000,
140 },
141);