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.getProjectsLocationsAgentsFlows({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("1 second"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsAgentsFlows on a missing flow fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 dialogflow.getProjectsLocationsAgentsFlows({
40 name: `projects/${project}/locations/global/agents/alchemy-missing/flows/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel, quotaTolerant),
47 {
48 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
49 timeout: 90_000,
50 },
51);
52
53test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a flow",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59 const agent = yield* ensureAgent(project, "alchemy-df-flow");
60
61 yield* Effect.gen(function* () {
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.Dialogflow.AgentsFlow("Ordering", {
65 agent: agent.name ?? "",
66 displayName: "ordering",
67 description: "order intake",
68 });
69 }),
70 );
71
72 expect(created.name).toContain("/flows/");
73 expect(created.displayName).toEqual("ordering");
74 expect(created.description).toEqual("order intake");
75
76 const fetched = yield* dialogflow.getProjectsLocationsAgentsFlows({
77 name: created.name,
78 });
79 expect(fetched.name).toEqual(created.name);
80 expect(fetched.description).toContain("[alchemy ");
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.Dialogflow.AgentsFlow("Ordering", {
85 agent: agent.name ?? "",
86 flowId: created.flowId,
87 displayName: "checkout",
88 description: "checkout flow",
89 });
90 }),
91 );
92
93 expect(updated.name).toEqual(created.name);
94 expect(updated.displayName).toEqual("checkout");
95 expect(updated.description).toEqual("checkout flow");
96
97 yield* stack.destroy();
98 const gone = yield* waitUntilGone(created.name);
99 expect(gone).toEqual("gone");
100 }).pipe(Effect.ensuring(deleteAgent(agent.name ?? "")));
101 }).pipe(logLevel, quotaTolerant),
102 {
103 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
104 timeout: 120_000,
105 },
106);