|
1 | 1 | @testable import FeedLibrary |
2 | 2 | import Foundation |
| 3 | +import MacMagazineLibrary |
3 | 4 | import StorageLibrary |
4 | 5 | import Testing |
5 | 6 |
|
@@ -379,6 +380,126 @@ struct StorageServiceTests { |
379 | 380 | #expect(cats.contains("Reviews")) |
380 | 381 | } |
381 | 382 |
|
| 383 | + // MARK: - Category Reconciliation Tests |
| 384 | + |
| 385 | + @Test("grouped save inserts a post carrying its fetched category key") |
| 386 | + func groupedSaveAddsCategoryKey() { |
| 387 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 388 | + let post = FeedDB(postId: "1", title: "Rumor", pubDate: Date(), categories: [NewsCategory.rumors.filterKey]) |
| 389 | + |
| 390 | + storage.save(feed: [(category: NewsCategory.rumors, posts: [post])]) |
| 391 | + |
| 392 | + let fetched = storage.fetch(FeedDB.self).first |
| 393 | + #expect(fetched?.categories.contains(NewsCategory.rumors.filterKey) == true) |
| 394 | + } |
| 395 | + |
| 396 | + @Test("grouped save removes a stale category key for a post that dropped out of its date window") |
| 397 | + func groupedSaveRemovesStaleCategoryKey() { |
| 398 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 399 | + let earlier = Date(timeIntervalSince1970: 900_000) |
| 400 | + let dehighlightedDate = Date(timeIntervalSince1970: 1_000_000) |
| 401 | + let later = Date(timeIntervalSince1970: 1_100_000) |
| 402 | + |
| 403 | + let dehighlighted = FeedDB( |
| 404 | + postId: "1", |
| 405 | + title: "No Longer Highlighted", |
| 406 | + pubDate: dehighlightedDate, |
| 407 | + categories: [NewsCategory.highlights.filterKey] |
| 408 | + ) |
| 409 | + storage.save(feed: dehighlighted) |
| 410 | + |
| 411 | + let before = FeedDB(postId: "2", title: "Before", pubDate: earlier, categories: [NewsCategory.highlights.filterKey]) |
| 412 | + let after = FeedDB(postId: "3", title: "After", pubDate: later, categories: [NewsCategory.highlights.filterKey]) |
| 413 | + |
| 414 | + storage.save(feed: [(category: NewsCategory.highlights, posts: [before, after])]) |
| 415 | + |
| 416 | + let predicate = #Predicate<FeedDB> { $0.postId == "1" } |
| 417 | + let fetched = storage.fetch(FeedDB.self, predicate: predicate).first |
| 418 | + #expect(fetched?.categories.contains(NewsCategory.highlights.filterKey) == false) |
| 419 | + } |
| 420 | + |
| 421 | + @Test("grouped save never touches a sibling category's key while reconciling") |
| 422 | + func groupedSavePreservesSiblingCategoryKey() { |
| 423 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 424 | + let date = Date(timeIntervalSince1970: 1_000_000) |
| 425 | + |
| 426 | + let multiCategory = FeedDB( |
| 427 | + postId: "1", |
| 428 | + title: "Review And Rumor", |
| 429 | + pubDate: date, |
| 430 | + categories: [NewsCategory.reviews.filterKey, NewsCategory.rumors.filterKey] |
| 431 | + ) |
| 432 | + storage.save(feed: multiCategory) |
| 433 | + |
| 434 | + let otherReview = FeedDB(postId: "2", title: "Other Review", pubDate: date, categories: [NewsCategory.reviews.filterKey]) |
| 435 | + storage.save(feed: [(category: NewsCategory.reviews, posts: [otherReview])]) |
| 436 | + |
| 437 | + let predicate = #Predicate<FeedDB> { $0.postId == "1" } |
| 438 | + let fetched = storage.fetch(FeedDB.self, predicate: predicate).first |
| 439 | + #expect(fetched?.categories.contains(NewsCategory.reviews.filterKey) == false) |
| 440 | + #expect(fetched?.categories.contains(NewsCategory.rumors.filterKey) == true) |
| 441 | + } |
| 442 | + |
| 443 | + @Test("grouped save skips reconciliation when the fetched category result is empty") |
| 444 | + func groupedSaveSkipsReconciliationForEmptyResult() { |
| 445 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 446 | + let highlighted = FeedDB( |
| 447 | + postId: "1", |
| 448 | + title: "Highlight", |
| 449 | + pubDate: Date(), |
| 450 | + categories: [NewsCategory.highlights.filterKey] |
| 451 | + ) |
| 452 | + storage.save(feed: highlighted) |
| 453 | + |
| 454 | + storage.save(feed: [(category: NewsCategory.highlights, posts: [])]) |
| 455 | + |
| 456 | + let fetched = storage.fetch(FeedDB.self).first |
| 457 | + #expect(fetched?.categories.contains(NewsCategory.highlights.filterKey) == true) |
| 458 | + } |
| 459 | + |
| 460 | + @Test("grouped save leaves a category key untouched when the post falls outside the fetched date window") |
| 461 | + func groupedSavePreservesKeyOutsideDateWindow() { |
| 462 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 463 | + let farInThePast = Date(timeIntervalSince1970: 0) |
| 464 | + let recent = Date(timeIntervalSince1970: 2_000_000) |
| 465 | + |
| 466 | + let oldHighlight = FeedDB( |
| 467 | + postId: "1", |
| 468 | + title: "Old Highlight", |
| 469 | + pubDate: farInThePast, |
| 470 | + categories: [NewsCategory.highlights.filterKey] |
| 471 | + ) |
| 472 | + storage.save(feed: oldHighlight) |
| 473 | + |
| 474 | + let recentHighlight = FeedDB(postId: "2", title: "Recent Highlight", pubDate: recent, categories: [NewsCategory.highlights.filterKey]) |
| 475 | + storage.save(feed: [(category: NewsCategory.highlights, posts: [recentHighlight])]) |
| 476 | + |
| 477 | + let predicate = #Predicate<FeedDB> { $0.postId == "1" } |
| 478 | + let fetched = storage.fetch(FeedDB.self, predicate: predicate).first |
| 479 | + #expect(fetched?.categories.contains(NewsCategory.highlights.filterKey) == true) |
| 480 | + } |
| 481 | + |
| 482 | + @Test("grouped save does not reconcile the news category") |
| 483 | + func groupedSaveDoesNotReconcileNewsCategory() { |
| 484 | + let storage = Database(models: [FeedDB.self], inMemory: true) |
| 485 | + let date = Date(timeIntervalSince1970: 1_000_000) |
| 486 | + |
| 487 | + let post = FeedDB( |
| 488 | + postId: "1", |
| 489 | + title: "Old News Marker", |
| 490 | + pubDate: date, |
| 491 | + categories: [NewsCategory.news.filterKey] |
| 492 | + ) |
| 493 | + storage.save(feed: post) |
| 494 | + |
| 495 | + let otherPost = FeedDB(postId: "2", title: "Other Post", pubDate: date, categories: [NewsCategory.news.filterKey]) |
| 496 | + storage.save(feed: [(category: NewsCategory.news, posts: [otherPost])]) |
| 497 | + |
| 498 | + let predicate = #Predicate<FeedDB> { $0.postId == "1" } |
| 499 | + let fetched = storage.fetch(FeedDB.self, predicate: predicate).first |
| 500 | + #expect(fetched?.categories.contains(NewsCategory.news.filterKey) == true) |
| 501 | + } |
| 502 | + |
382 | 503 | @Test("save should preserve favorite status on update for podcast") |
383 | 504 | func savePreservesFavoriteStatusForPodcast() { |
384 | 505 | let storage = Database(models: [PodcastDB.self], inMemory: true) |
|
0 commit comments