|
| 1 | +import type { IAgentRuntime } from "@elizaos/core"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + deployTextTriggerWorkflow, |
| 5 | + getWorkflowService, |
| 6 | +} from "./text-to-workflow.js"; |
| 7 | + |
| 8 | +interface DeployCall { |
| 9 | + workflow: { |
| 10 | + name: string; |
| 11 | + nodes: Array<{ |
| 12 | + type: string; |
| 13 | + parameters: Record<string, unknown>; |
| 14 | + }>; |
| 15 | + connections: Record<string, unknown>; |
| 16 | + active?: boolean; |
| 17 | + }; |
| 18 | + userId: string; |
| 19 | +} |
| 20 | + |
| 21 | +function makeRuntimeWithService( |
| 22 | + deploy: ( |
| 23 | + workflow: DeployCall["workflow"], |
| 24 | + userId: string, |
| 25 | + ) => Promise<{ id?: string; name?: string }>, |
| 26 | +): { runtime: IAgentRuntime; calls: DeployCall[] } { |
| 27 | + const calls: DeployCall[] = []; |
| 28 | + const service = { |
| 29 | + deployWorkflow: async ( |
| 30 | + workflow: DeployCall["workflow"], |
| 31 | + userId: string, |
| 32 | + ) => { |
| 33 | + calls.push({ workflow, userId }); |
| 34 | + return deploy(workflow, userId); |
| 35 | + }, |
| 36 | + }; |
| 37 | + const runtime = { |
| 38 | + getService: (type: string): unknown => (type === "workflow" ? service : null), |
| 39 | + } as unknown as IAgentRuntime; |
| 40 | + return { runtime, calls }; |
| 41 | +} |
| 42 | + |
| 43 | +describe("getWorkflowService", () => { |
| 44 | + it("returns null when the workflow service is not registered", () => { |
| 45 | + const runtime = { |
| 46 | + getService: () => null, |
| 47 | + } as unknown as IAgentRuntime; |
| 48 | + expect(getWorkflowService(runtime)).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("rejects services without a deployWorkflow method", () => { |
| 52 | + const runtime = { |
| 53 | + getService: () => ({ somethingElse: () => null }), |
| 54 | + } as unknown as IAgentRuntime; |
| 55 | + expect(getWorkflowService(runtime)).toBeNull(); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("deployTextTriggerWorkflow", () => { |
| 60 | + it("returns null when the workflow service is missing", async () => { |
| 61 | + const runtime = { |
| 62 | + getService: () => null, |
| 63 | + } as unknown as IAgentRuntime; |
| 64 | + const result = await deployTextTriggerWorkflow( |
| 65 | + runtime, |
| 66 | + { |
| 67 | + displayName: "Daily PR sweep", |
| 68 | + instructions: "Review open PRs and summarize.", |
| 69 | + wakeMode: "inject_now", |
| 70 | + }, |
| 71 | + "creator-1", |
| 72 | + ); |
| 73 | + expect(result).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("materializes a single-node respondToEvent workflow with the trigger fields", async () => { |
| 77 | + const { runtime, calls } = makeRuntimeWithService(async () => ({ |
| 78 | + id: "wf-123", |
| 79 | + name: "Daily PR sweep (auto)", |
| 80 | + })); |
| 81 | + |
| 82 | + const result = await deployTextTriggerWorkflow( |
| 83 | + runtime, |
| 84 | + { |
| 85 | + displayName: "Daily PR sweep", |
| 86 | + instructions: "Review open PRs and summarize.", |
| 87 | + wakeMode: "next_autonomy_cycle", |
| 88 | + }, |
| 89 | + "creator-1", |
| 90 | + ); |
| 91 | + |
| 92 | + expect(result).toEqual({ id: "wf-123", name: "Daily PR sweep (auto)" }); |
| 93 | + expect(calls).toHaveLength(1); |
| 94 | + const [call] = calls; |
| 95 | + expect(call.userId).toBe("creator-1"); |
| 96 | + expect(call.workflow.name).toBe("Daily PR sweep (auto)"); |
| 97 | + expect(call.workflow.nodes).toHaveLength(1); |
| 98 | + expect(call.workflow.nodes[0].type).toBe( |
| 99 | + "workflows-nodes-base.respondToEvent", |
| 100 | + ); |
| 101 | + expect(call.workflow.nodes[0].parameters).toEqual({ |
| 102 | + instructions: "Review open PRs and summarize.", |
| 103 | + displayName: "Daily PR sweep", |
| 104 | + wakeMode: "next_autonomy_cycle", |
| 105 | + }); |
| 106 | + expect(call.workflow.connections).toEqual({}); |
| 107 | + }); |
| 108 | + |
| 109 | + it("returns null when the workflow service did not return an id", async () => { |
| 110 | + const { runtime } = makeRuntimeWithService(async () => ({ |
| 111 | + id: "", |
| 112 | + name: "", |
| 113 | + })); |
| 114 | + const result = await deployTextTriggerWorkflow( |
| 115 | + runtime, |
| 116 | + { |
| 117 | + displayName: "no creds workflow", |
| 118 | + instructions: "Do the thing.", |
| 119 | + wakeMode: "inject_now", |
| 120 | + }, |
| 121 | + "creator-1", |
| 122 | + ); |
| 123 | + expect(result).toBeNull(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("falls back to the synthesized workflow name when the service returns no name", async () => { |
| 127 | + const deploy = vi.fn(async () => ({ id: "wf-1" })); |
| 128 | + const runtime = { |
| 129 | + getService: (type: string): unknown => |
| 130 | + type === "workflow" ? { deployWorkflow: deploy } : null, |
| 131 | + } as unknown as IAgentRuntime; |
| 132 | + const result = await deployTextTriggerWorkflow( |
| 133 | + runtime, |
| 134 | + { |
| 135 | + displayName: "Greet", |
| 136 | + instructions: "Say hi.", |
| 137 | + wakeMode: "inject_now", |
| 138 | + }, |
| 139 | + "creator-1", |
| 140 | + ); |
| 141 | + expect(result).toEqual({ id: "wf-1", name: "Greet (auto)" }); |
| 142 | + expect(deploy).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | +}); |
0 commit comments