mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
fix(Chat Trigger Node): Don't continue when action is load previous session and option is not set (#15438)
This commit is contained in:
@@ -563,7 +563,7 @@ ${cssVariables}
|
|||||||
return {
|
return {
|
||||||
webhookResponse: { data: messages },
|
webhookResponse: { data: messages },
|
||||||
};
|
};
|
||||||
} else if (options?.loadPreviousSession === 'notSupported') {
|
} else if (!options?.loadPreviousSession || options?.loadPreviousSession === 'notSupported') {
|
||||||
// If messages of a previous session should not be loaded, simply return an empty array
|
// If messages of a previous session should not be loaded, simply return an empty array
|
||||||
return {
|
return {
|
||||||
webhookResponse: { data: [] },
|
webhookResponse: { data: [] },
|
||||||
|
|||||||
@@ -0,0 +1,133 @@
|
|||||||
|
import { jest } from '@jest/globals';
|
||||||
|
import type { Request, Response } from 'express';
|
||||||
|
import { mock } from 'jest-mock-extended';
|
||||||
|
import type { IWebhookFunctions } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { ChatTrigger } from '../ChatTrigger.node';
|
||||||
|
import type { LoadPreviousSessionChatOption } from '../types';
|
||||||
|
|
||||||
|
jest.mock('../GenericFunctions', () => ({
|
||||||
|
validateAuth: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('ChatTrigger Node', () => {
|
||||||
|
const mockContext = mock<IWebhookFunctions>();
|
||||||
|
const mockRequest = mock<Request>();
|
||||||
|
const mockResponse = mock<Response>();
|
||||||
|
let chatTrigger: ChatTrigger;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
|
||||||
|
chatTrigger = new ChatTrigger();
|
||||||
|
|
||||||
|
mockContext.getRequestObject.mockReturnValue(mockRequest);
|
||||||
|
mockContext.getResponseObject.mockReturnValue(mockResponse);
|
||||||
|
mockContext.getNodeParameter.mockImplementation(
|
||||||
|
(
|
||||||
|
paramName: string,
|
||||||
|
defaultValue?: boolean | string | object,
|
||||||
|
): boolean | string | object | undefined => {
|
||||||
|
if (paramName === 'public') return true;
|
||||||
|
if (paramName === 'mode') return 'hostedChat';
|
||||||
|
if (paramName === 'options') return {};
|
||||||
|
return defaultValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
mockContext.getBodyData.mockReturnValue({});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('webhook method: loadPreviousSession action', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockContext.getBodyData.mockReturnValue({ action: 'loadPreviousSession' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return empty array when loadPreviousSession is undefined', async () => {
|
||||||
|
// Mock options with undefined loadPreviousSession
|
||||||
|
mockContext.getNodeParameter.mockImplementation(
|
||||||
|
(
|
||||||
|
paramName: string,
|
||||||
|
defaultValue?: boolean | string | object,
|
||||||
|
): boolean | string | object | undefined => {
|
||||||
|
if (paramName === 'public') return true;
|
||||||
|
if (paramName === 'mode') return 'hostedChat';
|
||||||
|
if (paramName === 'options') return { loadPreviousSession: undefined };
|
||||||
|
return defaultValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Call the webhook method
|
||||||
|
const result = await chatTrigger.webhook(mockContext);
|
||||||
|
|
||||||
|
// Verify the returned result contains empty data array
|
||||||
|
expect(result).toEqual({
|
||||||
|
webhookResponse: { data: [] },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return empty array when loadPreviousSession is "notSupported"', async () => {
|
||||||
|
// Mock options with notSupported loadPreviousSession
|
||||||
|
mockContext.getNodeParameter.mockImplementation(
|
||||||
|
(
|
||||||
|
paramName: string,
|
||||||
|
defaultValue?: boolean | string | object,
|
||||||
|
): boolean | string | object | undefined => {
|
||||||
|
if (paramName === 'public') return true;
|
||||||
|
if (paramName === 'mode') return 'hostedChat';
|
||||||
|
if (paramName === 'options') return { loadPreviousSession: 'notSupported' };
|
||||||
|
return defaultValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Call the webhook method
|
||||||
|
const result = await chatTrigger.webhook(mockContext);
|
||||||
|
|
||||||
|
// Verify the returned result contains empty data array
|
||||||
|
expect(result).toEqual({
|
||||||
|
webhookResponse: { data: [] },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle loadPreviousSession="memory" correctly', async () => {
|
||||||
|
// Mock chat history data
|
||||||
|
const mockMessages = [
|
||||||
|
{ toJSON: () => ({ content: 'Message 1' }) },
|
||||||
|
{ toJSON: () => ({ content: 'Message 2' }) },
|
||||||
|
];
|
||||||
|
|
||||||
|
// Mock memory with chat history
|
||||||
|
const mockMemory = {
|
||||||
|
chatHistory: {
|
||||||
|
getMessages: jest.fn().mockReturnValueOnce(mockMessages),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mock options with memory loadPreviousSession
|
||||||
|
mockContext.getNodeParameter.mockImplementation(
|
||||||
|
(
|
||||||
|
paramName: string,
|
||||||
|
defaultValue?: boolean | string | object,
|
||||||
|
): boolean | string | object | undefined => {
|
||||||
|
if (paramName === 'public') return true;
|
||||||
|
if (paramName === 'mode') return 'hostedChat';
|
||||||
|
if (paramName === 'options')
|
||||||
|
return { loadPreviousSession: 'memory' as LoadPreviousSessionChatOption };
|
||||||
|
return defaultValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Mock getInputConnectionData to return memory
|
||||||
|
mockContext.getInputConnectionData.mockResolvedValue(mockMemory);
|
||||||
|
|
||||||
|
// Call the webhook method
|
||||||
|
const result = await chatTrigger.webhook(mockContext);
|
||||||
|
|
||||||
|
// Verify the returned result contains messages from memory
|
||||||
|
expect(result).toEqual({
|
||||||
|
webhookResponse: {
|
||||||
|
data: [{ content: 'Message 1' }, { content: 'Message 2' }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user