mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-05 19:57:31 +00:00
Convert to Swift testing.
This commit is contained in:
1 parent
c9605ee0d9
commit
73e02b15f0
1 file changed
+122
-85
@@ -5,29 +5,18 @@
|
||||
// Created by Brent Simmons on 5/29/26.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
@testable import Account
|
||||
|
||||
@MainActor final class FeedlyFolderReconciliationTests: XCTestCase {
|
||||
|
||||
private var account: Account!
|
||||
private let accountManager = TestAccountManager()
|
||||
|
||||
override func setUp() async throws {
|
||||
try await super.setUp()
|
||||
account = accountManager.createAccount(type: .feedly)
|
||||
}
|
||||
|
||||
override func tearDown() async throws {
|
||||
if let account {
|
||||
accountManager.deleteAccount(account)
|
||||
}
|
||||
try await super.tearDown()
|
||||
}
|
||||
@MainActor struct FeedlyFolderReconciliationTests {
|
||||
|
||||
// MARK: - mirrorCollectionsAsFolders
|
||||
|
||||
func testMirrorCollectionsAsFoldersAddsFolders() {
|
||||
@Test func mirrorCollectionsAsFoldersAddsFolders() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let collections = [
|
||||
FeedlyCollection(feeds: [], label: "One", id: "collections/1"),
|
||||
FeedlyCollection(feeds: [], label: "Two", id: "collections/2")
|
||||
@@ -39,11 +28,15 @@ import XCTest
|
||||
let folderNames = Set(folders.compactMap { $0.nameForDisplay })
|
||||
let folderExternalIDs = Set(folders.compactMap { $0.externalID })
|
||||
|
||||
XCTAssertEqual(folderNames, Set(["One", "Two"]))
|
||||
XCTAssertEqual(folderExternalIDs, Set(["collections/1", "collections/2"]))
|
||||
#expect(folderNames == Set(["One", "Two"]))
|
||||
#expect(folderExternalIDs == Set(["collections/1", "collections/2"]))
|
||||
}
|
||||
|
||||
func testMirrorCollectionsAsFoldersRemovesFoldersWithoutCollections() {
|
||||
@Test func mirrorCollectionsAsFoldersRemovesFoldersWithoutCollections() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
_ = mirrorCollectionsAsFolders([
|
||||
FeedlyCollection(feeds: [], label: "One", id: "collections/1"),
|
||||
FeedlyCollection(feeds: [], label: "Two", id: "collections/2")
|
||||
@@ -52,32 +45,39 @@ import XCTest
|
||||
// Now remove all collections; both folders should disappear.
|
||||
let pairs = mirrorCollectionsAsFolders([], in: account)
|
||||
|
||||
XCTAssertTrue(pairs.isEmpty)
|
||||
#expect(pairs.isEmpty)
|
||||
|
||||
let folders = account.folders ?? Set()
|
||||
XCTAssertTrue(folders.isEmpty, "Folders should be removed when their collection no longer exists.")
|
||||
#expect(folders.isEmpty, "Folders should be removed when their collection no longer exists.")
|
||||
}
|
||||
|
||||
func testMirrorCollectionsAsFoldersMatchesByExternalIDAcrossRename() {
|
||||
@Test func mirrorCollectionsAsFoldersMatchesByExternalIDAcrossRename() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let pairsBefore = mirrorCollectionsAsFolders([
|
||||
FeedlyCollection(feeds: [], label: "News", id: "collections/1")
|
||||
], in: account)
|
||||
let folderBefore = pairsBefore.first?.folder
|
||||
XCTAssertNotNil(folderBefore)
|
||||
let folderBefore = try #require(pairsBefore.first?.folder)
|
||||
|
||||
// A local rename applied optimistically, with the refresh's collection snapshot still stale.
|
||||
folderBefore?.name = "Nachrichten"
|
||||
folderBefore.name = "Nachrichten"
|
||||
|
||||
let pairsAfter = mirrorCollectionsAsFolders([
|
||||
FeedlyCollection(feeds: [], label: "News", id: "collections/1")
|
||||
], in: account)
|
||||
|
||||
XCTAssertEqual(account.folders?.count, 1)
|
||||
XCTAssertTrue(pairsAfter.first?.folder === folderBefore, "The folder should be matched by external ID, not dropped and recreated.")
|
||||
XCTAssertEqual(folderBefore?.name, "News")
|
||||
#expect(account.folders?.count == 1)
|
||||
#expect(pairsAfter.first?.folder === folderBefore, "The folder should be matched by external ID, not dropped and recreated.")
|
||||
#expect(folderBefore.name == "News")
|
||||
}
|
||||
|
||||
func testMirrorCollectionsAsFoldersKeepsSameLabelCollectionsDistinct() {
|
||||
@Test func mirrorCollectionsAsFoldersKeepsSameLabelCollectionsDistinct() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let collections = [
|
||||
FeedlyCollection(feeds: [], label: "News", id: "collections/1"),
|
||||
FeedlyCollection(feeds: [], label: "News", id: "collections/2")
|
||||
@@ -85,18 +85,22 @@ import XCTest
|
||||
|
||||
let pairs = mirrorCollectionsAsFolders(collections, in: account)
|
||||
|
||||
XCTAssertEqual(pairs.count, 2)
|
||||
XCTAssertEqual(account.folders?.count, 2)
|
||||
#expect(pairs.count == 2)
|
||||
#expect(account.folders?.count == 2)
|
||||
let externalIDs = Set((account.folders ?? Set()).compactMap { $0.externalID })
|
||||
XCTAssertEqual(externalIDs, Set(["collections/1", "collections/2"]))
|
||||
#expect(externalIDs == Set(["collections/1", "collections/2"]))
|
||||
|
||||
// A second pass matches by ID and keeps both folders — no churn.
|
||||
let secondPairs = mirrorCollectionsAsFolders(collections, in: account)
|
||||
XCTAssertEqual(account.folders?.count, 2)
|
||||
XCTAssertEqual(Set(secondPairs.map { ObjectIdentifier($0.folder) }), Set(pairs.map { ObjectIdentifier($0.folder) }))
|
||||
#expect(account.folders?.count == 2)
|
||||
#expect(Set(secondPairs.map { ObjectIdentifier($0.folder) }) == Set(pairs.map { ObjectIdentifier($0.folder) }))
|
||||
}
|
||||
|
||||
func testMirrorCollectionsAsFoldersReturnsCollectionFeedsPairedWithFolders() {
|
||||
@Test func mirrorCollectionsAsFoldersReturnsCollectionFeedsPairedWithFolders() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let feedsForOne = [
|
||||
FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil),
|
||||
FeedlyFeed(id: "feed/2", title: "Feed Two", updated: nil, website: nil)
|
||||
@@ -112,7 +116,7 @@ import XCTest
|
||||
|
||||
let pairs = mirrorCollectionsAsFolders(collections, in: account)
|
||||
|
||||
XCTAssertEqual(pairs.count, 2)
|
||||
#expect(pairs.count == 2)
|
||||
|
||||
let pairedIDs = pairs.compactMap { pair -> [String: [String]]? in
|
||||
guard let id = pair.folder.externalID else {
|
||||
@@ -123,12 +127,16 @@ import XCTest
|
||||
let expectedIDs = collections.map { collection in
|
||||
[collection.id: collection.feeds.map { $0.id }.sorted()]
|
||||
}
|
||||
XCTAssertEqual(pairedIDs, expectedIDs)
|
||||
#expect(pairedIDs == expectedIDs)
|
||||
}
|
||||
|
||||
// MARK: - syncFeedsForCollectionFolders
|
||||
|
||||
func testSyncFeedsForCollectionFoldersAddsFeeds() {
|
||||
@Test func syncFeedsForCollectionFoldersAddsFeeds() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let feedsForOne = [
|
||||
FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil),
|
||||
FeedlyFeed(id: "feed/2", title: "Feed Two", updated: nil, website: nil)
|
||||
@@ -137,11 +145,11 @@ import XCTest
|
||||
FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil),
|
||||
FeedlyFeed(id: "feed/3", title: "Feed Three", updated: nil, website: nil)
|
||||
]
|
||||
let folderOne = makeFolder(name: "FolderOne", externalID: "folder/1")
|
||||
let folderTwo = makeFolder(name: "FolderTwo", externalID: "folder/2")
|
||||
let folderOne = try makeFolder(in: account, name: "FolderOne", externalID: "folder/1")
|
||||
let folderTwo = try makeFolder(in: account, name: "FolderTwo", externalID: "folder/2")
|
||||
let pairs = [(feedsForOne, folderOne), (feedsForTwo, folderTwo)]
|
||||
|
||||
XCTAssertTrue(account.flattenedFeeds().isEmpty)
|
||||
#expect(account.flattenedFeeds().isEmpty)
|
||||
|
||||
syncFeedsForCollectionFolders(pairs, in: account)
|
||||
|
||||
@@ -149,17 +157,21 @@ import XCTest
|
||||
let feedIDs = Set(accountFeeds.map { $0.feedID })
|
||||
let feedTitles = Set(accountFeeds.map { $0.nameForDisplay })
|
||||
|
||||
XCTAssertEqual(feedIDs, Set(["feed/1", "feed/2", "feed/3"]))
|
||||
XCTAssertEqual(feedTitles, Set(["Feed One", "Feed Two", "Feed Three"]))
|
||||
#expect(feedIDs == Set(["feed/1", "feed/2", "feed/3"]))
|
||||
#expect(feedTitles == Set(["Feed One", "Feed Two", "Feed Three"]))
|
||||
|
||||
assertFolder(folderOne, contains: ["feed/1", "feed/2"])
|
||||
assertFolder(folderTwo, contains: ["feed/1", "feed/3"])
|
||||
expectFolder(folderOne, contains: ["feed/1", "feed/2"])
|
||||
expectFolder(folderTwo, contains: ["feed/1", "feed/3"])
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersRemovesFeedsNoLongerInCollection() {
|
||||
@Test func syncFeedsForCollectionFoldersRemovesFeedsNoLongerInCollection() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let shared = FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil)
|
||||
let folderOne = makeFolder(name: "FolderOne", externalID: "folder/1")
|
||||
let folderTwo = makeFolder(name: "FolderTwo", externalID: "folder/2")
|
||||
let folderOne = try makeFolder(in: account, name: "FolderOne", externalID: "folder/1")
|
||||
let folderTwo = try makeFolder(in: account, name: "FolderTwo", externalID: "folder/2")
|
||||
|
||||
// Seed: both folders have feed/1.
|
||||
syncFeedsForCollectionFolders([
|
||||
@@ -167,7 +179,7 @@ import XCTest
|
||||
([shared, FeedlyFeed(id: "feed/3", title: "Feed Three", updated: nil, website: nil)], folderTwo)
|
||||
], in: account)
|
||||
|
||||
XCTAssertEqual(Set(account.flattenedFeeds().map { $0.feedID }), Set(["feed/1", "feed/2", "feed/3"]))
|
||||
#expect(Set(account.flattenedFeeds().map { $0.feedID }) == Set(["feed/1", "feed/2", "feed/3"]))
|
||||
|
||||
// Drop feed/1 from both collections; the remaining feeds should still belong to the right folders.
|
||||
syncFeedsForCollectionFolders([
|
||||
@@ -175,30 +187,38 @@ import XCTest
|
||||
([FeedlyFeed(id: "feed/3", title: "Feed Three", updated: nil, website: nil)], folderTwo)
|
||||
], in: account)
|
||||
|
||||
assertFolder(folderOne, contains: ["feed/2"])
|
||||
assertFolder(folderTwo, contains: ["feed/3"])
|
||||
expectFolder(folderOne, contains: ["feed/2"])
|
||||
expectFolder(folderTwo, contains: ["feed/3"])
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersRenamesFeedWhenCollectionTitleChanges() {
|
||||
let folder = makeFolder(name: "Folder", externalID: "folder/1")
|
||||
@Test func syncFeedsForCollectionFoldersRenamesFeedWhenCollectionTitleChanges() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let folder = try makeFolder(in: account, name: "Folder", externalID: "folder/1")
|
||||
|
||||
syncFeedsForCollectionFolders([
|
||||
([FeedlyFeed(id: "feed/1", title: "Original Title", updated: nil, website: nil)], folder)
|
||||
], in: account)
|
||||
|
||||
let originalFeed = folder.existingFeed(withFeedID: "feed/1")
|
||||
XCTAssertEqual(originalFeed?.nameForDisplay, "Original Title")
|
||||
#expect(originalFeed?.nameForDisplay == "Original Title")
|
||||
|
||||
syncFeedsForCollectionFolders([
|
||||
([FeedlyFeed(id: "feed/1", title: "Updated Title", updated: nil, website: nil)], folder)
|
||||
], in: account)
|
||||
|
||||
let updatedFeed = folder.existingFeed(withFeedID: "feed/1")
|
||||
XCTAssertEqual(updatedFeed?.nameForDisplay, "Updated Title")
|
||||
XCTAssertTrue(originalFeed === updatedFeed, "Renaming a feed should reuse the existing Feed instance.")
|
||||
#expect(updatedFeed?.nameForDisplay == "Updated Title")
|
||||
#expect(originalFeed === updatedFeed, "Renaming a feed should reuse the existing Feed instance.")
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersLeavesUntitledFeedNameAlone() {
|
||||
@Test func syncFeedsForCollectionFoldersLeavesUntitledFeedNameAlone() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let collections = [
|
||||
FeedlyCollection(feeds: [FeedlyFeed(id: "feed/1", title: nil, updated: nil, website: nil)], label: "One", id: "collections/1")
|
||||
]
|
||||
@@ -207,12 +227,15 @@ import XCTest
|
||||
syncFeedsForCollectionFolders(pairs, in: account)
|
||||
syncFeedsForCollectionFolders(pairs, in: account)
|
||||
|
||||
let feed = account.existingFeed(withFeedID: "feed/1")
|
||||
XCTAssertNotNil(feed)
|
||||
XCTAssertNil(feed?.name)
|
||||
let feed = try #require(account.existingFeed(withFeedID: "feed/1"))
|
||||
#expect(feed.name == nil)
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersKeepsRightToLeftFeedNameSanitized() {
|
||||
@Test func syncFeedsForCollectionFoldersKeepsRightToLeftFeedNameSanitized() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let rawTitle = "<div style=\"direction:rtl;text-align:right\">חדשות</div>"
|
||||
let collections = [
|
||||
FeedlyCollection(feeds: [FeedlyFeed(id: "feed/1", title: rawTitle, updated: nil, website: nil)], label: "One", id: "collections/1")
|
||||
@@ -223,13 +246,17 @@ import XCTest
|
||||
syncFeedsForCollectionFolders(pairs, in: account)
|
||||
|
||||
let feed = account.existingFeed(withFeedID: "feed/1")
|
||||
XCTAssertEqual(feed?.name, "חדשות", "The stored name should stay sanitized, not get rewritten to the raw RTL markup.")
|
||||
#expect(feed?.name == "חדשות", "The stored name should stay sanitized, not get rewritten to the raw RTL markup.")
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersRepairsStaleFeedID() {
|
||||
@Test func syncFeedsForCollectionFoldersRepairsStaleFeedID() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let url = "https://example.com/feed.xml"
|
||||
let canonicalFeedID = "feed/\(url)"
|
||||
let folder = makeFolder(name: "Folder", externalID: "folder/1")
|
||||
let folder = try makeFolder(in: account, name: "Folder", externalID: "folder/1")
|
||||
|
||||
// A feed created from OPML before its settings row existed gets the bare URL as its feedID.
|
||||
let staleFeed = account.createFeed(with: "Example", url: url, feedID: url, homePageURL: nil)
|
||||
@@ -241,20 +268,24 @@ import XCTest
|
||||
], in: account)
|
||||
|
||||
let feeds = account.flattenedFeeds()
|
||||
XCTAssertEqual(feeds.count, 1)
|
||||
#expect(feeds.count == 1)
|
||||
|
||||
let repairedFeed = feeds.first
|
||||
XCTAssertEqual(repairedFeed?.feedID, canonicalFeedID)
|
||||
XCTAssertEqual(repairedFeed?.url, url)
|
||||
assertFolder(folder, contains: [canonicalFeedID])
|
||||
#expect(repairedFeed?.feedID == canonicalFeedID)
|
||||
#expect(repairedFeed?.url == url)
|
||||
expectFolder(folder, contains: [canonicalFeedID])
|
||||
|
||||
XCTAssertEqual(repairedFeed?.newArticleNotificationsEnabled, true, "Settings should survive a feedID repair.")
|
||||
#expect(repairedFeed?.newArticleNotificationsEnabled == true, "Settings should survive a feedID repair.")
|
||||
}
|
||||
|
||||
func testSyncFeedsForCollectionFoldersAfterRepairReusesFeedInstance() {
|
||||
@Test func syncFeedsForCollectionFoldersAfterRepairReusesFeedInstance() throws {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let url = "https://example.com/feed.xml"
|
||||
let canonicalFeedID = "feed/\(url)"
|
||||
let folder = makeFolder(name: "Folder", externalID: "folder/1")
|
||||
let folder = try makeFolder(in: account, name: "Folder", externalID: "folder/1")
|
||||
|
||||
let staleFeed = account.createFeed(with: "Example", url: url, feedID: url, homePageURL: nil)
|
||||
folder.addFeedToTreeAtTopLevel(staleFeed)
|
||||
@@ -262,20 +293,23 @@ import XCTest
|
||||
let collectionFeeds = [FeedlyFeed(id: canonicalFeedID, title: "Example", updated: nil, website: nil)]
|
||||
|
||||
syncFeedsForCollectionFolders([(collectionFeeds, folder)], in: account)
|
||||
let repairedFeed = folder.existingFeed(withFeedID: canonicalFeedID)
|
||||
XCTAssertNotNil(repairedFeed)
|
||||
let repairedFeed = try #require(folder.existingFeed(withFeedID: canonicalFeedID))
|
||||
|
||||
// A second pass must not remove and recreate the feed — that was the pre-repair flapping.
|
||||
syncFeedsForCollectionFolders([(collectionFeeds, folder)], in: account)
|
||||
let secondPassFeed = folder.existingFeed(withFeedID: canonicalFeedID)
|
||||
|
||||
XCTAssertTrue(repairedFeed === secondPassFeed, "A repaired feed should be reused on subsequent syncs.")
|
||||
XCTAssertEqual(account.flattenedFeeds().count, 1)
|
||||
#expect(repairedFeed === secondPassFeed, "A repaired feed should be reused on subsequent syncs.")
|
||||
#expect(account.flattenedFeeds().count == 1)
|
||||
}
|
||||
|
||||
// MARK: - Combined behavior
|
||||
|
||||
func testMirrorThenRemoveAllRemovesFeedsToo() {
|
||||
@Test func mirrorThenRemoveAllRemovesFeedsToo() {
|
||||
let account = TestAccountManager.shared.createAccount(type: .feedly)
|
||||
defer {
|
||||
TestAccountManager.shared.deleteAccount(account)
|
||||
}
|
||||
let collections = [
|
||||
FeedlyCollection(
|
||||
feeds: [
|
||||
@@ -289,24 +323,27 @@ import XCTest
|
||||
let pairs = mirrorCollectionsAsFolders(collections, in: account)
|
||||
syncFeedsForCollectionFolders(pairs, in: account)
|
||||
|
||||
XCTAssertFalse(account.flattenedFeeds().isEmpty)
|
||||
#expect(!account.flattenedFeeds().isEmpty)
|
||||
|
||||
// Removing the collection should remove its folder, and the feeds should go with it.
|
||||
_ = mirrorCollectionsAsFolders([], in: account)
|
||||
|
||||
XCTAssertTrue(account.flattenedFeeds().isEmpty)
|
||||
#expect(account.flattenedFeeds().isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
// MARK: - Helpers
|
||||
|
||||
private func makeFolder(name: String, externalID: String) -> Folder {
|
||||
let folder = account.ensureFolder(with: name)!
|
||||
private extension FeedlyFolderReconciliationTests {
|
||||
|
||||
func makeFolder(in account: Account, name: String, externalID: String) throws -> Folder {
|
||||
let folder = try #require(account.ensureFolder(with: name))
|
||||
folder.externalID = externalID
|
||||
return folder
|
||||
}
|
||||
|
||||
private func assertFolder(_ folder: Folder, contains feedIDs: Set<String>, file: StaticString = #filePath, line: UInt = #line) {
|
||||
func expectFolder(_ folder: Folder, contains feedIDs: Set<String>, sourceLocation: SourceLocation = #_sourceLocation) {
|
||||
let actual = Set(folder.topLevelFeeds.map { $0.feedID })
|
||||
XCTAssertEqual(actual, feedIDs, "Folder \(folder.nameForDisplay) had unexpected feeds.", file: file, line: line)
|
||||
#expect(actual == feedIDs, "Folder \(folder.nameForDisplay) had unexpected feeds.", sourceLocation: sourceLocation)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user