Carry tags through the sync merge !9

merged merged by cmc on 2026-10-24 04:09 UTC · krz/domain-dig:fix-sync-field-loss into main

4 files changed, +76 −3

Layout: unified · split

.gitignore +3
@@ -1,3 +1,6 @@
11
22# Xcode per-user state
33xcuserdata/
4
5# Build output
6.build/
DomainDig/CloudSyncMerge.swift +11 −2
@@ -417,7 +417,8 @@ enum CloudSyncMerge {
417417 lastAlertAt: trackedDomain.lastAlertAt,
418418 monitoringState: trackedDomain.monitoringState,
419419 pendingMonitoringAlerts: trackedDomain.pendingMonitoringAlerts,
420 collaboration: trackedDomain.collaboration
420 collaboration: trackedDomain.collaboration,
421 tags: trackedDomain.tags
421422 )
422423 }
423424
@@ -475,7 +476,8 @@ enum CloudSyncMerge {
475476 lastAlertAt: [lhs.value.lastAlertAt, rhs.value.lastAlertAt].compactMap { $0 }.max(),
476477 monitoringState: preferredMonitoringState(lhs.value.monitoringState, rhs.value.monitoringState),
477478 pendingMonitoringAlerts: deduplicatedPendingAlerts(lhs.value.pendingMonitoringAlerts + rhs.value.pendingMonitoringAlerts),
478 collaboration: preferredCollaboration(lhs.value.collaboration, rhs.value.collaboration)
479 collaboration: preferredCollaboration(lhs.value.collaboration, rhs.value.collaboration),
480 tags: firstNonEmpty(winner.tags, lhs.value.tags, rhs.value.tags)
479481 ),
480482 orderKey: mergedOrderKey(lhs.orderKey, rhs.orderKey)
481483 )
@@ -488,6 +490,13 @@ enum CloudSyncMerge {
488490 SyncOrderKey.isAtLeastAsRecent(lhs, rhs) ? lhs : rhs
489491 }
490492
493 /// Records written before tags were carried decode with none at all, so an
494 /// empty list means "this side has nothing to say" rather than "cleared".
495 /// Without the fallback the first sync after the fix would still drop them.
496 private static func firstNonEmpty(_ candidates: [String]...) -> [String] {
497 candidates.first { !$0.isEmpty } ?? []
498 }
499
491500 private static func preferredTrackedDomain(
492501 _ lhs: SyncRow<TrackedDomain>,
493502 _ rhs: SyncRow<TrackedDomain>
DomainDigTests/CloudSyncMergeTests.swift +43 −1
@@ -204,10 +204,51 @@ final class CloudSyncMergeTests: XCTestCase {
204204 Date(timeIntervalSinceReferenceDate: offset)
205205 }
206206
207 // MARK: Tags
208
209 /// `payloadTrackedDomain` rebuilds the row field by field, and every field
210 /// it forgets is written back to the local store by `applyMergedPayload`.
211 /// It used to forget `tags`, so one sync emptied every tracked domain's
212 /// tags and the watchlist's tag filter with them.
213 func testTagsSurviveThePayloadSanitizer() {
214 let trackedDomain = TrackedDomain(
215 domain: "example.com",
216 createdAt: at(0),
217 updatedAt: at(100),
218 tags: ["client-a", "production"]
219 )
220
221 let sanitized = CloudSyncMerge.payloadTrackedDomain(trackedDomain)
222
223 XCTAssertEqual(sanitized.tags, ["client-a", "production"])
224 }
225
226 func testTagsSurviveAMerge() {
227 let local = trackedDomainRow("example.com", tags: ["client-a"], serverDate: at(100), deviceDate: at(100))
228 let remote = trackedDomainRow("example.com", tags: ["client-b"], serverDate: at(200), deviceDate: at(200))
229
230 let merged = CloudSyncMerge.mergeTrackedDomains([local], [remote])
231
232 XCTAssertEqual(merged.domains.first?.value.tags, ["client-b"])
233 }
234
235 /// Records written before tags were carried decode with none at all, so an
236 /// empty list means "this side has nothing to say", not "cleared".
237 /// Otherwise the first sync after the fix would still drop them.
238 func testTagsFallBackWhenTheWinningSideHasNone() {
239 let local = trackedDomainRow("example.com", tags: ["client-a"], serverDate: at(100), deviceDate: at(100))
240 let legacyRemote = trackedDomainRow("example.com", tags: [], serverDate: at(200), deviceDate: at(200))
241
242 let merged = CloudSyncMerge.mergeTrackedDomains([local], [legacyRemote])
243
244 XCTAssertEqual(merged.domains.first?.value.tags, ["client-a"])
245 }
246
207247 private func trackedDomainRow(
208248 _ domain: String,
209249 note: String? = nil,
210250 isPinned: Bool = false,
251 tags: [String] = [],
211252 serverDate: Date?,
212253 deviceDate: Date
213254 ) -> SyncRow<TrackedDomain> {
@@ -217,7 +258,8 @@ final class CloudSyncMergeTests: XCTestCase {
217258 createdAt: at(0),
218259 updatedAt: deviceDate,
219260 note: note,
220 isPinned: isPinned
261 isPinned: isPinned,
262 tags: tags
221263 ),
222264 orderKey: SyncOrderKey(serverDate: serverDate, deviceDate: deviceDate)
223265 )
DomainDigTests/DomainDataPortabilityServiceTests.swift +19
@@ -65,6 +65,25 @@ final class DomainDataPortabilityServiceTests: XCTestCase {
6565
6666 // MARK: - Merge / dedup
6767
68 /// `CloudSyncService` decides whether a row has been pushed by comparing a
69 /// recorded date against the row's own `updatedAt` after it has been written
70 /// to the store and read back. If that comparison were lossy, every row
71 /// would read as unpushed forever and local would always win every merge.
72 func testUpdatedAtSurvivesAStoreRoundTripExactly() {
73 let updatedAt = Date()
74 let trackedDomain = TrackedDomain(
75 domain: "example.com",
76 createdAt: updatedAt.addingTimeInterval(-3600),
77 updatedAt: updatedAt
78 )
79
80 DomainDataPortabilityService.saveTrackedDomains([trackedDomain], defaults: defaults)
81 let loaded = DomainDataPortabilityService.loadTrackedDomains(defaults: defaults)
82
83 XCTAssertEqual(loaded.count, 1)
84 XCTAssertEqual(loaded.first?.updatedAt, updatedAt)
85 }
86
6887 func testSaveLoadDeduplicatesSameDomainCaseInsensitively() {
6988 let older = TrackedDomain(
7089 domain: "example.com",