Skip to content

Commit

Permalink
refactor(engine): improve readablity
Browse files Browse the repository at this point in the history
  • Loading branch information
towersxu committed Aug 8, 2023
1 parent 7a218c9 commit 69f058c
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 58 deletions.
8 changes: 6 additions & 2 deletions packages/engine/__test__/01_index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import Engine from '../src/index';
describe('@logicflow/engine', () => {
test('Execution Process Completed, Returning Data Containing executionId', async () => {
// TODO: context在初始化engine时传入
const engine = new Engine();
const engine = new Engine({
context: {},
});
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
Expand All @@ -24,7 +29,6 @@ describe('@logicflow/engine', () => {
}
]
},
context: {},
globalData: {},
}
const flowModel = engine.load(flowData);
Expand Down
40 changes: 40 additions & 0 deletions packages/engine/__test__/02_recorder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ describe('@logicflow/engine Recorder', () => {
test('Using the getExecutionRecord API, receive the complete execution record of the process.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -54,4 +57,41 @@ describe('@logicflow/engine Recorder', () => {
expect(execution[1].nodeId).toBe('node2');
expect(execution[1].nodeType).toBe('TaskNode');
});
test('The execution record cannot be obtained when the number of executions exceeds the maximum number of executions.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
id: 'node1',
type: 'StartNode',
properties: {}
},
{
id: 'node2',
type: 'TaskNode',
properties: {}
}
],
edges: [
{
id: 'edge1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
}
]
},
global: {},
}
engine.load(flowData);
engine.recorder.setMaxRecorderNumber(2);
const result = await engine.execute();
await engine.execute();
await engine.execute();
const execution = await engine.getExecutionRecord(result.executionId);
expect(execution).toBe(null);
})
});
4 changes: 4 additions & 0 deletions packages/engine/__test__/03_condition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ describe('@logicflow/engine condition', () => {
test('The process will not continue its execution if the condition expression evaluates to false.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* |--> node3
*/
graphData: {
nodes: [
{
Expand Down
16 changes: 16 additions & 0 deletions packages/engine/__test__/04_execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, all of them are executed by default.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -59,6 +63,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, you can specify which start node to execute.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -118,6 +126,10 @@ describe('@logicflow/engine Execute', () => {
test('When attempting to execute a non-existent start node in a process, an execution exception is raised.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -177,6 +189,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, parallel execution is supported, and each start node generates a unique execution ID', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down
89 changes: 57 additions & 32 deletions packages/engine/__test__/05_customNode.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
import Engine, { TaskNode } from '../src/index';

describe('@logicflow/engine Customize Node', () => {
class UserTask extends TaskNode {
class DataNode extends TaskNode {
async action() {
return {
status: 'interrupted',
detail: {
formId: 'form_1'
}
};
this.globalData['dataSource'] = {
time: this.context.getTime(),
}
}
async onResume({ data }) {
this.globalData.formId = data.formId;
}
class Mod2Node extends TaskNode {
async action() {
const dataSource = this.globalData['dataSource'];
if (dataSource && dataSource.time) {
dataSource.time % 2 === 0 ? this.globalData['output'] = 'even' : this.globalData['output'] = 'odd';
}
}
}
class OutputNode extends TaskNode {
async action() {
const output = this.globalData['output'];
this.properties['output'] = output;
}
}
const engine = new Engine();
const engine = new Engine({
context: {
getTime() {
return new Date().getTime();
}
}
});
engine.register({
type: 'DataNode',
model: DataNode,
})
engine.register({
type: 'Mod2Node',
model: Mod2Node,
})
engine.register({
type: 'UserTask',
model: UserTask,
type: 'OutputNode',
model: OutputNode,
})

const flowData = {
/**
* node1 |--> node2(DataNode) |--> node3(Mod2Node) |--> node4(OutputNode)
*/
graphData: {
nodes: [
{
Expand All @@ -30,14 +56,19 @@ describe('@logicflow/engine Customize Node', () => {
},
{
id: 'node2',
type: 'UserTask',
type: 'DataNode',
properties: {}
},
{
id: 'node3',
type: 'TaskNode',
type: 'Mod2Node',
properties: {}
},
{
id: 'node4',
type: 'OutputNode',
properties: {}
}
},
],
edges: [
{
Expand All @@ -53,29 +84,23 @@ describe('@logicflow/engine Customize Node', () => {
targetNodeId: 'node3',
properties: {
}
}
},
{
id: 'edge3',
sourceNodeId: 'node3',
targetNodeId: 'node4',
properties: {
}
},
]
},
globalData: {
},
}
engine.load(flowData);
test('After executing the process, receive the flow status as "interrupted" and include detailed information returned by the custom node.', async () => {
test('When the process is completed, the output field in the properties attribute of the last node is odd or even.', async () => {
const result = await engine.execute();
expect(result.status).toBe('interrupted');
expect(result.detail.formId).toEqual('form_1');
});
test('After a process is interrupted, you can resume its execution using the API.', async () => {
const result = await engine.execute();
const result2 = await engine.resume({
executionId: result.executionId,
nodeId: result.nodeId,
actionId: result.actionId,
data: {
formId: 'form_2'
}
})
expect(result2.status).toBe('completed')
expect(result2.nodeId).toEqual('node3')
const execution = await engine.getExecutionRecord(result.executionId);
expect(['odd', 'even'].indexOf(execution[execution.length - 1].properties.output) !== -1).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ describe('@logicflow/engine parallel execution', () => {
model: FetchNode,
})
const flowData = {
/**
* node1 |--> node2(FetchTask)
* |--> node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -78,4 +82,3 @@ describe('@logicflow/engine parallel execution', () => {
expect(execution[3].nodeId).toEqual('node2')
});
})
// TODO: 增加某个节点出现异常和interrupt 后,控制其他分支节点是否要继续执行的测试用例。
Loading

0 comments on commit 69f058c

Please sign in to comment.