Sfoglia il codice sorgente

✅ test(integration): add passenger management API integration tests

- create passengers.integration.test.ts with CRUD operations tests
- add passenger creation, retrieval, update, deletion and search test cases
- include validation tests for passenger data and error handling
- add performance test for passenger list query response time

🔧 chore(scripts): update allowed bash commands in settings

- add "Bash(pnpm test:integration:*)" and "Bash(pnpm vitest run:*)" to allowed commands

♻️ refactor(test-utils): enhance test data factory and assertions

- add createTestPassenger method to TestDataFactory
- implement passenger existence assertions in IntegrationTestAssertions
- add passenger data generation utility functions
yourname 3 mesi fa
parent
commit
d64611c1b6

+ 3 - 1
.claude/settings.local.json

@@ -51,7 +51,9 @@
       "Bash(npx tsc:*)",
       "Bash(pnpm build:weapp:*)",
       "Bash(pnpm run build:weapp:*)",
-      "Bash(pnpm run build:client:*)"
+      "Bash(pnpm run build:client:*)",
+      "Bash(pnpm test:integration:*)",
+      "Bash(pnpm vitest run:*)"
     ],
     "deny": [],
     "ask": []

+ 498 - 0
tests/integration/server/admin/passengers.integration.test.ts

@@ -0,0 +1,498 @@
+import { describe, it, expect, beforeEach } from 'vitest';
+import { testClient } from 'hono/testing';
+import {
+  IntegrationTestDatabase,
+  setupIntegrationDatabaseHooks,
+  TestDataFactory
+} from '~/utils/server/integration-test-db';
+import { IntegrationTestAssertions } from '~/utils/server/integration-test-utils';
+import { adminPassengersRoutesExport } from '@/server/api';
+import { AuthService } from '@/server/modules/auth/auth.service';
+import { UserService } from '@/server/modules/users/user.service';
+import { IdType } from '@/server/modules/passengers/passenger.entity';
+
+// 设置集成测试钩子
+setupIntegrationDatabaseHooks()
+
+describe('乘客管理API集成测试', () => {
+  let client: ReturnType<typeof testClient<typeof adminPassengersRoutesExport>>['api']['v1']['admin'];
+  let testToken: string;
+
+  beforeEach(async () => {
+    // 创建测试客户端
+    client = testClient(adminPassengersRoutesExport).api.v1.admin;
+
+    // 创建测试用户并生成token
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+
+    const userService = new UserService(dataSource);
+    const authService = new AuthService(userService);
+
+    // 确保admin用户存在
+    const user = await authService.ensureAdminExists();
+
+    // 生成admin用户的token
+    testToken = authService.generateToken(user);
+  });
+
+  describe('乘客创建测试', () => {
+    it('应该成功创建乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const testUser = await TestDataFactory.createTestUser(dataSource);
+
+      const passengerData = {
+        userId: testUser.id,
+        name: '测试乘客',
+        idType: IdType.ID_CARD,
+        idNumber: '110101199001011234',
+        phone: '13812345678',
+        isDefault: false
+      };
+
+      const response = await client.passengers.$post({
+        json: passengerData,
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      // 断言响应
+      expect(response.status).toBe(201);
+      if (response.status === 201) {
+        const responseData = await response.json();
+        expect(responseData).toHaveProperty('id');
+        expect(responseData.name).toBe(passengerData.name);
+        expect(responseData.idType).toBe(passengerData.idType);
+        expect(responseData.idNumber).toBe(passengerData.idNumber);
+        expect(responseData.phone).toBe(passengerData.phone);
+        expect(responseData.isDefault).toBe(passengerData.isDefault);
+
+        // 断言数据库中存在乘客
+        await IntegrationTestAssertions.expectPassengerToExist(responseData.id);
+      }
+    });
+
+    it('应该成功创建默认乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const testUser = await TestDataFactory.createTestUser(dataSource);
+
+      const passengerData = {
+        userId: testUser.id,
+        name: '默认乘客',
+        idType: IdType.PASSPORT,
+        idNumber: 'E12345678',
+        phone: '13987654321',
+        isDefault: true
+      };
+
+      const response = await client.passengers.$post({
+        json: passengerData,
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(201);
+      if (response.status === 201) {
+        const responseData = await response.json();
+        expect(responseData.isDefault).toBe(true);
+      }
+    });
+
+    it('应该拒绝创建无效证件类型的乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      const testUser = await TestDataFactory.createTestUser(dataSource);
+
+      const passengerData = {
+        userId: testUser.id,
+        name: '测试乘客',
+        idType: 'invalid_type' as any, // 无效类型
+        idNumber: '110101199001011234',
+        phone: '13812345678',
+        isDefault: false
+      };
+
+      const response = await client.passengers.$post({
+        json: passengerData,
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      // 应该返回验证错误
+      expect([400, 500]).toContain(response.status);
+    });
+  });
+
+  describe('乘客读取测试', () => {
+    it('应该成功获取乘客列表', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      // 创建几个测试乘客
+      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客1' });
+      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客2' });
+
+      const response = await client.passengers.$get({
+        query: {}
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      if (response.status !== 200) {
+        const errorData = await response.json();
+        console.debug('获取乘客列表失败:', errorData);
+      }
+      expect(response.status).toBe(200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(Array.isArray(responseData.data)).toBe(true);
+        expect(responseData.data.length).toBeGreaterThanOrEqual(2);
+      }
+    });
+
+    it('应该成功获取单个乘客详情', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
+        name: '测试乘客详情'
+      });
+
+      const response = await client.passengers[':id'].$get({
+        param: { id: testPassenger.id }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(responseData.id).toBe(testPassenger.id);
+        expect(responseData.name).toBe(testPassenger.name);
+        expect(responseData.idType).toBe(testPassenger.idType);
+        expect(responseData.idNumber).toBe(testPassenger.idNumber);
+        expect(responseData.phone).toBe(testPassenger.phone);
+      }
+    });
+
+    it('应该返回404当乘客不存在时', async () => {
+      const response = await client.passengers[':id'].$get({
+        param: { id: 999999 }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(404);
+      if (response.status === 404) {
+        const responseData = await response.json();
+        expect(responseData.message).toContain('资源不存在');
+      }
+    });
+  });
+
+  describe('乘客更新测试', () => {
+    it('应该成功更新乘客信息', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
+        name: '测试乘客更新'
+      });
+
+      const updateData = {
+        name: '更新后的乘客名称',
+        phone: '13987654321',
+        isDefault: true
+      };
+
+      const response = await client.passengers[':id'].$put({
+        param: { id: testPassenger.id },
+        json: updateData
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(responseData.name).toBe(updateData.name);
+        expect(responseData.phone).toBe(updateData.phone);
+        expect(responseData.isDefault).toBe(updateData.isDefault);
+      }
+
+      // 验证数据库中的更新
+      const getResponse = await client.passengers[':id'].$get({
+        param: { id: testPassenger.id }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+      if (getResponse.status === 200) {
+        expect(getResponse.status).toBe(200);
+        const getResponseData = await getResponse.json();
+        expect(getResponseData.name).toBe(updateData.name);
+        expect(getResponseData.phone).toBe(updateData.phone);
+      }
+    });
+
+    it('应该返回404当更新不存在的乘客时', async () => {
+      const updateData = {
+        name: '更新后的名称'
+      };
+
+      const response = await client.passengers[':id'].$put({
+        param: { id: 999999 },
+        json: updateData
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      expect(response.status).toBe(404);
+      if (response.status === 404) {
+        const responseData = await response.json();
+        expect(responseData.message).toContain('资源不存在');
+      }
+    });
+  });
+
+  describe('乘客删除测试', () => {
+    it('应该成功删除乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
+        name: '测试乘客删除'
+      });
+
+      const response = await client.passengers[':id'].$delete({
+        param: { id: testPassenger.id }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 204);
+
+      // 验证乘客已从数据库中删除
+      await IntegrationTestAssertions.expectPassengerNotToExist(testPassenger.id);
+
+      // 验证再次获取乘客返回404
+      const getResponse = await client.passengers[':id'].$get({
+        param: { id: testPassenger.id }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+      IntegrationTestAssertions.expectStatus(getResponse, 404);
+    });
+
+    it('应该返回404当删除不存在的乘客时', async () => {
+      const response = await client.passengers[':id'].$delete({
+        param: { id: 999999 }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 404);
+      if (response.status === 404) {
+        const responseData = await response.json();
+        expect(responseData.message).toContain('资源不存在');
+      }
+    });
+  });
+
+  describe('乘客搜索测试', () => {
+    it('应该能够按乘客姓名搜索乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      await TestDataFactory.createTestPassenger(dataSource, { name: '搜索乘客1', phone: '13811111111' });
+      await TestDataFactory.createTestPassenger(dataSource, { name: '搜索乘客2', phone: '13822222222' });
+      await TestDataFactory.createTestPassenger(dataSource, { name: '其他乘客', phone: '13833333333' });
+
+      const response = await client.passengers.$get({
+        query: { keyword: '搜索乘客' }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(Array.isArray(responseData.data)).toBe(true);
+        expect(responseData.data.length).toBe(2);
+
+        // 验证搜索结果包含正确的乘客
+        const names = responseData.data.map((passenger) => passenger.name);
+        expect(names).toContain('搜索乘客1');
+        expect(names).toContain('搜索乘客2');
+        expect(names).not.toContain('其他乘客');
+      }
+    });
+
+    it('应该能够按手机号搜索乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客1', phone: '13811112222' });
+      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客2', phone: '13811113333' });
+
+      const response = await client.passengers.$get({
+        query: { keyword: '1381111' }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(responseData.data.length).toBe(2);
+
+        const phones = responseData.data.map((passenger) => passenger.phone);
+        expect(phones).toContain('13811112222');
+        expect(phones).toContain('13811113333');
+      }
+    });
+
+    it('应该能够按证件号码搜索乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      await TestDataFactory.createTestPassenger(dataSource, {
+        name: '乘客1',
+        idNumber: '110101199001011111'
+      });
+      await TestDataFactory.createTestPassenger(dataSource, {
+        name: '乘客2',
+        idNumber: '110101199001012222'
+      });
+
+      const response = await client.passengers.$get({
+        query: { keyword: '11010119900101' }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(responseData.data.length).toBe(2);
+
+        const idNumbers = responseData.data.map((passenger) => passenger.idNumber);
+        expect(idNumbers).toContain('110101199001011111');
+        expect(idNumbers).toContain('110101199001012222');
+      }
+    });
+
+    it('应该能够按用户ID筛选乘客', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      const testUser1 = await TestDataFactory.createTestUser(dataSource);
+      const testUser2 = await TestDataFactory.createTestUser(dataSource);
+
+      await TestDataFactory.createTestPassenger(dataSource, {
+        name: '用户1的乘客1',
+        userId: testUser1.id
+      });
+      await TestDataFactory.createTestPassenger(dataSource, {
+        name: '用户1的乘客2',
+        userId: testUser1.id
+      });
+      await TestDataFactory.createTestPassenger(dataSource, {
+        name: '用户2的乘客',
+        userId: testUser2.id
+      });
+
+      const response = await client.passengers.$get({
+        query: { filters: JSON.stringify({ userId: testUser1.id }) }
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+
+      IntegrationTestAssertions.expectStatus(response, 200);
+      if (response.status === 200) {
+        const responseData = await response.json();
+        expect(responseData.data.length).toBe(2);
+
+        const names = responseData.data.map((passenger) => passenger.name);
+        expect(names).toContain('用户1的乘客1');
+        expect(names).toContain('用户1的乘客2');
+        expect(names).not.toContain('用户2的乘客');
+      }
+    });
+  });
+
+  describe('性能测试', () => {
+    it('乘客列表查询响应时间应小于200ms', async () => {
+      const dataSource = await IntegrationTestDatabase.getDataSource();
+      if (!dataSource) throw new Error('Database not initialized');
+
+      // 创建一些测试数据
+      for (let i = 0; i < 10; i++) {
+        await TestDataFactory.createTestPassenger(dataSource, {
+          name: `性能测试乘客_${i}`,
+          phone: `138${i.toString().padStart(8, '0')}`
+        });
+      }
+
+      const startTime = Date.now();
+      const response = await client.passengers.$get({
+        query: {}
+      },
+      {
+        headers: {
+          'Authorization': `Bearer ${testToken}`
+        }
+      });
+      const endTime = Date.now();
+      const responseTime = endTime - startTime;
+
+      IntegrationTestAssertions.expectStatus(response, 200);
+      expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
+    });
+  });
+});

+ 33 - 0
tests/utils/server/integration-test-db.ts

@@ -7,6 +7,7 @@ import { RouteEntity } from '@/server/modules/routes/route.entity';
 import { LocationEntity } from '@/server/modules/locations/location.entity';
 import { AreaEntity } from '@/server/modules/areas/area.entity';
 import { VehicleType } from '@/server/modules/routes/route.schema';
+import { Passenger, IdType } from '@/server/modules/passengers/passenger.entity';
 import { AppDataSource } from '@/server/data-source';
 
 /**
@@ -266,6 +267,38 @@ export class TestDataFactory {
     const route = routeRepository.create(routeData);
     return await routeRepository.save(route);
   }
+
+  /**
+   * 创建测试乘客数据
+   */
+  static createPassengerData(overrides: Partial<Passenger> = {}): Partial<Passenger> {
+    const timestamp = Date.now();
+    return {
+      name: `测试乘客_${timestamp}`,
+      idType: IdType.ID_CARD,
+      idNumber: `11010119900101${timestamp.toString().slice(-4)}`,
+      phone: `138${timestamp.toString().slice(-8)}`,
+      isDefault: false,
+      ...overrides
+    };
+  }
+
+  /**
+   * 在数据库中创建测试乘客
+   */
+  static async createTestPassenger(dataSource: DataSource, overrides: Partial<Passenger> = {}): Promise<Passenger> {
+    const passengerData = this.createPassengerData(overrides);
+    const passengerRepository = dataSource.getRepository(Passenger);
+
+    // 如果没有提供userId,自动创建一个测试用户
+    if (!passengerData.userId) {
+      const testUser = await this.createTestUser(dataSource);
+      passengerData.userId = testUser.id;
+    }
+
+    const passenger = passengerRepository.create(passengerData);
+    return await passengerRepository.save(passenger);
+  }
 }
 
 /**

+ 35 - 0
tests/utils/server/integration-test-utils.ts

@@ -4,6 +4,7 @@ import { UserEntity } from '@/server/modules/users/user.entity';
 import { ActivityEntity } from '@/server/modules/activities/activity.entity';
 import { RouteEntity } from '@/server/modules/routes/route.entity';
 import { LocationEntity } from '@/server/modules/locations/location.entity';
+import { Passenger } from '@/server/modules/passengers/passenger.entity';
 
 
 
@@ -177,4 +178,38 @@ export class IntegrationTestAssertions {
       throw new Error(`Expected location ${locationId} not to exist in database`);
     }
   }
+
+  /**
+   * 断言乘客存在于数据库中
+   */
+  static async expectPassengerToExist(passengerId: number): Promise<void> {
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+    if (!dataSource) {
+      throw new Error('Database not initialized');
+    }
+
+    const passengerRepository = dataSource.getRepository(Passenger);
+    const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
+
+    if (!passenger) {
+      throw new Error(`Expected passenger ${passengerId} to exist in database`);
+    }
+  }
+
+  /**
+   * 断言乘客不存在于数据库中
+   */
+  static async expectPassengerNotToExist(passengerId: number): Promise<void> {
+    const dataSource = await IntegrationTestDatabase.getDataSource();
+    if (!dataSource) {
+      throw new Error('Database not initialized');
+    }
+
+    const passengerRepository = dataSource.getRepository(Passenger);
+    const passenger = await passengerRepository.findOne({ where: { id: passengerId } });
+
+    if (passenger) {
+      throw new Error(`Expected passenger ${passengerId} not to exist in database`);
+    }
+  }
 }