2023-12-18 07:10:30 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-09-12 09:07:18 -07:00
|
|
|
import Container from 'typedi';
|
|
|
|
|
2024-08-22 02:10:37 -07:00
|
|
|
import { ActiveWorkflowManager } from '@/active-workflow-manager';
|
2024-08-27 07:44:32 -07:00
|
|
|
import { SharedWorkflowRepository } from '@/databases/repositories/shared-workflow.repository';
|
2024-08-27 08:24:20 -07:00
|
|
|
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
|
2024-08-26 02:10:06 -07:00
|
|
|
import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus';
|
2024-01-22 02:16:29 -08:00
|
|
|
import { OrchestrationService } from '@/services/orchestration.service';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { Telemetry } from '@/telemetry';
|
2024-01-17 01:16:13 -08:00
|
|
|
import { WorkflowService } from '@/workflows/workflow.service';
|
|
|
|
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
import { createOwner } from '../shared/db/users';
|
|
|
|
import { createWorkflow } from '../shared/db/workflows';
|
2024-09-12 09:07:18 -07:00
|
|
|
import * as testDb from '../shared/test-db';
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2023-12-18 07:10:30 -08:00
|
|
|
let workflowService: WorkflowService;
|
2024-05-06 08:54:05 -07:00
|
|
|
const activeWorkflowManager = mockInstance(ActiveWorkflowManager);
|
2024-01-26 03:21:15 -08:00
|
|
|
const orchestrationService = mockInstance(OrchestrationService);
|
|
|
|
mockInstance(MessageEventBus);
|
|
|
|
mockInstance(Telemetry);
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.init();
|
2023-12-18 07:10:30 -08:00
|
|
|
|
|
|
|
workflowService = new WorkflowService(
|
|
|
|
mock(),
|
|
|
|
Container.get(SharedWorkflowRepository),
|
|
|
|
Container.get(WorkflowRepository),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
2024-01-22 02:16:29 -08:00
|
|
|
orchestrationService,
|
2023-12-18 07:10:30 -08:00
|
|
|
mock(),
|
2024-05-06 08:54:05 -07:00
|
|
|
activeWorkflowManager,
|
2024-05-17 01:53:15 -07:00
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
2024-06-20 03:32:22 -07:00
|
|
|
mock(),
|
2023-12-18 07:10:30 -08:00
|
|
|
);
|
2023-11-17 06:58:50 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await testDb.truncate(['Workflow']);
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update()', () => {
|
|
|
|
test('should remove and re-add to active workflows on `active: true` payload', async () => {
|
|
|
|
const owner = await createOwner();
|
|
|
|
const workflow = await createWorkflow({ active: true }, owner);
|
|
|
|
|
2024-05-06 08:54:05 -07:00
|
|
|
const removeSpy = jest.spyOn(activeWorkflowManager, 'remove');
|
|
|
|
const addSpy = jest.spyOn(activeWorkflowManager, 'add');
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2023-12-18 07:10:30 -08:00
|
|
|
await workflowService.update(owner, workflow, workflow.id);
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
expect(removeSpy).toHaveBeenCalledTimes(1);
|
|
|
|
const [removedWorkflowId] = removeSpy.mock.calls[0];
|
|
|
|
expect(removedWorkflowId).toBe(workflow.id);
|
|
|
|
|
|
|
|
expect(addSpy).toHaveBeenCalledTimes(1);
|
|
|
|
const [addedWorkflowId, activationMode] = addSpy.mock.calls[0];
|
|
|
|
expect(addedWorkflowId).toBe(workflow.id);
|
|
|
|
expect(activationMode).toBe('update');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should remove from active workflows on `active: false` payload', async () => {
|
|
|
|
const owner = await createOwner();
|
|
|
|
const workflow = await createWorkflow({ active: true }, owner);
|
|
|
|
|
2024-05-06 08:54:05 -07:00
|
|
|
const removeSpy = jest.spyOn(activeWorkflowManager, 'remove');
|
|
|
|
const addSpy = jest.spyOn(activeWorkflowManager, 'add');
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
workflow.active = false;
|
2023-12-18 07:10:30 -08:00
|
|
|
await workflowService.update(owner, workflow, workflow.id);
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
expect(removeSpy).toHaveBeenCalledTimes(1);
|
|
|
|
const [removedWorkflowId] = removeSpy.mock.calls[0];
|
|
|
|
expect(removedWorkflowId).toBe(workflow.id);
|
|
|
|
|
|
|
|
expect(addSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|