Selaa lähdekoodia

🔧 fix(tests): fix TypeScript errors and test data setup

- Fix query parameter validation using filters field
- Add type checks for response data properties
- Fix file entity creation with required fields
- Update test descriptions and validation logic

🤖 Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 1 kuukausi sitten
vanhempi
sitoutus
78978913bc

+ 20 - 9
packages/goods-module/tests/integration/admin-goods-categories.integration.test.ts

@@ -292,10 +292,15 @@ describe('管理员商品分类管理API集成测试', () => {
 
       expect(response.status).toBe(200);
       const data = await response.json();
-      expect(Array.isArray(data.data)).toBe(true);
 
-      // 验证分类数量
-      expect(data.data.length).toBeGreaterThanOrEqual(3);
+      // 类型检查确保data属性存在
+      if ('data' in data && Array.isArray(data.data)) {
+        // 验证分类数量
+        expect(data.data.length).toBeGreaterThanOrEqual(3);
+      } else {
+        // 如果响应是错误格式,应该失败
+        expect(data).toHaveProperty('data');
+      }
     });
   });
 
@@ -326,7 +331,7 @@ describe('管理员商品分类管理API集成测试', () => {
 
       // 验证状态过滤
       const response = await client.index.$get({
-        query: { state: 1 }
+        query: { filters: JSON.stringify({ state: 1 }) }
       }, {
         headers: {
           'Authorization': `Bearer ${adminToken}`
@@ -336,12 +341,18 @@ describe('管理员商品分类管理API集成测试', () => {
       expect(response.status).toBe(200);
       const data = await response.json();
 
-      // 应该只返回可用状态的分类
-      const activeCategories = data.data.filter((category: any) => category.state === 1);
-      expect(activeCategories.length).toBeGreaterThan(0);
+      // 类型检查确保data属性存在
+      if ('data' in data && Array.isArray(data.data)) {
+        // 应该只返回可用状态的分类
+        const activeCategories = data.data.filter((category: any) => category.state === 1);
+        expect(activeCategories.length).toBeGreaterThan(0);
 
-      const inactiveCategories = data.data.filter((category: any) => category.state === 2);
-      expect(inactiveCategories.length).toBe(0);
+        const inactiveCategories = data.data.filter((category: any) => category.state === 2);
+        expect(inactiveCategories.length).toBe(0);
+      } else {
+        // 如果响应是错误格式,应该失败
+        expect(data).toHaveProperty('data');
+      }
     });
   });
 });

+ 23 - 11
packages/goods-module/tests/integration/admin-goods-routes.integration.test.ts

@@ -412,7 +412,7 @@ describe('管理员商品管理API集成测试', () => {
 
       // 验证状态过滤
       const response = await client.index.$get({
-        query: { state: 1 }
+        query: { filters: JSON.stringify({ state: 1 }) }
       }, {
         headers: {
           'Authorization': `Bearer ${adminToken}`
@@ -422,12 +422,18 @@ describe('管理员商品管理API集成测试', () => {
       expect(response.status).toBe(200);
       const data = await response.json();
 
-      // 应该只返回可用状态的商品
-      const activeGoodsInResponse = data.data.filter((goods: any) => goods.state === 1);
-      const inactiveGoodsInResponse = data.data.filter((goods: any) => goods.state === 2);
+      // 类型检查确保data属性存在
+      if ('data' in data && Array.isArray(data.data)) {
+        // 应该只返回可用状态的商品
+        const activeGoodsInResponse = data.data.filter((goods: any) => goods.state === 1);
+        const inactiveGoodsInResponse = data.data.filter((goods: any) => goods.state === 2);
 
-      expect(activeGoodsInResponse.length).toBeGreaterThan(0);
-      expect(inactiveGoodsInResponse.length).toBe(0);
+        expect(activeGoodsInResponse.length).toBeGreaterThan(0);
+        expect(inactiveGoodsInResponse.length).toBe(0);
+      } else {
+        // 如果响应是错误格式,应该失败
+        expect(data).toHaveProperty('data');
+      }
     });
   });
 
@@ -529,12 +535,18 @@ describe('管理员商品管理API集成测试', () => {
       expect(response.status).toBe(200);
       const data = await response.json();
 
-      // 验证返回所有用户的商品
-      const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
-      const adminGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testAdmin.id);
+      // 类型检查确保data属性存在
+      if ('data' in data && Array.isArray(data.data)) {
+        // 验证返回所有用户的商品
+        const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
+        const adminGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testAdmin.id);
 
-      expect(userGoodsInResponse.length).toBeGreaterThan(0);
-      expect(adminGoodsInResponse.length).toBeGreaterThan(0);
+        expect(userGoodsInResponse.length).toBeGreaterThan(0);
+        expect(adminGoodsInResponse.length).toBeGreaterThan(0);
+      } else {
+        // 如果响应是错误格式,应该失败
+        expect(data).toHaveProperty('data');
+      }
     });
   });
 });

+ 4 - 4
packages/goods-module/tests/integration/public-goods-random.integration.test.ts

@@ -199,9 +199,9 @@ describe('公开随机商品API集成测试', () => {
       }
     });
 
-    it('应该支持按商品类型过滤', async () => {
+    it('应该支持按分类过滤', async () => {
       const response = await client.index.$get({
-        query: { goodsType: 2 }
+        query: { categoryId: testCategory1.id }
       });
 
       expect(response.status).toBe(200);
@@ -210,9 +210,9 @@ describe('公开随机商品API集成测试', () => {
         const data = await response.json();
         expect(Array.isArray(data.data)).toBe(true);
 
-        // 验证只返回指定类的商品
+        // 验证只返回指定类的商品
         data.data.forEach((goods: any) => {
-          expect(goods.goodsType).toBe(2);
+          expect(goods.categoryId1).toBe(testCategory1.id);
           expect(goods.state).toBe(1);
         });
       }

+ 2 - 2
packages/goods-module/tests/integration/public-goods-routes.integration.test.ts

@@ -195,7 +195,7 @@ describe('公开商品API集成测试', () => {
 
     it('应该支持按分类过滤', async () => {
       const response = await client.index.$get({
-        query: { categoryId1: testCategory.id }
+        query: { filters: JSON.stringify({ categoryId1: testCategory.id }) }
       });
 
       expect(response.status).toBe(200);
@@ -214,7 +214,7 @@ describe('公开商品API集成测试', () => {
 
     it('应该支持按商品类型过滤', async () => {
       const response = await client.index.$get({
-        query: { goodsType: 2 }
+        query: { filters: JSON.stringify({ goodsType: 2 }) }
       });
 
       expect(response.status).toBe(200);

+ 29 - 5
packages/goods-module/tests/integration/user-goods-routes.integration.test.ts

@@ -23,6 +23,7 @@ describe('用户商品管理API集成测试', () => {
   let testCategory: GoodsCategory;
   let testSupplier: Supplier;
   let testMerchant: Merchant;
+  let testFile: File;
 
   beforeEach(async () => {
     // 创建测试客户端
@@ -100,6 +101,20 @@ describe('用户商品管理API集成测试', () => {
       createdBy: testUser.id
     });
     await merchantRepository.save(testMerchant);
+
+    // 创建测试文件
+    const fileRepository = dataSource.getRepository(File);
+    testFile = fileRepository.create({
+      name: 'test_image.jpg',
+      type: 'image/jpeg',
+      size: 102400,
+      path: 'images/test_image.jpg',
+      uploadUserId: testUser.id,
+      uploadTime: new Date(),
+      createdAt: new Date(),
+      updatedAt: new Date()
+    });
+    await fileRepository.save(testFile);
   });
 
   describe('GET /goods', () => {
@@ -118,6 +133,7 @@ describe('用户商品管理API集成测试', () => {
         goodsType: 1,
         supplierId: testSupplier.id,
         merchantId: testMerchant.id,
+        imageFileId: testFile.id,
         state: 1,
         stock: 100,
         lowestBuy: 1,
@@ -135,6 +151,7 @@ describe('用户商品管理API集成测试', () => {
         goodsType: 1,
         supplierId: testSupplier.id,
         merchantId: testMerchant.id,
+        imageFileId: testFile.id,
         state: 1,
         stock: 50,
         lowestBuy: 1,
@@ -153,6 +170,7 @@ describe('用户商品管理API集成测试', () => {
         goodsType: 1,
         supplierId: testSupplier.id,
         merchantId: testMerchant.id,
+        imageFileId: testFile.id,
         state: 1,
         stock: 30,
         lowestBuy: 1,
@@ -551,12 +569,18 @@ describe('用户商品管理API集成测试', () => {
       expect(response.status).toBe(200);
       const data = await response.json();
 
-      // 验证只返回测试用户的商品
-      const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
-      const otherUserGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === otherUser.id);
+      // 类型检查确保data属性存在
+      if ('data' in data && Array.isArray(data.data)) {
+        // 验证只返回测试用户的商品
+        const userGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === testUser.id);
+        const otherUserGoodsInResponse = data.data.filter((goods: any) => goods.createdBy === otherUser.id);
 
-      expect(userGoodsInResponse.length).toBeGreaterThan(0);
-      expect(otherUserGoodsInResponse.length).toBe(0);
+        expect(userGoodsInResponse.length).toBeGreaterThan(0);
+        expect(otherUserGoodsInResponse.length).toBe(0);
+      } else {
+        // 如果响应是错误格式,应该失败
+        expect(data).toHaveProperty('data');
+      }
     });
   });
 });