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-wh";
23
24const waitUntilGone = (name: string) =>
25 dialogflow.getProjectsLocationsAgentsWebhooks({ 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 "getProjectsLocationsAgentsWebhooks on a missing webhook 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.getProjectsLocationsAgentsWebhooks({
44 name: `projects/${project}/locations/${location}/agents/missing/webhooks/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 a webhook",
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.AgentsWebhook("Fulfillment", {
71 agent: agentName,
72 location,
73 displayName: "orders",
74 genericWebService: { uri: "https://example.com/dialogflow" },
75 });
76 }),
77 );
78
79 expect(created.name).toContain("/webhooks/");
80 expect(created.agent).toEqual(agentName);
81 expect(created.location).toEqual(location);
82 expect(created.displayName).toEqual("orders");
83 expect(created.disabled).toEqual(false);
84 expect(created.genericWebService?.uri).toEqual(
85 "https://example.com/dialogflow",
86 );
87
88 const fetched = yield* dialogflow.getProjectsLocationsAgentsWebhooks({
89 name: created.name,
90 });
91 expect(fetched.name).toEqual(created.name);
92 expect(fetched.displayName).toMatch(/^\[(alchemy|alc) /);
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.Dialogflow.AgentsWebhook("Fulfillment", {
97 agent: agentName,
98 location,
99 webhookId: created.webhookId,
100 displayName: "orders-v2",
101 disabled: true,
102 genericWebService: {
103 uri: "https://example.com/dialogflow-v2",
104 },
105 });
106 }),
107 );
108
109 expect(updated.name).toEqual(created.name);
110 expect(updated.displayName).toEqual("orders-v2");
111 expect(updated.disabled).toEqual(true);
112 expect(updated.genericWebService?.uri).toEqual(
113 "https://example.com/dialogflow-v2",
114 );
115
116 yield* stack.destroy();
117 const gone = yield* waitUntilGone(created.name);
118 expect(gone).toEqual("gone");
119 }).pipe(Effect.ensuring(deleteAgent(agentName)));
120 }).pipe(logLevel, quotaTolerant),
121 {
122 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
123 timeout: 120_000,
124 },
125);