前段
Change-Id: I718d4d07ea03c6d2b6bcbd4d426c5d1af2201bf4
diff --git a/src/api/torrent.test.js b/src/api/torrent.test.js
new file mode 100644
index 0000000..c4de6c5
--- /dev/null
+++ b/src/api/torrent.test.js
@@ -0,0 +1,58 @@
+import MockAdapter from 'axios-mock-adapter';
+import { api } from './auth'; // 添加api导入
+import { createTorrent, getTorrents, getTorrentDetail, likeTorrent, addTorrentComment } from './torrent';
+
+describe('种子资源API', () => {
+ let mockAxios;
+
+ beforeEach(() => {
+ mockAxios = new MockAdapter(api);
+ });
+
+ afterEach(() => {
+ mockAxios.restore();
+ });
+
+ describe('createTorrent - 创建种子', () => {
+ it('应该发送完整的种子数据', async () => {
+ const torrentData = {
+ torrentName: '测试种子',
+ description: '描述内容',
+ username: 'user1',
+ category: '电影',
+ region: '美国',
+ resolution: '1080p',
+ subtitle: '中文字幕',
+ filePath: '/path/to/file'
+ };
+ mockAxios.onPost('/torrent', torrentData).reply(201, { code: 201 });
+
+ const response = await createTorrent(...Object.values(torrentData));
+ expect(response.status).toBe(201);
+ });
+ });
+
+ describe('getTorrentDetail - 获取种子详情', () => {
+ it('应该规范化返回的数据结构', async () => {
+ const mockData = {
+ data: {
+ post: { id: '123', name: '测试种子' },
+ comments: [{ id: '1', content: '评论1' }]
+ }
+ };
+ mockAxios.onGet('/torrent/123').reply(200, mockData);
+
+ const response = await getTorrentDetail('123');
+ expect(response.data.data.torrent.name).toBe('测试种子');
+ expect(response.data.data.comments).toHaveLength(1);
+ });
+ });
+
+ describe('likeTorrent - 点赞种子', () => {
+ it('应该成功发送点赞请求', async () => {
+ mockAxios.onPost('/torrent/t123/like').reply(200, { code: 200 });
+ const response = await likeTorrent('t123');
+ expect(response.status).toBe(200);
+ });
+ });
+});
\ No newline at end of file