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-tv";
20
21const waitUntilGone = (name: string) =>
22 dialogflow.getProjectsLocationsAgentsToolsVersions({ 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 "getProjectsLocationsAgentsToolsVersions 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.getProjectsLocationsAgentsToolsVersions({
41 name: `projects/${project}/locations/global/agents/missing/tools/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 tool 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 tool = yield* GCP.Dialogflow.AgentsTool("Lookup", {
67 agent: agentName,
68 displayName: "lookup-tv",
69 description: "Look up an order.",
70 });
71 const version = yield* GCP.Dialogflow.AgentsToolsVersion("v1", {
72 tool: tool.name,
73 displayName: "initial",
74 });
75 return { tool, version };
76 }),
77 );
78
79 expect(created.version.name).toContain("/versions/");
80 expect(created.version.tool).toEqual(created.tool.name);
81 expect(created.version.displayName).toEqual("initial");
82
83 const fetched = yield* dialogflow.getProjectsLocationsAgentsToolsVersions(
84 { name: created.version.name },
85 );
86 expect(fetched.name).toEqual(created.version.name);
87
88 yield* stack.destroy();
89
90 const gone = yield* waitUntilGone(created.version.name);
91 expect(gone).toEqual("gone");
92
93 yield* deleteAgent(agentName);
94 }).pipe(logLevel, quotaTolerant),
95 {
96 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
97 timeout: 120_000,
98 },
99);