|
1 | 1 | import {
|
2 |
| - createDeferredPromise, |
3 | 2 | ICredentialsHelper,
|
4 |
| - IExecuteWorkflowInfo, |
| 3 | + IExecuteFunctions, |
| 4 | + IExecuteSingleFunctions, |
| 5 | + INodeExecutionData, |
5 | 6 | IRun,
|
6 |
| - IWorkflowBase, |
7 |
| - IWorkflowExecuteAdditionalData, |
8 |
| - LoggerProxy, |
9 |
| - Workflow, |
| 7 | + ITaskData, |
10 | 8 | } from 'n8n-workflow';
|
11 |
| -import { WorkflowExecute, ExecutionLifecycleHooks } from 'n8n-core'; |
12 | 9 | import { nodeTypes } from './nodeTypesClass';
|
| 10 | +import * as fixtures from './fixtures'; |
13 | 11 |
|
14 | 12 | export type ExecuteWorkflowArgs = {
|
15 | 13 | workflow: any;
|
16 | 14 | credentialsHelper: ICredentialsHelper;
|
17 | 15 | };
|
18 | 16 |
|
19 |
| -export const executeWorkflow = async ({ credentialsHelper, ...args }: ExecuteWorkflowArgs) => { |
20 |
| - LoggerProxy.init({ |
21 |
| - debug() {}, |
22 |
| - error() {}, |
23 |
| - info() {}, |
24 |
| - warn() {}, |
25 |
| - }); |
| 17 | +export const executeWorkflow = async ({ |
| 18 | + credentialsHelper, |
| 19 | + workflow: workflowJson, |
| 20 | +}: ExecuteWorkflowArgs): Promise<{ executionData: IRun }> => { |
| 21 | + // Pick the first node (your crawler) |
| 22 | + const [node] = workflowJson.nodes; |
26 | 23 |
|
27 |
| - const workflow = new Workflow({ |
28 |
| - id: 'test', |
29 |
| - active: true, |
30 |
| - connections: args.workflow.connections, |
31 |
| - nodes: args.workflow.nodes, |
32 |
| - nodeTypes, |
33 |
| - }); |
| 24 | + // Minimal fake executeFunctions |
| 25 | + const fakeExecuteFunctions = { |
| 26 | + getCredentials: async (name: string) => { |
| 27 | + return credentialsHelper.getDecrypted({} as any, { name } as any, name, 'manual'); |
| 28 | + }, |
| 29 | + getNodeParameter: (parameterName: string) => { |
| 30 | + return node.parameters[parameterName]; |
| 31 | + }, |
| 32 | + getInputData: (): INodeExecutionData[] => { |
| 33 | + // Provide at least one empty item so loops like `for (let i = 0; i < items.length; i++)` |
| 34 | + // still work |
| 35 | + return [{ json: {} }]; |
| 36 | + }, |
| 37 | + continueOnFail: () => { |
| 38 | + return false; |
| 39 | + }, |
| 40 | + getNode: () => { |
| 41 | + return node; |
| 42 | + }, |
| 43 | + helpers: { |
| 44 | + requestWithAuthentication: async function ( |
| 45 | + this: IExecuteFunctions, |
| 46 | + _credentialType: string, |
| 47 | + options: any, |
| 48 | + ) { |
| 49 | + const url = options.url as string; |
34 | 50 |
|
35 |
| - const waitPromise = createDeferredPromise<IRun>(); |
| 51 | + if (url.includes('/builds/default')) { |
| 52 | + return fixtures.getBuildResult(); |
| 53 | + } |
| 54 | + if (url.includes('/runs')) { |
| 55 | + return fixtures.runActorResult(); |
| 56 | + } |
| 57 | + if (url.includes('/actor-runs/')) { |
| 58 | + return fixtures.getSuccessRunResult(); |
| 59 | + } |
| 60 | + if (url.includes('/actors/')) { |
| 61 | + return fixtures.getActorResult(); |
| 62 | + } |
| 63 | + if (url.includes('/datasets/')) { |
| 64 | + return fixtures.getDatasetItems(); |
| 65 | + } |
36 | 66 |
|
37 |
| - const workflowData: IWorkflowBase = { |
38 |
| - id: 'test', |
39 |
| - name: 'test', |
40 |
| - createdAt: new Date(), |
41 |
| - updatedAt: new Date(), |
42 |
| - active: true, |
43 |
| - nodes: args.workflow.nodes, |
44 |
| - connections: args.workflow.connections, |
45 |
| - }; |
46 |
| - |
47 |
| - const additionalData: IWorkflowExecuteAdditionalData = { |
48 |
| - credentialsHelper, |
49 |
| - hooks: new ExecutionLifecycleHooks('trigger', '1', workflowData), |
50 |
| - executeWorkflow: async (workflowInfo: IExecuteWorkflowInfo): Promise<any> => {}, |
51 |
| - restApiUrl: 'http://localhost:5678', |
52 |
| - webhookBaseUrl: 'http://localhost:5678', |
53 |
| - webhookWaitingBaseUrl: 'http://localhost:5678', |
54 |
| - webhookTestBaseUrl: 'http://localhost:5678', |
55 |
| - userId: 'userId', |
56 |
| - instanceBaseUrl: 'http://localhost:5678', |
57 |
| - formWaitingBaseUrl: 'http://localhost:5678', |
58 |
| - variables: {}, |
59 |
| - secretsHelpers: {} as any, |
60 |
| - logAiEvent: async () => {}, |
61 |
| - startRunnerTask: (async () => {}) as any, |
62 |
| - }; |
| 67 | + throw new Error(`Unhandled request in fixture stub: ${url}`); |
| 68 | + }, |
| 69 | + returnJsonArray: (items: any[]) => { |
| 70 | + return items.map((i) => ({ json: i })); |
| 71 | + }, |
| 72 | + constructExecutionMetaData: (inputData: any, _options: any) => { |
| 73 | + return inputData; |
| 74 | + }, |
| 75 | + }, |
| 76 | + } as unknown as IExecuteFunctions & IExecuteSingleFunctions; |
63 | 77 |
|
64 |
| - const workflowExecute = new WorkflowExecute(additionalData, 'cli'); |
| 78 | + // Run the node directly |
| 79 | + const nodeType = nodeTypes.getByNameAndVersion(node.type, node.typeVersion); |
| 80 | + if (!('execute' in nodeType) || typeof nodeType.execute !== 'function') { |
| 81 | + throw new Error(`Node ${node.type} has no execute() method`); |
| 82 | + } |
| 83 | + const result = await (nodeType.execute as Function).call(fakeExecuteFunctions); |
65 | 84 |
|
66 |
| - const executionData = await workflowExecute.run(workflow); |
| 85 | + // Build fake ITaskData |
| 86 | + const taskData: ITaskData = { |
| 87 | + startTime: Date.now(), |
| 88 | + executionTime: 1, |
| 89 | + executionStatus: 'success', |
| 90 | + data: { main: result as any }, |
| 91 | + source: [ |
| 92 | + { |
| 93 | + previousNode: '', |
| 94 | + }, |
| 95 | + ], |
| 96 | + }; |
67 | 97 |
|
68 |
| - return { |
69 |
| - workflow, |
70 |
| - waitPromise, |
71 |
| - executionData, |
72 |
| - additionalData, |
| 98 | + // Wrap in fake IRun-like structure |
| 99 | + const executionData: IRun = { |
| 100 | + mode: 'manual', |
| 101 | + status: 'success', |
| 102 | + data: { |
| 103 | + resultData: { |
| 104 | + runData: { |
| 105 | + [node.name]: [taskData], |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + finished: true, |
| 110 | + startedAt: new Date(), |
| 111 | + stoppedAt: new Date(), |
73 | 112 | };
|
| 113 | + |
| 114 | + return { executionData }; |
74 | 115 | };
|
0 commit comments