2023-12-15 03:59:56 -08:00
|
|
|
import Container from 'typedi';
|
2023-12-18 07:10:30 -08:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-01-17 01:16:13 -08:00
|
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
|
|
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
2023-12-18 07:10:30 -08:00
|
|
|
import { Telemetry } from '@/telemetry';
|
2023-12-27 07:55:01 -08:00
|
|
|
import { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee';
|
2024-01-17 01:16:13 -08:00
|
|
|
import { WorkflowService } from '@/workflows/workflow.service';
|
|
|
|
|
|
|
|
import * as testDb from '../shared/testDb';
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
import { createOwner } from '../shared/db/users';
|
|
|
|
import { createWorkflow } from '../shared/db/workflows';
|
2023-11-17 06:58:50 -08:00
|
|
|
|
2023-12-18 07:10:30 -08:00
|
|
|
let workflowService: WorkflowService;
|
|
|
|
let activeWorkflowRunner: ActiveWorkflowRunner;
|
2023-12-27 07:55:01 -08:00
|
|
|
let multiMainSetup: MultiMainSetup;
|
2023-11-17 06:58:50 -08:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testDb.init();
|
2023-12-18 07:10:30 -08:00
|
|
|
|
|
|
|
activeWorkflowRunner = mockInstance(ActiveWorkflowRunner);
|
2023-12-27 07:55:01 -08:00
|
|
|
multiMainSetup = mockInstance(MultiMainSetup);
|
2023-12-18 07:10:30 -08:00
|
|
|
mockInstance(Telemetry);
|
|
|
|
|
|
|
|
workflowService = new WorkflowService(
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
Container.get(SharedWorkflowRepository),
|
|
|
|
Container.get(WorkflowRepository),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
mock(),
|
2023-12-27 07:55:01 -08:00
|
|
|
multiMainSetup,
|
2023-12-18 07:10:30 -08:00
|
|
|
mock(),
|
|
|
|
activeWorkflowRunner,
|
|
|
|
);
|
2023-11-17 06:58:50 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await testDb.truncate(['Workflow']);
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const removeSpy = jest.spyOn(activeWorkflowRunner, 'remove');
|
|
|
|
const addSpy = jest.spyOn(activeWorkflowRunner, 'add');
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const removeSpy = jest.spyOn(activeWorkflowRunner, 'remove');
|
|
|
|
const addSpy = jest.spyOn(activeWorkflowRunner, 'add');
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2023-12-27 07:55:01 -08:00
|
|
|
|
|
|
|
test('should broadcast active workflow state change if state changed', async () => {
|
|
|
|
const owner = await createOwner();
|
|
|
|
const workflow = await createWorkflow({ active: true }, owner);
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
const publishSpy = jest.spyOn(multiMainSetup, 'publish');
|
2023-12-27 07:55:01 -08:00
|
|
|
|
|
|
|
workflow.active = false;
|
|
|
|
await workflowService.update(owner, workflow, workflow.id);
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
expect(publishSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(publishSpy).toHaveBeenCalledWith(
|
|
|
|
'workflowActiveStateChanged',
|
|
|
|
expect.objectContaining({
|
|
|
|
newState: false,
|
|
|
|
oldState: true,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
}),
|
|
|
|
);
|
2023-12-27 07:55:01 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should not broadcast active workflow state change if state did not change', async () => {
|
|
|
|
const owner = await createOwner();
|
|
|
|
const workflow = await createWorkflow({ active: true }, owner);
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
const publishSpy = jest.spyOn(multiMainSetup, 'publish');
|
2023-12-27 07:55:01 -08:00
|
|
|
|
|
|
|
await workflowService.update(owner, workflow, workflow.id);
|
|
|
|
|
2024-01-12 02:48:58 -08:00
|
|
|
expect(publishSpy).not.toHaveBeenCalled();
|
2023-12-27 07:55:01 -08:00
|
|
|
});
|
2023-11-17 06:58:50 -08:00
|
|
|
});
|