summaryrefslogtreecommitdiff
path: root/SoraTests/SettingsManagerSyncTests.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-18 11:45:06 -0800
committerFuwn <[email protected]>2026-02-18 11:57:36 -0800
commit07c533116dd267e3e6e70d673f97bbb87f600f1f (patch)
treee744ebf8b78bc23e4639cef9b4483942140c4055 /SoraTests/SettingsManagerSyncTests.swift
parenttest: add baseline test harness and performance baseline report (diff)
downloadsora-testing-07c533116dd267e3e6e70d673f97bbb87f600f1f.tar.xz
sora-testing-07c533116dd267e3e6e70d673f97bbb87f600f1f.zip
refactor: remove duplicate settings serialisation work
Diffstat (limited to 'SoraTests/SettingsManagerSyncTests.swift')
-rw-r--r--SoraTests/SettingsManagerSyncTests.swift92
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