mirror of
https://github.com/n8n-io/n8n.git
synced 2026-12-05 17:21:04 +00:00
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
176 lines
6.2 KiB
TypeScript
176 lines
6.2 KiB
TypeScript
import { createWorkflow, testDb } from '@n8n/backend-test-utils';
|
|
import type { CreateExecutionPayload, WorkflowEntity } from '@n8n/db';
|
|
import { ExecutionRepository } from '@n8n/db';
|
|
import { Container } from '@n8n/di';
|
|
import { createEmptyRunExecutionData, createRunExecutionData } from 'n8n-workflow';
|
|
|
|
import { DuplicateExecutionError } from '@/errors/duplicate-execution.error';
|
|
import { ExecutionPersistence } from '@/executions/execution-persistence';
|
|
|
|
describe('ExecutionPersistence', () => {
|
|
let executionPersistence: ExecutionPersistence;
|
|
let executionRepository: ExecutionRepository;
|
|
|
|
beforeAll(async () => {
|
|
await testDb.init();
|
|
executionPersistence = Container.get(ExecutionPersistence);
|
|
executionRepository = Container.get(ExecutionRepository);
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await testDb.truncate(['ExecutionEntity']);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await testDb.terminate();
|
|
});
|
|
|
|
describe('create with deduplicationKey', () => {
|
|
const buildPayload = (
|
|
workflow: WorkflowEntity,
|
|
deduplicationKey?: string,
|
|
status: CreateExecutionPayload['status'] = 'new',
|
|
): CreateExecutionPayload => ({
|
|
data: createEmptyRunExecutionData(),
|
|
workflowData: workflow,
|
|
mode: 'trigger',
|
|
finished: false,
|
|
status,
|
|
workflowId: workflow.id,
|
|
deduplicationKey,
|
|
});
|
|
|
|
it('creates multiple executions when deduplicationKey is null', async () => {
|
|
const workflow = await createWorkflow();
|
|
|
|
const id1 = await executionPersistence.create(buildPayload(workflow));
|
|
const id2 = await executionPersistence.create(buildPayload(workflow));
|
|
const id3 = await executionPersistence.create(buildPayload(workflow));
|
|
|
|
expect(new Set([id1, id2, id3]).size).toBe(3);
|
|
});
|
|
|
|
it('creates executions with distinct deduplicationKeys', async () => {
|
|
const workflow = await createWorkflow();
|
|
|
|
const id1 = await executionPersistence.create(buildPayload(workflow, 'wf:node:t1'));
|
|
const id2 = await executionPersistence.create(buildPayload(workflow, 'wf:node:t2'));
|
|
|
|
expect(id1).not.toBe(id2);
|
|
});
|
|
|
|
it('throws DuplicateExecutionError when a dispatched execution already holds the key', async () => {
|
|
const workflow = await createWorkflow();
|
|
const key = 'wf:node:t1';
|
|
|
|
// A dispatched execution (status past `new`) is a real effect, not a tombstone.
|
|
await executionPersistence.create(buildPayload(workflow, key, 'running'));
|
|
|
|
await expect(executionPersistence.create(buildPayload(workflow, key))).rejects.toBeInstanceOf(
|
|
DuplicateExecutionError,
|
|
);
|
|
await expect(executionPersistence.create(buildPayload(workflow, key))).rejects.toMatchObject({
|
|
deduplicationKey: key,
|
|
});
|
|
});
|
|
|
|
it('reclaims a tombstone (never-dispatched `new` row) when the key is reused', async () => {
|
|
const workflow = await createWorkflow();
|
|
const key = 'wf:node:t1';
|
|
|
|
// A prior attempt inserted the execution `new`, then died before dispatch.
|
|
const tombstoneId = await executionPersistence.create(buildPayload(workflow, key));
|
|
|
|
// The redelivery takes over the key instead of colliding, yielding a fresh row.
|
|
const reclaimedId = await executionPersistence.create(buildPayload(workflow, key));
|
|
|
|
expect(reclaimedId).not.toBe(tombstoneId);
|
|
expect(await executionRepository.findOneBy({ id: tombstoneId })).toBeNull();
|
|
expect(await executionRepository.findOneBy({ id: reclaimedId })).not.toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('unsaved manual executions', () => {
|
|
/** A finished manual run that n8n did not save, soft-deleted when it finished. */
|
|
const createUnsavedManualExecution = async () => {
|
|
const workflow = await createWorkflow();
|
|
const executionId = await executionPersistence.create({
|
|
data: createRunExecutionData({ resultData: { lastNodeExecuted: 'Get Message' } }),
|
|
workflowData: workflow,
|
|
mode: 'manual',
|
|
finished: false,
|
|
status: 'error',
|
|
workflowId: workflow.id,
|
|
});
|
|
await executionRepository.softDelete(executionId);
|
|
return { workflow, executionId };
|
|
};
|
|
|
|
it('writes run data to a soft-deleted execution only when asked to', async () => {
|
|
const { executionId } = await createUnsavedManualExecution();
|
|
const finished = {
|
|
status: 'error' as const,
|
|
data: createRunExecutionData({ resultData: { lastNodeExecuted: 'Get Message' } }),
|
|
};
|
|
|
|
expect(await executionPersistence.updateExistingExecution(executionId, finished)).toBe(false);
|
|
expect(
|
|
await executionPersistence.updateExistingExecution(executionId, finished, {
|
|
includeDeleted: true,
|
|
}),
|
|
).toBe(true);
|
|
});
|
|
|
|
it('does not restore an execution deleted between the read and the write of an update', async () => {
|
|
const workflow = await createWorkflow();
|
|
const executionId = await executionPersistence.create({
|
|
data: createEmptyRunExecutionData(),
|
|
workflowData: workflow,
|
|
mode: 'manual',
|
|
finished: false,
|
|
status: 'running',
|
|
workflowId: workflow.id,
|
|
});
|
|
// A caller writing back what it read earlier, like stopping a run.
|
|
const snapshot = await executionPersistence.findSingleExecution(executionId, {
|
|
includeData: true,
|
|
unflattenData: true,
|
|
});
|
|
const findOne = executionRepository.findOne.bind(executionRepository);
|
|
vi.spyOn(executionRepository, 'findOne').mockImplementationOnce(async (options) => {
|
|
const entity = await findOne(options);
|
|
await executionRepository.softDelete(executionId);
|
|
return entity;
|
|
});
|
|
|
|
await executionPersistence.updateExistingExecution(
|
|
executionId,
|
|
{ ...snapshot, status: 'canceled' },
|
|
{ requireStatus: 'running' },
|
|
);
|
|
|
|
const row = await executionRepository.findOne({
|
|
where: { id: executionId },
|
|
withDeleted: true,
|
|
});
|
|
expect(row?.status).toBe('canceled');
|
|
expect(row?.deletedAt).toBeInstanceOf(Date);
|
|
});
|
|
|
|
it('returns the run data of an unsaved manual execution', async () => {
|
|
const { workflow, executionId } = await createUnsavedManualExecution();
|
|
|
|
const execution = await executionPersistence.findUnsavedManualExecution(executionId);
|
|
|
|
expect(execution).toMatchObject({ id: executionId, workflowId: workflow.id, mode: 'manual' });
|
|
expect(execution?.data.resultData.lastNodeExecuted).toBe('Get Message');
|
|
expect(
|
|
await executionPersistence.findSingleExecution(executionId, {
|
|
includeData: true,
|
|
unflattenData: true,
|
|
}),
|
|
).toBeUndefined();
|
|
});
|
|
});
|
|
});
|