pull:初次提交

This commit is contained in:
Yep_Q
2025-09-08 04:48:28 +08:00
parent 5c0619656d
commit f64f498365
11751 changed files with 1953723 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
import { mock } from 'jest-mock-extended';
import type { INode, INodeTypeBaseDescription, ITriggerFunctions } from 'n8n-workflow';
import { type ICredentialsDataImap } from '@credentials/Imap.credentials';
import { EmailReadImapV2 } from '../../v2/EmailReadImapV2.node';
jest.mock('@n8n/imap', () => {
const originalModule = jest.requireActual('@n8n/imap');
return {
...originalModule,
connect: jest.fn().mockImplementation(() => ({
then: jest.fn().mockImplementation(() => ({
openBox: jest.fn().mockResolvedValue({}),
})),
})),
};
});
describe('Test IMap V2', () => {
const triggerFunctions = mock<ITriggerFunctions>({
helpers: {
createDeferredPromise: jest.fn().mockImplementation(() => {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}),
},
});
const credentials: ICredentialsDataImap = {
host: 'imap.gmail.com',
port: 993,
user: 'user',
password: 'password',
secure: false,
allowUnauthorizedCerts: false,
};
triggerFunctions.getCredentials.calledWith('imap').mockResolvedValue(credentials);
triggerFunctions.getNode.mockReturnValue(mock<INode>({ typeVersion: 2.1 }));
triggerFunctions.logger.debug = jest.fn();
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({
name: 'Mark as Read',
value: 'read',
});
const baseDescription: INodeTypeBaseDescription = {
displayName: 'EmailReadImapV2',
name: 'emailReadImapV2',
icon: 'file:removeDuplicates.svg',
group: ['transform'],
description: 'Delete items with matching field values',
};
afterEach(() => jest.resetAllMocks());
it('should run return a close function on success', async () => {
const result = await new EmailReadImapV2(baseDescription).trigger.call(triggerFunctions);
expect(result.closeFunction).toBeDefined();
});
});

View File

@@ -0,0 +1,100 @@
import { type ImapSimple } from '@n8n/imap';
import { mock, mockDeep } from 'jest-mock-extended';
import { returnJsonArray } from 'n8n-core';
import type { INode, ITriggerFunctions } from 'n8n-workflow';
import { getNewEmails } from '../../v2/utils';
describe('Test IMap V2 utils', () => {
afterEach(() => jest.resetAllMocks());
describe('getNewEmails', () => {
const triggerFunctions = mockDeep<ITriggerFunctions>({
helpers: { returnJsonArray },
});
const message = {
attributes: {
uuid: 1,
uid: 873,
struct: {},
},
parts: [
{ which: '', body: 'Body content' },
{ which: 'HEADER', body: 'h' },
{ which: 'TEXT', body: 'txt' },
],
};
const imapConnection = mock<ImapSimple>({
search: jest.fn().mockReturnValue(Promise.resolve([message])),
});
const getText = jest.fn().mockReturnValue('text');
const getAttachment = jest.fn().mockReturnValue(['attachment']);
it('should return new emails', async () => {
const expectedResults = [
{
format: 'resolved',
expected: {
json: {
attachments: undefined,
headers: { '': 'Body content' },
headerLines: undefined,
html: false,
attributes: {
uid: 873,
},
},
binary: undefined,
},
},
{
format: 'simple',
expected: {
json: {
textHtml: 'text',
textPlain: 'text',
metadata: {
'0': 'h',
},
attributes: {
uid: 873,
},
},
},
},
{
format: 'raw',
expected: {
json: { raw: 'txt' },
},
},
];
expectedResults.forEach(async (expectedResult) => {
triggerFunctions.getNode.mockReturnValue(mock<INode>({ typeVersion: 2.1 }));
triggerFunctions.getNodeParameter
.calledWith('format')
.mockReturnValue(expectedResult.format);
triggerFunctions.getNodeParameter
.calledWith('dataPropertyAttachmentsPrefixName')
.mockReturnValue('resolved');
triggerFunctions.getWorkflowStaticData.mockReturnValue({});
const onEmailBatch = jest.fn();
await getNewEmails.call(triggerFunctions, {
imapConnection,
searchCriteria: [],
postProcessAction: '',
getText,
getAttachment,
onEmailBatch,
});
expect(onEmailBatch).toHaveBeenCalledTimes(1);
expect(onEmailBatch).toHaveBeenCalledWith([expectedResult.expected]);
});
});
});
});