diff options
Diffstat (limited to 'SoraTests/SettingsManagerSyncTests.swift')
| -rw-r--r-- | SoraTests/SettingsManagerSyncTests.swift | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/SoraTests/SettingsManagerSyncTests.swift b/SoraTests/SettingsManagerSyncTests.swift index 27f05f5..72cf2ad 100644 --- a/SoraTests/SettingsManagerSyncTests.swift +++ b/SoraTests/SettingsManagerSyncTests.swift @@ -1,7 +1,98 @@ import Foundation import XCTest +// swiftlint:disable type_body_length final class SettingsManagerSyncTests: XCTestCase { + func testBookmarkMutationPathReusesEncodedPayload() throws { + let source = try loadSource(at: "Sora/Data/Settings/SettingsManager.swift") + let addBookmarkSection = try extractFunction( + named: "func addBookmark(provider: BooruProvider, tags: [String], folder: UUID? = nil)", + from: source + ) + let updateBookmarksSection = try extractFunction(named: "func updateBookmarks(", from: source) + let bookmarksSetterSection = try extractFunction(named: "var bookmarks: [SettingsBookmark]", from: source) + let normalizedAddBookmark = strippingCommentsAndStrings(from: addBookmarkSection) + let normalizedUpdateBookmarks = strippingCommentsAndStrings(from: updateBookmarksSection) + let normalizedBookmarksSetter = strippingCommentsAndStrings(from: bookmarksSetterSection) + + let addBookmarkInlineEncodeCount = tokenCount( + matching: #"Self\s*\.\s*encode\s*\(\s*updatedBookmarks\s*\)"#, + in: normalizedAddBookmark + ) + let updateBookmarksPreEncodedForwardCount = tokenCount( + matching: #"syncableData\s*\([\s\S]*?\bencodedData\s*:"#, + in: normalizedUpdateBookmarks + ) + let bookmarksSetterPreEncodedForwardCount = tokenCount( + matching: #"syncableData\s*\([\s\S]*?\bencodedData\s*:"#, + in: normalizedBookmarksSetter + ) + + // swiftlint:disable:next prefer_nimble + XCTAssertEqual( + addBookmarkInlineEncodeCount, + 0, + "addBookmark should not pre-encode and then trigger another encode downstream." + ) + // swiftlint:disable:next prefer_nimble + XCTAssertGreaterThan( + updateBookmarksPreEncodedForwardCount, + 0, + "updateBookmarks should forward a pre-encoded payload to syncableData to avoid duplicate encoding." + ) + // swiftlint:disable:next prefer_nimble + XCTAssertGreaterThan( + bookmarksSetterPreEncodedForwardCount, + 0, + "bookmarks setter should forward a pre-encoded payload to syncableData." + ) + } + + func testFavoriteMutationPathReusesEncodedPayload() throws { + let source = try loadSource(at: "Sora/Data/Settings/SettingsManager.swift") + let addFavoriteSection = try extractFunction( + named: "func addFavorite(post: BooruPost, provider: BooruProvider, folder: UUID? = nil)", + from: source + ) + let updateFavoritesSection = try extractFunction(named: "func updateFavorites(", from: source) + let favoritesSetterSection = try extractFunction(named: "var favorites: [SettingsFavoritePost]", from: source) + let normalizedAddFavorite = strippingCommentsAndStrings(from: addFavoriteSection) + let normalizedUpdateFavorites = strippingCommentsAndStrings(from: updateFavoritesSection) + let normalizedFavoritesSetter = strippingCommentsAndStrings(from: favoritesSetterSection) + + let addFavoriteInlineEncodeCount = tokenCount( + matching: #"Self\s*\.\s*encode\s*\(\s*updatedFavorites\s*\)"#, + in: normalizedAddFavorite + ) + let updateFavoritesPreEncodedForwardCount = tokenCount( + matching: #"syncableData\s*\([\s\S]*?\bencodedData\s*:"#, + in: normalizedUpdateFavorites + ) + let favoritesSetterPreEncodedForwardCount = tokenCount( + matching: #"syncableData\s*\([\s\S]*?\bencodedData\s*:"#, + in: normalizedFavoritesSetter + ) + + // swiftlint:disable:next prefer_nimble + XCTAssertEqual( + addFavoriteInlineEncodeCount, + 0, + "addFavorite should not pre-encode and then trigger another encode downstream." + ) + // swiftlint:disable:next prefer_nimble + XCTAssertGreaterThan( + updateFavoritesPreEncodedForwardCount, + 0, + "updateFavorites should forward a pre-encoded payload to syncableData to avoid duplicate encoding." + ) + // swiftlint:disable:next prefer_nimble + XCTAssertGreaterThan( + favoritesSetterPreEncodedForwardCount, + 0, + "favorites setter should forward a pre-encoded payload to syncableData." + ) + } + func testBatchedSyncPathGuardsUnchangedPayloadWrites() throws { let source = try loadSource(at: "Sora/Data/Settings/SettingsManager.swift") let triggerSyncSection = try extractFunction( @@ -235,3 +326,4 @@ final class SettingsManagerSyncTests: XCTestCase { return regex.numberOfMatches(in: source, range: range) } } +// swiftlint:enable type_body_length |