Skip to content

Commit 29a10c1

Browse files
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>
1 parent 2887737 commit 29a10c1

File tree

2 files changed

+192
-1
lines changed

2 files changed

+192
-1
lines changed

projects/app/src/global/core/chat/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ChatHistoryItemResType, ChatItemType } from '@fastgpt/global/core/chat/
33
import { SearchDataResponseItemType } from '@fastgpt/global/core/dataset/type';
44
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
55

6-
const isLLMNode = (item: ChatHistoryItemResType) =>
6+
export const isLLMNode = (item: ChatHistoryItemResType) =>
77
item.moduleType === FlowNodeTypeEnum.chatNode || item.moduleType === FlowNodeTypeEnum.tools;
88

99
export function transformPreviewHistories(
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
3+
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
4+
import { ChatHistoryItemResType, ChatItemType } from '@fastgpt/global/core/chat/type';
5+
import {
6+
transformPreviewHistories,
7+
addStatisticalDataToHistoryItem
8+
} from '@/global/core/chat/utils';
9+
10+
describe('transformPreviewHistories', () => {
11+
it('should transform histories correctly with responseDetail=true', () => {
12+
const histories: ChatItemType[] = [
13+
{
14+
obj: ChatRoleEnum.AI,
15+
value: 'test response',
16+
responseData: [
17+
{
18+
moduleType: FlowNodeTypeEnum.chatNode,
19+
runningTime: 1.5
20+
}
21+
]
22+
}
23+
];
24+
25+
const result = transformPreviewHistories(histories, true);
26+
27+
expect(result[0]).toEqual({
28+
obj: ChatRoleEnum.AI,
29+
value: 'test response',
30+
responseData: undefined,
31+
llmModuleAccount: 1,
32+
totalQuoteList: [],
33+
totalRunningTime: 1.5,
34+
historyPreviewLength: undefined
35+
});
36+
});
37+
38+
it('should transform histories correctly with responseDetail=false', () => {
39+
const histories: ChatItemType[] = [
40+
{
41+
obj: ChatRoleEnum.AI,
42+
value: 'test response',
43+
responseData: [
44+
{
45+
moduleType: FlowNodeTypeEnum.chatNode,
46+
runningTime: 1.5
47+
}
48+
]
49+
}
50+
];
51+
52+
const result = transformPreviewHistories(histories, false);
53+
54+
expect(result[0]).toEqual({
55+
obj: ChatRoleEnum.AI,
56+
value: 'test response',
57+
responseData: undefined,
58+
llmModuleAccount: 1,
59+
totalQuoteList: undefined,
60+
totalRunningTime: 1.5,
61+
historyPreviewLength: undefined
62+
});
63+
});
64+
});
65+
66+
describe('addStatisticalDataToHistoryItem', () => {
67+
it('should return original item if obj is not AI', () => {
68+
const item: ChatItemType = {
69+
obj: ChatRoleEnum.Human,
70+
value: 'test'
71+
};
72+
73+
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
74+
});
75+
76+
it('should return original item if totalQuoteList is already defined', () => {
77+
const item: ChatItemType = {
78+
obj: ChatRoleEnum.AI,
79+
value: 'test',
80+
totalQuoteList: []
81+
};
82+
83+
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
84+
});
85+
86+
it('should return original item if responseData is undefined', () => {
87+
const item: ChatItemType = {
88+
obj: ChatRoleEnum.AI,
89+
value: 'test'
90+
};
91+
92+
expect(addStatisticalDataToHistoryItem(item)).toBe(item);
93+
});
94+
95+
it('should calculate statistics correctly', () => {
96+
const item: ChatItemType = {
97+
obj: ChatRoleEnum.AI,
98+
value: 'test',
99+
responseData: [
100+
{
101+
moduleType: FlowNodeTypeEnum.chatNode,
102+
runningTime: 1.5,
103+
historyPreview: ['preview1']
104+
},
105+
{
106+
moduleType: FlowNodeTypeEnum.datasetSearchNode,
107+
quoteList: [{ id: '1', q: 'test', a: 'answer' }],
108+
runningTime: 0.5
109+
},
110+
{
111+
moduleType: FlowNodeTypeEnum.tools,
112+
runningTime: 1,
113+
toolDetail: [
114+
{
115+
moduleType: FlowNodeTypeEnum.chatNode,
116+
runningTime: 0.5
117+
}
118+
]
119+
}
120+
]
121+
};
122+
123+
const result = addStatisticalDataToHistoryItem(item);
124+
125+
expect(result).toEqual({
126+
...item,
127+
llmModuleAccount: 3,
128+
totalQuoteList: [{ id: '1', q: 'test', a: 'answer' }],
129+
totalRunningTime: 3,
130+
historyPreviewLength: 1
131+
});
132+
});
133+
134+
it('should handle empty arrays and undefined values', () => {
135+
const item: ChatItemType = {
136+
obj: ChatRoleEnum.AI,
137+
value: 'test',
138+
responseData: [
139+
{
140+
moduleType: FlowNodeTypeEnum.chatNode,
141+
runningTime: 0
142+
}
143+
]
144+
};
145+
146+
const result = addStatisticalDataToHistoryItem(item);
147+
148+
expect(result).toEqual({
149+
...item,
150+
llmModuleAccount: 1,
151+
totalQuoteList: [],
152+
totalRunningTime: 0,
153+
historyPreviewLength: undefined
154+
});
155+
});
156+
157+
it('should handle nested plugin and loop details', () => {
158+
const item: ChatItemType = {
159+
obj: ChatRoleEnum.AI,
160+
value: 'test',
161+
responseData: [
162+
{
163+
moduleType: FlowNodeTypeEnum.chatNode,
164+
runningTime: 1,
165+
pluginDetail: [
166+
{
167+
moduleType: FlowNodeTypeEnum.chatNode,
168+
runningTime: 0.5
169+
}
170+
],
171+
loopDetail: [
172+
{
173+
moduleType: FlowNodeTypeEnum.tools,
174+
runningTime: 0.3
175+
}
176+
]
177+
}
178+
]
179+
};
180+
181+
const result = addStatisticalDataToHistoryItem(item);
182+
183+
expect(result).toEqual({
184+
...item,
185+
llmModuleAccount: 3,
186+
totalQuoteList: [],
187+
totalRunningTime: 1,
188+
historyPreviewLength: undefined
189+
});
190+
});
191+
});

0 commit comments

Comments
 (0)