The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { deleteAgent, ensureAgent, quotaTolerant } from "./parent.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const runLifecycle = !process.env.FAST; |
| 16 | // Non-global Dialogflow CX locations need the {region}-dialogflow host, which |
| 17 | // distilled does not route to yet; the global location works everywhere. |
| 18 | const location = "global"; |
| 19 | const agentDisplayName = "alch-df-atrg"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | dialogflow.getProjectsLocationsAgentsTransitionRouteGroups({ 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 | "getProjectsLocationsAgentsTransitionRouteGroups on a missing group 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.getProjectsLocationsAgentsTransitionRouteGroups({ |
| 41 | name: `projects/${project}/locations/${location}/agents/missing/transitionRouteGroups/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 an agent transition route group", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const { project } = yield* GcpEnvironment.current; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const agent = yield* ensureAgent(project, agentDisplayName, location); |
| 62 | const agentName = agent.name ?? ""; |
| 63 | |
| 64 | yield* Effect.gen(function* () { |
| 65 | const created = yield* stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | return yield* GCP.Dialogflow.AgentsTransitionRouteGroup("Fallback", { |
| 68 | agent: agentName, |
| 69 | location, |
| 70 | displayName: "g1", |
| 71 | transitionRoutes: [ |
| 72 | { |
| 73 | condition: "true", |
| 74 | triggerFulfillment: { |
| 75 | messages: [{ text: { text: ["ok"] } }], |
| 76 | }, |
| 77 | }, |
| 78 | ], |
| 79 | }); |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | expect(created.name).toContain("/transitionRouteGroups/"); |
| 84 | expect(created.name).not.toContain("/flows/"); |
| 85 | expect(created.agent).toEqual(agentName); |
| 86 | expect(created.location).toEqual(location); |
| 87 | expect(created.displayName).toEqual("g1"); |
| 88 | expect(created.transitionRoutes[0]?.condition).toEqual("true"); |
| 89 | |
| 90 | const fetched = yield* dialogflow.getProjectsLocationsAgentsTransitionRouteGroups({ |
| 91 | name: created.name, |
| 92 | }); |
| 93 | expect(fetched.name).toEqual(created.name); |
| 94 | expect(fetched.displayName).toContain("[alc "); |
| 95 | |
| 96 | const updated = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* GCP.Dialogflow.AgentsTransitionRouteGroup("Fallback", { |
| 99 | agent: agentName, |
| 100 | location, |
| 101 | transitionRouteGroupId: created.transitionRouteGroupId, |
| 102 | displayName: "g2", |
| 103 | transitionRoutes: [ |
| 104 | { |
| 105 | condition: "true", |
| 106 | triggerFulfillment: { |
| 107 | messages: [{ text: { text: ["sorry"] } }], |
| 108 | }, |
| 109 | }, |
| 110 | ], |
| 111 | }); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | expect(updated.name).toEqual(created.name); |
| 116 | expect(updated.displayName).toEqual("g2"); |
| 117 | expect( |
| 118 | updated.transitionRoutes[0]?.triggerFulfillment?.messages?.[0]?.text?.text?.[0], |
| 119 | ).toEqual("sorry"); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | const gone = yield* waitUntilGone(created.name); |
| 123 | expect(gone).toEqual("gone"); |
| 124 | }).pipe(Effect.ensuring(deleteAgent(agentName))); |
| 125 | }).pipe(logLevel, quotaTolerant), |
| 126 | { |
| 127 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 128 | timeout: 120_000, |
| 129 | }, |
| 130 | ); |