import { APIRequestContext } from '@playwright/test'; export class TestDataCleaner { private request: APIRequestContext; private baseURL: string; constructor(request: APIRequestContext, baseURL: string = 'http://localhost:8080') { this.request = request; this.baseURL = baseURL; } async login(username: string = 'admin', password: string = 'Test@123'): Promise { const response = await this.request.post(`${this.baseURL}/api/auth/login`, { data: { username, password }, }); if (!response.ok()) { throw new Error(`Login failed: ${response.status()}`); } const data = await response.json(); return data.token; } async cleanupUsers(token: string, preserveIds: number[] = [1, 2, 3, 4, 5, 6, 7]): Promise { console.log('🧹 清理测试用户数据...'); try { const response = await this.request.get(`${this.baseURL}/api/users`, { headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok()) { console.log('⚠️ 无法获取用户列表,跳过清理'); return; } const users = await response.json(); let deletedCount = 0; for (const user of users) { if (!preserveIds.includes(user.id)) { try { const deleteResponse = await this.request.delete(`${this.baseURL}/api/users/${user.id}`, { headers: { Authorization: `Bearer ${token}` }, }); if (deleteResponse.ok()) { deletedCount++; console.log(` ✅ 删除用户: ${user.username} (ID: ${user.id})`); } } catch (error) { console.log(` ⚠️ 无法删除用户 ${user.username}: ${error}`); } } } console.log(`✅ 用户清理完成,共删除 ${deletedCount} 个测试用户`); } catch (error) { console.log('⚠️ 用户清理失败:', error); } } async cleanupRoles(token: string, preserveIds: number[] = [1, 2, 3, 4, 5, 6, 7, 8]): Promise { console.log('🧹 清理测试角色数据...'); try { const response = await this.request.get(`${this.baseURL}/api/roles`, { headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok()) { console.log('⚠️ 无法获取角色列表,跳过清理'); return; } const roles = await response.json(); let deletedCount = 0; for (const role of roles) { if (!preserveIds.includes(role.id)) { try { const deleteResponse = await this.request.delete(`${this.baseURL}/api/roles/${role.id}`, { headers: { Authorization: `Bearer ${token}` }, }); if (deleteResponse.ok()) { deletedCount++; console.log(` ✅ 删除角色: ${role.roleName} (ID: ${role.id})`); } } catch (error) { console.log(` ⚠️ 无法删除角色 ${role.roleName}: ${error}`); } } } console.log(`✅ 角色清理完成,共删除 ${deletedCount} 个测试角色`); } catch (error) { console.log('⚠️ 角色清理失败:', error); } } async cleanupDictionaryData(token: string, preserveIds: number[] = [1, 2, 3, 4, 5, 6, 7, 8]): Promise { console.log('🧹 清理测试字典数据...'); try { const response = await this.request.get(`${this.baseURL}/api/dict/types`, { headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok()) { console.log('⚠️ 无法获取字典列表,跳过清理'); return; } const dictTypes = await response.json(); let deletedCount = 0; for (const dictType of dictTypes) { if (!preserveIds.includes(dictType.id)) { try { const deleteResponse = await this.request.delete(`${this.baseURL}/api/dict/types/${dictType.id}`, { headers: { Authorization: `Bearer ${token}` }, }); if (deleteResponse.ok()) { deletedCount++; console.log(` ✅ 删除字典: ${dictType.dictName} (ID: ${dictType.id})`); } } catch (error) { console.log(` ⚠️ 无法删除字典 ${dictType.dictName}: ${error}`); } } } console.log(`✅ 字典清理完成,共删除 ${deletedCount} 个测试字典`); } catch (error) { console.log('⚠️ 字典清理失败:', error); } } async cleanupSystemConfig(token: string, preserveIds: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9]): Promise { console.log('🧹 清理测试系统配置数据...'); try { const response = await this.request.get(`${this.baseURL}/api/config`, { headers: { Authorization: `Bearer ${token}` }, }); if (!response.ok()) { console.log('⚠️ 无法获取系统配置列表,跳过清理'); return; } const configs = await response.json(); let deletedCount = 0; for (const config of configs) { if (!preserveIds.includes(config.id)) { try { const deleteResponse = await this.request.delete(`${this.baseURL}/api/config/${config.id}`, { headers: { Authorization: `Bearer ${token}` }, }); if (deleteResponse.ok()) { deletedCount++; console.log(` ✅ 删除配置: ${config.configName} (ID: ${config.id})`); } } catch (error) { console.log(` ⚠️ 无法删除配置 ${config.configName}: ${error}`); } } } console.log(`✅ 系统配置清理完成,共删除 ${deletedCount} 个测试配置`); } catch (error) { console.log('⚠️ 系统配置清理失败:', error); } } async cleanupAll(token?: string): Promise { console.log('🧹 开始清理所有测试数据...'); try { const authToken = token || await this.login(); await this.cleanupUsers(authToken); await this.cleanupRoles(authToken); await this.cleanupDictionaryData(authToken); await this.cleanupSystemConfig(authToken); console.log('✅ 所有测试数据清理完成'); } catch (error) { console.error('❌ 测试数据清理失败:', error); throw error; } } }