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.getProjectsLocationsAuthorizedViewSetsAuthorizedViews({ 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 "getProjectsLocationsAuthorizedViewSetsAuthorizedViews on a missing view 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.getProjectsLocationsAuthorizedViewSetsAuthorizedViews({
37 name: `projects/${project}/locations/us-central1/authorizedViewSets/alchemy-missing-set/authorizedViews/alchemy-missing-view`,
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: creating the parent authorized view set 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 an authorized view",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet(
61 "QaViews",
62 { displayName: "qa" },
63 );
64 const view =
65 yield* GCP.ContactCenterInsights.AuthorizedViewSetsAuthorizedView(
66 "Reviewers",
67 {
68 parent: set.name,
69 displayName: "rv",
70 },
71 );
72 return { set, view };
73 }),
74 );
75
76 expect(created.view.authorizedViewId).toEqual(expect.any(String));
77 expect(created.view.name).toContain("/authorizedViews/");
78 expect(created.view.parent).toEqual(created.set.name);
79 expect(created.view.displayName).toEqual("rv");
80
81 const fetched =
82 yield* cci.getProjectsLocationsAuthorizedViewSetsAuthorizedViews({
83 name: created.view.name,
84 });
85 expect(fetched.name).toEqual(created.view.name);
86 expect(fetched.displayName).toContain("alchemy-id=");
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet(
91 "QaViews",
92 {
93 authorizedViewSetId: created.set.authorizedViewSetId,
94 location: "us-central1",
95 displayName: "qa",
96 },
97 );
98 const view =
99 yield* GCP.ContactCenterInsights.AuthorizedViewSetsAuthorizedView(
100 "Reviewers",
101 {
102 parent: set.name,
103 authorizedViewId: created.view.authorizedViewId,
104 displayName: "rv-2",
105 conversationFilter: 'agent_id="alchemy-agent"',
106 },
107 );
108 return { set, view };
109 }),
110 );
111
112 expect(updated.view.name).toEqual(created.view.name);
113 expect(updated.view.displayName).toEqual("rv-2");
114 expect(updated.view.conversationFilter).toEqual(
115 'agent_id="alchemy-agent"',
116 );
117
118 const fetchedUpdate =
119 yield* cci.getProjectsLocationsAuthorizedViewSetsAuthorizedViews({
120 name: updated.view.name,
121 });
122 expect(fetchedUpdate.conversationFilter).toEqual(
123 'agent_id="alchemy-agent"',
124 );
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(created.view.name);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 {
132 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
133 timeout: 90_000,
134 },
135);