2023-05-02 01:37:19 -07:00
import type { IConnections , IExecuteData , INode , IRunExecutionData } from '@/Interfaces' ;
2022-11-09 06:25:00 -08:00
import { Workflow } from '@/Workflow' ;
import { WorkflowDataProxy } from '@/WorkflowDataProxy' ;
2022-03-13 01:34:44 -08:00
import * as Helpers from './Helpers' ;
2023-06-22 16:45:54 -07:00
import { ExpressionError } from '@/ExpressionError' ;
2022-03-13 01:34:44 -08:00
describe ( 'WorkflowDataProxy' , ( ) = > {
describe ( 'test data proxy' , ( ) = > {
const nodes : INode [ ] = [
{
name : 'Start' ,
type : 'test.set' ,
2022-09-29 14:02:25 -07:00
parameters : { } ,
2022-03-13 01:34:44 -08:00
typeVersion : 1 ,
2022-08-03 04:06:53 -07:00
id : 'uuid-1' ,
2022-03-13 01:34:44 -08:00
position : [ 100 , 200 ] ,
} ,
{
2022-09-29 14:02:25 -07:00
name : 'Function' ,
type : 'test.set' ,
2022-03-13 01:34:44 -08:00
parameters : {
functionCode :
2022-09-29 03:33:16 -07:00
'// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.function/\nconst { DateTime, Duration, Interval } = require("luxon");\n\nconst data = [\n {\n "length": 105\n },\n {\n "length": 160\n },\n {\n "length": 121\n },\n {\n "length": 275\n },\n {\n "length": 950\n },\n];\n\nreturn data.map(fact => ({json: fact}));' ,
2022-03-13 01:34:44 -08:00
} ,
typeVersion : 1 ,
2022-08-03 04:06:53 -07:00
id : 'uuid-2' ,
2022-03-13 01:34:44 -08:00
position : [ 280 , 200 ] ,
} ,
{
name : 'Rename' ,
type : 'test.set' ,
2022-09-29 14:02:25 -07:00
parameters : {
value1 : 'data' ,
value2 : 'initialName' ,
} ,
2022-03-13 01:34:44 -08:00
typeVersion : 1 ,
2022-08-03 04:06:53 -07:00
id : 'uuid-3' ,
2022-03-13 01:34:44 -08:00
position : [ 460 , 200 ] ,
} ,
2022-09-29 14:02:25 -07:00
{
2023-06-22 16:45:54 -07:00
name : 'Set' ,
2022-09-29 14:02:25 -07:00
type : 'test.set' ,
parameters : { } ,
typeVersion : 1 ,
id : 'uuid-4' ,
position : [ 640 , 200 ] ,
} ,
2023-06-22 16:45:54 -07:00
{
name : 'End' ,
type : 'test.set' ,
parameters : { } ,
typeVersion : 1 ,
id : 'uuid-5' ,
position : [ 640 , 200 ] ,
} ,
2022-03-13 01:34:44 -08:00
] ;
const connections : IConnections = {
Start : {
main : [
[
{
node : 'Function' ,
type : 'main' ,
index : 0 ,
} ,
] ,
] ,
} ,
Function : {
main : [
[
{
node : 'Rename' ,
type : 'main' ,
index : 0 ,
} ,
] ,
] ,
} ,
2022-09-29 14:02:25 -07:00
Rename : {
main : [
[
{
node : 'End' ,
type : 'main' ,
index : 0 ,
} ,
] ,
] ,
} ,
2022-03-13 01:34:44 -08:00
} ;
const runExecutionData : IRunExecutionData = {
resultData : {
runData : {
2022-09-29 14:02:25 -07:00
Start : [
{
startTime : 1 ,
executionTime : 1 ,
data : {
main : [
[
{
json : { } ,
} ,
] ,
] ,
} ,
source : [ ] ,
} ,
] ,
2022-03-13 01:34:44 -08:00
Function : [
{
startTime : 1 ,
executionTime : 1 ,
data : {
main : [
[
{
2022-09-29 14:02:25 -07:00
json : { initialName : 105 } ,
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
{
2022-09-29 14:02:25 -07:00
json : { initialName : 160 } ,
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
{
2022-09-29 14:02:25 -07:00
json : { initialName : 121 } ,
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
{
2022-09-29 14:02:25 -07:00
json : { initialName : 275 } ,
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
{
2022-09-29 14:02:25 -07:00
json : { initialName : 950 } ,
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
] ,
] ,
} ,
2022-09-29 14:02:25 -07:00
source : [
{
previousNode : 'Start' ,
} ,
] ,
2022-03-13 01:34:44 -08:00
} ,
] ,
Rename : [
{
startTime : 1 ,
executionTime : 1 ,
data : {
main : [
[
{
json : { data : 105 } ,
2022-09-29 14:02:25 -07:00
pairedItem : { item : 0 } ,
2022-03-13 01:34:44 -08:00
} ,
{
json : { data : 160 } ,
2022-09-29 14:02:25 -07:00
pairedItem : { item : 1 } ,
2022-03-13 01:34:44 -08:00
} ,
{
json : { data : 121 } ,
2022-09-29 14:02:25 -07:00
pairedItem : { item : 2 } ,
2022-03-13 01:34:44 -08:00
} ,
{
json : { data : 275 } ,
2022-09-29 14:02:25 -07:00
pairedItem : { item : 3 } ,
2022-03-13 01:34:44 -08:00
} ,
{
json : { data : 950 } ,
2022-09-29 14:02:25 -07:00
pairedItem : { item : 4 } ,
2022-03-13 01:34:44 -08:00
} ,
] ,
] ,
} ,
2022-09-29 14:02:25 -07:00
source : [
{
previousNode : 'Function' ,
} ,
] ,
} ,
] ,
End : [
{
startTime : 1 ,
executionTime : 1 ,
data : {
main : [
[
{
json : { data : 105 } ,
pairedItem : { item : 0 } ,
} ,
{
json : { data : 160 } ,
pairedItem : { item : 1 } ,
} ,
{
json : { data : 121 } ,
pairedItem : { item : 2 } ,
} ,
{
json : { data : 275 } ,
pairedItem : { item : 3 } ,
} ,
{
json : { data : 950 } ,
pairedItem : { item : 4 } ,
} ,
] ,
] ,
} ,
source : [
{
previousNode : 'Rename' ,
} ,
] ,
2022-03-13 01:34:44 -08:00
} ,
] ,
} ,
} ,
} ;
const nodeTypes = Helpers . NodeTypes ( ) ;
2022-09-29 14:02:25 -07:00
const workflow = new Workflow ( {
id : '123' ,
name : 'test workflow' ,
nodes ,
connections ,
active : false ,
nodeTypes ,
} ) ;
const nameLastNode = 'End' ;
const lastNodeConnectionInputData =
runExecutionData . resultData . runData [ nameLastNode ] [ 0 ] . data ! . main [ 0 ] ;
const executeData : IExecuteData = {
data : runExecutionData.resultData.runData [ nameLastNode ] [ 0 ] . data ! ,
node : nodes.find ( ( node ) = > node . name === nameLastNode ) as INode ,
source : {
2023-05-02 01:37:19 -07:00
main : runExecutionData.resultData.runData [ nameLastNode ] [ 0 ] . source ,
2022-09-29 14:02:25 -07:00
} ,
} ;
2022-03-13 01:34:44 -08:00
const dataProxy = new WorkflowDataProxy (
workflow ,
runExecutionData ,
0 ,
0 ,
2022-09-29 14:02:25 -07:00
nameLastNode ,
2023-10-27 05:17:52 -07:00
lastNodeConnectionInputData ? ? [ ] ,
2022-03-13 01:34:44 -08:00
{ } ,
'manual' ,
{ } ,
2022-09-29 14:02:25 -07:00
executeData ,
2022-03-13 01:34:44 -08:00
) ;
const proxy = dataProxy . getDataProxy ( ) ;
test ( 'test $("NodeName").all()' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . all ( ) [ 1 ] . json . data ) . toEqual ( 160 ) ;
} ) ;
test ( 'test $("NodeName").all() length' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . all ( ) . length ) . toEqual ( 5 ) ;
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $("NodeName").item' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . item ) . toEqual ( { json : { data : 105 } , pairedItem : { item : 0 } } ) ;
} ) ;
test ( 'test $("NodeNameEarlier").item' , ( ) = > {
expect ( proxy . $ ( 'Function' ) . item ) . toEqual ( {
json : { initialName : 105 } ,
pairedItem : { item : 0 } ,
} ) ;
2022-03-13 01:34:44 -08:00
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $("NodeName").itemMatching(2)' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . itemMatching ( 2 ) . json . data ) . toEqual ( 121 ) ;
2022-03-13 01:34:44 -08:00
} ) ;
test ( 'test $("NodeName").first()' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . first ( ) . json . data ) . toEqual ( 105 ) ;
} ) ;
test ( 'test $("NodeName").last()' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . last ( ) . json . data ) . toEqual ( 950 ) ;
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $("NodeName").params' , ( ) = > {
expect ( proxy . $ ( 'Rename' ) . params ) . toEqual ( { value1 : 'data' , value2 : 'initialName' } ) ;
} ) ;
2023-06-22 16:45:54 -07:00
test ( '$("NodeName")' , ( ) = > {
expect ( ( ) = > proxy . $ ( 'doNotExist' ) ) . toThrowError ( ExpressionError ) ;
} ) ;
test ( '$("NodeName")' , ( ) = > {
expect ( ( ) = > proxy . $ ( 'Set' ) ) . toThrowError ( ExpressionError ) ;
} ) ;
2022-03-13 01:34:44 -08:00
test ( 'test $input.all()' , ( ) = > {
2022-09-29 14:02:25 -07:00
expect ( proxy . $input . all ( ) [ 1 ] . json . data ) . toEqual ( 160 ) ;
2022-03-13 01:34:44 -08:00
} ) ;
test ( 'test $input.all() length' , ( ) = > {
expect ( proxy . $input . all ( ) . length ) . toEqual ( 5 ) ;
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $input.first()' , ( ) = > {
expect ( proxy . $input . first ( ) . json . data ) . toEqual ( 105 ) ;
} ) ;
test ( 'test $input.last()' , ( ) = > {
expect ( proxy . $input . last ( ) . json . data ) . toEqual ( 950 ) ;
} ) ;
test ( 'test $input.item' , ( ) = > {
expect ( proxy . $input . item . json . data ) . toEqual ( 105 ) ;
2022-03-13 01:34:44 -08:00
} ) ;
test ( 'test $thisItem' , ( ) = > {
2022-09-29 14:02:25 -07:00
expect ( proxy . $thisItem . json . data ) . toEqual ( 105 ) ;
2022-03-13 01:34:44 -08:00
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $binary' , ( ) = > {
expect ( proxy . $binary ) . toEqual ( { } ) ;
2022-03-13 01:34:44 -08:00
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $json' , ( ) = > {
expect ( proxy . $json ) . toEqual ( { data : 105 } ) ;
2022-03-13 01:34:44 -08:00
} ) ;
2022-09-29 14:02:25 -07:00
test ( 'test $itemIndex' , ( ) = > {
expect ( proxy . $itemIndex ) . toEqual ( 0 ) ;
} ) ;
test ( 'test $prevNode' , ( ) = > {
expect ( proxy . $prevNode ) . toEqual ( { name : 'Rename' , outputIndex : 0 , runIndex : 0 } ) ;
} ) ;
test ( 'test $runIndex' , ( ) = > {
expect ( proxy . $runIndex ) . toEqual ( 0 ) ;
} ) ;
test ( 'test $workflow' , ( ) = > {
expect ( proxy . $workflow ) . toEqual ( {
active : false ,
id : '123' ,
name : 'test workflow' ,
} ) ;
2022-03-13 01:34:44 -08:00
} ) ;
} ) ;
} ) ;