@lobehub/chat 0.120.6 → 0.121.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/.github/ISSUE_TEMPLATE/2_feature_request_cn.yml +1 -1
  2. package/CHANGELOG.md +58 -0
  3. package/README.md +14 -14
  4. package/README.zh-CN.md +14 -14
  5. package/docs/Development/Feature-Development-Frontend.zh-CN.md +126 -0
  6. package/docs/Development/Feature-Development.zh-CN.md +664 -83
  7. package/docs/Development/{State-Management/State-Management-Intro.zh-CN.md → State-Management-Intro.zh-CN.md} +9 -9
  8. package/docs/Home.md +1 -0
  9. package/docs/_Sidebar.md +1 -0
  10. package/locales/ar/chat.json +13 -1
  11. package/locales/ar/common.json +1 -0
  12. package/locales/de-DE/chat.json +13 -1
  13. package/locales/de-DE/common.json +1 -0
  14. package/locales/en-US/chat.json +13 -1
  15. package/locales/en-US/common.json +1 -0
  16. package/locales/es-ES/chat.json +13 -1
  17. package/locales/es-ES/common.json +1 -0
  18. package/locales/fr-FR/chat.json +13 -1
  19. package/locales/fr-FR/common.json +1 -0
  20. package/locales/it-IT/chat.json +13 -1
  21. package/locales/it-IT/common.json +1 -0
  22. package/locales/ja-JP/chat.json +13 -1
  23. package/locales/ja-JP/common.json +1 -0
  24. package/locales/ko-KR/chat.json +13 -1
  25. package/locales/ko-KR/common.json +1 -0
  26. package/locales/nl-NL/chat.json +13 -1
  27. package/locales/nl-NL/common.json +1 -0
  28. package/locales/pl-PL/chat.json +13 -1
  29. package/locales/pl-PL/common.json +1 -0
  30. package/locales/pt-BR/chat.json +13 -1
  31. package/locales/pt-BR/common.json +1 -0
  32. package/locales/ru-RU/chat.json +13 -1
  33. package/locales/ru-RU/common.json +1 -0
  34. package/locales/tr-TR/chat.json +13 -1
  35. package/locales/tr-TR/common.json +1 -0
  36. package/locales/vi-VN/chat.json +13 -1
  37. package/locales/vi-VN/common.json +1 -0
  38. package/locales/zh-CN/chat.json +13 -1
  39. package/locales/zh-CN/common.json +1 -0
  40. package/locales/zh-TW/chat.json +13 -1
  41. package/locales/zh-TW/common.json +1 -0
  42. package/package.json +4 -6
  43. package/src/app/chat/(desktop)/features/ChatInput/Footer/index.tsx +18 -5
  44. package/src/app/chat/(desktop)/features/ChatInput/index.tsx +1 -1
  45. package/src/app/chat/features/ChatHeader/ShareButton/index.tsx +0 -1
  46. package/src/app/chat/features/Migration/Failed.tsx +2 -0
  47. package/src/app/chat/features/SessionListContent/CollapseGroup/Actions.tsx +115 -0
  48. package/src/app/chat/features/SessionListContent/CollapseGroup/index.tsx +6 -0
  49. package/src/app/chat/features/SessionListContent/DefaultMode.tsx +63 -23
  50. package/src/app/chat/features/SessionListContent/List/AddButton.tsx +3 -3
  51. package/src/app/chat/features/SessionListContent/List/Item/Actions.tsx +89 -35
  52. package/src/app/chat/features/SessionListContent/List/Item/index.tsx +35 -15
  53. package/src/app/chat/features/SessionListContent/List/index.tsx +4 -2
  54. package/src/app/chat/features/SessionListContent/Modals/ConfigGroupModal/GroupItem.tsx +84 -0
  55. package/src/app/chat/features/SessionListContent/Modals/ConfigGroupModal/index.tsx +79 -0
  56. package/src/app/chat/features/SessionListContent/Modals/CreateGroupModal.tsx +63 -0
  57. package/src/app/chat/features/SessionListContent/Modals/RenameGroupModal.tsx +50 -0
  58. package/src/app/chat/features/SessionListContent/SearchMode.tsx +1 -1
  59. package/src/app/settings/common/Common.tsx +11 -2
  60. package/src/app/settings/llm/LLM/index.tsx +1 -0
  61. package/src/components/FullscreenLoading/index.tsx +1 -1
  62. package/src/database/core/__tests__/db-upgrade.test.ts +42 -0
  63. package/src/database/core/db.ts +26 -4
  64. package/src/database/core/schemas.ts +14 -1
  65. package/src/database/core/types/db.ts +2 -0
  66. package/src/database/models/__tests__/message.test.ts +42 -0
  67. package/src/database/models/__tests__/plugin.test.ts +77 -0
  68. package/src/database/models/__tests__/session.test.ts +202 -63
  69. package/src/database/models/__tests__/sessionGroup.test.ts +195 -0
  70. package/src/database/models/__tests__/topic.test.ts +76 -1
  71. package/src/database/models/message.ts +7 -8
  72. package/src/database/models/session.ts +103 -7
  73. package/src/database/models/sessionGroup.ts +76 -0
  74. package/src/database/schemas/session.ts +1 -0
  75. package/src/database/schemas/sessionGroup.ts +8 -0
  76. package/src/features/ChatInput/ActionBar/Clear.tsx +2 -1
  77. package/src/features/Conversation/Actions/Assistant.tsx +1 -1
  78. package/src/features/Conversation/Actions/Function.tsx +2 -2
  79. package/src/features/Conversation/Actions/User.tsx +1 -1
  80. package/src/features/Conversation/Error/ApiKeyForm.tsx +1 -0
  81. package/src/features/Conversation/Error/InvalidAccess.tsx +1 -0
  82. package/src/features/Conversation/Plugins/Inspector/index.tsx +1 -1
  83. package/src/features/Conversation/Plugins/Render/DefaultType/SystemJsRender/index.tsx +7 -2
  84. package/src/features/Conversation/hooks/useChatListActionsBar.tsx +3 -3
  85. package/src/features/PluginDevModal/index.tsx +3 -0
  86. package/src/features/PluginSettings/PluginSettingRender.tsx +1 -0
  87. package/src/features/PluginStore/PluginItem/Action.tsx +2 -0
  88. package/src/locales/default/chat.ts +13 -1
  89. package/src/locales/default/common.ts +1 -0
  90. package/src/migrations/FromV2ToV3/fixtures/input-v2-session.json +72 -0
  91. package/src/migrations/FromV2ToV3/fixtures/output-v3-from-v1.json +203 -0
  92. package/src/migrations/FromV2ToV3/fixtures/output-v3.json +74 -0
  93. package/src/migrations/FromV2ToV3/index.ts +30 -0
  94. package/src/migrations/FromV2ToV3/migrations.test.ts +42 -0
  95. package/src/migrations/FromV2ToV3/types/v3.ts +27 -0
  96. package/src/migrations/index.ts +7 -1
  97. package/src/services/__tests__/session.test.ts +198 -7
  98. package/src/services/config.ts +23 -7
  99. package/src/services/session.ts +67 -14
  100. package/src/store/global/slices/common/action.test.ts +130 -0
  101. package/src/store/global/slices/common/action.ts +14 -0
  102. package/src/store/global/slices/common/initialState.ts +6 -2
  103. package/src/store/global/slices/common/selectors.ts +1 -4
  104. package/src/store/global/slices/settings/__snapshots__/selectors.test.ts.snap +83 -0
  105. package/src/store/global/slices/settings/action.ts +2 -2
  106. package/src/store/global/slices/settings/selectors.test.ts +132 -182
  107. package/src/store/global/store.ts +7 -0
  108. package/src/store/session/initialState.ts +5 -30
  109. package/src/store/session/selectors.ts +2 -1
  110. package/src/store/session/slices/session/action.test.ts +56 -0
  111. package/src/store/session/slices/session/action.ts +13 -9
  112. package/src/store/session/slices/session/helpers.ts +2 -3
  113. package/src/store/session/slices/session/initialState.ts +31 -23
  114. package/src/store/session/slices/session/selectors/index.ts +0 -1
  115. package/src/store/session/slices/session/selectors/list.ts +11 -18
  116. package/src/store/session/slices/sessionGroup/action.ts +54 -0
  117. package/src/store/session/slices/sessionGroup/initialState.ts +5 -0
  118. package/src/store/session/slices/sessionGroup/selectors.ts +15 -0
  119. package/src/store/session/store.ts +9 -2
  120. package/src/styles/antdOverride.ts +0 -7
  121. package/src/types/exportConfig.ts +3 -1
  122. package/src/types/session.ts +27 -3
  123. package/src/store/session/slices/session/selectors/export.ts +0 -31
  124. /package/docs/Development/{Feature-Development.md → Feature-Development-Frontend.md} +0 -0
  125. /package/docs/Development/{State-Management/State-Management-Intro.md → State-Management-Intro.md} +0 -0
  126. /package/docs/Development/{State-Management/Selectors.md → State-Management-Selectors.md} +0 -0
  127. /package/docs/Development/{State-Management/Selectors.zh-CN.md → State-Management-Selectors.zh-CN.md} +0 -0
@@ -0,0 +1,77 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import { DB_Plugin } from '../../schemas/plugin';
4
+ import { PluginModel } from '../plugin';
5
+
6
+ describe('PluginModel', () => {
7
+ let pluginData: DB_Plugin;
8
+
9
+ beforeEach(() => {
10
+ // 设置正确结构的插件数据
11
+ pluginData = {
12
+ identifier: 'test-plugin',
13
+ manifest: {},
14
+ type: 'plugin',
15
+ };
16
+ });
17
+
18
+ afterEach(async () => {
19
+ // 每次测试后清理数据库
20
+ await PluginModel.clear();
21
+ });
22
+
23
+ describe('getList', () => {
24
+ it('should fetch and return the plugin list', async () => {
25
+ await PluginModel.create(pluginData);
26
+ const plugins = await PluginModel.getList();
27
+ expect(plugins).toHaveLength(1);
28
+ expect(plugins[0]).toEqual(pluginData);
29
+ });
30
+ });
31
+
32
+ describe('create', () => {
33
+ it('should create a plugin record', async () => {
34
+ await PluginModel.create(pluginData);
35
+ const plugins = await PluginModel.getList();
36
+ expect(plugins).toHaveLength(1);
37
+ expect(plugins[0]).toEqual(pluginData);
38
+ });
39
+ });
40
+
41
+ describe('batchCreate', () => {
42
+ it('should batch create plugin records', async () => {
43
+ await PluginModel.batchCreate([pluginData, { ...pluginData, identifier: 'abc' }]);
44
+ const plugins = await PluginModel.getList();
45
+ expect(plugins).toHaveLength(2);
46
+ });
47
+ });
48
+
49
+ describe('delete', () => {
50
+ it('should delete a plugin', async () => {
51
+ await PluginModel.create(pluginData);
52
+ await PluginModel.delete(pluginData.identifier);
53
+ const plugins = await PluginModel.getList();
54
+ expect(plugins).toHaveLength(0);
55
+ });
56
+ });
57
+
58
+ describe('update', () => {
59
+ it('should update a plugin', async () => {
60
+ await PluginModel.create(pluginData);
61
+ const updatedPluginData: DB_Plugin = { ...pluginData, type: 'customPlugin' };
62
+ await PluginModel.update(pluginData.identifier, updatedPluginData);
63
+ const plugins = await PluginModel.getList();
64
+ expect(plugins).toHaveLength(1);
65
+ expect(plugins[0]).toEqual(updatedPluginData);
66
+ });
67
+ });
68
+
69
+ describe('clear', () => {
70
+ it('should clear the table', async () => {
71
+ await PluginModel.create(pluginData);
72
+ await PluginModel.clear();
73
+ const plugins = await PluginModel.getList();
74
+ expect(plugins).toHaveLength(0);
75
+ });
76
+ });
77
+ });
@@ -2,8 +2,15 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
2
 
3
3
  import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
4
4
  import { CreateMessageParams, MessageModel } from '@/database/models/message';
5
+ import { SessionGroupModel } from '@/database/models/sessionGroup';
5
6
  import { TopicModel } from '@/database/models/topic';
6
- import { LobeAgentSession, LobeSessionType, SessionGroupKey } from '@/types/session';
7
+ import { LobeAgentConfig } from '@/types/agent';
8
+ import {
9
+ LobeAgentSession,
10
+ LobeSessionType,
11
+ SessionDefaultGroup,
12
+ SessionGroupId,
13
+ } from '@/types/session';
7
14
 
8
15
  import { SessionModel } from '../session';
9
16
 
@@ -25,93 +32,225 @@ describe('SessionModel', () => {
25
32
  // Clean up the database after each test
26
33
  await SessionModel.clearTable();
27
34
  });
35
+ describe('create', () => {
36
+ it('should create a session record', async () => {
37
+ const result = await SessionModel.create('agent', sessionData);
28
38
 
29
- it('should create a session record', async () => {
30
- const result = await SessionModel.create('agent', sessionData);
39
+ expect(result).toHaveProperty('id');
40
+ // Verify that the session has been added to the database
41
+ // Assuming findById is a method that retrieves a session by ID
42
+ const sessionInDb = await SessionModel.findById(result.id);
31
43
 
32
- expect(result).toHaveProperty('id');
33
- // Verify that the session has been added to the database
34
- // Assuming findById is a method that retrieves a session by ID
35
- const sessionInDb = await SessionModel.findById(result.id);
44
+ expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
45
+ });
46
+ });
47
+
48
+ describe('batchCreate', () => {
49
+ it('should batch create session records', async () => {
50
+ const sessionsToCreate = [sessionData, sessionData];
51
+ const results = await SessionModel.batchCreate(sessionsToCreate as LobeAgentSession[]);
52
+
53
+ expect(results.ids).toHaveLength(sessionsToCreate.length);
54
+ // Verify that the sessions have been added to the database
55
+ for (const result of results.ids!) {
56
+ const sessionInDb = await SessionModel.findById(result);
57
+ expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
58
+ }
59
+ });
36
60
 
37
- expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
61
+ it('should set group to default if it does not exist in SessionGroup', async () => {
62
+ const sessionDataWithInvalidGroup = {
63
+ ...sessionData,
64
+ group: 'nonExistentGroup',
65
+ } as LobeAgentSession;
66
+
67
+ const results = await SessionModel.batchCreate([sessionDataWithInvalidGroup]);
68
+
69
+ // Verify that the group has been set to default
70
+ for (const result of results.ids!) {
71
+ const sessionInDb = await SessionModel.findById(result);
72
+ expect(sessionInDb.group).toEqual(SessionDefaultGroup.Default);
73
+ }
74
+ });
38
75
  });
39
76
 
40
- it('should batch create session records', async () => {
41
- const sessionsToCreate = [sessionData, sessionData];
42
- const results = await SessionModel.batchCreate(sessionsToCreate as LobeAgentSession[]);
77
+ describe('query', () => {
78
+ it('should query sessions with pagination', async () => {
79
+ // Create multiple sessions to test the query method
80
+ await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
43
81
 
44
- expect(results.ids).toHaveLength(sessionsToCreate.length);
45
- // Verify that the sessions have been added to the database
46
- for (const result of results.ids!) {
47
- const sessionInDb = await SessionModel.findById(result);
48
- expect(sessionInDb).toEqual(expect.objectContaining(sessionData));
49
- }
82
+ const queriedSessions = await SessionModel.query({ pageSize: 1, current: 0 });
83
+
84
+ expect(queriedSessions).toHaveLength(1);
85
+ });
50
86
  });
51
87
 
52
- it('should query sessions with pagination', async () => {
53
- // Create multiple sessions to test the query method
54
- await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
88
+ describe('querySessionsByGroupId', () => {
89
+ it('should query sessions by group', async () => {
90
+ // Create multiple sessions to test the queryByGroup method
91
+ const group: SessionGroupId = 'testGroup';
92
+ await SessionGroupModel.create('测试分组', 0, group);
55
93
 
56
- const queriedSessions = await SessionModel.query({ pageSize: 1, current: 0 });
94
+ await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
57
95
 
58
- expect(queriedSessions).toHaveLength(1);
96
+ const sessionsByGroup = await SessionModel.querySessionsByGroupId(group);
97
+
98
+ // Assuming all created sessions belong to the same group
99
+ expect(sessionsByGroup).toHaveLength(2);
100
+ expect(sessionsByGroup.every((i) => i.group === group)).toBeTruthy();
101
+ });
59
102
  });
60
103
 
61
- it('should query sessions by group', async () => {
62
- // Create multiple sessions to test the queryByGroup method
63
- const group: SessionGroupKey = 'testGroup';
64
- await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
104
+ describe('update', () => {
105
+ it('should update a session', async () => {
106
+ const createdSession = await SessionModel.create('agent', sessionData);
107
+ const updateData = { group: 'newGroup' };
65
108
 
66
- const sessionsByGroup = await SessionModel.queryByGroup(group);
109
+ await SessionModel.update(createdSession.id, updateData);
110
+ const updatedSession = await SessionModel.findById(createdSession.id);
67
111
 
68
- // Assuming all created sessions belong to the same group
69
- expect(sessionsByGroup).toHaveLength(2);
70
- expect(sessionsByGroup.every((i) => i.group === group)).toBeTruthy();
112
+ expect(updatedSession).toHaveProperty('group', 'newGroup');
113
+ });
71
114
  });
72
115
 
73
- it('should update a session', async () => {
74
- const createdSession = await SessionModel.create('agent', sessionData);
75
- const updateData = { group: 'newGroup' };
116
+ describe('updatePinned', () => {
117
+ it('should update pinned status of a session', async () => {
118
+ const createdSession = await SessionModel.create('agent', sessionData);
119
+ await SessionModel.updatePinned(createdSession.id, true);
120
+ const updatedSession = await SessionModel.findById(createdSession.id);
121
+ expect(updatedSession).toHaveProperty('pinned', 1);
122
+ });
123
+ });
76
124
 
77
- await SessionModel.update(createdSession.id, updateData);
78
- const updatedSession = await SessionModel.findById(createdSession.id);
125
+ describe('updateConfig', () => {
126
+ it('should update config of a session', async () => {
127
+ const { id } = await SessionModel.create('agent', sessionData);
128
+ const dbSession = await SessionModel.findById(id);
79
129
 
80
- expect(updatedSession).toHaveProperty('group', 'newGroup');
130
+ const newConfig = { ...dbSession.config, systemRole: 'newValue' } as LobeAgentConfig;
131
+ await SessionModel.updateConfig(id, newConfig);
132
+ const updatedSession = await SessionModel.findById(id);
133
+ expect(updatedSession.config).toEqual(newConfig);
134
+ });
81
135
  });
82
136
 
83
- // 删除一个 session 时,也需要同步删除具有 sessionId 的 topic 和 message
84
- it('should delete a session and its associated data', async () => {
85
- // create session , topic and message test data
86
- const { id: sessionId } = await SessionModel.create('agent', sessionData);
137
+ describe('clearTable', () => {
138
+ it('should clear all sessions', async () => {
139
+ await SessionModel.batchCreate([sessionData, sessionData] as LobeAgentSession[]);
140
+ await SessionModel.clearTable();
141
+ const sessionsInDb = await SessionModel.query();
142
+ expect(sessionsInDb).toHaveLength(0);
143
+ });
144
+ });
87
145
 
88
- const topicData = {
89
- title: 'Test Topic',
90
- sessionId: sessionId,
91
- favorite: false,
92
- };
93
- const createdTopic = await TopicModel.create(topicData);
146
+ describe('isEmpty', () => {
147
+ it('should check if table is empty', async () => {
148
+ await SessionModel.clearTable();
149
+ const isEmpty = await SessionModel.isEmpty();
150
+ expect(isEmpty).toBeTruthy();
151
+ await SessionModel.create('agent', sessionData);
152
+ const isNotEmpty = await SessionModel.isEmpty();
153
+ expect(isNotEmpty).toBeFalsy();
154
+ });
155
+ });
94
156
 
95
- const messageData: CreateMessageParams = {
96
- content: 'Test Message',
97
- sessionId: sessionId,
98
- topicId: createdTopic.id,
99
- role: 'user',
100
- };
101
- await MessageModel.create(messageData);
157
+ describe('queryByKeyword', () => {
158
+ it('should query sessions by keyword', async () => {
159
+ const keyword = 'testKeyword';
160
+ const sessionWithKeyword = { ...sessionData, meta: { title: keyword } };
161
+ await SessionModel.create('agent', sessionWithKeyword);
162
+ const sessionsByKeyword = await SessionModel.queryByKeyword(keyword);
163
+ expect(sessionsByKeyword).toHaveLength(1);
164
+ expect(sessionsByKeyword[0].meta.title).toContain(keyword);
165
+ });
166
+ });
102
167
 
103
- await SessionModel.delete(sessionId);
168
+ describe('getPinnedSessions', () => {
169
+ it('should get pinned sessions', async () => {
170
+ const pinnedSession = { ...sessionData, pinned: true };
171
+ const unpinnedSession = { ...sessionData, pinned: false };
172
+ await SessionModel.batchCreate([pinnedSession, unpinnedSession] as LobeAgentSession[]);
173
+ const pinnedSessions = await SessionModel.getPinnedSessions();
174
+ expect(pinnedSessions).toHaveLength(1);
175
+ expect(pinnedSessions[0].pinned).toBeTruthy();
176
+ });
177
+ });
178
+
179
+ describe('queryWithGroups', () => {
180
+ it('should query sessions with groups', async () => {
181
+ await SessionModel.create('agent', sessionData);
182
+
183
+ const sessionsWithGroups = await SessionModel.queryWithGroups();
184
+ expect(sessionsWithGroups.all).toHaveLength(1);
185
+ expect(sessionsWithGroups.all[0]).toEqual(expect.objectContaining(sessionData));
186
+ });
187
+ });
188
+
189
+ describe('queryByGroupIds', () => {
190
+ it('should query sessions by group ids', async () => {
191
+ const createdSession = await SessionModel.create('agent', sessionData);
192
+ const session = await SessionModel.findById(createdSession.id);
193
+ const sessionsByGroupIds = await SessionModel.queryByGroupIds([session.group]);
194
+ expect(sessionsByGroupIds[session.group]).toHaveLength(1);
195
+ expect(sessionsByGroupIds[session.group][0]).toEqual(expect.objectContaining(sessionData));
196
+ });
197
+ });
198
+
199
+ describe('delete', () => {
200
+ it('should delete a session', async () => {
201
+ const createdSession = await SessionModel.create('agent', sessionData);
202
+ await SessionModel.delete(createdSession.id);
203
+ const sessionInDb = await SessionModel.findById(createdSession.id);
204
+ expect(sessionInDb).toBeUndefined();
205
+ });
206
+
207
+ // 删除一个 session 时,也需要同步删除具有 sessionId 的 topic 和 message
208
+ it('should delete a session and its associated data', async () => {
209
+ // create session , topic and message test data
210
+ const { id: sessionId } = await SessionModel.create('agent', sessionData);
211
+
212
+ const topicData = {
213
+ title: 'Test Topic',
214
+ sessionId: sessionId,
215
+ favorite: false,
216
+ };
217
+ const createdTopic = await TopicModel.create(topicData);
218
+
219
+ const messageData: CreateMessageParams = {
220
+ content: 'Test Message',
221
+ sessionId: sessionId,
222
+ topicId: createdTopic.id,
223
+ role: 'user',
224
+ };
225
+ await MessageModel.create(messageData);
226
+
227
+ await SessionModel.delete(sessionId);
228
+
229
+ // Verify the session and its related data (topics, messages) are deleted
230
+ const sessionInDb = await SessionModel.findById(sessionId);
231
+ expect(sessionInDb).toBeUndefined();
232
+
233
+ // You need to verify that topics and messages related to the session are also deleted
234
+ const topicsInDb = await TopicModel.findBySessionId(sessionId);
235
+ expect(topicsInDb).toHaveLength(0);
236
+
237
+ // Verify all associated messages are deleted
238
+ const messagesInDb = await MessageModel.query({ sessionId });
239
+ expect(messagesInDb).toHaveLength(0);
240
+ });
241
+ });
104
242
 
105
- // Verify the session and its related data (topics, messages) are deleted
106
- const sessionInDb = await SessionModel.findById(sessionId);
107
- expect(sessionInDb).toBeUndefined();
243
+ describe('duplicate', () => {
244
+ it('should duplicate a session', async () => {
245
+ const createdSession = await SessionModel.create('agent', sessionData);
246
+ const duplicatedSession = await SessionModel.duplicate(createdSession.id);
247
+ const sessionsInDb = await SessionModel.query();
248
+ expect(sessionsInDb).toHaveLength(2);
108
249
 
109
- // You need to verify that topics and messages related to the session are also deleted
110
- const topicsInDb = await TopicModel.findBySessionId(sessionId);
111
- expect(topicsInDb).toHaveLength(0);
250
+ if (!duplicatedSession) return;
251
+ const session = await SessionModel.findById(duplicatedSession.id);
112
252
 
113
- // Verify all associated messages are deleted
114
- const messagesInDb = await MessageModel.query({ sessionId });
115
- expect(messagesInDb).toHaveLength(0);
253
+ expect(session).toEqual(expect.objectContaining(sessionData));
254
+ });
116
255
  });
117
256
  });
@@ -0,0 +1,195 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import { SessionModel } from '@/database/models/session';
4
+ import { SessionGroups } from '@/types/session';
5
+
6
+ import { DB_SessionGroup } from '../../schemas/sessionGroup';
7
+ import { SessionGroupModel } from '../sessionGroup';
8
+
9
+ describe('SessionGroupModel', () => {
10
+ let sessionGroupData: DB_SessionGroup;
11
+
12
+ beforeEach(() => {
13
+ // 设置正确结构的会话组数据
14
+ sessionGroupData = {
15
+ name: 'test-group',
16
+ sort: 1,
17
+ };
18
+ });
19
+
20
+ afterEach(async () => {
21
+ // 每次测试后清理数据库
22
+ await SessionGroupModel.clear();
23
+ });
24
+
25
+ describe('create', () => {
26
+ it('should create a session group record', async () => {
27
+ const createdGroup = await SessionGroupModel.create(
28
+ sessionGroupData.name,
29
+ sessionGroupData.sort,
30
+ );
31
+ const item = await SessionGroupModel.findById(createdGroup.id);
32
+ expect(item.name).toEqual(sessionGroupData.name);
33
+ expect(item.sort).toEqual(sessionGroupData.sort);
34
+ });
35
+ });
36
+
37
+ describe('batchCreate', () => {
38
+ it('should batch create session group records', async () => {
39
+ const groups = [
40
+ sessionGroupData,
41
+ { ...sessionGroupData, name: 'another-group' },
42
+ ] as SessionGroups;
43
+ await SessionGroupModel.batchCreate(groups);
44
+ const fetchedGroups = await SessionGroupModel.query();
45
+ expect(fetchedGroups).toHaveLength(2);
46
+ });
47
+ });
48
+
49
+ describe('update', () => {
50
+ it('should update a session group', async () => {
51
+ const createdGroup = await SessionGroupModel.create(
52
+ sessionGroupData.name,
53
+ sessionGroupData.sort,
54
+ );
55
+ const updatedGroupData: DB_SessionGroup = { ...sessionGroupData, name: 'updated-group' };
56
+ await SessionGroupModel.update(createdGroup.id, updatedGroupData);
57
+ const fetchedGroups = await SessionGroupModel.query();
58
+ expect(fetchedGroups[0].name).toEqual(updatedGroupData.name);
59
+ });
60
+ });
61
+
62
+ describe('delete', () => {
63
+ it('should delete a session group', async () => {
64
+ const createdGroup = await SessionGroupModel.create(
65
+ sessionGroupData.name,
66
+ sessionGroupData.sort,
67
+ );
68
+ await SessionGroupModel.delete(createdGroup.id);
69
+ const fetchedGroups = await SessionGroupModel.query();
70
+ expect(fetchedGroups).toHaveLength(0);
71
+ });
72
+ it('should delete a session group and update associated sessions', async () => {
73
+ // Create a new session group
74
+ const createdGroup = await SessionGroupModel.create(
75
+ sessionGroupData.name,
76
+ sessionGroupData.sort,
77
+ );
78
+
79
+ // Delete the created session group
80
+ await SessionGroupModel.delete(createdGroup.id);
81
+
82
+ // Fetch all session groups to confirm deletion
83
+ const fetchedGroups = await SessionGroupModel.query();
84
+ expect(fetchedGroups).toHaveLength(0);
85
+
86
+ // Fetch all sessions to confirm update
87
+ const fetchedSessions = await SessionModel.query();
88
+ fetchedSessions.forEach((session) => {
89
+ expect(session.group).not.toEqual(createdGroup.id);
90
+ });
91
+ });
92
+ });
93
+
94
+ describe('query', () => {
95
+ it('should fetch and return the session group list', async () => {
96
+ await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
97
+ const fetchedGroups = await SessionGroupModel.query();
98
+ expect(fetchedGroups).toHaveLength(1);
99
+ expect(fetchedGroups[0].name).toEqual(sessionGroupData.name);
100
+ });
101
+
102
+ it('should return an empty array when there are no session groups', async () => {
103
+ const fetchedGroups = await SessionGroupModel.query();
104
+ expect(fetchedGroups).toEqual([]);
105
+ });
106
+
107
+ it('should return session groups sorted by sort field when it exists', async () => {
108
+ const group1 = await SessionGroupModel.create('group1', 2);
109
+ const group2 = await SessionGroupModel.create('group2', 1);
110
+ const fetchedGroups = await SessionGroupModel.query();
111
+ expect(fetchedGroups[0].id).toEqual(group2.id);
112
+ expect(fetchedGroups[1].id).toEqual(group1.id);
113
+ });
114
+
115
+ it('should return session groups sorted by createdAt when sort field does not exist', async () => {
116
+ const group1 = await SessionGroupModel.create('group1');
117
+ await new Promise((resolve) => setTimeout(() => resolve(undefined), 300));
118
+ const group2 = await SessionGroupModel.create('group2');
119
+
120
+ const fetchedGroups = await SessionGroupModel.query();
121
+
122
+ expect(fetchedGroups[0].id).toEqual(group2.id);
123
+ expect(fetchedGroups[1].id).toEqual(group1.id);
124
+ });
125
+
126
+ it('should return session groups sorted by sort field first and then by createdAt', async () => {
127
+ const group0 = await SessionGroupModel.create('group0');
128
+ const group1 = await SessionGroupModel.create('group1', 1);
129
+ const group2 = await SessionGroupModel.create('group2');
130
+ const group3 = await SessionGroupModel.create('group3', 2);
131
+ const fetchedGroups = await SessionGroupModel.query();
132
+ expect(fetchedGroups[0].id).toEqual(group1.id);
133
+ expect(fetchedGroups[1].id).toEqual(group3.id);
134
+ expect(fetchedGroups[2].id).toEqual(group2.id);
135
+ expect(fetchedGroups[3].id).toEqual(group0.id);
136
+ });
137
+
138
+ it('should return session groups sorted by sort ', async () => {
139
+ const group1 = await SessionGroupModel.create('group1', 1);
140
+ const group2 = await SessionGroupModel.create('group2');
141
+ const group3 = await SessionGroupModel.create('group3', 2);
142
+ const fetchedGroups = await SessionGroupModel.query();
143
+ expect(fetchedGroups[0].id).toEqual(group1.id);
144
+ expect(fetchedGroups[1].id).toEqual(group3.id);
145
+ expect(fetchedGroups[2].id).toEqual(group2.id);
146
+ });
147
+
148
+ it('should return session groups sorted by createdAt when sort fields are equal', async () => {
149
+ const group1 = await SessionGroupModel.create('group1', 1);
150
+ await new Promise((resolve) => setTimeout(() => resolve(undefined), 300));
151
+ const group2 = await SessionGroupModel.create('group2', 1);
152
+ const fetchedGroups = await SessionGroupModel.query();
153
+ expect(fetchedGroups[0].id).toEqual(group2.id);
154
+ expect(fetchedGroups[1].id).toEqual(group1.id);
155
+ });
156
+ });
157
+
158
+ describe('updateOrder', () => {
159
+ it('should update the order of session groups', async () => {
160
+ const group1 = await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
161
+ const group2 = await SessionGroupModel.create('another-group', 2);
162
+ await SessionGroupModel.updateOrder([
163
+ { id: group1.id, sort: 2 },
164
+ { id: group2.id, sort: 1 },
165
+ ]);
166
+ const fetchedGroups = await SessionGroupModel.query();
167
+ expect(fetchedGroups[0].id).toEqual(group2.id);
168
+ expect(fetchedGroups[1].id).toEqual(group1.id);
169
+ });
170
+ });
171
+
172
+ describe('findById', () => {
173
+ it('should find a session group by id', async () => {
174
+ const createdGroup = await SessionGroupModel.create(
175
+ sessionGroupData.name,
176
+ sessionGroupData.sort,
177
+ );
178
+ const fetchedGroup = await SessionGroupModel.findById(createdGroup.id);
179
+ expect(fetchedGroup).toMatchObject({ ...createdGroup, ...sessionGroupData });
180
+ });
181
+ });
182
+
183
+ describe('clear', () => {
184
+ it('should clear all session groups', async () => {
185
+ await SessionGroupModel.create(sessionGroupData.name, sessionGroupData.sort);
186
+ const first = await SessionGroupModel.query();
187
+ expect(first).toHaveLength(1);
188
+
189
+ await SessionGroupModel.clear();
190
+
191
+ const fetchedGroups = await SessionGroupModel.query();
192
+ expect(fetchedGroups).toHaveLength(0);
193
+ });
194
+ });
195
+ });