mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 16:44:07 -08:00
wip: mocking trigger nodes
This commit is contained in:
parent
d433589b58
commit
e8031e9e69
|
@ -4,30 +4,48 @@ import assert from 'node:assert';
|
||||||
import { Container, Service } from 'typedi';
|
import { Container, Service } from 'typedi';
|
||||||
|
|
||||||
import { ActiveExecutions } from '@/active-executions';
|
import { ActiveExecutions } from '@/active-executions';
|
||||||
|
import type { ExecutionEntity } from '@/databases/entities/execution-entity';
|
||||||
import type { User } from '@/databases/entities/user';
|
import type { User } from '@/databases/entities/user';
|
||||||
|
import type { WorkflowEntity } from '@/databases/entities/workflow-entity';
|
||||||
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
|
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
|
||||||
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
|
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
|
||||||
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
||||||
import { TestDefinitionService } from '@/evaluation/test-definition.service.ee';
|
import { TestDefinitionService } from '@/evaluation/test-definition.service.ee';
|
||||||
import type { IExecutionDb, IExecutionResponse } from '@/interfaces';
|
import type { IExecutionDb, IExecutionResponse } from '@/interfaces';
|
||||||
import { WorkflowRunner } from '@/workflow-runner';
|
import { WorkflowRunner } from '@/workflow-runner';
|
||||||
// import { WorkflowExecutionService } from '@/workflows/workflow-execution.service';
|
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export class TestRunnerService {
|
export class TestRunnerService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly testDefinitionsService: TestDefinitionService,
|
private readonly testDefinitionsService: TestDefinitionService,
|
||||||
private readonly workflowRepository: WorkflowRepository,
|
private readonly workflowRepository: WorkflowRepository,
|
||||||
// private readonly workflowExecutionService: WorkflowExecutionService,
|
|
||||||
private readonly workflowRunner: WorkflowRunner,
|
private readonly workflowRunner: WorkflowRunner,
|
||||||
private readonly executionRepository: ExecutionRepository,
|
private readonly executionRepository: ExecutionRepository,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
private createPinDataFromExecution(
|
||||||
|
workflow: WorkflowEntity,
|
||||||
|
execution: ExecutionEntity,
|
||||||
|
): IPinData {
|
||||||
|
const executionData = parse(execution.executionData.data) as IExecutionResponse['data'];
|
||||||
|
|
||||||
|
const triggerNodes = workflow.nodes.filter((node) => /trigger$/i.test(node.type));
|
||||||
|
|
||||||
|
const pinData = {} as IPinData;
|
||||||
|
|
||||||
|
for (const triggerNode of triggerNodes) {
|
||||||
|
const triggerData = executionData.resultData.runData[triggerNode.name];
|
||||||
|
if (triggerData[0]?.data?.main?.[0]) {
|
||||||
|
pinData[triggerNode.name] = triggerData[0]?.data?.main?.[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pinData;
|
||||||
|
}
|
||||||
|
|
||||||
public async runTest(user: User, testId: number, accessibleWorkflowIds: string[]): Promise<any> {
|
public async runTest(user: User, testId: number, accessibleWorkflowIds: string[]): Promise<any> {
|
||||||
const test = await this.testDefinitionsService.findOne(testId, accessibleWorkflowIds);
|
const test = await this.testDefinitionsService.findOne(testId, accessibleWorkflowIds);
|
||||||
|
|
||||||
console.log({ test });
|
|
||||||
|
|
||||||
if (!test) {
|
if (!test) {
|
||||||
throw new NotFoundError('Test definition not found');
|
throw new NotFoundError('Test definition not found');
|
||||||
}
|
}
|
||||||
|
@ -35,15 +53,6 @@ export class TestRunnerService {
|
||||||
const workflow = await this.workflowRepository.findById(test.workflowId);
|
const workflow = await this.workflowRepository.findById(test.workflowId);
|
||||||
assert(workflow, 'Workflow not found');
|
assert(workflow, 'Workflow not found');
|
||||||
|
|
||||||
// Make a list of test cases
|
|
||||||
// const executions = await this.executionRepository.findManyByRangeQuery({
|
|
||||||
// kind: 'range',
|
|
||||||
// range: {
|
|
||||||
// limit: 99,
|
|
||||||
// },
|
|
||||||
// annotationTags: [test.annotationTagId],
|
|
||||||
// accessibleWorkflowIds,
|
|
||||||
// });
|
|
||||||
const executions = await this.executionRepository
|
const executions = await this.executionRepository
|
||||||
.createQueryBuilder('execution')
|
.createQueryBuilder('execution')
|
||||||
.leftJoin('execution.annotation', 'annotation')
|
.leftJoin('execution.annotation', 'annotation')
|
||||||
|
@ -55,33 +64,22 @@ export class TestRunnerService {
|
||||||
.getMany();
|
.getMany();
|
||||||
|
|
||||||
const testCases = executions.map((execution) => {
|
const testCases = executions.map((execution) => {
|
||||||
const executionData = parse(execution.executionData.data) as IExecutionResponse['data'];
|
return this.createPinDataFromExecution(workflow, execution);
|
||||||
|
|
||||||
return {
|
|
||||||
pinData: {
|
|
||||||
'When clicking ‘Test workflow’':
|
|
||||||
executionData.resultData.runData['When clicking ‘Test workflow’'][0]?.data?.main?.[0],
|
|
||||||
} as IPinData,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log({ testCases });
|
|
||||||
|
|
||||||
for (const testCase of testCases) {
|
for (const testCase of testCases) {
|
||||||
|
console.log({ testCase });
|
||||||
|
|
||||||
// Start the workflow
|
// Start the workflow
|
||||||
const data: IWorkflowExecutionDataProcess = {
|
const data: IWorkflowExecutionDataProcess = {
|
||||||
executionMode: 'evaluation',
|
executionMode: 'evaluation',
|
||||||
runData: {},
|
runData: {},
|
||||||
pinData: testCase.pinData,
|
pinData: testCase,
|
||||||
workflowData: workflow,
|
workflowData: workflow,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
partialExecutionVersion: '-1',
|
partialExecutionVersion: '-1',
|
||||||
};
|
};
|
||||||
|
|
||||||
// if (pinnedTrigger && !hasRunData(pinnedTrigger)) {
|
|
||||||
// data.startNodes = [{ name: pinnedTrigger.name, sourceData: null }];
|
|
||||||
// }
|
|
||||||
|
|
||||||
const executionId = await this.workflowRunner.run(data);
|
const executionId = await this.workflowRunner.run(data);
|
||||||
|
|
||||||
assert(executionId);
|
assert(executionId);
|
||||||
|
@ -91,8 +89,7 @@ export class TestRunnerService {
|
||||||
) as Promise<IExecutionDb | undefined>;
|
) as Promise<IExecutionDb | undefined>;
|
||||||
|
|
||||||
const execution = await executePromise;
|
const execution = await executePromise;
|
||||||
console.log({ execution });
|
console.log(execution?.data.resultData.runData);
|
||||||
console.log(execution?.data.resultData.runData.Code?.[0].data?.main[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
|
|
Loading…
Reference in a new issue