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.getProjectsLocationsAssessmentRules({ 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 "getProjectsLocationsAssessmentRules 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.getProjectsLocationsAssessmentRules({
37 name: `projects/${project}/locations/us-central1/assessmentRules/alchemymissingrule`,
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 returns 404 NOT_FOUND "Requested entity was not found." and list returns 400 INVALID_ARGUMENT.
52test.provider.skipIf(!process.env.GCP_TEST_CCAI_QUALITY || !!process.env.FAST)(
53 "create, update, and delete an assessment rule",
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.AssessmentRule("Qa", {
61 displayName: "qa",
62 active: false,
63 sampleRule: { samplePercentage: 0.1 },
64 scheduleInfo: { schedule: "every 1 hours", timeZone: "UTC" },
65 });
66 }),
67 );
68
69 expect(created.assessmentRuleId).toEqual(expect.any(String));
70 expect(created.name).toContain("/assessmentRules/");
71 expect(created.location).toEqual("us-central1");
72 expect(created.displayName).toEqual("qa");
73 expect(created.active).toEqual(false);
74 expect(created.sampleRule?.samplePercentage).toEqual(0.1);
75
76 const fetched = yield* cci.getProjectsLocationsAssessmentRules({
77 name: created.name,
78 });
79 expect(fetched.name).toEqual(created.name);
80 expect(fetched.displayName).toContain("alchemy-id=");
81 expect(fetched.active ?? false).toEqual(false);
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* GCP.ContactCenterInsights.AssessmentRule("Qa", {
86 assessmentRuleId: created.assessmentRuleId,
87 location: "us-central1",
88 displayName: "qa-2",
89 active: false,
90 sampleRule: { samplePercentage: 0.2 },
91 scheduleInfo: { schedule: "every 2 hours", timeZone: "UTC" },
92 });
93 }),
94 );
95
96 expect(updated.name).toEqual(created.name);
97 expect(updated.displayName).toEqual("qa-2");
98 expect(updated.sampleRule?.samplePercentage).toEqual(0.2);
99 expect(updated.scheduleInfo?.schedule).toEqual("every 2 hours");
100
101 const fetchedUpdate = yield* cci.getProjectsLocationsAssessmentRules({
102 name: updated.name,
103 });
104 expect(fetchedUpdate.sampleRule?.samplePercentage).toEqual(0.2);
105
106 yield* stack.destroy();
107
108 const gone = yield* waitUntilGone(created.name);
109 expect(gone).toEqual("gone");
110 }).pipe(logLevel),
111 {
112 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
113 timeout: 90_000,
114 },
115);