| 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-ex"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | dialogflow.getProjectsLocationsAgentsPlaybooksExamples({ 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 | "getProjectsLocationsAgentsPlaybooksExamples on a missing example 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.getProjectsLocationsAgentsPlaybooksExamples({ |
| 41 | name: `projects/${project}/locations/global/agents/missing/playbooks/missing/examples/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 playbook example", |
| 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 | const playbook = yield* GCP.Dialogflow.AgentsPlaybook("Support", { |
| 67 | agent: agentName, |
| 68 | displayName: "support-ex", |
| 69 | goal: "Greet the user.", |
| 70 | }); |
| 71 | const example = yield* GCP.Dialogflow.AgentsPlaybooksExample( |
| 72 | "Hello", |
| 73 | { |
| 74 | playbook: playbook.name, |
| 75 | displayName: "hello", |
| 76 | conversationState: "OUTPUT_STATE_OK", |
| 77 | actions: [ |
| 78 | { userUtterance: { text: "hello" } }, |
| 79 | { agentUtterance: { text: "Hi, how can I help?" } }, |
| 80 | ], |
| 81 | }, |
| 82 | ); |
| 83 | return { playbook, example }; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.example.name).toContain("/examples/"); |
| 88 | expect(created.example.playbook).toEqual(created.playbook.name); |
| 89 | expect(created.example.displayName).toEqual("hello"); |
| 90 | |
| 91 | const fetched = |
| 92 | yield* dialogflow.getProjectsLocationsAgentsPlaybooksExamples({ |
| 93 | name: created.example.name, |
| 94 | }); |
| 95 | expect(fetched.name).toEqual(created.example.name); |
| 96 | |
| 97 | const updated = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | const playbook = yield* GCP.Dialogflow.AgentsPlaybook("Support", { |
| 100 | agent: agentName, |
| 101 | playbookId: created.playbook.playbookId, |
| 102 | displayName: "support-ex", |
| 103 | goal: "Greet the user.", |
| 104 | }); |
| 105 | const example = yield* GCP.Dialogflow.AgentsPlaybooksExample( |
| 106 | "Hello", |
| 107 | { |
| 108 | playbook: playbook.name, |
| 109 | exampleId: created.example.exampleId, |
| 110 | displayName: "hello", |
| 111 | conversationState: "OUTPUT_STATE_OK", |
| 112 | actions: [ |
| 113 | { userUtterance: { text: "hi" } }, |
| 114 | { agentUtterance: { text: "Hello there." } }, |
| 115 | ], |
| 116 | }, |
| 117 | ); |
| 118 | return { playbook, example }; |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.example.name).toEqual(created.example.name); |
| 123 | expect(updated.example.actions?.[0]?.userUtterance?.text).toEqual("hi"); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | const gone = yield* waitUntilGone(created.example.name); |
| 128 | expect(gone).toEqual("gone"); |
| 129 | |
| 130 | yield* deleteAgent(agentName); |
| 131 | }).pipe(logLevel, quotaTolerant), |
| 132 | { |
| 133 | tags: ["provider:gcp", "provider:gcp:dialogflow", "live"], |
| 134 | timeout: 120_000, |
| 135 | }, |
| 136 | ); |