wip: mocking trigger nodes

This commit is contained in:
Eugene Molodkin 2024-11-13 18:14:04 +01:00
parent c71b97175b
commit 05837d5041
No known key found for this signature in database

View file

@ -4,30 +4,48 @@ import assert from 'node:assert';
import { Container, Service } from 'typedi';
import { ActiveExecutions } from '@/active-executions';
import type { ExecutionEntity } from '@/databases/entities/execution-entity';
import type { User } from '@/databases/entities/user';
import type { WorkflowEntity } from '@/databases/entities/workflow-entity';
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { TestDefinitionService } from '@/evaluation/test-definition.service.ee';
import type { IExecutionDb, IExecutionResponse } from '@/interfaces';
import { WorkflowRunner } from '@/workflow-runner';
// import { WorkflowExecutionService } from '@/workflows/workflow-execution.service';
@Service()
export class TestRunnerService {
constructor(
private readonly testDefinitionsService: TestDefinitionService,
private readonly workflowRepository: WorkflowRepository,
// private readonly workflowExecutionService: WorkflowExecutionService,
private readonly workflowRunner: WorkflowRunner,
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> {
const test = await this.testDefinitionsService.findOne(testId, accessibleWorkflowIds);
console.log({ test });
if (!test) {
throw new NotFoundError('Test definition not found');
}
@ -35,15 +53,6 @@ export class TestRunnerService {
const workflow = await this.workflowRepository.findById(test.workflowId);
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
.createQueryBuilder('execution')
.leftJoin('execution.annotation', 'annotation')
@ -55,33 +64,22 @@ export class TestRunnerService {
.getMany();
const testCases = executions.map((execution) => {
const executionData = parse(execution.executionData.data) as IExecutionResponse['data'];
return {
pinData: {
'When clicking Test workflow':
executionData.resultData.runData['When clicking Test workflow'][0]?.data?.main?.[0],
} as IPinData,
};
return this.createPinDataFromExecution(workflow, execution);
});
console.log({ testCases });
for (const testCase of testCases) {
console.log({ testCase });
// Start the workflow
const data: IWorkflowExecutionDataProcess = {
executionMode: 'evaluation',
runData: {},
pinData: testCase.pinData,
pinData: testCase,
workflowData: workflow,
userId: user.id,
partialExecutionVersion: '-1',
};
// if (pinnedTrigger && !hasRunData(pinnedTrigger)) {
// data.startNodes = [{ name: pinnedTrigger.name, sourceData: null }];
// }
const executionId = await this.workflowRunner.run(data);
assert(executionId);
@ -91,8 +89,7 @@ export class TestRunnerService {
) as Promise<IExecutionDb | undefined>;
const execution = await executePromise;
console.log({ execution });
console.log(execution?.data.resultData.runData.Code?.[0].data?.main[0]);
console.log(execution?.data.resultData.runData);
}
return { success: true };