|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { lambdaClient } from '@/libs/trpc/client'; |
| 4 | +import { LobeSessionType } from '@/types/session'; |
| 5 | + |
| 6 | +import { ServerService } from './server'; |
| 7 | + |
| 8 | +vi.mock('@/libs/trpc/client', () => ({ |
| 9 | + lambdaClient: { |
| 10 | + session: { |
| 11 | + createSession: { mutate: vi.fn() }, |
| 12 | + batchCreateSessions: { mutate: vi.fn() }, |
| 13 | + cloneSession: { mutate: vi.fn() }, |
| 14 | + getGroupedSessions: { query: vi.fn() }, |
| 15 | + countSessions: { query: vi.fn() }, |
| 16 | + rankSessions: { query: vi.fn() }, |
| 17 | + updateSession: { mutate: vi.fn() }, |
| 18 | + updateSessionConfig: { mutate: vi.fn() }, |
| 19 | + updateSessionChatConfig: { mutate: vi.fn() }, |
| 20 | + getSessions: { query: vi.fn() }, |
| 21 | + searchSessions: { query: vi.fn() }, |
| 22 | + removeSession: { mutate: vi.fn() }, |
| 23 | + removeAllSessions: { mutate: vi.fn() }, |
| 24 | + }, |
| 25 | + agent: { |
| 26 | + getAgentConfig: { query: vi.fn() }, |
| 27 | + }, |
| 28 | + sessionGroup: { |
| 29 | + createSessionGroup: { mutate: vi.fn() }, |
| 30 | + getSessionGroup: { query: vi.fn() }, |
| 31 | + removeSessionGroup: { mutate: vi.fn() }, |
| 32 | + removeAllSessionGroups: { mutate: vi.fn() }, |
| 33 | + updateSessionGroup: { mutate: vi.fn() }, |
| 34 | + updateSessionGroupOrder: { mutate: vi.fn() }, |
| 35 | + }, |
| 36 | + }, |
| 37 | +})); |
| 38 | + |
| 39 | +describe('ServerService', () => { |
| 40 | + let service: ServerService; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks(); |
| 44 | + service = new ServerService(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('hasSessions should return true if count is 0', async () => { |
| 48 | + vi.mocked(lambdaClient.session.countSessions.query).mockResolvedValue(0); |
| 49 | + const result = await service.hasSessions(); |
| 50 | + expect(result).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('createSession should call lambdaClient with correct params', async () => { |
| 54 | + const mockData = { |
| 55 | + config: { |
| 56 | + model: 'gpt-3.5', |
| 57 | + params: {}, |
| 58 | + systemRole: '', |
| 59 | + chatConfig: { |
| 60 | + autoCreateTopicThreshold: 2, |
| 61 | + compressThreshold: 10, |
| 62 | + enableAutoCreateTopic: true, |
| 63 | + enableCompressThreshold: true, |
| 64 | + maxTokens: 2000, |
| 65 | + model: 'gpt-3.5-turbo', |
| 66 | + params: {}, |
| 67 | + temperature: 0.7, |
| 68 | + title: 'test', |
| 69 | + }, |
| 70 | + tts: { |
| 71 | + showAllLocaleVoice: false, |
| 72 | + sttLocale: 'auto', |
| 73 | + ttsService: 'openai' as const, |
| 74 | + voice: { |
| 75 | + model: 'tts-1', |
| 76 | + name: 'alloy', |
| 77 | + type: 'tts', |
| 78 | + openai: 'voice-id', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + group: 'testGroup', |
| 83 | + meta: { description: 'test' }, |
| 84 | + title: 'Test Session', |
| 85 | + }; |
| 86 | + |
| 87 | + await service.createSession(LobeSessionType.Agent, mockData); |
| 88 | + |
| 89 | + expect(lambdaClient.session.createSession.mutate).toBeCalledWith({ |
| 90 | + config: { ...mockData.config, description: 'test' }, |
| 91 | + session: { title: 'Test Session', groupId: 'testGroup' }, |
| 92 | + type: LobeSessionType.Agent, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('batchCreateSessions should call lambdaClient', async () => { |
| 97 | + const mockSessions = [ |
| 98 | + { |
| 99 | + id: '1', |
| 100 | + title: 'Test', |
| 101 | + config: { |
| 102 | + model: 'gpt-3.5', |
| 103 | + params: {}, |
| 104 | + systemRole: '', |
| 105 | + chatConfig: { |
| 106 | + autoCreateTopicThreshold: 2, |
| 107 | + compressThreshold: 10, |
| 108 | + enableAutoCreateTopic: true, |
| 109 | + enableCompressThreshold: true, |
| 110 | + maxTokens: 2000, |
| 111 | + model: 'gpt-3.5-turbo', |
| 112 | + params: {}, |
| 113 | + temperature: 0.7, |
| 114 | + title: 'test', |
| 115 | + }, |
| 116 | + tts: { |
| 117 | + showAllLocaleVoice: false, |
| 118 | + sttLocale: 'auto', |
| 119 | + ttsService: 'openai' as const, |
| 120 | + voice: { |
| 121 | + model: 'tts-1', |
| 122 | + name: 'alloy', |
| 123 | + type: 'tts', |
| 124 | + openai: 'voice-id', |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + createdAt: new Date(), |
| 129 | + meta: { description: 'test' }, |
| 130 | + model: 'gpt-3.5', |
| 131 | + type: LobeSessionType.Agent, |
| 132 | + updatedAt: new Date(), |
| 133 | + }, |
| 134 | + ]; |
| 135 | + |
| 136 | + await service.batchCreateSessions(mockSessions as any); |
| 137 | + expect(lambdaClient.session.batchCreateSessions.mutate).toBeCalledWith(mockSessions); |
| 138 | + }); |
| 139 | + |
| 140 | + it('cloneSession should call lambdaClient', async () => { |
| 141 | + await service.cloneSession('123', 'New Title'); |
| 142 | + expect(lambdaClient.session.cloneSession.mutate).toBeCalledWith({ |
| 143 | + id: '123', |
| 144 | + newTitle: 'New Title', |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('getGroupedSessions should call lambdaClient', async () => { |
| 149 | + await service.getGroupedSessions(); |
| 150 | + expect(lambdaClient.session.getGroupedSessions.query).toBeCalled(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('countSessions should call lambdaClient with params', async () => { |
| 154 | + const params = { startDate: '2023-01-01' }; |
| 155 | + await service.countSessions(params); |
| 156 | + expect(lambdaClient.session.countSessions.query).toBeCalledWith(params); |
| 157 | + }); |
| 158 | + |
| 159 | + it('rankSessions should call lambdaClient with limit', async () => { |
| 160 | + await service.rankSessions(10); |
| 161 | + expect(lambdaClient.session.rankSessions.query).toBeCalledWith(10); |
| 162 | + }); |
| 163 | + |
| 164 | + it('updateSession should call lambdaClient with correct params', async () => { |
| 165 | + const mockData = { |
| 166 | + group: 'default', |
| 167 | + pinned: true, |
| 168 | + meta: { description: 'bar' }, |
| 169 | + updatedAt: new Date(), |
| 170 | + }; |
| 171 | + |
| 172 | + await service.updateSession('123', mockData); |
| 173 | + |
| 174 | + expect(lambdaClient.session.updateSession.mutate).toBeCalledWith({ |
| 175 | + id: '123', |
| 176 | + value: { |
| 177 | + groupId: null, |
| 178 | + pinned: true, |
| 179 | + description: 'bar', |
| 180 | + updatedAt: mockData.updatedAt, |
| 181 | + }, |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('getSessionConfig should call lambdaClient', async () => { |
| 186 | + await service.getSessionConfig('123'); |
| 187 | + expect(lambdaClient.agent.getAgentConfig.query).toBeCalledWith({ sessionId: '123' }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('updateSessionConfig should call lambdaClient', async () => { |
| 191 | + const config = { model: 'gpt-4' }; |
| 192 | + const signal = new AbortController().signal; |
| 193 | + await service.updateSessionConfig('123', config, signal); |
| 194 | + expect(lambdaClient.session.updateSessionConfig.mutate).toBeCalledWith( |
| 195 | + { id: '123', value: config }, |
| 196 | + { signal }, |
| 197 | + ); |
| 198 | + }); |
| 199 | + |
| 200 | + it('getSessionsByType should call lambdaClient', async () => { |
| 201 | + await service.getSessionsByType('all'); |
| 202 | + expect(lambdaClient.session.getSessions.query).toBeCalledWith({}); |
| 203 | + }); |
| 204 | + |
| 205 | + it('searchSessions should call lambdaClient with keywords', async () => { |
| 206 | + await service.searchSessions('test'); |
| 207 | + expect(lambdaClient.session.searchSessions.query).toBeCalledWith({ keywords: 'test' }); |
| 208 | + }); |
| 209 | + |
| 210 | + it('removeSession should call lambdaClient', async () => { |
| 211 | + await service.removeSession('123'); |
| 212 | + expect(lambdaClient.session.removeSession.mutate).toBeCalledWith({ id: '123' }); |
| 213 | + }); |
| 214 | + |
| 215 | + it('removeAllSessions should call lambdaClient', async () => { |
| 216 | + await service.removeAllSessions(); |
| 217 | + expect(lambdaClient.session.removeAllSessions.mutate).toBeCalled(); |
| 218 | + }); |
| 219 | + |
| 220 | + it('createSessionGroup should call lambdaClient', async () => { |
| 221 | + await service.createSessionGroup('Test Group', 1); |
| 222 | + expect(lambdaClient.sessionGroup.createSessionGroup.mutate).toBeCalledWith({ |
| 223 | + name: 'Test Group', |
| 224 | + sort: 1, |
| 225 | + }); |
| 226 | + }); |
| 227 | + |
| 228 | + it('getSessionGroups should call lambdaClient', async () => { |
| 229 | + await service.getSessionGroups(); |
| 230 | + expect(lambdaClient.sessionGroup.getSessionGroup.query).toBeCalled(); |
| 231 | + }); |
| 232 | + |
| 233 | + it('removeSessionGroup should call lambdaClient', async () => { |
| 234 | + await service.removeSessionGroup('123', true); |
| 235 | + expect(lambdaClient.sessionGroup.removeSessionGroup.mutate).toBeCalledWith({ |
| 236 | + id: '123', |
| 237 | + removeChildren: true, |
| 238 | + }); |
| 239 | + }); |
| 240 | + |
| 241 | + it('updateSessionGroup should call lambdaClient', async () => { |
| 242 | + const value = { name: 'Updated Group' }; |
| 243 | + await service.updateSessionGroup('123', value); |
| 244 | + expect(lambdaClient.sessionGroup.updateSessionGroup.mutate).toBeCalledWith({ |
| 245 | + id: '123', |
| 246 | + value, |
| 247 | + }); |
| 248 | + }); |
| 249 | + |
| 250 | + it('updateSessionGroupOrder should call lambdaClient', async () => { |
| 251 | + const sortMap = [{ id: '123', sort: 1 }]; |
| 252 | + await service.updateSessionGroupOrder(sortMap); |
| 253 | + expect(lambdaClient.sessionGroup.updateSessionGroupOrder.mutate).toBeCalledWith({ sortMap }); |
| 254 | + }); |
| 255 | +}); |
0 commit comments