| 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 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | dialogflow.getProjectsLocationsAgentsFlowsVersions({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsAgentsFlowsVersions on a missing version 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.getProjectsLocationsAgentsFlowsVersions({ |
| 40 | name: `projects/${project}/locations/global/agents/alchemy-missing/flows/alchemy-missing/versions/999999`, |
| 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 | |
| 53 | test.provider.skipIf(!runLifecycle)( |
| 54 | "create, update, and delete a flow version", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | const { project } = yield* GcpEnvironment.current; |
| 58 | yield* stack.destroy(); |
| 59 | const agent = yield* ensureAgent(project, "alchemy-df-version"); |
| 60 | |
| 61 | yield* Effect.gen(function* () { |
| 62 | const created = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | const flow = yield* GCP.Dialogflow.AgentsFlow("Main", { |
| 65 | agent: agent.name ?? "", |
| 66 | displayName: "main", |
| 67 | }); |
| 68 | const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", { |
| 69 | flow: flow.name, |
| 70 | displayName: "v1", |
| 71 | description: "initial snapshot", |
| 72 | }); |
| 73 | return { flow, version }; |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | expect(created.version.name).toContain("/versions/"); |
| 78 | expect(created.version.displayName).toEqual("v1"); |
| 79 | expect(created.version.description).toEqual("initial snapshot"); |
| 80 | expect(["SUCCEEDED", "RUNNING", undefined]).toContain( |
| 81 | created.version.state, |
| 82 | ); |
| 83 | |
| 84 | const fetched = |
| 85 | yield* dialogflow.getProjectsLocationsAgentsFlowsVersions({ |
| 86 | name: created.version.name, |
| 87 | }); |
| 88 | expect(fetched.name).toEqual(created.version.name); |
| 89 | expect(fetched.description).toContain("[alchemy "); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | const flow = yield* GCP.Dialogflow.AgentsFlow("Main", { |
| 94 | agent: agent.name ?? "", |
| 95 | flowId: created.flow.flowId, |
| 96 | displayName: "main", |
| 97 | }); |
| 98 | const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", { |
| 99 | flow: flow.name, |
| 100 | versionId: created.version.versionId, |
| 101 | displayName: "v1-ga", |
| 102 | description: "ga snapshot", |
| 103 | }); |
| 104 | return { flow, version }; |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(updated.version.name).toEqual(created.version.name); |
| 109 | expect(updated.version.displayName).toEqual("v1-ga"); |
| 110 | expect(updated.version.description).toEqual("ga snapshot"); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | const gone = yield* waitUntilGone(created.version.name); |
| 114 | expect(gone).toEqual("gone"); |
| 115 | }).pipe(Effect.ensuring(deleteAgent(agent.name ?? ""))); |
| 116 | }).pipe(logLevel, quotaTolerant), |
| 117 | { |
| 118 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 119 | timeout: 120_000, |
| 120 | }, |
| 121 | ); |