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.getProjectsLocationsQaScorecards({ 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 "getProjectsLocationsQaScorecards on a missing scorecard 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.getProjectsLocationsQaScorecards({
37 name: `projects/${project}/locations/us-central1/qaScorecards/alchemy-missing-card`,
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: create succeeds but every 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 scorecard",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* GCP.ContactCenterInsights.QaScorecard("Quality", {
61 location: "us-central1",
62 displayName: "quality",
63 description: "call quality",
64 });
65 }),
66 );
67
68 expect(created.name).toContain("/qaScorecards/");
69 expect(created.location).toEqual("us-central1");
70 expect(created.displayName).toEqual("quality");
71 expect(created.description).toEqual("call quality");
72
73 const fetched = yield* cci.getProjectsLocationsQaScorecards({
74 name: created.name,
75 });
76 expect(fetched.name).toEqual(created.name);
77 expect(fetched.description).toContain("alchemy-id=");
78 expect(fetched.displayName).toEqual("quality");
79
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.ContactCenterInsights.QaScorecard("Quality", {
83 qaScorecardId: created.qaScorecardId,
84 location: "us-central1",
85 displayName: "quality-v2",
86 description: "updated quality",
87 });
88 }),
89 );
90
91 expect(updated.name).toEqual(created.name);
92 expect(updated.displayName).toEqual("quality-v2");
93 expect(updated.description).toEqual("updated quality");
94
95 const fetchedUpdate = yield* cci.getProjectsLocationsQaScorecards({
96 name: updated.name,
97 });
98 expect(fetchedUpdate.displayName).toEqual("quality-v2");
99 expect(fetchedUpdate.description).toContain("updated quality");
100
101 yield* stack.destroy();
102 const gone = yield* waitUntilGone(created.name);
103 expect(gone).toEqual("gone");
104 }).pipe(logLevel),
105 {
106 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
107 timeout: 90_000,
108 },
109);