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.getProjectsLocationsIssueModels({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsIssueModels on a missing model 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.getProjectsLocationsIssueModels({
37 name: `projects/${project}/locations/us-central1/issueModels/alchemy-missing-model`,
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 a project with at least 100 analyzed conversations; otherwise create fails with
51// BadRequest "Project: `...` has too few conversations with the given filters.
52// Current number: 0. Minimum requirement is: 100."
53test.provider.skipIf(
54 !!process.env.FAST || !process.env.GCP_TEST_CCI_ISSUE_MODELS,
55)(
56 "create, update, and delete an issue model",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.ContactCenterInsights.IssueModel("Topics", {
64 location: "us-central1",
65 displayName: "billing-topics",
66 languageCode: "en-US",
67 modelType: "TYPE_V2",
68 });
69 }),
70 );
71
72 expect(created.name).toContain("/issueModels/");
73 expect(created.displayName).toEqual("billing-topics");
74
75 const fetched = yield* cci.getProjectsLocationsIssueModels({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.displayName).toContain("alchemy-id=");
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.ContactCenterInsights.IssueModel("Topics", {
84 location: "us-central1",
85 displayName: "billing-topics-v2",
86 languageCode: "en-US",
87 modelType: "TYPE_V2",
88 });
89 }),
90 );
91 expect(updated.name).toEqual(created.name);
92 expect(updated.displayName).toEqual("billing-topics-v2");
93
94 yield* stack.destroy();
95 const gone = yield* waitUntilGone(created.name);
96 expect(gone).toEqual("gone");
97 }).pipe(logLevel),
98 {
99 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
100 timeout: 120_000,
101 },
102);