|
| 1 | +import {expect, describe, test, jest} from "@jest/globals"; |
| 2 | +import { SetVariableTaskDef, TaskType, WorkflowDef} from "../../src/common"; |
| 3 | +import { orkesConductorClient } from "../../src/orkes"; |
| 4 | +import { WorkflowExecutor } from "../../src/core/executor"; |
| 5 | +import { v4 as uuidv4 } from "uuid"; |
| 6 | +import {TestUtil} from "../utils/test-util"; |
| 7 | +import { httpTask } from "../../src/core/sdk"; |
| 8 | +import { TaskClient } from "../../src/core/taskClient"; |
| 9 | + |
| 10 | +describe("Executor", () => { |
| 11 | + const clientPromise = orkesConductorClient(); |
| 12 | + |
| 13 | + jest.setTimeout(15000); |
| 14 | + const name = `testWorkflow-${Date.now()}`; |
| 15 | + const version = 1; |
| 16 | + test("Should be able to register a workflow", async () => { |
| 17 | + const client = await clientPromise; |
| 18 | + const executor = new WorkflowExecutor(client); |
| 19 | + |
| 20 | + const workflowDefinition: WorkflowDef = { |
| 21 | + name, |
| 22 | + version, |
| 23 | + tasks: [ |
| 24 | + { |
| 25 | + type: TaskType.SET_VARIABLE, |
| 26 | + name: "setVariable", |
| 27 | + taskReferenceName: "httpTaskRef", |
| 28 | + inputParameters: { |
| 29 | + hello: "world", |
| 30 | + }, |
| 31 | + }, |
| 32 | + ], |
| 33 | + inputParameters: [], |
| 34 | + timeoutSeconds: 15, |
| 35 | + }; |
| 36 | + |
| 37 | + await expect( |
| 38 | + executor.registerWorkflow(true, workflowDefinition) |
| 39 | + ).resolves.not.toThrow(); |
| 40 | + const workflowDefinitionFromApi = await client.metadataResource.get( |
| 41 | + name, |
| 42 | + version |
| 43 | + ); |
| 44 | + expect(workflowDefinitionFromApi.name).toEqual(name); |
| 45 | + expect(workflowDefinitionFromApi.version).toEqual(version); |
| 46 | + expect(workflowDefinitionFromApi.tasks[0].name).toEqual( |
| 47 | + workflowDefinition.tasks[0].name |
| 48 | + ); |
| 49 | + expect(workflowDefinitionFromApi.tasks[0].taskReferenceName).toEqual( |
| 50 | + workflowDefinition.tasks[0].taskReferenceName |
| 51 | + ); |
| 52 | + expect(workflowDefinitionFromApi.tasks[0].inputParameters).toEqual( |
| 53 | + (workflowDefinition.tasks[0] as SetVariableTaskDef).inputParameters |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + let executionId: string | undefined = undefined; |
| 58 | + test("Should be able to start a workflow", async () => { |
| 59 | + const client = await clientPromise; |
| 60 | + const executor = new WorkflowExecutor(client); |
| 61 | + executionId = await executor.startWorkflow({ name, version }); |
| 62 | + expect(executionId).toBeTruthy(); |
| 63 | + }); |
| 64 | + |
| 65 | + test("Should be able to execute workflow synchronously", async () => { |
| 66 | + const client = await clientPromise; |
| 67 | + const executor = new WorkflowExecutor(client); |
| 68 | + const workflowRun = await executor.executeWorkflow( |
| 69 | + { |
| 70 | + name: name, |
| 71 | + version: version, |
| 72 | + }, |
| 73 | + name, |
| 74 | + version, |
| 75 | + uuidv4() |
| 76 | + ); |
| 77 | + expect(workflowRun.status).toEqual("COMPLETED"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("Should be able to get workflow execution status ", async () => { |
| 81 | + const client = await clientPromise; |
| 82 | + const executor = new WorkflowExecutor(client); |
| 83 | + const workflowStatus = await executor.getWorkflowStatus( |
| 84 | + executionId!, |
| 85 | + true, |
| 86 | + true |
| 87 | + ); |
| 88 | + expect(workflowStatus.status).toBeTruthy(); |
| 89 | + }); |
| 90 | + |
| 91 | + test("Should return workflow status detail", async () => { |
| 92 | + const client = await clientPromise; |
| 93 | + const executor = new WorkflowExecutor(client); |
| 94 | + const workflowStatus = await executor.getWorkflow(executionId!, true); |
| 95 | + |
| 96 | + expect(workflowStatus.status).toBeTruthy(); |
| 97 | + expect(workflowStatus.tasks?.length).toBe(1); |
| 98 | + }); |
| 99 | + test("Should execute a workflow with indempotency key", async () => { |
| 100 | + const client = await clientPromise; |
| 101 | + const executor = new WorkflowExecutor(client); |
| 102 | + const idempotencyKey = uuidv4(); |
| 103 | + const executionId = await executor.startWorkflow({ |
| 104 | + name: name, |
| 105 | + version: version, |
| 106 | + idempotencyKey, |
| 107 | + idempotencyStrategy: "RETURN_EXISTING", |
| 108 | + }); |
| 109 | + |
| 110 | + const executionDetails = await executor.getWorkflow(executionId!, true); |
| 111 | + expect(executionDetails.idempotencyKey).toEqual(idempotencyKey); |
| 112 | + }); |
| 113 | + |
| 114 | + test("Should run workflow with http task with asyncComplete true", async () => { |
| 115 | + const client = await clientPromise; |
| 116 | + const executor = new WorkflowExecutor(client); |
| 117 | + |
| 118 | + await executor.registerWorkflow(true, { |
| 119 | + name: "test_jssdk_workflow_with_http_task_with_asyncComplete_true", |
| 120 | + version: 1, |
| 121 | + ownerEmail: "developers@orkes.io", |
| 122 | + tasks: [httpTask("test_jssdk_http_task_with_asyncComplete_true", { uri: "http://www.yahoo.com", method: "GET" }, true)], |
| 123 | + inputParameters: [], |
| 124 | + outputParameters: {}, |
| 125 | + timeoutSeconds: 300, |
| 126 | + }); |
| 127 | + |
| 128 | + const executionId = await executor.startWorkflow({ |
| 129 | + name: "test_jssdk_workflow_with_http_task_with_asyncComplete_true", |
| 130 | + input: {}, |
| 131 | + version: 1, |
| 132 | + }); |
| 133 | + |
| 134 | + const workflowStatusBefore = await TestUtil.waitForWorkflowStatus(executor, executionId, "RUNNING"); |
| 135 | + |
| 136 | + expect(["IN_PROGRESS", "SCHEDULED"]).toContain(workflowStatusBefore.tasks?.[0]?.status); |
| 137 | + |
| 138 | + const taskClient = new TaskClient(client); |
| 139 | + taskClient.updateTaskResult(executionId, "test_jssdk_http_task_with_asyncComplete_true", "COMPLETED", { hello: "From manuall api call updating task result" }); |
| 140 | + |
| 141 | + const workflowStatusAfter = await TestUtil.waitForWorkflowStatus(executor, executionId, "COMPLETED"); |
| 142 | + |
| 143 | + expect(workflowStatusAfter.tasks?.[0]?.status).toEqual("COMPLETED"); |
| 144 | + }); |
| 145 | + |
| 146 | + test("Should run workflow with an optional http task", async () => { |
| 147 | + const executor = new WorkflowExecutor(await clientPromise); |
| 148 | + |
| 149 | + await executor.registerWorkflow(true, { |
| 150 | + name: "test_jssdk_workflow_with_optional_http_task", |
| 151 | + version: 1, |
| 152 | + ownerEmail: "developers@orkes.io", |
| 153 | + tasks: [httpTask("test_jssdk_optional_http_task", { uri: "uncorrect_uri", method: "GET" }, false, true)], |
| 154 | + inputParameters: [], |
| 155 | + outputParameters: {}, |
| 156 | + timeoutSeconds: 300, |
| 157 | + }); |
| 158 | + |
| 159 | + const executionId = await executor.startWorkflow({ |
| 160 | + name: "test_jssdk_workflow_with_optional_http_task", |
| 161 | + input: {}, |
| 162 | + version: 1, |
| 163 | + }); |
| 164 | + |
| 165 | + const workflowStatus = await TestUtil.waitForWorkflowStatus(executor, executionId, "COMPLETED"); |
| 166 | + expect(["FAILED", "COMPLETED_WITH_ERRORS"]).toContain(workflowStatus.tasks?.[0]?.status); |
| 167 | + }); |
| 168 | +}); |
0 commit comments