| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { deleteAgent, ensureAgent, quotaTolerant } from "./parent.ts"; |
| 9 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | const agentDisplayName = "alch-df-gen"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | dialogflow.getProjectsLocationsAgentsGenerators({ 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 | |
| 32 | test.provider( |
| 33 | "getProjectsLocationsAgentsGenerators on a missing generator 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.getProjectsLocationsAgentsGenerators({ |
| 41 | name: `projects/${project}/locations/global/agents/missing/generators/missing`, |
| 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 | |
| 54 | test.provider.skipIf(!runLifecycle)( |
| 55 | "create, update, and delete a generator", |
| 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 | return yield* GCP.Dialogflow.AgentsGenerator("Greeting", { |
| 67 | agent: agentName, |
| 68 | displayName: "greeting", |
| 69 | promptText: "Greet the user by name.", |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(created.name).toContain("/generators/"); |
| 75 | expect(created.agent).toEqual(agentName); |
| 76 | expect(created.displayName).toEqual("greeting"); |
| 77 | expect(created.promptText).toEqual("Greet the user by name."); |
| 78 | |
| 79 | const fetched = yield* dialogflow.getProjectsLocationsAgentsGenerators({ |
| 80 | name: created.name, |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.name); |
| 83 | |
| 84 | const updated = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | return yield* GCP.Dialogflow.AgentsGenerator("Greeting", { |
| 87 | agent: agentName, |
| 88 | generatorId: created.generatorId, |
| 89 | displayName: "greeting", |
| 90 | promptText: "Greet the user warmly.", |
| 91 | }); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(updated.name).toEqual(created.name); |
| 96 | expect(updated.promptText).toEqual("Greet the user warmly."); |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const gone = yield* waitUntilGone(created.name); |
| 101 | expect(gone).toEqual("gone"); |
| 102 | |
| 103 | yield* deleteAgent(agentName); |
| 104 | }).pipe(logLevel, quotaTolerant), |
| 105 | { |
| 106 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 107 | timeout: 120_000, |
| 108 | }, |
| 109 | ); |