Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): Move ExecutionLifecycleHooks to core #13042

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions packages/cli/src/__tests__/workflow-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import type {
IWorkflowExecutionDataProcess,
StartNodeData,
} from 'n8n-workflow';
import {
Workflow,
WorkflowHooks,
type ExecutionError,
type IWorkflowExecuteHooks,
} from 'n8n-workflow';
import { Workflow, type ExecutionError } from 'n8n-workflow';
import PCancelable from 'p-cancelable';

import { ActiveExecutions } from '@/active-executions';
import config from '@/config';
import type { ExecutionEntity } from '@/databases/entities/execution-entity';
import type { User } from '@/databases/entities/user';
import type { WorkflowEntity } from '@/databases/entities/workflow-entity';
import { ExecutionNotFoundError } from '@/errors/execution-not-found-error';
import { Telemetry } from '@/telemetry';
import { PermissionChecker } from '@/user-management/permission-checker';
Expand All @@ -36,25 +33,14 @@ import { setupTestServer } from '@test-integration/utils';

let owner: User;
let runner: WorkflowRunner;
let hookFunctions: IWorkflowExecuteHooks;
setupTestServer({ endpointGroups: [] });

mockInstance(Telemetry);

class Watchers {
workflowExecuteAfter = jest.fn();
}
const watchers = new Watchers();
const watchedWorkflowExecuteAfter = jest.spyOn(watchers, 'workflowExecuteAfter');

beforeAll(async () => {
owner = await createUser({ role: 'global:owner' });

runner = Container.get(WorkflowRunner);

hookFunctions = {
workflowExecuteAfter: [watchers.workflowExecuteAfter],
};
});

afterAll(() => {
Expand All @@ -67,6 +53,20 @@ beforeEach(async () => {
});

describe('processError', () => {
let workflow: WorkflowEntity;
let execution: ExecutionEntity;
let hooks: core.ExecutionLifecycleHooks;

const watcher = mock<{ workflowExecuteAfter: () => Promise<void> }>();

beforeEach(async () => {
jest.clearAllMocks();
workflow = await createWorkflow({}, owner);
execution = await createExecution({ status: 'success', finished: true }, workflow);
hooks = new core.ExecutionLifecycleHooks('webhook', execution.id, workflow);
hooks.addHandler('workflowExecuteAfter', watcher.workflowExecuteAfter);
});

test('processError should return early in Bull stalled edge case', async () => {
const workflow = await createWorkflow({}, owner);
const execution = await createExecution(
Expand All @@ -82,9 +82,9 @@ describe('processError', () => {
new Date(),
'webhook',
execution.id,
new WorkflowHooks(hookFunctions, 'webhook', execution.id, workflow),
hooks,
);
expect(watchedWorkflowExecuteAfter).toHaveBeenCalledTimes(0);
expect(watcher.workflowExecuteAfter).toHaveBeenCalledTimes(0);
});

test('processError should return early if the error is `ExecutionNotFoundError`', async () => {
Expand All @@ -95,9 +95,9 @@ describe('processError', () => {
new Date(),
'webhook',
execution.id,
new WorkflowHooks(hookFunctions, 'webhook', execution.id, workflow),
hooks,
);
expect(watchedWorkflowExecuteAfter).toHaveBeenCalledTimes(0);
expect(watcher.workflowExecuteAfter).toHaveBeenCalledTimes(0);
});

test('processError should process error', async () => {
Expand All @@ -119,9 +119,9 @@ describe('processError', () => {
new Date(),
'webhook',
execution.id,
new WorkflowHooks(hookFunctions, 'webhook', execution.id, workflow),
hooks,
);
expect(watchedWorkflowExecuteAfter).toHaveBeenCalledTimes(1);
expect(watcher.workflowExecuteAfter).toHaveBeenCalledTimes(1);
});
});

Expand Down
Loading