import { IntegrationTestDatabase } from '@d8d/shared-test-util'; import { File } from '../../src/entities'; /** * 集成测试断言工具 */ export class IntegrationTestAssertions { /** * 断言响应状态码 */ static expectStatus(response: { status: number }, expectedStatus: number): void { if (response.status !== expectedStatus) { throw new Error(`Expected status ${expectedStatus}, but got ${response.status}`); } } /** * 断言响应包含特定字段 */ static expectResponseToHave(response: { data: any }, expectedFields: Record): void { for (const [key, value] of Object.entries(expectedFields)) { if (response.data[key] !== value) { throw new Error(`Expected field ${key} to be ${value}, but got ${response.data[key]}`); } } } /** * 断言响应包含特定结构 */ static expectResponseStructure(response: { data: any }, structure: Record): void { for (const key of Object.keys(structure)) { if (!(key in response.data)) { throw new Error(`Expected response to have key: ${key}`); } } } /** * 断言文件存在于数据库中 */ static async expectFileToExist(name: string): Promise { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) { throw new Error('Database not initialized'); } const fileRepository = dataSource.getRepository(File); const file = await fileRepository.findOne({ where: { name } }); if (!file) { throw new Error(`Expected file ${name} to exist in database`); } } /** * 断言文件不存在于数据库中 */ static async expectFileNotToExist(name: string): Promise { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) { throw new Error('Database not initialized'); } const fileRepository = dataSource.getRepository(File); const file = await fileRepository.findOne({ where: { name } }); if (file) { throw new Error(`Expected file ${name} not to exist in database`); } } /** * 断言文件存在于数据库中(通过ID) */ static async expectFileToExistById(id: number): Promise { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) { throw new Error('Database not initialized'); } const fileRepository = dataSource.getRepository(File); const file = await fileRepository.findOne({ where: { id } }); if (!file) { throw new Error(`Expected file with ID ${id} to exist in database`); } } /** * 断言文件不存在于数据库中(通过ID) */ static async expectFileNotToExistById(id: number): Promise { const dataSource = await IntegrationTestDatabase.getDataSource(); if (!dataSource) { throw new Error('Database not initialized'); } const fileRepository = dataSource.getRepository(File); const file = await fileRepository.findOne({ where: { id } }); if (file) { throw new Error(`Expected file with ID ${id} not to exist in database`); } } }