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 | const agentDisplayName = "alch-df-ex"; |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | dialogflow.getProjectsLocationsAgentsPlaybooksExamples({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("2 seconds"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsAgentsPlaybooksExamples on a missing example fails with a typed tag", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const error = yield* Effect.flip( |
| 37 | dialogflow.getProjectsLocationsAgentsPlaybooksExamples({ |
| 38 | name: `projects/${project}/locations/global/agents/missing/playbooks/missing/examples/missing`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(error._tag).toEqual("NotFound"); |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | }).pipe(logLevel, quotaTolerant), |
| 45 | { |
| 46 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 47 | timeout: 90_000, |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | test.provider.skipIf(!runLifecycle)( |
| 52 | "create, update, and delete a playbook example", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const { project } = yield* GcpEnvironment.current; |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const agent = yield* ensureAgent(project, agentDisplayName); |
| 59 | const agentName = agent.name ?? ""; |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const playbook = yield* GCP.Dialogflow.AgentsPlaybook("Support", { |
| 64 | agent: agentName, |
| 65 | displayName: "support-ex", |
| 66 | goal: "Greet the user.", |
| 67 | }); |
| 68 | const example = yield* GCP.Dialogflow.AgentsPlaybooksExample("Hello", { |
| 69 | playbook: playbook.name, |
| 70 | displayName: "hello", |
| 71 | conversationState: "OUTPUT_STATE_OK", |
| 72 | actions: [ |
| 73 | { userUtterance: { text: "hello" } }, |
| 74 | { agentUtterance: { text: "Hi, how can I help?" } }, |
| 75 | ], |
| 76 | }); |
| 77 | return { playbook, example }; |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(created.example.name).toContain("/examples/"); |
| 82 | expect(created.example.playbook).toEqual(created.playbook.name); |
| 83 | expect(created.example.displayName).toEqual("hello"); |
| 84 | |
| 85 | const fetched = yield* dialogflow.getProjectsLocationsAgentsPlaybooksExamples({ |
| 86 | name: created.example.name, |
| 87 | }); |
| 88 | expect(fetched.name).toEqual(created.example.name); |
| 89 | |
| 90 | const updated = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | const playbook = yield* GCP.Dialogflow.AgentsPlaybook("Support", { |
| 93 | agent: agentName, |
| 94 | playbookId: created.playbook.playbookId, |
| 95 | displayName: "support-ex", |
| 96 | goal: "Greet the user.", |
| 97 | }); |
| 98 | const example = yield* GCP.Dialogflow.AgentsPlaybooksExample("Hello", { |
| 99 | playbook: playbook.name, |
| 100 | exampleId: created.example.exampleId, |
| 101 | displayName: "hello", |
| 102 | conversationState: "OUTPUT_STATE_OK", |
| 103 | actions: [ |
| 104 | { userUtterance: { text: "hi" } }, |
| 105 | { agentUtterance: { text: "Hello there." } }, |
| 106 | ], |
| 107 | }); |
| 108 | return { playbook, example }; |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.example.name).toEqual(created.example.name); |
| 113 | expect(updated.example.actions?.[0]?.userUtterance?.text).toEqual("hi"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone(created.example.name); |
| 118 | expect(gone).toEqual("gone"); |
| 119 | |
| 120 | yield* deleteAgent(agentName); |
| 121 | }).pipe(logLevel, quotaTolerant), |
| 122 | { |
| 123 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 124 | timeout: 120_000, |
| 125 | }, |
| 126 | ); |