// // JXAPIServiceTests.m // AICityTests // // Feature: 010-ui-ios // API服务单元测试 - Phase 1 API层 // #import #import "JXAPIService.h" #import "JXNetworkManager.h" @interface JXAPIServiceTests : XCTestCase @property (nonatomic, strong) JXAPIService *apiService; @end @implementation JXAPIServiceTests - (void)setUp { [super setUp]; self.apiService = [[JXAPIService alloc] init]; } - (void)tearDown { self.apiService = nil; [super tearDown]; } #pragma mark - 内容相关API测试 - (void)testGetDramaList { XCTestExpectation *expectation = [self expectationWithDescription:@"Get drama list"]; [self.apiService getDramaListWithCategory:@"romance" page:1 pageSize:20 success:^(id responseObject) { XCTAssertNotNil(responseObject); XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]]); NSLog(@"✅ 获取短剧列表成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { XCTFail(@"获取短剧列表失败: %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testGetDramaDetail { XCTestExpectation *expectation = [self expectationWithDescription:@"Get drama detail"]; [self.apiService getDramaDetailWithDramaId:@"test_drama_001" success:^(id responseObject) { XCTAssertNotNil(responseObject); XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]]); NSDictionary *data = responseObject[@"data"]; XCTAssertNotNil(data); NSLog(@"✅ 获取短剧详情成功: %@", data); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 获取短剧详情失败(预期可能失败): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testSearchDramas { XCTestExpectation *expectation = [self expectationWithDescription:@"Search dramas"]; [self.apiService searchDramasWithKeyword:@"爱情" page:1 pageSize:20 success:^(id responseObject) { XCTAssertNotNil(responseObject); NSLog(@"✅ 搜索短剧成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 搜索短剧失败: %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } #pragma mark - 交互相关API测试 - (void)testLikeDrama { XCTestExpectation *expectation = [self expectationWithDescription:@"Like drama"]; [self.apiService likeDramaWithDramaId:@"test_drama_001" success:^(id responseObject) { XCTAssertNotNil(responseObject); NSLog(@"✅ 点赞短剧成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 点赞短剧失败(可能需要登录): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testFavoriteDrama { XCTestExpectation *expectation = [self expectationWithDescription:@"Favorite drama"]; [self.apiService favoriteDramaWithDramaId:@"test_drama_001" success:^(id responseObject) { XCTAssertNotNil(responseObject); NSLog(@"✅ 收藏短剧成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 收藏短剧失败(可能需要登录): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } #pragma mark - 评论相关API测试 - (void)testGetComments { XCTestExpectation *expectation = [self expectationWithDescription:@"Get comments"]; [self.apiService getCommentsWithDramaId:@"test_drama_001" page:1 pageSize:20 success:^(id responseObject) { XCTAssertNotNil(responseObject); NSLog(@"✅ 获取评论列表成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 获取评论列表失败: %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testSubmitComment { XCTestExpectation *expectation = [self expectationWithDescription:@"Submit comment"]; [self.apiService submitCommentWithDramaId:@"test_drama_001" content:@"这部剧太好看了!" success:^(id responseObject) { XCTAssertNotNil(responseObject); NSLog(@"✅ 提交评论成功: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 提交评论失败(可能需要登录): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } #pragma mark - 合集相关API测试 - (void)testGetEpisodes { XCTestExpectation *expectation = [self expectationWithDescription:@"Get episodes"]; [self.apiService getEpisodesWithDramaId:@"test_drama_001" success:^(id responseObject) { XCTAssertNotNil(responseObject); if ([responseObject isKindOfClass:[NSArray class]]) { NSArray *episodes = (NSArray *)responseObject; NSLog(@"✅ 获取剧集列表成功,共%lu集", (unsigned long)episodes.count); } [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 获取剧集列表失败: %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } #pragma mark - API并发测试 - (void)testConcurrentAPIRequests { XCTestExpectation *expectation1 = [self expectationWithDescription:@"Request 1"]; XCTestExpectation *expectation2 = [self expectationWithDescription:@"Request 2"]; XCTestExpectation *expectation3 = [self expectationWithDescription:@"Request 3"]; // 同时发起3个API请求 [self.apiService getDramaListWithCategory:@"romance" page:1 pageSize:10 success:^(id responseObject) { NSLog(@"✅ 并发请求1完成"); [expectation1 fulfill]; } failure:^(NSError *error) { [expectation1 fulfill]; }]; [self.apiService getDramaListWithCategory:@"urban" page:1 pageSize:10 success:^(id responseObject) { NSLog(@"✅ 并发请求2完成"); [expectation2 fulfill]; } failure:^(NSError *error) { [expectation2 fulfill]; }]; [self.apiService searchDramasWithKeyword:@"测试" page:1 pageSize:10 success:^(id responseObject) { NSLog(@"✅ 并发请求3完成"); [expectation3 fulfill]; } failure:^(NSError *error) { [expectation3 fulfill]; }]; [self waitForExpectationsWithTimeout:15.0 handler:nil]; } #pragma mark - 网络错误处理测试 - (void)testInvalidDramaId { XCTestExpectation *expectation = [self expectationWithDescription:@"Invalid drama ID"]; [self.apiService getDramaDetailWithDramaId:@"invalid_id_999999" success:^(id responseObject) { NSLog(@"⚠️ 意外成功(可能返回404或空数据): %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"✅ 正确处理无效ID: %@", error.localizedDescription); XCTAssertNotNil(error); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testEmptySearchKeyword { XCTestExpectation *expectation = [self expectationWithDescription:@"Empty keyword"]; [self.apiService searchDramasWithKeyword:@"" page:1 pageSize:20 success:^(id responseObject) { NSLog(@"✅ 空关键词搜索完成: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"✅ 空关键词搜索失败(预期行为): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } #pragma mark - 性能测试 - (void)testPerformanceGetDramaList { [self measureBlock:^{ XCTestExpectation *expectation = [self expectationWithDescription:@"Performance test"]; [self.apiService getDramaListWithCategory:@"all" page:1 pageSize:20 success:^(id responseObject) { [expectation fulfill]; } failure:^(NSError *error) { [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:5.0 handler:nil]; }]; } #pragma mark - 参数验证测试 - (void)testPaginationParameters { XCTestExpectation *expectation = [self expectationWithDescription:@"Pagination test"]; // 测试第2页,每页10条 [self.apiService getDramaListWithCategory:@"all" page:2 pageSize:10 success:^(id responseObject) { NSLog(@"✅ 分页参数测试成功"); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 分页参数测试失败: %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } - (void)testLargePageSize { XCTestExpectation *expectation = [self expectationWithDescription:@"Large page size"]; // 测试大页码(可能被服务器限制) [self.apiService getDramaListWithCategory:@"all" page:1 pageSize:100 success:^(id responseObject) { NSLog(@"✅ 大页码测试完成: %@", responseObject); [expectation fulfill]; } failure:^(NSError *error) { NSLog(@"⚠️ 大页码被服务器拒绝(预期行为): %@", error.localizedDescription); [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:10.0 handler:nil]; } @end