mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 09:44:47 +00:00
test: Add unit test for projects/app/src/global/core/chat/utils.ts (#4328)
* Add unit tests for chat utility functions and enhance statistical data calculation in utils.ts. * Update utils.ts --------- Co-authored-by: gru-agent[bot] <185149714+gru-agent[bot]@users.noreply.github.com> Co-authored-by: Archer <545436317@qq.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import { ChatHistoryItemResType, ChatItemType } from '@fastgpt/global/core/chat/
|
|||||||
import { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type';
|
import { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type';
|
||||||
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
|
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
|
||||||
|
|
||||||
const isLLMNode = (item: ChatHistoryItemResType) =>
|
export const isLLMNode = (item: ChatHistoryItemResType) =>
|
||||||
item.moduleType === FlowNodeTypeEnum.chatNode || item.moduleType === FlowNodeTypeEnum.tools;
|
item.moduleType === FlowNodeTypeEnum.chatNode || item.moduleType === FlowNodeTypeEnum.tools;
|
||||||
|
|
||||||
export function transformPreviewHistories(
|
export function transformPreviewHistories(
|
||||||
|
191
test/cases/global/core/chat/utils.test.ts
Normal file
191
test/cases/global/core/chat/utils.test.ts
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
|
||||||
|
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
|
||||||
|
import { ChatHistoryItemResType, ChatItemType } from '@fastgpt/global/core/chat/type';
|
||||||
|
import {
|
||||||
|
transformPreviewHistories,
|
||||||
|
addStatisticalDataToHistoryItem
|
||||||
|
} from '@/global/core/chat/utils';
|
||||||
|
|
||||||
|
describe('transformPreviewHistories', () => {
|
||||||
|
it('should transform histories correctly with responseDetail=true', () => {
|
||||||
|
const histories: ChatItemType[] = [
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test response',
|
||||||
|
responseData: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 1.5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = transformPreviewHistories(histories, true);
|
||||||
|
|
||||||
|
expect(result[0]).toEqual({
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test response',
|
||||||
|
responseData: undefined,
|
||||||
|
llmModuleAccount: 1,
|
||||||
|
totalQuoteList: [],
|
||||||
|
totalRunningTime: 1.5,
|
||||||
|
historyPreviewLength: undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should transform histories correctly with responseDetail=false', () => {
|
||||||
|
const histories: ChatItemType[] = [
|
||||||
|
{
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test response',
|
||||||
|
responseData: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 1.5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = transformPreviewHistories(histories, false);
|
||||||
|
|
||||||
|
expect(result[0]).toEqual({
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test response',
|
||||||
|
responseData: undefined,
|
||||||
|
llmModuleAccount: 1,
|
||||||
|
totalQuoteList: undefined,
|
||||||
|
totalRunningTime: 1.5,
|
||||||
|
historyPreviewLength: undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('addStatisticalDataToHistoryItem', () => {
|
||||||
|
it('should return original item if obj is not AI', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.Human,
|
||||||
|
value: 'test'
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return original item if totalQuoteList is already defined', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test',
|
||||||
|
totalQuoteList: []
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return original item if responseData is undefined', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test'
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should calculate statistics correctly', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test',
|
||||||
|
responseData: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 1.5,
|
||||||
|
historyPreview: ['preview1']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.datasetSearchNode,
|
||||||
|
quoteList: [{ id: '1', q: 'test', a: 'answer' }],
|
||||||
|
runningTime: 0.5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.tools,
|
||||||
|
runningTime: 1,
|
||||||
|
toolDetail: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 0.5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = addStatisticalDataToHistoryItem(item);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
...item,
|
||||||
|
llmModuleAccount: 3,
|
||||||
|
totalQuoteList: [{ id: '1', q: 'test', a: 'answer' }],
|
||||||
|
totalRunningTime: 3,
|
||||||
|
historyPreviewLength: 1
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty arrays and undefined values', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test',
|
||||||
|
responseData: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = addStatisticalDataToHistoryItem(item);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
...item,
|
||||||
|
llmModuleAccount: 1,
|
||||||
|
totalQuoteList: [],
|
||||||
|
totalRunningTime: 0,
|
||||||
|
historyPreviewLength: undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle nested plugin and loop details', () => {
|
||||||
|
const item: ChatItemType = {
|
||||||
|
obj: ChatRoleEnum.AI,
|
||||||
|
value: 'test',
|
||||||
|
responseData: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 1,
|
||||||
|
pluginDetail: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.chatNode,
|
||||||
|
runningTime: 0.5
|
||||||
|
}
|
||||||
|
],
|
||||||
|
loopDetail: [
|
||||||
|
{
|
||||||
|
moduleType: FlowNodeTypeEnum.tools,
|
||||||
|
runningTime: 0.3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = addStatisticalDataToHistoryItem(item);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
...item,
|
||||||
|
llmModuleAccount: 3,
|
||||||
|
totalQuoteList: [],
|
||||||
|
totalRunningTime: 1,
|
||||||
|
historyPreviewLength: undefined
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user