1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { deleteAgent, ensureAgent, quotaTolerant } from "./parent.ts";
9import { GcpEnvironment } from "@/GCP/Environment";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const runLifecycle = !process.env.FAST;
19// Non-global Dialogflow CX locations need the {region}-dialogflow host, which
20// distilled does not route to yet; the global location works everywhere.
21const location = "global";
22const agentDisplayName = "alch-df-atrg";
23
24const waitUntilGone = (name: string) =>
25 dialogflow.getProjectsLocationsAgentsTransitionRouteGroups({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsAgentsTransitionRouteGroups on a missing group fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 dialogflow.getProjectsLocationsAgentsTransitionRouteGroups({
44 name: `projects/${project}/locations/${location}/agents/missing/transitionRouteGroups/missing`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel, quotaTolerant),
51 {
52 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider.skipIf(!runLifecycle)(
58 "create, update, and delete an agent transition route group",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63
64 const agent = yield* ensureAgent(project, agentDisplayName, location);
65 const agentName = agent.name ?? "";
66
67 yield* Effect.gen(function* () {
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.Dialogflow.AgentsTransitionRouteGroup(
71 "Fallback",
72 {
73 agent: agentName,
74 location,
75 displayName: "g1",
76 transitionRoutes: [
77 {
78 condition: "true",
79 triggerFulfillment: {
80 messages: [{ text: { text: ["ok"] } }],
81 },
82 },
83 ],
84 },
85 );
86 }),
87 );
88
89 expect(created.name).toContain("/transitionRouteGroups/");
90 expect(created.name).not.toContain("/flows/");
91 expect(created.agent).toEqual(agentName);
92 expect(created.location).toEqual(location);
93 expect(created.displayName).toEqual("g1");
94 expect(created.transitionRoutes[0]?.condition).toEqual("true");
95
96 const fetched =
97 yield* dialogflow.getProjectsLocationsAgentsTransitionRouteGroups({
98 name: created.name,
99 });
100 expect(fetched.name).toEqual(created.name);
101 expect(fetched.displayName).toContain("[alc ");
102
103 const updated = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.Dialogflow.AgentsTransitionRouteGroup(
106 "Fallback",
107 {
108 agent: agentName,
109 location,
110 transitionRouteGroupId: created.transitionRouteGroupId,
111 displayName: "g2",
112 transitionRoutes: [
113 {
114 condition: "true",
115 triggerFulfillment: {
116 messages: [{ text: { text: ["sorry"] } }],
117 },
118 },
119 ],
120 },
121 );
122 }),
123 );
124
125 expect(updated.name).toEqual(created.name);
126 expect(updated.displayName).toEqual("g2");
127 expect(
128 updated.transitionRoutes[0]?.triggerFulfillment?.messages?.[0]?.text
129 ?.text?.[0],
130 ).toEqual("sorry");
131
132 yield* stack.destroy();
133 const gone = yield* waitUntilGone(created.name);
134 expect(gone).toEqual("gone");
135 }).pipe(Effect.ensuring(deleteAgent(agentName)));
136 }).pipe(logLevel, quotaTolerant),
137 {
138 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
139 timeout: 120_000,
140 },
141);