2024-04-18 22:50:18 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-01-23 00:48:50 -08:00
|
|
|
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
|
|
|
import { ExecutionsController } from '@/executions/executions.controller';
|
2024-04-18 22:50:18 -07:00
|
|
|
import type { ExecutionRequest, ExecutionSummaries } from '@/executions/execution.types';
|
|
|
|
import type { ExecutionService } from '@/executions/execution.service';
|
2024-01-23 00:48:50 -08:00
|
|
|
import type { WorkflowSharingService } from '@/workflows/workflowSharing.service';
|
|
|
|
|
|
|
|
describe('ExecutionsController', () => {
|
2024-04-18 22:50:18 -07:00
|
|
|
const executionService = mock<ExecutionService>();
|
2024-01-23 00:48:50 -08:00
|
|
|
const workflowSharingService = mock<WorkflowSharingService>();
|
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
const executionsController = new ExecutionsController(
|
|
|
|
executionService,
|
|
|
|
mock(),
|
|
|
|
workflowSharingService,
|
|
|
|
mock(),
|
|
|
|
);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
describe('getMany', () => {
|
|
|
|
const NO_EXECUTIONS = { count: 0, estimated: false, results: [] };
|
|
|
|
|
|
|
|
const QUERIES_WITH_EITHER_STATUS_OR_RANGE: ExecutionSummaries.RangeQuery[] = [
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: undefined,
|
|
|
|
range: { lastId: '999', firstId: '111', limit: 20 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: [],
|
|
|
|
range: { lastId: '999', firstId: '111', limit: 20 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: ['waiting'],
|
|
|
|
range: { lastId: undefined, firstId: undefined, limit: 20 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: [],
|
|
|
|
range: { lastId: '999', firstId: '111', limit: 20 },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const QUERIES_NEITHER_STATUS_NOR_RANGE_PROVIDED: ExecutionSummaries.RangeQuery[] = [
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: undefined,
|
|
|
|
range: { lastId: undefined, firstId: undefined, limit: 20 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: [],
|
|
|
|
range: { lastId: undefined, firstId: undefined, limit: 20 },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
describe('if either status or range provided', () => {
|
|
|
|
test.each(QUERIES_WITH_EITHER_STATUS_OR_RANGE)(
|
|
|
|
'should fetch executions per query',
|
|
|
|
async (rangeQuery) => {
|
|
|
|
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
|
|
|
|
executionService.findAllRunningAndLatest.mockResolvedValue(NO_EXECUTIONS);
|
|
|
|
|
|
|
|
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
|
|
|
|
|
|
|
|
await executionsController.getMany(req);
|
|
|
|
|
|
|
|
expect(executionService.findAllRunningAndLatest).not.toHaveBeenCalled();
|
|
|
|
expect(executionService.findRangeWithCount).toHaveBeenCalledWith(rangeQuery);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if neither status nor range provided', () => {
|
|
|
|
test.each(QUERIES_NEITHER_STATUS_NOR_RANGE_PROVIDED)(
|
|
|
|
'should fetch executions per query',
|
|
|
|
async (rangeQuery) => {
|
|
|
|
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
|
|
|
|
executionService.findAllRunningAndLatest.mockResolvedValue(NO_EXECUTIONS);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
await executionsController.getMany(req);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
expect(executionService.findAllRunningAndLatest).toHaveBeenCalled();
|
|
|
|
expect(executionService.findRangeWithCount).not.toHaveBeenCalled();
|
|
|
|
},
|
|
|
|
);
|
2024-01-23 00:48:50 -08:00
|
|
|
});
|
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
describe('if both status and range provided', () => {
|
|
|
|
it('should fetch executions per query', async () => {
|
|
|
|
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
|
|
|
|
executionService.findAllRunningAndLatest.mockResolvedValue(NO_EXECUTIONS);
|
|
|
|
|
|
|
|
const rangeQuery: ExecutionSummaries.RangeQuery = {
|
|
|
|
kind: 'range',
|
|
|
|
workflowId: undefined,
|
|
|
|
status: ['success'],
|
|
|
|
range: { lastId: '999', firstId: '111', limit: 5 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = mock<ExecutionRequest.GetMany>({ rangeQuery });
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
await executionsController.getMany(req);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
expect(executionService.findAllRunningAndLatest).not.toHaveBeenCalled();
|
|
|
|
expect(executionService.findRangeWithCount).toHaveBeenCalledWith(rangeQuery);
|
|
|
|
});
|
2024-01-23 00:48:50 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
describe('stop', () => {
|
|
|
|
const executionId = '999';
|
|
|
|
const req = mock<ExecutionRequest.Stop>({ params: { id: executionId } });
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
it('should 404 when execution is inaccessible for user', async () => {
|
|
|
|
workflowSharingService.getSharedWorkflowIds.mockResolvedValue([]);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
const promise = executionsController.stop(req);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
|
|
|
await expect(promise).rejects.toThrow(NotFoundError);
|
2024-04-18 22:50:18 -07:00
|
|
|
expect(executionService.stop).not.toHaveBeenCalled();
|
2024-01-23 00:48:50 -08:00
|
|
|
});
|
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
it('should call ask for an execution to be stopped', async () => {
|
|
|
|
workflowSharingService.getSharedWorkflowIds.mockResolvedValue(['123']);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
await executionsController.stop(req);
|
2024-01-23 00:48:50 -08:00
|
|
|
|
2024-04-18 22:50:18 -07:00
|
|
|
expect(executionService.stop).toHaveBeenCalledWith(executionId);
|
2024-01-23 00:48:50 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|