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
20const waitUntilGone = (name: string) =>
21 dialogflow
22 .getProjectsLocationsAgentsFlowsTransitionRouteGroups({ name })
23 .pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33test.provider(
34 "getProjectsLocationsAgentsFlowsTransitionRouteGroups on a missing route group fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 dialogflow.getProjectsLocationsAgentsFlowsTransitionRouteGroups({
42 name: `projects/${project}/locations/global/agents/alchemy-missing/flows/alchemy-missing/transitionRouteGroups/alchemy-missing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel, quotaTolerant),
49 {
50 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
51 timeout: 90_000,
52 },
53);
54
55test.provider.skipIf(!runLifecycle)(
56 "create, update, and delete a transition route group",
57 (stack) =>
58 Effect.gen(function* () {
59 const { project } = yield* GcpEnvironment.current;
60 yield* stack.destroy();
61 const agent = yield* ensureAgent(project, "alchemy-df-trg");
62
63 yield* Effect.gen(function* () {
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const flow = yield* GCP.Dialogflow.AgentsFlow("Main", {
67 agent: agent.name ?? "",
68 displayName: "main",
69 });
70 const group = yield* GCP.Dialogflow.AgentsFlowsTransitionRouteGroup(
71 "Fallback",
72 {
73 flow: flow.name,
74 displayName: "fallback",
75 transitionRoutes: [
76 {
77 condition: "true",
78 triggerFulfillment: {
79 messages: [{ text: { text: ["ok"] } }],
80 },
81 },
82 ],
83 },
84 );
85 return { flow, group };
86 }),
87 );
88
89 expect(created.group.name).toContain("/transitionRouteGroups/");
90 expect(created.group.displayName).toEqual("fallback");
91 expect(created.group.transitionRoutes.length).toBeGreaterThan(0);
92
93 const fetched =
94 yield* dialogflow.getProjectsLocationsAgentsFlowsTransitionRouteGroups(
95 {
96 name: created.group.name,
97 },
98 );
99 expect(fetched.name).toEqual(created.group.name);
100 expect(fetched.displayName).toMatch(/^\[(alchemy|alc) /);
101
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 const flow = yield* GCP.Dialogflow.AgentsFlow("Main", {
105 agent: agent.name ?? "",
106 flowId: created.flow.flowId,
107 displayName: "main",
108 });
109 const group = yield* GCP.Dialogflow.AgentsFlowsTransitionRouteGroup(
110 "Fallback",
111 {
112 flow: flow.name,
113 transitionRouteGroupId: created.group.transitionRouteGroupId,
114 displayName: "defaults",
115 transitionRoutes: [
116 {
117 condition: "true",
118 triggerFulfillment: {
119 messages: [{ text: { text: ["all good"] } }],
120 },
121 },
122 ],
123 },
124 );
125 return { flow, group };
126 }),
127 );
128
129 expect(updated.group.name).toEqual(created.group.name);
130 expect(updated.group.displayName).toEqual("defaults");
131
132 yield* stack.destroy();
133 const gone = yield* waitUntilGone(created.group.name);
134 expect(gone).toEqual("gone");
135 }).pipe(Effect.ensuring(deleteAgent(agent.name ?? "")));
136 }).pipe(logLevel, quotaTolerant),
137 {
138 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
139 timeout: 120_000,
140 },
141);