import 'package:flutter_test/flutter_test.dart'; import 'package:get_it/get_it.dart'; import 'package:superport/models/equipment_unified_model.dart'; import 'package:superport/services/equipment_service.dart'; import 'package:superport/services/company_service.dart'; import 'package:superport/services/warehouse_service.dart'; import 'test_helper.dart'; void main() { late EquipmentService equipmentService; late CompanyService companyService; late WarehouseService warehouseService; String? authToken; int? createdEquipmentId; int? testCompanyId; int? testWarehouseId; setUpAll(() async { await RealApiTestHelper.setupTestEnvironment(); // 로그인하여 인증 토큰 획득 authToken = await RealApiTestHelper.loginAndGetToken(); expect(authToken, isNotNull, reason: '로그인에 실패했습니다'); // 서비스 가져오기 equipmentService = GetIt.instance(); companyService = GetIt.instance(); warehouseService = GetIt.instance(); // 테스트용 회사 가져오기 final companies = await companyService.getCompanies(page: 1, perPage: 1); if (companies.isNotEmpty) { testCompanyId = companies.first.id; // 테스트용 창고 가져오기 final warehouses = await warehouseService.getWarehouseLocations( page: 1, perPage: 1, ); if (warehouses.isNotEmpty) { testWarehouseId = warehouses.first.id; } } }); tearDownAll(() async { await RealApiTestHelper.teardownTestEnvironment(); }); group('Equipment CRUD API 테스트', skip: 'Real API tests - skipping in CI', () { test('장비 목록 조회', () async { final equipments = await equipmentService.getEquipments( page: 1, perPage: 10, ); expect(equipments, isNotNull); expect(equipments, isA>()); if (equipments.isNotEmpty) { final firstEquipment = equipments.first; expect(firstEquipment.id, isNotNull); expect(firstEquipment.name, isNotEmpty); } }); test('장비 생성', () async { if (testCompanyId == null || testWarehouseId == null) { // 장비를 생성할 회사 또는 창고가 없습니다 return; } final newEquipment = Equipment( manufacturer: 'Integration Test Manufacturer', name: 'Integration Test Equipment \${DateTime.now().millisecondsSinceEpoch}', category: 'IT', subCategory: 'Computer', subSubCategory: 'Laptop', serialNumber: 'SN-\${DateTime.now().millisecondsSinceEpoch}', quantity: 1, inDate: DateTime.now(), remark: '통합 테스트용 장비', ); final createdEquipment = await equipmentService.createEquipment(newEquipment); expect(createdEquipment, isNotNull); expect(createdEquipment.id, isNotNull); expect(createdEquipment.name, equals(newEquipment.name)); expect(createdEquipment.serialNumber, equals(newEquipment.serialNumber)); createdEquipmentId = createdEquipment.id; }); test('장비 상세 조회', () async { if (createdEquipmentId == null) { // 장비 목록에서 첫 번째 장비 ID 사용 final equipments = await equipmentService.getEquipments(page: 1, perPage: 1); if (equipments.isEmpty) { // 조회할 장비가 없습니다 return; } createdEquipmentId = equipments.first.id; } final equipment = await equipmentService.getEquipment(createdEquipmentId!); expect(equipment, isNotNull); expect(equipment.id, equals(createdEquipmentId)); expect(equipment.name, isNotEmpty); }); test('장비 정보 수정', () async { if (createdEquipmentId == null) { // 수정할 장비가 없습니다 return; } // 먼저 현재 장비 정보 조회 final currentEquipment = await equipmentService.getEquipment(createdEquipmentId!); // 수정할 정보 final updatedEquipment = Equipment( id: currentEquipment.id, manufacturer: currentEquipment.manufacturer, name: '\${currentEquipment.name} - Updated', category: currentEquipment.category, subCategory: currentEquipment.subCategory, subSubCategory: currentEquipment.subSubCategory, serialNumber: currentEquipment.serialNumber, quantity: currentEquipment.quantity, inDate: currentEquipment.inDate, remark: 'Updated equipment', ); final result = await equipmentService.updateEquipment(createdEquipmentId!, updatedEquipment); expect(result, isNotNull); expect(result.id, equals(createdEquipmentId)); expect(result.name, contains('Updated')); }); test('장비 상태별 필터링', () async { // 입고 상태 장비 조회 final inStockEquipments = await equipmentService.getEquipments( page: 1, perPage: 10, status: 'I', // 입고 ); expect(inStockEquipments, isNotNull); expect(inStockEquipments, isA>()); // 출고 상태 장비 조회 final outStockEquipments = await equipmentService.getEquipments( page: 1, perPage: 10, status: 'O', // 출고 ); expect(outStockEquipments, isNotNull); expect(outStockEquipments, isA>()); }); test('회사별 장비 조회', () async { if (testCompanyId == null) { // 테스트할 회사가 없습니다 return; } final companyEquipments = await equipmentService.getEquipments( page: 1, perPage: 10, companyId: testCompanyId, ); expect(companyEquipments, isNotNull); expect(companyEquipments, isA>()); }); test('창고별 장비 조회', () async { if (testWarehouseId == null) { // 테스트할 창고가 없습니다 return; } final warehouseEquipments = await equipmentService.getEquipments( page: 1, perPage: 10, warehouseLocationId: testWarehouseId, ); expect(warehouseEquipments, isNotNull); expect(warehouseEquipments, isA>()); }); test('장비 삭제', () async { if (createdEquipmentId == null) { // 삭제할 장비가 없습니다 return; } // 삭제 실행 await equipmentService.deleteEquipment(createdEquipmentId!); // 삭제 확인 (404 에러 예상) try { await equipmentService.getEquipment(createdEquipmentId!); fail('삭제된 장비가 여전히 조회됩니다'); } catch (e) { // 삭제 성공 - 404 에러가 발생해야 함 expect(e.toString(), isNotEmpty); } }); test('잘못된 ID로 장비 조회 시 에러', () async { try { await equipmentService.getEquipment(999999); fail('존재하지 않는 장비가 조회되었습니다'); } catch (e) { // 에러가 발생해야 정상 expect(e.toString(), isNotEmpty); } }); test('필수 정보 없이 장비 생성 시 에러', () async { try { final invalidEquipment = Equipment( manufacturer: '', name: '', // 빈 이름 category: '', subCategory: '', subSubCategory: '', quantity: 0, ); await equipmentService.createEquipment(invalidEquipment); fail('잘못된 데이터로 장비가 생성되었습니다'); } catch (e) { // 에러가 발생해야 정상 expect(e.toString(), isNotEmpty); } }); test('중복 시리얼 번호로 장비 생성 시 에러', () async { if (testCompanyId == null || testWarehouseId == null) { // 테스트할 회사 또는 창고가 없습니다 return; } // 기존 장비의 시리얼 번호 가져오기 final equipments = await equipmentService.getEquipments(page: 1, perPage: 1); if (equipments.isEmpty || equipments.first.serialNumber == null) { // 중복 테스트할 시리얼 번호가 없습니다 return; } try { final duplicateEquipment = Equipment( manufacturer: 'Test Manufacturer', name: 'Duplicate Serial Equipment', category: 'IT', subCategory: 'Computer', subSubCategory: 'Laptop', quantity: 1, serialNumber: equipments.first.serialNumber, // 중복 시리얼 번호 ); await equipmentService.createEquipment(duplicateEquipment); fail('중복 시리얼 번호로 장비가 생성되었습니다'); } catch (e) { // 에러가 발생해야 정상 expect(e.toString(), isNotEmpty); } }); }); }