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.getProjectsLocationsAgentsEnvironments({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsAgentsEnvironments on a missing environment 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.getProjectsLocationsAgentsEnvironments({
40 name: `projects/${project}/locations/global/agents/alchemy-missing/environments/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 an environment",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59 const agent = yield* ensureAgent(project, "alchemy-df-environment");
60 const startFlow = agent.startFlow ?? "";
61
62 yield* Effect.gen(function* () {
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
66 flow: startFlow,
67 displayName: "v1",
68 description: "env snapshot",
69 });
70 const environment = yield* GCP.Dialogflow.AgentsEnvironment(
71 "Prod",
72 {
73 agent: agent.name ?? "",
74 displayName: "prod",
75 description: "production",
76 versionConfigs: [{ version: version.name }],
77 },
78 );
79 return { version, environment };
80 }),
81 );
82
83 expect(created.environment.name).toContain("/environments/");
84 expect(created.environment.displayName).toEqual("prod");
85 expect(created.environment.description).toEqual("production");
86 expect(created.environment.versionConfigs).toEqual(
87 expect.arrayContaining([
88 expect.objectContaining({ version: created.version.name }),
89 ]),
90 );
91
92 const fetched =
93 yield* dialogflow.getProjectsLocationsAgentsEnvironments({
94 name: created.environment.name,
95 });
96 expect(fetched.name).toEqual(created.environment.name);
97 expect(fetched.description).toContain("[alchemy ");
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
102 flow: startFlow,
103 versionId: created.version.versionId,
104 displayName: "v1",
105 description: "env snapshot",
106 });
107 const environment = yield* GCP.Dialogflow.AgentsEnvironment(
108 "Prod",
109 {
110 agent: agent.name ?? "",
111 environmentId: created.environment.environmentId,
112 displayName: "staging",
113 description: "staging env",
114 versionConfigs: [{ version: version.name }],
115 },
116 );
117 return { version, environment };
118 }),
119 );
120
121 expect(updated.environment.name).toEqual(created.environment.name);
122 expect(updated.environment.displayName).toEqual("staging");
123 expect(updated.environment.description).toEqual("staging env");
124
125 yield* stack.destroy();
126 const gone = yield* waitUntilGone(created.environment.name);
127 expect(gone).toEqual("gone");
128 }).pipe(Effect.ensuring(deleteAgent(agent.name ?? "")));
129 }).pipe(logLevel, quotaTolerant),
130 {
131 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
132 timeout: 120_000,
133 },
134);