blob: 3a929e8433478b2023eaed0157fbbf1f2d3847ea [file] [log] [blame]
DREWae420b22025-06-02 14:07:20 +08001// src/api/__tests__/personal.test.js
2import {
3 getUserInfo,
4 formatFileSize,
5 getDownloadQuota,
6 getDownloadProgress,
7 getUserTorrents,
8 deleteTorrent,
9 generateInviteCode,
10 getUserInviteCodes,
11 exchangeUpload,
12 updatePassword
13} from './personal';
14import { api } from './auth';
15
16// 模拟整个 auth 模块
17jest.mock('./auth');
18
19describe('Personal API', () => {
20 beforeEach(() => {
21 // 在每个测试前重置模拟
22 jest.clearAllMocks();
23 });
24
25 describe('getUserInfo', () => {
26 it('should return formatted user info when API call succeeds', async () => {
27 const mockUserData = {
28 username: 'testuser',
29 level: 3,
30 registTime: '2023-01-01T00:00:00Z',
31 magicPoints: 100,
32 upload: 1024,
33 download: 512,
34 shareRate: 2.0
35 };
36
37 api.get.mockResolvedValue({
38 data: {
39 code: 200,
40 data: mockUserData,
41 message: 'success'
42 }
43 });
44
45 const result = await getUserInfo();
46
47 expect(api.get).toHaveBeenCalledWith('/user/userInfo');
48 expect(result).toEqual({
49 username: 'testuser',
50 level: 3,
51 registTime: '2023-01-01',
52 magicPoints: 100,
53 upload: 1024,
54 download: 512,
55 shareRate: '2.00'
56 });
57 });
58
59 it('should throw error when API call fails', async () => {
60 api.get.mockResolvedValue({
61 data: {
62 code: 500,
63 message: 'Internal server error'
64 }
65 });
66
67 await expect(getUserInfo()).rejects.toThrow('Internal server error');
68 });
69 });
70
71 describe('formatFileSize', () => {
72 it('should format bytes correctly', () => {
73 expect(formatFileSize(500)).toBe('500 B');
74 expect(formatFileSize(1024)).toBe('1.00 KB');
75 expect(formatFileSize(2048)).toBe('2.00 KB');
76 expect(formatFileSize(1024 * 1024)).toBe('1.00 MB');
77 expect(formatFileSize(1024 * 1024 * 2.5)).toBe('2.50 MB');
78 expect(formatFileSize(1024 * 1024 * 1024)).toBe('1.00 GB');
79 expect(formatFileSize(1024 * 1024 * 1024 * 3.7)).toBe('3.70 GB');
80 });
81 });
82
83 describe('getDownloadQuota', () => {
84 it('should return download quota data when API call succeeds', async () => {
85 const mockData = {
86 total: 1073741824, // 1GB in bytes
87 used: 536870912, // 512MB
88 remaining: 536870912
89 };
90
91 api.get.mockResolvedValue({
92 data: {
93 code: 200,
94 data: mockData,
95 message: 'success'
96 }
97 });
98
99 const result = await getDownloadQuota();
100
101 expect(api.get).toHaveBeenCalledWith('/user/allowDownload');
102 expect(result).toEqual(mockData);
103 });
104
105 it('should throw error when API call fails', async () => {
106 api.get.mockResolvedValue({
107 data: {
108 code: 403,
109 message: 'Forbidden'
110 }
111 });
112
113 await expect(getDownloadQuota()).rejects.toThrow('Forbidden');
114 });
115 });
116
117 describe('getDownloadProgress', () => {
118 it('should return download progress when API call succeeds', async () => {
119 const mockProgresses = [
120 { id: 1, name: 'file1', progress: 50 },
121 { id: 2, name: 'file2', progress: 75 }
122 ];
123
124 api.get.mockResolvedValue({
125 data: {
126 code: 200,
127 data: { progresses: mockProgresses },
128 message: 'success'
129 }
130 });
131
132 const result = await getDownloadProgress();
133
134 expect(api.get).toHaveBeenCalledWith('/torrent/getProgress');
135 expect(result).toEqual(mockProgresses);
136 });
137
138 it('should throw error when API call fails', async () => {
139 api.get.mockResolvedValue({
140 data: {
141 code: 404,
142 message: 'Not found'
143 }
144 });
145
146 await expect(getDownloadProgress()).rejects.toThrow('Not found');
147 });
148 });
149
150 describe('getUserTorrents', () => {
151 it('should return user torrents with pagination when API call succeeds', async () => {
152 const mockResponse = {
153 records: [
154 {
155 torrent: { id: 1, name: 'torrent1', size: 1024 },
156 downloadCount: 10,
157 formattedSize: '1 KB'
158 }
159 ],
160 total: 1
161 };
162
163 api.get.mockResolvedValue({
164 data: {
165 code: 200,
166 data: mockResponse,
167 message: 'success'
168 }
169 });
170
171 const result = await getUserTorrents(1, 5);
172
173 expect(api.get).toHaveBeenCalledWith('/torrent/get/torrentMyself', {
174 params: { page: 1, size: 5 }
175 });
176 expect(result).toEqual({
177 records: [
178 {
179 id: 1,
180 name: 'torrent1',
181 size: 1024,
182 downloadCount: 10,
183 formattedSize: '1 KB'
184 }
185 ],
186 total: 1
187 });
188 });
189
190 it('should throw error when API call fails', async () => {
191 api.get.mockResolvedValue({
192 data: {
193 code: 500,
194 message: 'Server error'
195 }
196 });
197
198 await expect(getUserTorrents()).rejects.toThrow('Server error');
199 });
200 });
201
202 describe('deleteTorrent', () => {
203 it('should successfully delete torrent when API call succeeds', async () => {
204 const mockResponse = {
205 code: 200,
206 message: 'Deleted successfully'
207 };
208
209 api.delete.mockResolvedValue({
210 data: mockResponse
211 });
212
213 const result = await deleteTorrent(123);
214
215 expect(api.delete).toHaveBeenCalledWith('/torrent/deleteTorrent/123');
216 expect(result).toEqual(mockResponse);
217 });
218
219 it('should throw error when API call fails', async () => {
220 api.delete.mockResolvedValue({
221 data: {
222 code: 404,
223 message: 'Torrent not found'
224 }
225 });
226
227 await expect(deleteTorrent(123)).rejects.toThrow('Torrent not found');
228 });
229 });
230
231 describe('generateInviteCode', () => {
232 it('should return generated invite code when API call succeeds', async () => {
233 const mockCode = 'ABCD-EFGH-IJKL';
234
235 api.post.mockResolvedValue({
236 data: {
237 code: 200,
238 data: { inviteCode: mockCode },
239 message: 'success'
240 }
241 });
242
243 const result = await generateInviteCode();
244
245 expect(api.post).toHaveBeenCalledWith('/invitecode/generate');
246 expect(result).toBe(mockCode);
247 });
248
249 it('should throw error when API call fails', async () => {
250 api.post.mockResolvedValue({
251 data: {
252 code: 403,
253 message: 'Permission denied'
254 }
255 });
256
257 await expect(generateInviteCode()).rejects.toThrow('Permission denied');
258 });
259 });
260
261 describe('getUserInviteCodes', () => {
262 it('should return user invite codes when API call succeeds', async () => {
263 const mockCodes = ['CODE1', 'CODE2'];
264
265 api.get.mockResolvedValue({
266 data: {
267 code: 200,
268 data: { inviteCode: mockCodes },
269 message: 'success'
270 }
271 });
272
273 const result = await getUserInviteCodes();
274
275 expect(api.get).toHaveBeenCalledWith('/invitecode/userInviteCode');
276 expect(result).toEqual(mockCodes);
277 });
278
279 it('should throw error when API call fails', async () => {
280 api.get.mockResolvedValue({
281 data: {
282 code: 500,
283 message: 'Server error'
284 }
285 });
286
287 await expect(getUserInviteCodes()).rejects.toThrow('Server error');
288 });
289 });
290
291 describe('exchangeUpload', () => {
292 it('should successfully exchange magic points for upload when API call succeeds', async () => {
293 const mockResponse = {
294 code: 200,
295 message: 'Exchange successful'
296 };
297
298 api.post.mockResolvedValue({
299 data: mockResponse
300 });
301
302 const result = await exchangeUpload(100);
303
304 expect(api.post).toHaveBeenCalledWith('/user/exchangeUpload', {
305 magicPoint: 100
306 });
307 expect(result).toEqual(mockResponse);
308 });
309
310 it('should throw error when API call fails', async () => {
311 api.post.mockResolvedValue({
312 data: {
313 code: 400,
314 message: 'Not enough points'
315 }
316 });
317
318 await expect(exchangeUpload(100)).rejects.toThrow('Not enough points');
319 });
320 });
321
322 describe('updatePassword', () => {
323 it('should successfully update password when API call succeeds', async () => {
324 const mockResponse = {
325 code: 200,
326 message: 'Password updated'
327 };
328
329 api.put.mockResolvedValue({
330 data: mockResponse
331 });
332
333 const result = await updatePassword('oldPass', 'newPass');
334
335 expect(api.put).toHaveBeenCalledWith('/user/password', {
336 oldPassword: 'oldPass',
337 newPassword: 'newPass'
338 });
339 expect(result).toEqual(mockResponse);
340 });
341
342 it('should throw error when API call fails', async () => {
343 api.put.mockResolvedValue({
344 data: {
345 code: 401,
346 message: 'Invalid old password'
347 }
348 });
349
350 await expect(updatePassword('wrongPass', 'newPass'))
351 .rejects.toThrow('Invalid old password');
352 });
353 });
354});