|
| 1 | +import { DeepPartial } from 'utility-types'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { lambdaClient } from '@/libs/trpc/client'; |
| 5 | +import { UserInitializationState, UserPreference } from '@/types/user'; |
| 6 | +import { UserSettings } from '@/types/user/settings'; |
| 7 | + |
| 8 | +import { ServerService } from './server'; |
| 9 | + |
| 10 | +vi.mock('@/libs/trpc/client', () => ({ |
| 11 | + lambdaClient: { |
| 12 | + user: { |
| 13 | + getUserRegistrationDuration: { |
| 14 | + query: vi.fn(), |
| 15 | + }, |
| 16 | + getUserState: { |
| 17 | + query: vi.fn(), |
| 18 | + }, |
| 19 | + getUserSSOProviders: { |
| 20 | + query: vi.fn(), |
| 21 | + }, |
| 22 | + unlinkSSOProvider: { |
| 23 | + mutate: vi.fn(), |
| 24 | + }, |
| 25 | + makeUserOnboarded: { |
| 26 | + mutate: vi.fn(), |
| 27 | + }, |
| 28 | + updatePreference: { |
| 29 | + mutate: vi.fn(), |
| 30 | + }, |
| 31 | + updateGuide: { |
| 32 | + mutate: vi.fn(), |
| 33 | + }, |
| 34 | + updateSettings: { |
| 35 | + mutate: vi.fn(), |
| 36 | + }, |
| 37 | + resetSettings: { |
| 38 | + mutate: vi.fn(), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | +})); |
| 43 | + |
| 44 | +describe('ServerService', () => { |
| 45 | + const service = new ServerService(); |
| 46 | + |
| 47 | + it('should get user registration duration', async () => { |
| 48 | + const mockData = { |
| 49 | + createdAt: '2023-01-01', |
| 50 | + duration: 100, |
| 51 | + updatedAt: '2023-01-02', |
| 52 | + }; |
| 53 | + vi.mocked(lambdaClient.user.getUserRegistrationDuration.query).mockResolvedValue(mockData); |
| 54 | + |
| 55 | + const result = await service.getUserRegistrationDuration(); |
| 56 | + expect(result).toEqual(mockData); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should get user state', async () => { |
| 60 | + const mockState: UserInitializationState = { |
| 61 | + isOnboard: true, |
| 62 | + preference: { |
| 63 | + telemetry: true, |
| 64 | + }, |
| 65 | + settings: {}, |
| 66 | + }; |
| 67 | + vi.mocked(lambdaClient.user.getUserState.query).mockResolvedValue(mockState); |
| 68 | + |
| 69 | + const result = await service.getUserState(); |
| 70 | + expect(result).toEqual(mockState); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should get user SSO providers', async () => { |
| 74 | + const mockProviders = [ |
| 75 | + { |
| 76 | + provider: 'google', |
| 77 | + providerAccountId: '123', |
| 78 | + userId: 'user1', |
| 79 | + type: 'oauth' as const, |
| 80 | + access_token: 'token', |
| 81 | + token_type: 'bearer' as const, |
| 82 | + expires_at: 123, |
| 83 | + scope: 'email profile', |
| 84 | + }, |
| 85 | + ]; |
| 86 | + vi.mocked(lambdaClient.user.getUserSSOProviders.query).mockResolvedValue(mockProviders); |
| 87 | + |
| 88 | + const result = await service.getUserSSOProviders(); |
| 89 | + expect(result).toEqual(mockProviders); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should unlink SSO provider', async () => { |
| 93 | + const provider = 'google'; |
| 94 | + const providerAccountId = '123'; |
| 95 | + await service.unlinkSSOProvider(provider, providerAccountId); |
| 96 | + |
| 97 | + expect(lambdaClient.user.unlinkSSOProvider.mutate).toHaveBeenCalledWith({ |
| 98 | + provider, |
| 99 | + providerAccountId, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should make user onboarded', async () => { |
| 104 | + await service.makeUserOnboarded(); |
| 105 | + expect(lambdaClient.user.makeUserOnboarded.mutate).toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should update user preference', async () => { |
| 109 | + const preference: Partial<UserPreference> = { |
| 110 | + telemetry: true, |
| 111 | + useCmdEnterToSend: true, |
| 112 | + }; |
| 113 | + await service.updatePreference(preference); |
| 114 | + expect(lambdaClient.user.updatePreference.mutate).toHaveBeenCalledWith(preference); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should update user guide', async () => { |
| 118 | + const guide = { |
| 119 | + moveSettingsToAvatar: true, |
| 120 | + topic: false, |
| 121 | + uploadFileInKnowledgeBase: true, |
| 122 | + }; |
| 123 | + await service.updateGuide(guide); |
| 124 | + expect(lambdaClient.user.updateGuide.mutate).toHaveBeenCalledWith(guide); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should update user settings', async () => { |
| 128 | + const settings: DeepPartial<UserSettings> = { |
| 129 | + defaultAgent: { |
| 130 | + config: { |
| 131 | + model: 'gpt-4', |
| 132 | + provider: 'openai', |
| 133 | + }, |
| 134 | + meta: { |
| 135 | + avatar: 'avatar', |
| 136 | + description: 'test agent', |
| 137 | + }, |
| 138 | + }, |
| 139 | + }; |
| 140 | + const signal = new AbortController().signal; |
| 141 | + await service.updateUserSettings(settings, signal); |
| 142 | + expect(lambdaClient.user.updateSettings.mutate).toHaveBeenCalledWith(settings, { signal }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should reset user settings', async () => { |
| 146 | + await service.resetUserSettings(); |
| 147 | + expect(lambdaClient.user.resetSettings.mutate).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments