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;
19const agentDisplayName = "alch-df-pv";
20
21const waitUntilGone = (name: string) =>
22 dialogflow.getProjectsLocationsAgentsPlaybooksVersions({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsLocationsAgentsPlaybooksVersions on a missing version fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 dialogflow.getProjectsLocationsAgentsPlaybooksVersions({
41 name: `projects/${project}/locations/global/agents/missing/playbooks/missing/versions/999999`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel, quotaTolerant),
48 {
49 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
50 timeout: 90_000,
51 },
52);
53
54test.provider.skipIf(!runLifecycle)(
55 "create and delete a playbook version",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const agent = yield* ensureAgent(project, agentDisplayName);
62 const agentName = agent.name ?? "";
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const playbook = yield* GCP.Dialogflow.AgentsPlaybook("Support", {
67 agent: agentName,
68 displayName: "support-pv",
69 goal: "Greet the user.",
70 });
71 const version = yield* GCP.Dialogflow.AgentsPlaybooksVersion("v1", {
72 playbook: playbook.name,
73 description: "initial",
74 });
75 return { playbook, version };
76 }),
77 );
78
79 expect(created.version.name).toContain("/versions/");
80 expect(created.version.playbook).toEqual(created.playbook.name);
81 expect(created.version.description).toEqual("initial");
82
83 const fetched =
84 yield* dialogflow.getProjectsLocationsAgentsPlaybooksVersions({
85 name: created.version.name,
86 });
87 expect(fetched.name).toEqual(created.version.name);
88
89 yield* stack.destroy();
90
91 const gone = yield* waitUntilGone(created.version.name);
92 expect(gone).toEqual("gone");
93
94 yield* deleteAgent(agentName);
95 }).pipe(logLevel, quotaTolerant),
96 {
97 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
98 timeout: 120_000,
99 },
100);