JXIntegrationTests.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // JXIntegrationTests.m
  3. // AICityTests
  4. //
  5. // Feature: 003-ios-api-https
  6. // 剧星平台集成测试
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "JXPlaybackProgressStorage.h"
  10. #import "JXAPIService.h"
  11. #import "JXCountFormatter.h"
  12. @interface JXIntegrationTests : XCTestCase
  13. @property (nonatomic, strong) JXPlaybackProgressStorage *storage;
  14. @property (nonatomic, strong) JXAPIService *apiService;
  15. @end
  16. @implementation JXIntegrationTests
  17. - (void)setUp {
  18. [super setUp];
  19. self.storage = [JXPlaybackProgressStorage sharedStorage];
  20. self.apiService = [JXAPIService sharedService];
  21. // 清空所有数据
  22. [self.storage clearAll];
  23. }
  24. - (void)tearDown {
  25. [self.storage clearAll];
  26. [super tearDown];
  27. }
  28. #pragma mark - 存储与API集成测试
  29. - (void)testStorageAPIIntegration {
  30. // Given
  31. NSString *episodeId = @"integration_ep_001";
  32. NSString *dramaId = @"integration_drama_001";
  33. XCTestExpectation *expectation = [self expectationWithDescription:@"Storage API integration"];
  34. // When - 保存进度到本地
  35. [self.storage saveProgressWithEpisodeId:episodeId
  36. dramaId:dramaId
  37. position:45000
  38. duration:120000
  39. isCompleted:NO
  40. synced:NO
  41. completion:^(BOOL success, NSError *error) {
  42. XCTAssertTrue(success, @"本地保存应该成功");
  43. // Then - 验证本地数据
  44. JXPlaybackProgressModel *progress = [self.storage getProgressWithEpisodeId:episodeId];
  45. XCTAssertNotNil(progress, @"应该能获取到本地数据");
  46. XCTAssertEqual(progress.position, 45000);
  47. XCTAssertEqualWithAccuracy(progress.progress, 0.375f, 0.01f); // 45000/120000
  48. [expectation fulfill];
  49. }];
  50. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  51. }
  52. - (void)testMultipleEpisodesIntegration {
  53. // Given
  54. NSString *dramaId = @"multi_integration_drama";
  55. NSArray *episodes = @[@"ep001", @"ep002", @"ep003"];
  56. XCTestExpectation *expectation = [self expectationWithDescription:@"Multiple episodes"];
  57. // When - 保存多集进度
  58. __block NSInteger completedCount = 0;
  59. for (NSInteger i = 0; i < episodes.count; i++) {
  60. NSString *episodeId = episodes[i];
  61. [self.storage saveProgressWithEpisodeId:episodeId
  62. dramaId:dramaId
  63. position:(i + 1) * 30000
  64. duration:120000
  65. isCompleted:NO
  66. synced:NO
  67. completion:^(BOOL success, NSError *error) {
  68. completedCount++;
  69. if (completedCount == episodes.count) {
  70. // Then - 验证所有进度
  71. NSArray<JXPlaybackProgressModel *> *progresses = [self.storage getProgressByDramaId:dramaId];
  72. XCTAssertEqual(progresses.count, 3, @"应该有3集的进度");
  73. // 验证进度按更新时间排序
  74. for (NSInteger j = 0; j < progresses.count - 1; j++) {
  75. XCTAssertTrue([progresses[j].updatedAt compare:progresses[j+1].updatedAt] != NSOrderedAscending,
  76. @"进度应该按更新时间排序");
  77. }
  78. [expectation fulfill];
  79. }
  80. }];
  81. }
  82. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  83. }
  84. - (void)testProgressUpdateFlow {
  85. // Given
  86. NSString *episodeId = @"update_flow_ep";
  87. NSString *dramaId = @"update_flow_drama";
  88. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Initial save"];
  89. XCTestExpectation *expectation2 = [self expectationWithDescription:@"Update progress"];
  90. XCTestExpectation *expectation3 = [self expectationWithDescription:@"Complete progress"];
  91. // Step 1: 初始进度
  92. [self.storage saveProgressWithEpisodeId:episodeId
  93. dramaId:dramaId
  94. position:30000
  95. duration:120000
  96. isCompleted:NO
  97. synced:NO
  98. completion:^(BOOL success, NSError *error) {
  99. XCTAssertTrue(success);
  100. JXPlaybackProgressModel *progress = [self.storage getProgressWithEpisodeId:episodeId];
  101. XCTAssertEqual(progress.position, 30000);
  102. [expectation1 fulfill];
  103. // Step 2: 更新进度
  104. [self.storage saveProgressWithEpisodeId:episodeId
  105. dramaId:dramaId
  106. position:90000
  107. duration:120000
  108. isCompleted:NO
  109. synced:NO
  110. completion:^(BOOL success, NSError *error) {
  111. XCTAssertTrue(success);
  112. JXPlaybackProgressModel *updatedProgress = [self.storage getProgressWithEpisodeId:episodeId];
  113. XCTAssertEqual(updatedProgress.position, 90000);
  114. XCTAssertEqualWithAccuracy(updatedProgress.progress, 0.75f, 0.01f);
  115. [expectation2 fulfill];
  116. // Step 3: 完成观看
  117. [self.storage saveProgressWithEpisodeId:episodeId
  118. dramaId:dramaId
  119. position:114000
  120. duration:120000
  121. isCompleted:YES
  122. synced:NO
  123. completion:^(BOOL success, NSError *error) {
  124. XCTAssertTrue(success);
  125. JXPlaybackProgressModel *completedProgress = [self.storage getProgressWithEpisodeId:episodeId];
  126. XCTAssertTrue(completedProgress.isCompleted);
  127. XCTAssertTrue(completedProgress.progress >= 0.95f);
  128. [expectation3 fulfill];
  129. }];
  130. }];
  131. }];
  132. [self waitForExpectationsWithTimeout:15.0 handler:nil];
  133. }
  134. - (void)testSyncStatusManagement {
  135. // Given
  136. NSArray *episodeIds = @[@"sync_ep_001", @"sync_ep_002", @"sync_ep_003"];
  137. NSString *dramaId = @"sync_drama";
  138. XCTestExpectation *expectation = [self expectationWithDescription:@"Sync management"];
  139. // When - 保存未同步的进度
  140. __block NSInteger savedCount = 0;
  141. for (NSString *episodeId in episodeIds) {
  142. [self.storage saveProgressWithEpisodeId:episodeId
  143. dramaId:dramaId
  144. position:30000
  145. duration:120000
  146. isCompleted:NO
  147. synced:NO
  148. completion:^(BOOL success, NSError *error) {
  149. savedCount++;
  150. if (savedCount == episodeIds.count) {
  151. // 验证未同步状态
  152. NSArray<JXPlaybackProgressModel *> *unsyncedProgresses = [self.storage getUnsyncedProgresses];
  153. XCTAssertTrue(unsyncedProgresses.count >= episodeIds.count, @"应该有未同步的进度");
  154. // 标记第一个为已同步
  155. [self.storage markAsSyncedWithEpisodeId:episodeIds[0]];
  156. // 验证同步状态
  157. JXPlaybackProgressModel *syncedProgress = [self.storage getProgressWithEpisodeId:episodeIds[0]];
  158. XCTAssertTrue(syncedProgress.synced, @"应该被标记为已同步");
  159. NSArray<JXPlaybackProgressModel *> *remainingUnsynced = [self.storage getUnsyncedProgresses];
  160. BOOL foundSyncedInUnsynced = NO;
  161. for (JXPlaybackProgressModel *progress in remainingUnsynced) {
  162. if ([progress.episodeId isEqualToString:episodeIds[0]]) {
  163. foundSyncedInUnsynced = YES;
  164. break;
  165. }
  166. }
  167. XCTAssertFalse(foundSyncedInUnsynced, @"已同步的进度不应该在未同步列表中");
  168. [expectation fulfill];
  169. }
  170. }];
  171. }
  172. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  173. }
  174. #pragma mark - 数据一致性测试
  175. - (void)testDataConsistencyAcrossOperations {
  176. // Given
  177. NSString *dramaId = @"consistency_drama";
  178. NSArray *episodeIds = @[@"ep001", @"ep002", @"ep003", @"ep004", @"ep005"];
  179. XCTestExpectation *expectation = [self expectationWithDescription:@"Data consistency"];
  180. // When - 并发保存多个进度
  181. __block NSInteger completedOperations = 0;
  182. for (NSString *episodeId in episodeIds) {
  183. int64_t position = [episodeId hash] % 100000;
  184. [self.storage saveProgressWithEpisodeId:episodeId
  185. dramaId:dramaId
  186. position:position
  187. duration:120000
  188. isCompleted:NO
  189. synced:NO
  190. completion:^(BOOL success, NSError *error) {
  191. completedOperations++;
  192. if (completedOperations == episodeIds.count) {
  193. // Then - 验证数据一致性
  194. NSArray<JXPlaybackProgressModel *> *allProgresses = [self.storage getProgressByDramaId:dramaId];
  195. XCTAssertEqual(allProgresses.count, episodeIds.count, @"应该有所有集的进度");
  196. // 验证每个进度的数据完整性
  197. for (NSString *episodeId in episodeIds) {
  198. JXPlaybackProgressModel *progress = [self.storage getProgressWithEpisodeId:episodeId];
  199. XCTAssertNotNil(progress, @"进度应该存在");
  200. XCTAssertEqualObjects(progress.dramaId, dramaId);
  201. XCTAssertTrue(progress.position >= 0);
  202. XCTAssertEqual(progress.duration, 120000);
  203. XCTAssertTrue(progress.progress >= 0.0f && progress.progress <= 1.0f);
  204. }
  205. [expectation fulfill];
  206. }
  207. }];
  208. }
  209. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  210. }
  211. #pragma mark - 格式化工具集成测试
  212. - (void)testCountFormatterIntegration {
  213. // 测试格式化工具在实际场景中的使用
  214. NSArray *testCounts = @[@0, @999, @1500, @12345, @123456, @1234567, @123456789];
  215. for (NSNumber *count in testCounts) {
  216. long long countValue = [count longLongValue];
  217. // 测试所有格式化方法都能正常工作
  218. NSString *basicFormat = [JXCountFormatter formatCount:countValue];
  219. NSString *likeFormat = [JXCountFormatter formatLikeCount:countValue];
  220. NSString *commentFormat = [JXCountFormatter formatCommentCount:countValue];
  221. NSString *shareFormat = [JXCountFormatter formatShareCount:countValue];
  222. NSString *favoriteFormat = [JXCountFormatter formatFavoriteCount:countValue];
  223. NSString *playFormat = [JXCountFormatter formatPlayCount:countValue];
  224. // 验证格式化结果不为空
  225. XCTAssertNotNil(basicFormat);
  226. XCTAssertNotNil(likeFormat);
  227. XCTAssertNotNil(commentFormat);
  228. XCTAssertNotNil(shareFormat);
  229. XCTAssertNotNil(favoriteFormat);
  230. XCTAssertNotNil(playFormat);
  231. // 验证非零值的格式化一致性
  232. if (countValue > 0) {
  233. XCTAssertEqualObjects(basicFormat, likeFormat);
  234. XCTAssertEqualObjects(basicFormat, commentFormat);
  235. XCTAssertEqualObjects(basicFormat, shareFormat);
  236. XCTAssertEqualObjects(basicFormat, favoriteFormat);
  237. }
  238. }
  239. }
  240. #pragma mark - 错误处理测试
  241. - (void)testErrorHandlingIntegration {
  242. // 测试各种错误情况下的系统行为
  243. // 测试空字符串输入
  244. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Empty string handling"];
  245. [self.storage saveProgressWithEpisodeId:@""
  246. dramaId:@"test_drama"
  247. position:30000
  248. duration:120000
  249. isCompleted:NO
  250. synced:NO
  251. completion:^(BOOL success, NSError *error) {
  252. // 应该处理错误而不崩溃
  253. [expectation1 fulfill];
  254. }];
  255. // 测试极端数值
  256. XCTestExpectation *expectation2 = [self expectationWithDescription:@"Extreme values"];
  257. [self.storage saveProgressWithEpisodeId:@"extreme_test"
  258. dramaId:@"test_drama"
  259. position:LLONG_MAX
  260. duration:120000
  261. isCompleted:NO
  262. synced:NO
  263. completion:^(BOOL success, NSError *error) {
  264. // 应该处理极端值而不崩溃
  265. [expectation2 fulfill];
  266. }];
  267. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  268. // 验证系统仍然正常工作
  269. XCTestExpectation *expectation3 = [self expectationWithDescription:@"Normal operation after errors"];
  270. [self.storage saveProgressWithEpisodeId:@"normal_after_error"
  271. dramaId:@"test_drama"
  272. position:30000
  273. duration:120000
  274. isCompleted:NO
  275. synced:NO
  276. completion:^(BOOL success, NSError *error) {
  277. XCTAssertTrue(success, @"正常操作应该仍然有效");
  278. [expectation3 fulfill];
  279. }];
  280. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  281. }
  282. #pragma mark - 单例模式测试
  283. - (void)testSingletonBehavior {
  284. // 验证存储管理器的单例行为
  285. JXPlaybackProgressStorage *storage1 = [JXPlaybackProgressStorage sharedStorage];
  286. JXPlaybackProgressStorage *storage2 = [JXPlaybackProgressStorage sharedStorage];
  287. XCTAssertTrue(storage1 == storage2, @"应该返回同一个实例");
  288. // 验证API服务的单例行为
  289. JXAPIService *service1 = [JXAPIService sharedService];
  290. JXAPIService *service2 = [JXAPIService sharedService];
  291. XCTAssertTrue(service1 == service2, @"应该返回同一个实例");
  292. }
  293. #pragma mark - 清理操作测试
  294. - (void)testCleanupOperations {
  295. // Given - 创建测试数据
  296. NSArray *episodeIds = @[@"cleanup_ep_001", @"cleanup_ep_002", @"cleanup_ep_003"];
  297. NSString *dramaId = @"cleanup_drama";
  298. XCTestExpectation *setupExpectation = [self expectationWithDescription:@"Setup data"];
  299. __block NSInteger setupCount = 0;
  300. for (NSString *episodeId in episodeIds) {
  301. [self.storage saveProgressWithEpisodeId:episodeId
  302. dramaId:dramaId
  303. position:30000
  304. duration:120000
  305. isCompleted:NO
  306. synced:YES
  307. completion:^(BOOL success, NSError *error) {
  308. setupCount++;
  309. if (setupCount == episodeIds.count) {
  310. [setupExpectation fulfill];
  311. }
  312. }];
  313. }
  314. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  315. // When - 执行清理操作
  316. [self.storage cleanupOldRecords];
  317. // Then - 验证新数据不会被清理(因为刚创建)
  318. for (NSString *episodeId in episodeIds) {
  319. JXPlaybackProgressModel *progress = [self.storage getProgressWithEpisodeId:episodeId];
  320. XCTAssertNotNil(progress, @"新数据不应该被清理");
  321. }
  322. // 测试按剧删除
  323. [self.storage deleteProgressByDramaId:dramaId];
  324. NSArray<JXPlaybackProgressModel *> *remainingProgresses = [self.storage getProgressByDramaId:dramaId];
  325. XCTAssertEqual(remainingProgresses.count, 0, @"删除后应该没有进度");
  326. }
  327. @end