Commit b2991e55fc
Verified · cmc
Layout: unified · split
DomainDig.xcodeproj/project.pbxproj +4
| @@ -31,6 +31,7 @@ | |||
| 31 | A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; }; | 31 | A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; }; |
| 32 | A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */; }; | 32 | A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */; }; |
| 33 | A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */; }; | 33 | A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */; }; |
| 34 | A1B2C3D40000000000001002 /* DomainViewModelStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000001001 /* DomainViewModelStoreTests.swift */; }; | ||
| 34 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; | 35 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; |
| 35 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; }; | 36 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; }; |
| 36 | A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; }; | 37 | A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; }; |
| @@ -109,6 +110,7 @@ | |||
| 109 | A1B2C3D40000000000000701 /* SMTPMessageTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = SMTPMessageTests.swift; sourceTree = "<group>"; }; | 110 | A1B2C3D40000000000000701 /* SMTPMessageTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = SMTPMessageTests.swift; sourceTree = "<group>"; }; |
| 110 | A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = LocalAPIRoutingTests.swift; sourceTree = "<group>"; }; | 111 | A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = LocalAPIRoutingTests.swift; sourceTree = "<group>"; }; |
| 111 | A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = MonitoringSchedulingTests.swift; sourceTree = "<group>"; }; | 112 | A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = MonitoringSchedulingTests.swift; sourceTree = "<group>"; }; |
| 113 | A1B2C3D40000000000001001 /* DomainViewModelStoreTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainViewModelStoreTests.swift; sourceTree = "<group>"; }; | ||
| 112 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; | 114 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; |
| 113 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; }; | 115 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; }; |
| 114 | A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; }; | 116 | A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; }; |
| @@ -252,6 +254,7 @@ | |||
| 252 | A1B2C3D40000000000000701 /* SMTPMessageTests.swift */, | 254 | A1B2C3D40000000000000701 /* SMTPMessageTests.swift */, |
| 253 | A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */, | 255 | A1B2C3D40000000000000801 /* LocalAPIRoutingTests.swift */, |
| 254 | A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */, | 256 | A1B2C3D40000000000000901 /* MonitoringSchedulingTests.swift */, |
| 257 | A1B2C3D40000000000001001 /* DomainViewModelStoreTests.swift */, | ||
| 255 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */, | 258 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */, |
| 256 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */, | 259 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */, |
| 257 | A1B2C3D40000000000000501 /* DomainNameTests.swift */, | 260 | A1B2C3D40000000000000501 /* DomainNameTests.swift */, |
| @@ -520,6 +523,7 @@ | |||
| 520 | A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */, | 523 | A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */, |
| 521 | A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */, | 524 | A1B2C3D40000000000000802 /* LocalAPIRoutingTests.swift in Sources */, |
| 522 | A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */, | 525 | A1B2C3D40000000000000902 /* MonitoringSchedulingTests.swift in Sources */, |
| 526 | A1B2C3D40000000000001002 /* DomainViewModelStoreTests.swift in Sources */, | ||
| 523 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, | 527 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, |
| 524 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */, | 528 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */, |
| 525 | A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */, | 529 | A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */, |
DomainDig/DomainMonitoringService.swift +18 −18
| @@ -17,36 +17,36 @@ enum MonitoringStorage { | |||
| 17 | static let historyKey = "lookupHistory" | 17 | static let historyKey = "lookupHistory" |
| 18 | static let maxLogs = 40 | 18 | static let maxLogs = 40 |
| 19 | 19 | ||
| 20 | static func loadSettings() -> MonitoringSettings { | 20 | static func loadSettings(defaults: UserDefaults = .standard) -> MonitoringSettings { |
| 21 | DataMigrationService.migrateIfNeeded() | 21 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 22 | return DomainDataPortabilityService.loadMonitoringSettings() | 22 | return DomainDataPortabilityService.loadMonitoringSettings(defaults: defaults) |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | static func saveSettings(_ settings: MonitoringSettings) { | 25 | static func saveSettings(_ settings: MonitoringSettings, defaults: UserDefaults = .standard) { |
| 26 | DomainDataPortabilityService.saveMonitoringSettings(settings) | 26 | DomainDataPortabilityService.saveMonitoringSettings(settings, defaults: defaults) |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | static func loadLogs() -> [MonitoringLog] { | 29 | static func loadLogs(defaults: UserDefaults = .standard) -> [MonitoringLog] { |
| 30 | DataMigrationService.migrateIfNeeded() | 30 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 31 | return DomainDataPortabilityService.loadMonitoringLogs() | 31 | return DomainDataPortabilityService.loadMonitoringLogs(defaults: defaults) |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | static func saveLogs(_ logs: [MonitoringLog]) { | 34 | static func saveLogs(_ logs: [MonitoringLog], defaults: UserDefaults = .standard) { |
| 35 | DomainDataPortabilityService.saveMonitoringLogs(Array(logs.prefix(maxLogs))) | 35 | DomainDataPortabilityService.saveMonitoringLogs(Array(logs.prefix(maxLogs)), defaults: defaults) |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | static func loadTrackedDomains() -> [TrackedDomain] { | 38 | static func loadTrackedDomains(defaults: UserDefaults = .standard) -> [TrackedDomain] { |
| 39 | DataMigrationService.migrateIfNeeded() | 39 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 40 | return DomainDataPortabilityService.loadTrackedDomains() | 40 | return DomainDataPortabilityService.loadTrackedDomains(defaults: defaults) |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | static func saveTrackedDomains(_ domains: [TrackedDomain]) { | 43 | static func saveTrackedDomains(_ domains: [TrackedDomain], defaults: UserDefaults = .standard) { |
| 44 | DomainDataPortabilityService.saveTrackedDomains(domains) | 44 | DomainDataPortabilityService.saveTrackedDomains(domains, defaults: defaults) |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | static func loadHistoryEntries() -> [HistoryEntry] { | 47 | static func loadHistoryEntries(defaults: UserDefaults = .standard) -> [HistoryEntry] { |
| 48 | DataMigrationService.migrateIfNeeded() | 48 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 49 | return DomainDataPortabilityService.loadHistoryEntries() | 49 | return DomainDataPortabilityService.loadHistoryEntries(defaults: defaults) |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | static func saveHistoryEntries(_ entries: [HistoryEntry]) { | 52 | static func saveHistoryEntries(_ entries: [HistoryEntry]) { |
DomainDig/DomainViewModel+Export.swift +10 −9
| @@ -59,27 +59,27 @@ extension DomainViewModel { | |||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | func exportFullBackupData() -> Data? { | 61 | func exportFullBackupData() -> Data? { |
| 62 | try? DomainDataPortabilityService.backupData() | 62 | try? DomainDataPortabilityService.backupData(defaults: defaults) |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | func exportPortableTrackedDomainsJSONData() -> Data? { | 65 | func exportPortableTrackedDomainsJSONData() -> Data? { |
| 66 | try? DomainDataPortabilityService.trackedDomainsExportData() | 66 | try? DomainDataPortabilityService.trackedDomainsExportData(defaults: defaults) |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | func exportPortableTrackedDomainsCSV() -> String { | 69 | func exportPortableTrackedDomainsCSV() -> String { |
| 70 | DomainDataPortabilityService.trackedDomainsCSV() | 70 | DomainDataPortabilityService.trackedDomainsCSV(defaults: defaults) |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | func exportPortableWorkflowsJSONData() -> Data? { | 73 | func exportPortableWorkflowsJSONData() -> Data? { |
| 74 | try? DomainDataPortabilityService.workflowsExportData() | 74 | try? DomainDataPortabilityService.workflowsExportData(defaults: defaults) |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | func exportPortableWorkflowsCSV() -> String { | 77 | func exportPortableWorkflowsCSV() -> String { |
| 78 | DomainDataPortabilityService.workflowsCSV() | 78 | DomainDataPortabilityService.workflowsCSV(defaults: defaults) |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | func exportPortableHistoryJSONData() -> Data? { | 81 | func exportPortableHistoryJSONData() -> Data? { |
| 82 | try? DomainDataPortabilityService.historyExportData() | 82 | try? DomainDataPortabilityService.historyExportData(defaults: defaults) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | func prepareDataImport( | 85 | func prepareDataImport( |
| @@ -87,11 +87,11 @@ extension DomainViewModel { | |||
| 87 | fileName: String, | 87 | fileName: String, |
| 88 | mode: DataPortabilityImportMode | 88 | mode: DataPortabilityImportMode |
| 89 | ) throws -> DataImportPreview { | 89 | ) throws -> DataImportPreview { |
| 90 | try DomainDataPortabilityService.prepareImport(data: data, fileName: fileName, mode: mode) | 90 | try DomainDataPortabilityService.prepareImport(data: data, fileName: fileName, mode: mode, defaults: defaults) |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | func applyDataImport(_ preview: DataImportPreview, mode: DataPortabilityImportMode) throws -> DataImportResult { | 93 | func applyDataImport(_ preview: DataImportPreview, mode: DataPortabilityImportMode) throws -> DataImportResult { |
| 94 | let result = try DomainDataPortabilityService.applyImport(preview, mode: mode) | 94 | let result = try DomainDataPortabilityService.applyImport(preview, mode: mode, defaults: defaults) |
| 95 | refreshPersistedData() | 95 | refreshPersistedData() |
| 96 | portabilityStatusMessage = result.summary | 96 | portabilityStatusMessage = result.summary |
| 97 | CloudSyncService.shared.markAppSettingsChanged() | 97 | CloudSyncService.shared.markAppSettingsChanged() |
| @@ -106,7 +106,8 @@ extension DomainViewModel { | |||
| 106 | recentSearches: recentSearches, | 106 | recentSearches: recentSearches, |
| 107 | resolverURLString: resolverURLString, | 107 | resolverURLString: resolverURLString, |
| 108 | appDensityRawValue: appDensityRawValue | 108 | appDensityRawValue: appDensityRawValue |
| 109 | ) | 109 | ), |
| 110 | defaults: defaults | ||
| 110 | ) | 111 | ) |
| 111 | CloudSyncService.shared.markAppSettingsChanged() | 112 | CloudSyncService.shared.markAppSettingsChanged() |
| 112 | refreshDataLifecycleSummary() | 113 | refreshDataLifecycleSummary() |
DomainDig/DomainViewModel+Monitoring.swift +1 −1
| @@ -112,7 +112,7 @@ extension DomainViewModel { | |||
| 112 | guard let index = trackedDomains.firstIndex(where: { $0.id == trackedDomain.id }) else { return } | 112 | guard let index = trackedDomains.firstIndex(where: { $0.id == trackedDomain.id }) else { return } |
| 113 | trackedDomains[index].monitoringEnabled.toggle() | 113 | trackedDomains[index].monitoringEnabled.toggle() |
| 114 | trackedDomains[index].updatedAt = Date() | 114 | trackedDomains[index].updatedAt = Date() |
| 115 | MonitoringStorage.saveTrackedDomains(trackedDomains) | 115 | MonitoringStorage.saveTrackedDomains(trackedDomains, defaults: defaults) |
| 116 | CloudSyncService.shared.scheduleSyncIfNeeded() | 116 | CloudSyncService.shared.scheduleSyncIfNeeded() |
| 117 | sanitizeMonitoringSelection() | 117 | sanitizeMonitoringSelection() |
| 118 | } | 118 | } |
DomainDig/DomainViewModel+Workflows.swift +1 −1
| @@ -132,7 +132,7 @@ extension DomainViewModel { | |||
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | func refreshWorkflowList() async { | 134 | func refreshWorkflowList() async { |
| 135 | workflows = Self.loadWorkflows() | 135 | workflows = Self.loadWorkflows(defaults: defaults) |
| 136 | await Task.yield() | 136 | await Task.yield() |
| 137 | } | 137 | } |
| 138 | } | 138 | } |
DomainDig/DomainViewModel.swift +75 −55
| @@ -294,19 +294,19 @@ final class DomainViewModel { | |||
| 294 | 294 | ||
| 295 | private static let recentSearchesKey = "recentSearches" | 295 | private static let recentSearchesKey = "recentSearches" |
| 296 | private static let maxRecent = 20 | 296 | private static let maxRecent = 20 |
| 297 | var recentSearches: [String] = DomainDataPortabilityService.loadRecentSearches() | 297 | var recentSearches: [String] |
| 298 | 298 | ||
| 299 | 299 | ||
| 300 | private static let trackedDomainsKey = "trackedDomains" | 300 | private static let trackedDomainsKey = "trackedDomains" |
| 301 | private static let legacyWatchedDomainsKey = "watchedDomains" | 301 | private static let legacyWatchedDomainsKey = "watchedDomains" |
| 302 | var trackedDomains: [TrackedDomain] = DomainViewModel.loadTrackedDomains() | 302 | var trackedDomains: [TrackedDomain] |
| 303 | 303 | ||
| 304 | private static let historyKey = "lookupHistory" | 304 | private static let historyKey = "lookupHistory" |
| 305 | private static let maxHistory = 250 | 305 | private static let maxHistory = 250 |
| 306 | var history: [HistoryEntry] = DomainViewModel.loadHistoryEntries() | 306 | var history: [HistoryEntry] |
| 307 | var auditSessions: [AuditSession] = DomainDataPortabilityService.loadAuditSessions() | 307 | var auditSessions: [AuditSession] |
| 308 | private static let workflowsKey = "domainWorkflows" | 308 | private static let workflowsKey = "domainWorkflows" |
| 309 | var workflows: [DomainWorkflow] = DomainViewModel.loadWorkflows() | 309 | var workflows: [DomainWorkflow] |
| 310 | var historySearchText = "" | 310 | var historySearchText = "" |
| 311 | var historyDateFilter: HistoryDateFilter = .all | 311 | var historyDateFilter: HistoryDateFilter = .all |
| 312 | var historyChangeFilter: ChangeFilterOption = .all | 312 | var historyChangeFilter: ChangeFilterOption = .all |
| @@ -317,15 +317,15 @@ final class DomainViewModel { | |||
| 317 | var watchlistFilter: WatchlistFilterOption = .all | 317 | var watchlistFilter: WatchlistFilterOption = .all |
| 318 | var watchlistSortOption: WatchlistSortOption = .pinned | 318 | var watchlistSortOption: WatchlistSortOption = .pinned |
| 319 | var watchlistTagFilter: String? | 319 | var watchlistTagFilter: String? |
| 320 | var watchlistSavedViews: [WatchlistSavedView] = DomainViewModel.loadWatchlistSavedViews() | 320 | var watchlistSavedViews: [WatchlistSavedView] |
| 321 | var dashboardSearchText = "" | 321 | var dashboardSearchText = "" |
| 322 | var dashboardFilter: PortfolioFilterOption = .all | 322 | var dashboardFilter: PortfolioFilterOption = .all |
| 323 | var monitoringSettings: MonitoringSettings = MonitoringStorage.loadSettings() | 323 | var monitoringSettings: MonitoringSettings |
| 324 | var monitoringLogs: [MonitoringLog] = MonitoringStorage.loadLogs() | 324 | var monitoringLogs: [MonitoringLog] |
| 325 | var monitoringRunInProgress = false | 325 | var monitoringRunInProgress = false |
| 326 | var monitoringStatusMessage: String? | 326 | var monitoringStatusMessage: String? |
| 327 | var monitoringNotificationStatus: UNAuthorizationStatus = .notDetermined | 327 | var monitoringNotificationStatus: UNAuthorizationStatus = .notDetermined |
| 328 | var dataLifecycleSummary = DomainDataPortabilityService.lifecycleSummary() | 328 | var dataLifecycleSummary: DataLifecycleSummary |
| 329 | var portabilityStatusMessage: String? | 329 | var portabilityStatusMessage: String? |
| 330 | var upgradePrompt: UpgradePromptContext? | 330 | var upgradePrompt: UpgradePromptContext? |
| 331 | var isPaywallPresented = false | 331 | var isPaywallPresented = false |
| @@ -334,7 +334,27 @@ final class DomainViewModel { | |||
| 334 | var activeDiffChangeIndex = 0 | 334 | var activeDiffChangeIndex = 0 |
| 335 | 335 | ||
| 336 | private static let historyAutoPruneKey = "historyAutoPrune" | 336 | private static let historyAutoPruneKey = "historyAutoPrune" |
| 337 | var historyAutoPruneOption: HistoryAutoPruneOption = DomainViewModel.loadHistoryAutoPruneOption() | 337 | var historyAutoPruneOption: HistoryAutoPruneOption |
| 338 | |||
| 339 | /// The store every persisted property is read from and written back to. | ||
| 340 | /// Injected so a test can drive the whole lifecycle — history pruning, | ||
| 341 | /// tracking limits, batch state — against a scratch store instead of the | ||
| 342 | /// one the app is using. | ||
| 343 | let defaults: UserDefaults | ||
| 344 | |||
| 345 | init(defaults: UserDefaults = .standard) { | ||
| 346 | self.defaults = defaults | ||
| 347 | recentSearches = DomainDataPortabilityService.loadRecentSearches(defaults: defaults) | ||
| 348 | trackedDomains = DomainViewModel.loadTrackedDomains(defaults: defaults) | ||
| 349 | history = DomainViewModel.loadHistoryEntries(defaults: defaults) | ||
| 350 | auditSessions = DomainDataPortabilityService.loadAuditSessions(defaults: defaults) | ||
| 351 | workflows = DomainViewModel.loadWorkflows(defaults: defaults) | ||
| 352 | watchlistSavedViews = DomainViewModel.loadWatchlistSavedViews(defaults: defaults) | ||
| 353 | monitoringSettings = MonitoringStorage.loadSettings(defaults: defaults) | ||
| 354 | monitoringLogs = MonitoringStorage.loadLogs(defaults: defaults) | ||
| 355 | dataLifecycleSummary = DomainDataPortabilityService.lifecycleSummary(defaults: defaults) | ||
| 356 | historyAutoPruneOption = DomainViewModel.loadHistoryAutoPruneOption(defaults: defaults) | ||
| 357 | } | ||
| 338 | 358 | ||
| 339 | var trimmedDomain: String { | 359 | var trimmedDomain: String { |
| 340 | domain | 360 | domain |
| @@ -904,8 +924,8 @@ final class DomainViewModel { | |||
| 904 | 924 | ||
| 905 | private static let watchlistSavedViewsKey = "watchlistSavedViews" | 925 | private static let watchlistSavedViewsKey = "watchlistSavedViews" |
| 906 | 926 | ||
| 907 | private static func loadWatchlistSavedViews() -> [WatchlistSavedView] { | 927 | private static func loadWatchlistSavedViews(defaults: UserDefaults) -> [WatchlistSavedView] { |
| 908 | guard let data = UserDefaults.standard.data(forKey: watchlistSavedViewsKey), | 928 | guard let data = defaults.data(forKey: watchlistSavedViewsKey), |
| 909 | let views = try? JSONDecoder().decode([WatchlistSavedView].self, from: data) | 929 | let views = try? JSONDecoder().decode([WatchlistSavedView].self, from: data) |
| 910 | else { return [] } | 930 | else { return [] } |
| 911 | return views | 931 | return views |
| @@ -913,7 +933,7 @@ final class DomainViewModel { | |||
| 913 | 933 | ||
| 914 | private func persistWatchlistSavedViews() { | 934 | private func persistWatchlistSavedViews() { |
| 915 | guard let data = try? JSONEncoder().encode(watchlistSavedViews) else { return } | 935 | guard let data = try? JSONEncoder().encode(watchlistSavedViews) else { return } |
| 916 | UserDefaults.standard.set(data, forKey: Self.watchlistSavedViewsKey) | 936 | defaults.set(data, forKey: Self.watchlistSavedViewsKey) |
| 917 | } | 937 | } |
| 918 | 938 | ||
| 919 | private static func normalizedTags(_ tags: [String]) -> [String] { | 939 | private static func normalizedTags(_ tags: [String]) -> [String] { |
| @@ -970,7 +990,7 @@ final class DomainViewModel { | |||
| 970 | 990 | ||
| 971 | func clearRecentSearches() { | 991 | func clearRecentSearches() { |
| 972 | recentSearches.removeAll() | 992 | recentSearches.removeAll() |
| 973 | DomainDataPortabilityService.saveRecentSearches([]) | 993 | DomainDataPortabilityService.saveRecentSearches([], defaults: defaults) |
| 974 | CloudSyncService.shared.markAppSettingsChanged() | 994 | CloudSyncService.shared.markAppSettingsChanged() |
| 975 | refreshDataLifecycleSummary() | 995 | refreshDataLifecycleSummary() |
| 976 | } | 996 | } |
| @@ -981,20 +1001,20 @@ final class DomainViewModel { | |||
| 981 | // scene activation); the disk reload below would wipe the fixtures. | 1001 | // scene activation); the disk reload below would wipe the fixtures. |
| 982 | if auditFixturesActive { return } | 1002 | if auditFixturesActive { return } |
| 983 | #endif | 1003 | #endif |
| 984 | DataMigrationService.migrateIfNeeded() | 1004 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 985 | trackedDomains = Self.loadTrackedDomains() | 1005 | trackedDomains = Self.loadTrackedDomains(defaults: defaults) |
| 986 | history = Self.loadHistoryEntries() | 1006 | history = Self.loadHistoryEntries(defaults: defaults) |
| 987 | monitoringSettings = MonitoringStorage.sanitizeSettings( | 1007 | monitoringSettings = MonitoringStorage.sanitizeSettings( |
| 988 | MonitoringStorage.loadSettings(), | 1008 | MonitoringStorage.loadSettings(defaults: defaults), |
| 989 | trackedDomains: trackedDomains | 1009 | trackedDomains: trackedDomains |
| 990 | ) | 1010 | ) |
| 991 | monitoringLogs = MonitoringStorage.loadLogs() | 1011 | monitoringLogs = MonitoringStorage.loadLogs(defaults: defaults) |
| 992 | persistMonitoringSettings() | 1012 | persistMonitoringSettings() |
| 993 | refreshDataLifecycleSummary() | 1013 | refreshDataLifecycleSummary() |
| 994 | } | 1014 | } |
| 995 | 1015 | ||
| 996 | func refreshDataLifecycleSummary() { | 1016 | func refreshDataLifecycleSummary() { |
| 997 | dataLifecycleSummary = DomainDataPortabilityService.lifecycleSummary() | 1017 | dataLifecycleSummary = DomainDataPortabilityService.lifecycleSummary(defaults: defaults) |
| 998 | } | 1018 | } |
| 999 | 1019 | ||
| 1000 | func refreshPersistedData() { | 1020 | func refreshPersistedData() { |
| @@ -1002,16 +1022,16 @@ final class DomainViewModel { | |||
| 1002 | // A reload from disk would silently replace the in-memory fixtures. | 1022 | // A reload from disk would silently replace the in-memory fixtures. |
| 1003 | if auditFixturesActive { return } | 1023 | if auditFixturesActive { return } |
| 1004 | #endif | 1024 | #endif |
| 1005 | recentSearches = DomainDataPortabilityService.loadRecentSearches() | 1025 | recentSearches = DomainDataPortabilityService.loadRecentSearches(defaults: defaults) |
| 1006 | trackedDomains = Self.loadTrackedDomains() | 1026 | trackedDomains = Self.loadTrackedDomains(defaults: defaults) |
| 1007 | history = Self.loadHistoryEntries() | 1027 | history = Self.loadHistoryEntries(defaults: defaults) |
| 1008 | auditSessions = DomainDataPortabilityService.loadAuditSessions() | 1028 | auditSessions = DomainDataPortabilityService.loadAuditSessions(defaults: defaults) |
| 1009 | workflows = Self.loadWorkflows() | 1029 | workflows = Self.loadWorkflows(defaults: defaults) |
| 1010 | monitoringSettings = MonitoringStorage.sanitizeSettings( | 1030 | monitoringSettings = MonitoringStorage.sanitizeSettings( |
| 1011 | MonitoringStorage.loadSettings(), | 1031 | MonitoringStorage.loadSettings(defaults: defaults), |
| 1012 | trackedDomains: trackedDomains | 1032 | trackedDomains: trackedDomains |
| 1013 | ) | 1033 | ) |
| 1014 | monitoringLogs = MonitoringStorage.loadLogs() | 1034 | monitoringLogs = MonitoringStorage.loadLogs(defaults: defaults) |
| 1015 | refreshDataLifecycleSummary() | 1035 | refreshDataLifecycleSummary() |
| 1016 | } | 1036 | } |
| 1017 | 1037 | ||
| @@ -1049,14 +1069,14 @@ final class DomainViewModel { | |||
| 1049 | activeDiffChangeIndex = 0 | 1069 | activeDiffChangeIndex = 0 |
| 1050 | latestBatchSweepSummary = nil | 1070 | latestBatchSweepSummary = nil |
| 1051 | latestWorkflowRunSummary = nil | 1071 | latestWorkflowRunSummary = nil |
| 1052 | historyAutoPruneOption = Self.loadHistoryAutoPruneOption() | 1072 | historyAutoPruneOption = Self.loadHistoryAutoPruneOption(defaults: defaults) |
| 1053 | refreshDataLifecycleSummary() | 1073 | refreshDataLifecycleSummary() |
| 1054 | await refreshMonitoringAuthorizationStatus() | 1074 | await refreshMonitoringAuthorizationStatus() |
| 1055 | } | 1075 | } |
| 1056 | 1076 | ||
| 1057 | func rerunLookup(from entry: HistoryEntry, useSnapshotResolver: Bool) { | 1077 | func rerunLookup(from entry: HistoryEntry, useSnapshotResolver: Bool) { |
| 1058 | if useSnapshotResolver { | 1078 | if useSnapshotResolver { |
| 1059 | UserDefaults.standard.set(entry.resolverURLString, forKey: DNSResolverOption.userDefaultsKey) | 1079 | defaults.set(entry.resolverURLString, forKey: DNSResolverOption.userDefaultsKey) |
| 1060 | } | 1080 | } |
| 1061 | domain = entry.domain | 1081 | domain = entry.domain |
| 1062 | run() | 1082 | run() |
| @@ -1065,7 +1085,7 @@ final class DomainViewModel { | |||
| 1065 | 1085 | ||
| 1066 | func rerunInspection(for trackedDomain: TrackedDomain, useSnapshotResolver: Bool) { | 1086 | func rerunInspection(for trackedDomain: TrackedDomain, useSnapshotResolver: Bool) { |
| 1067 | if useSnapshotResolver, let snapshot = latestSnapshot(for: trackedDomain) { | 1087 | if useSnapshotResolver, let snapshot = latestSnapshot(for: trackedDomain) { |
| 1068 | UserDefaults.standard.set(snapshot.resolverURLString, forKey: DNSResolverOption.userDefaultsKey) | 1088 | defaults.set(snapshot.resolverURLString, forKey: DNSResolverOption.userDefaultsKey) |
| 1069 | } | 1089 | } |
| 1070 | domain = trackedDomain.domain | 1090 | domain = trackedDomain.domain |
| 1071 | run() | 1091 | run() |
| @@ -1800,19 +1820,19 @@ final class DomainViewModel { | |||
| 1800 | return | 1820 | return |
| 1801 | } | 1821 | } |
| 1802 | let persistStartedAt = DomainDebugLog.signpostStart("DomainViewModel.persistHistory") | 1822 | let persistStartedAt = DomainDebugLog.signpostStart("DomainViewModel.persistHistory") |
| 1803 | DomainDataPortabilityService.saveHistoryEntries(history) | 1823 | DomainDataPortabilityService.saveHistoryEntries(history, defaults: defaults) |
| 1804 | refreshDataLifecycleSummary() | 1824 | refreshDataLifecycleSummary() |
| 1805 | DomainDebugLog.signpostEnd("DomainViewModel.persistHistory", start: persistStartedAt, extra: "count=\(history.count)") | 1825 | DomainDebugLog.signpostEnd("DomainViewModel.persistHistory", start: persistStartedAt, extra: "count=\(history.count)") |
| 1806 | } | 1826 | } |
| 1807 | 1827 | ||
| 1808 | func persistAuditSessions() { | 1828 | func persistAuditSessions() { |
| 1809 | DomainDataPortabilityService.saveAuditSessions(auditSessions) | 1829 | DomainDataPortabilityService.saveAuditSessions(auditSessions, defaults: defaults) |
| 1810 | refreshDataLifecycleSummary() | 1830 | refreshDataLifecycleSummary() |
| 1811 | } | 1831 | } |
| 1812 | 1832 | ||
| 1813 | func setHistoryAutoPruneOption(_ option: HistoryAutoPruneOption) { | 1833 | func setHistoryAutoPruneOption(_ option: HistoryAutoPruneOption) { |
| 1814 | historyAutoPruneOption = option | 1834 | historyAutoPruneOption = option |
| 1815 | UserDefaults.standard.set(option.rawValue, forKey: Self.historyAutoPruneKey) | 1835 | defaults.set(option.rawValue, forKey: Self.historyAutoPruneKey) |
| 1816 | trimHistoryToLimit() | 1836 | trimHistoryToLimit() |
| 1817 | persistHistory() | 1837 | persistHistory() |
| 1818 | } | 1838 | } |
| @@ -1846,7 +1866,7 @@ final class DomainViewModel { | |||
| 1846 | trackedDomainsPersistenceDirty = true | 1866 | trackedDomainsPersistenceDirty = true |
| 1847 | return | 1867 | return |
| 1848 | } | 1868 | } |
| 1849 | DomainDataPortabilityService.saveTrackedDomains(trackedDomains) | 1869 | DomainDataPortabilityService.saveTrackedDomains(trackedDomains, defaults: defaults) |
| 1850 | CloudSyncService.shared.scheduleSyncIfNeeded() | 1870 | CloudSyncService.shared.scheduleSyncIfNeeded() |
| 1851 | refreshDataLifecycleSummary() | 1871 | refreshDataLifecycleSummary() |
| 1852 | refreshWidgetData() | 1872 | refreshWidgetData() |
| @@ -1865,13 +1885,13 @@ final class DomainViewModel { | |||
| 1865 | 1885 | ||
| 1866 | if trackedDomainsPersistenceDirty { | 1886 | if trackedDomainsPersistenceDirty { |
| 1867 | trackedDomainsPersistenceDirty = false | 1887 | trackedDomainsPersistenceDirty = false |
| 1868 | DomainDataPortabilityService.saveTrackedDomains(trackedDomains) | 1888 | DomainDataPortabilityService.saveTrackedDomains(trackedDomains, defaults: defaults) |
| 1869 | CloudSyncService.shared.scheduleSyncIfNeeded() | 1889 | CloudSyncService.shared.scheduleSyncIfNeeded() |
| 1870 | } | 1890 | } |
| 1871 | 1891 | ||
| 1872 | if historyPersistenceDirty { | 1892 | if historyPersistenceDirty { |
| 1873 | historyPersistenceDirty = false | 1893 | historyPersistenceDirty = false |
| 1874 | DomainDataPortabilityService.saveHistoryEntries(history) | 1894 | DomainDataPortabilityService.saveHistoryEntries(history, defaults: defaults) |
| 1875 | } | 1895 | } |
| 1876 | 1896 | ||
| 1877 | refreshDataLifecycleSummary() | 1897 | refreshDataLifecycleSummary() |
| @@ -1895,20 +1915,20 @@ final class DomainViewModel { | |||
| 1895 | 1915 | ||
| 1896 | func persistMonitoringSettings(localActivationConfirmed: Bool = false) { | 1916 | func persistMonitoringSettings(localActivationConfirmed: Bool = false) { |
| 1897 | monitoringSettings = MonitoringStorage.sanitizeSettings(monitoringSettings, trackedDomains: trackedDomains) | 1917 | monitoringSettings = MonitoringStorage.sanitizeSettings(monitoringSettings, trackedDomains: trackedDomains) |
| 1898 | MonitoringStorage.saveSettings(monitoringSettings) | 1918 | MonitoringStorage.saveSettings(monitoringSettings, defaults: defaults) |
| 1899 | CloudSyncService.shared.markMonitoringSettingsChanged(localActivationConfirmed: localActivationConfirmed) | 1919 | CloudSyncService.shared.markMonitoringSettingsChanged(localActivationConfirmed: localActivationConfirmed) |
| 1900 | } | 1920 | } |
| 1901 | 1921 | ||
| 1902 | func sanitizeMonitoringSelection() { | 1922 | func sanitizeMonitoringSelection() { |
| 1903 | monitoringSettings = MonitoringStorage.sanitizeSettings(monitoringSettings, trackedDomains: trackedDomains) | 1923 | monitoringSettings = MonitoringStorage.sanitizeSettings(monitoringSettings, trackedDomains: trackedDomains) |
| 1904 | MonitoringStorage.saveSettings(monitoringSettings) | 1924 | MonitoringStorage.saveSettings(monitoringSettings, defaults: defaults) |
| 1905 | CloudSyncService.shared.markMonitoringSettingsChanged(localActivationConfirmed: false) | 1925 | CloudSyncService.shared.markMonitoringSettingsChanged(localActivationConfirmed: false) |
| 1906 | } | 1926 | } |
| 1907 | 1927 | ||
| 1908 | private func clearMonitoringLogs() { | 1928 | private func clearMonitoringLogs() { |
| 1909 | monitoringLogs.removeAll() | 1929 | monitoringLogs.removeAll() |
| 1910 | monitoringStatusMessage = nil | 1930 | monitoringStatusMessage = nil |
| 1911 | MonitoringStorage.saveLogs([]) | 1931 | MonitoringStorage.saveLogs([], defaults: defaults) |
| 1912 | } | 1932 | } |
| 1913 | 1933 | ||
| 1914 | private func updateTrackedDomainAvailability(for domain: String, status: DomainAvailabilityStatus) { | 1934 | private func updateTrackedDomainAvailability(for domain: String, status: DomainAvailabilityStatus) { |
| @@ -2030,18 +2050,18 @@ final class DomainViewModel { | |||
| 2030 | } | 2050 | } |
| 2031 | } | 2051 | } |
| 2032 | 2052 | ||
| 2033 | private func addRecentSearch(_ domain: String) { | 2053 | func addRecentSearch(_ domain: String) { |
| 2034 | recentSearches.removeAll { $0.lowercased() == domain.lowercased() } | 2054 | recentSearches.removeAll { $0.lowercased() == domain.lowercased() } |
| 2035 | recentSearches.insert(domain, at: 0) | 2055 | recentSearches.insert(domain, at: 0) |
| 2036 | if recentSearches.count > Self.maxRecent { | 2056 | if recentSearches.count > Self.maxRecent { |
| 2037 | recentSearches = Array(recentSearches.prefix(Self.maxRecent)) | 2057 | recentSearches = Array(recentSearches.prefix(Self.maxRecent)) |
| 2038 | } | 2058 | } |
| 2039 | DomainDataPortabilityService.saveRecentSearches(recentSearches) | 2059 | DomainDataPortabilityService.saveRecentSearches(recentSearches, defaults: defaults) |
| 2040 | CloudSyncService.shared.markAppSettingsChanged() | 2060 | CloudSyncService.shared.markAppSettingsChanged() |
| 2041 | refreshDataLifecycleSummary() | 2061 | refreshDataLifecycleSummary() |
| 2042 | } | 2062 | } |
| 2043 | 2063 | ||
| 2044 | private func beginLookup(for target: String, cancelExistingTask: Bool = true) -> UUID { | 2064 | func beginLookup(for target: String, cancelExistingTask: Bool = true) -> UUID { |
| 2045 | if cancelExistingTask { | 2065 | if cancelExistingTask { |
| 2046 | lookupTask?.cancel() | 2066 | lookupTask?.cancel() |
| 2047 | } | 2067 | } |
| @@ -2429,7 +2449,7 @@ final class DomainViewModel { | |||
| 2429 | sections.first(where: { $0.recordType == .A })?.records.first?.value | 2449 | sections.first(where: { $0.recordType == .A })?.records.first?.value |
| 2430 | } | 2450 | } |
| 2431 | 2451 | ||
| 2432 | private func isCurrentLookup(_ lookupID: UUID) -> Bool { | 2452 | func isCurrentLookup(_ lookupID: UUID) -> Bool { |
| 2433 | activeLookupID == lookupID | 2453 | activeLookupID == lookupID |
| 2434 | } | 2454 | } |
| 2435 | 2455 | ||
| @@ -2993,33 +3013,33 @@ final class DomainViewModel { | |||
| 2993 | ) | 3013 | ) |
| 2994 | } | 3014 | } |
| 2995 | 3015 | ||
| 2996 | private static func loadHistoryEntries() -> [HistoryEntry] { | 3016 | private static func loadHistoryEntries(defaults: UserDefaults) -> [HistoryEntry] { |
| 2997 | DataMigrationService.migrateIfNeeded() | 3017 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 2998 | return DomainDataPortabilityService.loadHistoryEntries() | 3018 | return DomainDataPortabilityService.loadHistoryEntries(defaults: defaults) |
| 2999 | } | 3019 | } |
| 3000 | 3020 | ||
| 3001 | private static func loadHistoryAutoPruneOption() -> HistoryAutoPruneOption { | 3021 | private static func loadHistoryAutoPruneOption(defaults: UserDefaults) -> HistoryAutoPruneOption { |
| 3002 | guard let rawValue = UserDefaults.standard.string(forKey: historyAutoPruneKey), | 3022 | guard let rawValue = defaults.string(forKey: historyAutoPruneKey), |
| 3003 | let option = HistoryAutoPruneOption(rawValue: rawValue) else { | 3023 | let option = HistoryAutoPruneOption(rawValue: rawValue) else { |
| 3004 | return .unlimited | 3024 | return .unlimited |
| 3005 | } | 3025 | } |
| 3006 | return option | 3026 | return option |
| 3007 | } | 3027 | } |
| 3008 | 3028 | ||
| 3009 | private static func loadTrackedDomains() -> [TrackedDomain] { | 3029 | private static func loadTrackedDomains(defaults: UserDefaults) -> [TrackedDomain] { |
| 3010 | DataMigrationService.migrateIfNeeded() | 3030 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 3011 | return DomainDataPortabilityService.loadTrackedDomains() | 3031 | return DomainDataPortabilityService.loadTrackedDomains(defaults: defaults) |
| 3012 | } | 3032 | } |
| 3013 | 3033 | ||
| 3014 | func persistWorkflows() { | 3034 | func persistWorkflows() { |
| 3015 | DomainDataPortabilityService.saveWorkflows(workflows) | 3035 | DomainDataPortabilityService.saveWorkflows(workflows, defaults: defaults) |
| 3016 | CloudSyncService.shared.scheduleSyncIfNeeded() | 3036 | CloudSyncService.shared.scheduleSyncIfNeeded() |
| 3017 | refreshDataLifecycleSummary() | 3037 | refreshDataLifecycleSummary() |
| 3018 | } | 3038 | } |
| 3019 | 3039 | ||
| 3020 | static func loadWorkflows() -> [DomainWorkflow] { | 3040 | static func loadWorkflows(defaults: UserDefaults) -> [DomainWorkflow] { |
| 3021 | DataMigrationService.migrateIfNeeded() | 3041 | DataMigrationService.migrateIfNeeded(defaults: defaults) |
| 3022 | return DomainDataPortabilityService.loadWorkflows() | 3042 | return DomainDataPortabilityService.loadWorkflows(defaults: defaults) |
| 3023 | } | 3043 | } |
| 3024 | 3044 | ||
| 3025 | private static func deduplicatedTrackedDomains(_ domains: [TrackedDomain]) -> [TrackedDomain] { | 3045 | private static func deduplicatedTrackedDomains(_ domains: [TrackedDomain]) -> [TrackedDomain] { |
DomainDigTests/DomainNameTests.swift +6 −1
| @@ -107,7 +107,12 @@ final class DomainViewModelInputTests: XCTestCase { | |||
| 107 | /// implicit main-actor-isolated deinit through the iOS 17 back-deployment | 107 | /// implicit main-actor-isolated deinit through the iOS 17 back-deployment |
| 108 | /// shim, which aborts in the simulator. The app holds one for its whole | 108 | /// shim, which aborts in the simulator. The app holds one for its whole |
| 109 | /// lifetime, so nothing releases one there either. | 109 | /// lifetime, so nothing releases one there either. |
| 110 | private static let viewModel = DomainViewModel() | 110 | /// On a scratch store: `trackDomain` persists, so on `.standard` this |
| 111 | /// test wrote "localhost" into the store of whichever app shares the | ||
| 112 | /// simulator. | ||
| 113 | private static let viewModel = DomainViewModel( | ||
| 114 | defaults: UserDefaults(suiteName: "net.cleberg.DomainDig.tests.input") ?? .standard | ||
| 115 | ) | ||
| 111 | 116 | ||
| 112 | func testAnInvalidDomainDoesNotStartALookup() { | 117 | func testAnInvalidDomainDoesNotStartALookup() { |
| 113 | let viewModel = Self.viewModel | 118 | let viewModel = Self.viewModel |
DomainDigTests/DomainViewModelStoreTests.swift added +321
| @@ -0,0 +1,321 @@ | |||
| 1 | import XCTest | ||
| 2 | @testable import DomainDig | ||
| 3 | |||
| 4 | /// Every persisted property on the view model used to read from and write to | ||
| 5 | /// `UserDefaults.standard`, so no test could drive its lifecycle without | ||
| 6 | /// writing into the store the app on the same simulator is using. The store is | ||
| 7 | /// injected now. These cover the seam itself and the lifecycle rules that sit | ||
| 8 | /// on top of it: history pruning, recent-search truncation, tracking, and the | ||
| 9 | /// lookup generation that keeps a stale section result off a new snapshot. | ||
| 10 | @MainActor | ||
| 11 | final class DomainViewModelStoreTests: XCTestCase { | ||
| 12 | |||
| 13 | private var suiteName = "" | ||
| 14 | private var scratch = UserDefaults.standard | ||
| 15 | |||
| 16 | override func setUpWithError() throws { | ||
| 17 | try super.setUpWithError() | ||
| 18 | suiteName = "net.cleberg.DomainDig.tests.\(UUID().uuidString)" | ||
| 19 | scratch = try XCTUnwrap(UserDefaults(suiteName: suiteName)) | ||
| 20 | } | ||
| 21 | |||
| 22 | override func tearDown() { | ||
| 23 | UserDefaults.standard.removePersistentDomain(forName: suiteName) | ||
| 24 | super.tearDown() | ||
| 25 | } | ||
| 26 | |||
| 27 | /// Releasing a main-actor-isolated object aborts the test runner on iOS | ||
| 28 | /// 18.6 — finding E1, a Swift runtime bug in `MarkerItem::create` fixed by | ||
| 29 | /// iOS 26.5. `Scripts/test.sh` runs the floor tier, so a view model that | ||
| 30 | /// went out of scope would kill the whole run there. Holding every instance | ||
| 31 | /// for the life of the process avoids the release and needs no app change. | ||
| 32 | private static var retained: [DomainViewModel] = [] | ||
| 33 | |||
| 34 | private func makeViewModel() -> DomainViewModel { | ||
| 35 | let viewModel = DomainViewModel(defaults: scratch) | ||
| 36 | Self.retained.append(viewModel) | ||
| 37 | return viewModel | ||
| 38 | } | ||
| 39 | |||
| 40 | private func makeEntry(domain: String, at timestamp: Date) -> HistoryEntry { | ||
| 41 | HistoryEntry( | ||
| 42 | identity: .init(domain: domain, timestamp: timestamp), | ||
| 43 | inspection: .init( | ||
| 44 | dnsSections: [], | ||
| 45 | sslInfo: nil, | ||
| 46 | httpHeaders: [], | ||
| 47 | reachabilityResults: [], | ||
| 48 | ipGeolocation: nil | ||
| 49 | ), | ||
| 50 | provenance: .init(resolverDisplayName: "Test Resolver", resolverURLString: "") | ||
| 51 | ) | ||
| 52 | } | ||
| 53 | |||
| 54 | private func makeEntries(_ count: Int) -> [HistoryEntry] { | ||
| 55 | (0..<count).map { | ||
| 56 | makeEntry( | ||
| 57 | domain: "domain\($0).example", | ||
| 58 | at: Date(timeIntervalSince1970: 1_700_000_000 - Double($0)) | ||
| 59 | ) | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | // MARK: The seam | ||
| 64 | |||
| 65 | func testTheViewModelReadsTheStoreItWasGiven() { | ||
| 66 | DomainDataPortabilityService.saveRecentSearches(["a.example", "b.example"], defaults: scratch) | ||
| 67 | DomainDataPortabilityService.saveTrackedDomains( | ||
| 68 | [TrackedDomain(domain: "tracked.example", createdAt: Date(), updatedAt: Date())], | ||
| 69 | defaults: scratch | ||
| 70 | ) | ||
| 71 | DomainDataPortabilityService.saveHistoryEntries(makeEntries(3), defaults: scratch) | ||
| 72 | |||
| 73 | let viewModel = makeViewModel() | ||
| 74 | |||
| 75 | XCTAssertEqual(viewModel.recentSearches, ["a.example", "b.example"]) | ||
| 76 | XCTAssertEqual(viewModel.trackedDomains.map(\.domain), ["tracked.example"]) | ||
| 77 | XCTAssertEqual(viewModel.history.count, 3) | ||
| 78 | } | ||
| 79 | |||
| 80 | /// The point of the seam: a test may drive the whole lifecycle without the | ||
| 81 | /// app's own store on the same simulator changing underneath it. | ||
| 82 | func testWritesLandInTheInjectedStoreAndNotTheDefaultOne() { | ||
| 83 | let marker = "seam-\(UUID().uuidString).example" | ||
| 84 | let viewModel = makeViewModel() | ||
| 85 | |||
| 86 | viewModel.addRecentSearch(marker) | ||
| 87 | |||
| 88 | XCTAssertEqual(DomainDataPortabilityService.loadRecentSearches(defaults: scratch).first, marker) | ||
| 89 | XCTAssertFalse( | ||
| 90 | DomainDataPortabilityService.loadRecentSearches(defaults: .standard).contains(marker), | ||
| 91 | "a view model write reached UserDefaults.standard" | ||
| 92 | ) | ||
| 93 | } | ||
| 94 | |||
| 95 | func testTwoViewModelsOnSeparateStoresDoNotSeeEachOther() throws { | ||
| 96 | let otherName = "net.cleberg.DomainDig.tests.\(UUID().uuidString)" | ||
| 97 | defer { UserDefaults.standard.removePersistentDomain(forName: otherName) } | ||
| 98 | let other = try XCTUnwrap(UserDefaults(suiteName: otherName)) | ||
| 99 | |||
| 100 | let first = makeViewModel() | ||
| 101 | let second = DomainViewModel(defaults: other) | ||
| 102 | Self.retained.append(second) | ||
| 103 | |||
| 104 | first.addRecentSearch("first.example") | ||
| 105 | second.addRecentSearch("second.example") | ||
| 106 | |||
| 107 | XCTAssertEqual(DomainDataPortabilityService.loadRecentSearches(defaults: scratch), ["first.example"]) | ||
| 108 | XCTAssertEqual(DomainDataPortabilityService.loadRecentSearches(defaults: other), ["second.example"]) | ||
| 109 | } | ||
| 110 | |||
| 111 | // MARK: Recent searches | ||
| 112 | |||
| 113 | func testRecentSearchesTruncateAtTheCap() { | ||
| 114 | let viewModel = makeViewModel() | ||
| 115 | |||
| 116 | for index in 0..<40 { | ||
| 117 | viewModel.addRecentSearch("domain\(index).example") | ||
| 118 | } | ||
| 119 | |||
| 120 | XCTAssertEqual(viewModel.recentSearches.count, 20) | ||
| 121 | XCTAssertEqual(viewModel.recentSearches.first, "domain39.example") | ||
| 122 | XCTAssertEqual(viewModel.recentSearches.last, "domain20.example") | ||
| 123 | XCTAssertEqual(DomainDataPortabilityService.loadRecentSearches(defaults: scratch).count, 20) | ||
| 124 | } | ||
| 125 | |||
| 126 | func testARepeatedSearchMovesToTheFrontWithoutDuplicating() { | ||
| 127 | let viewModel = makeViewModel() | ||
| 128 | |||
| 129 | viewModel.addRecentSearch("one.example") | ||
| 130 | viewModel.addRecentSearch("two.example") | ||
| 131 | viewModel.addRecentSearch("ONE.EXAMPLE") | ||
| 132 | |||
| 133 | XCTAssertEqual(viewModel.recentSearches, ["ONE.EXAMPLE", "two.example"]) | ||
| 134 | } | ||
| 135 | |||
| 136 | // MARK: History pruning | ||
| 137 | |||
| 138 | func testTheAutoPruneOptionTrimsHistoryAndPersistsTheResult() { | ||
| 139 | let viewModel = makeViewModel() | ||
| 140 | viewModel.history = makeEntries(120) | ||
| 141 | |||
| 142 | viewModel.setHistoryAutoPruneOption(.keep50) | ||
| 143 | |||
| 144 | XCTAssertEqual(viewModel.history.count, 50) | ||
| 145 | XCTAssertEqual(DomainDataPortabilityService.loadHistoryEntries(defaults: scratch).count, 50) | ||
| 146 | } | ||
| 147 | |||
| 148 | /// Pruning trims from the front, so the rows kept must be the newest ones. | ||
| 149 | /// A trim that dropped the head would silently discard the only snapshots | ||
| 150 | /// the dashboard and the diff view read. | ||
| 151 | func testPruningKeepsTheNewestRows() { | ||
| 152 | let viewModel = makeViewModel() | ||
| 153 | let seeded = makeEntries(120) | ||
| 154 | viewModel.history = seeded | ||
| 155 | |||
| 156 | viewModel.setHistoryAutoPruneOption(.keep50) | ||
| 157 | |||
| 158 | XCTAssertEqual(viewModel.history.map(\.id), seeded.prefix(50).map(\.id)) | ||
| 159 | } | ||
| 160 | |||
| 161 | /// "Unlimited" is unlimited only up to the store's own hard cap. The | ||
| 162 | /// setting has no bearing on that ceiling. | ||
| 163 | func testTheHardCapAppliesEvenWhenPruningIsUnlimited() { | ||
| 164 | let viewModel = makeViewModel() | ||
| 165 | viewModel.history = makeEntries(300) | ||
| 166 | |||
| 167 | viewModel.setHistoryAutoPruneOption(.unlimited) | ||
| 168 | |||
| 169 | XCTAssertEqual(viewModel.history.count, 250) | ||
| 170 | } | ||
| 171 | |||
| 172 | /// A store already over the cap must still be readable. Pruning happens on | ||
| 173 | /// a write, not on a read, so loading must not drop rows. | ||
| 174 | func testAStoreAlreadyOverTheCapLoadsEveryRow() { | ||
| 175 | DomainDataPortabilityService.saveHistoryEntries(makeEntries(300), defaults: scratch) | ||
| 176 | |||
| 177 | XCTAssertEqual(makeViewModel().history.count, 300) | ||
| 178 | } | ||
| 179 | |||
| 180 | // MARK: Tracking | ||
| 181 | |||
| 182 | func testTrackingADomainPersistsToTheInjectedStore() { | ||
| 183 | let viewModel = makeViewModel() | ||
| 184 | |||
| 185 | XCTAssertTrue(viewModel.trackDomain(domain: "tracked.example", availabilityStatus: nil)) | ||
| 186 | |||
| 187 | XCTAssertEqual( | ||
| 188 | DomainDataPortabilityService.loadTrackedDomains(defaults: scratch).map(\.domain), | ||
| 189 | ["tracked.example"] | ||
| 190 | ) | ||
| 191 | } | ||
| 192 | |||
| 193 | /// The store key is the normalized domain, so a differently-cased repeat is | ||
| 194 | /// the same row. Returning true without inserting is what keeps the Track | ||
| 195 | /// button idempotent. | ||
| 196 | func testTrackingTheSameDomainInADifferentCaseDoesNotAddASecondRow() { | ||
| 197 | let viewModel = makeViewModel() | ||
| 198 | |||
| 199 | XCTAssertTrue(viewModel.trackDomain(domain: "tracked.example", availabilityStatus: nil)) | ||
| 200 | XCTAssertTrue(viewModel.trackDomain(domain: "TRACKED.EXAMPLE", availabilityStatus: nil)) | ||
| 201 | |||
| 202 | XCTAssertEqual(viewModel.trackedDomains.count, 1) | ||
| 203 | XCTAssertEqual(DomainDataPortabilityService.loadTrackedDomains(defaults: scratch).count, 1) | ||
| 204 | } | ||
| 205 | |||
| 206 | /// The gate is the live entitlement's ceiling rather than a pinned number: | ||
| 207 | /// the tier comes from `PurchaseService.shared`, which a cached purchase on | ||
| 208 | /// the simulator can change. | ||
| 209 | func testTrackingRefusesPastTheCeilingAndRaisesTheUpgradePrompt() { | ||
| 210 | let limit = FeatureAccessService.entitlements.trackedDomainLimit | ||
| 211 | let viewModel = makeViewModel() | ||
| 212 | viewModel.trackedDomains = (0..<limit).map { | ||
| 213 | TrackedDomain(domain: "filler\($0).example", createdAt: Date(), updatedAt: Date()) | ||
| 214 | } | ||
| 215 | |||
| 216 | XCTAssertFalse(viewModel.trackDomain(domain: "overflow.example", availabilityStatus: nil)) | ||
| 217 | |||
| 218 | XCTAssertNotNil(viewModel.upgradePrompt) | ||
| 219 | XCTAssertEqual(viewModel.trackedDomains.count, limit) | ||
| 220 | } | ||
| 221 | |||
| 222 | /// A lapsed subscriber holds more than the new ceiling. The limit gates | ||
| 223 | /// adding, not holding, so the rows above it must still load and read back. | ||
| 224 | func testAStoreOverTheCeilingStillLoadsEveryTrackedDomain() { | ||
| 225 | let over = FeatureAccessService.entitlements.trackedDomainLimit + 3 | ||
| 226 | DomainDataPortabilityService.saveTrackedDomains( | ||
| 227 | (0..<over).map { TrackedDomain(domain: "held\($0).example", createdAt: Date(), updatedAt: Date()) }, | ||
| 228 | defaults: scratch | ||
| 229 | ) | ||
| 230 | |||
| 231 | let viewModel = makeViewModel() | ||
| 232 | |||
| 233 | XCTAssertEqual(viewModel.trackedDomains.count, over) | ||
| 234 | XCTAssertFalse(viewModel.canTrackCurrentDomain) | ||
| 235 | } | ||
| 236 | |||
| 237 | // MARK: Batch cancellation | ||
| 238 | |||
| 239 | /// Cancelling must resolve every row it started. A pending row left pending | ||
| 240 | /// reads as still running in the batch view, and the sweep summary counts | ||
| 241 | /// it as outstanding forever. | ||
| 242 | /// | ||
| 243 | /// Start and cancel both run on the main actor with no suspension between | ||
| 244 | /// them, so the batch task cannot begin before the cancel lands — the | ||
| 245 | /// transition under test is the synchronous one. | ||
| 246 | func testCancellingABatchResolvesEveryPendingRow() { | ||
| 247 | let viewModel = makeViewModel() | ||
| 248 | |||
| 249 | viewModel.startBatchLookup(domains: ["one.example", "two.example"], source: .manual) | ||
| 250 | XCTAssertTrue(viewModel.batchLookupRunning) | ||
| 251 | XCTAssertEqual(viewModel.batchResults.map(\.status), [.pending, .pending]) | ||
| 252 | |||
| 253 | viewModel.cancelBatchLookup() | ||
| 254 | |||
| 255 | XCTAssertFalse(viewModel.batchLookupRunning) | ||
| 256 | XCTAssertNil(viewModel.batchCurrentDomain) | ||
| 257 | XCTAssertTrue(viewModel.batchResults.allSatisfy { $0.quickStatus == "Cancelled" }) | ||
| 258 | XCTAssertFalse(viewModel.batchResults.contains { $0.status == .pending || $0.status == .running }) | ||
| 259 | } | ||
| 260 | |||
| 261 | /// `batchLookupRunning` and the one-second throttle both refuse the second | ||
| 262 | /// start. Either way the running batch must not be replaced out from under | ||
| 263 | /// the results view. | ||
| 264 | func testASecondBatchStartDoesNotReplaceTheRunningOne() { | ||
| 265 | let viewModel = makeViewModel() | ||
| 266 | |||
| 267 | viewModel.startBatchLookup(domains: ["one.example"], source: .manual) | ||
| 268 | viewModel.startBatchLookup(domains: ["a.example", "b.example", "c.example"], source: .manual) | ||
| 269 | |||
| 270 | XCTAssertEqual(viewModel.batchTotalCount, 1) | ||
| 271 | XCTAssertEqual(viewModel.batchResults.count, 1) | ||
| 272 | } | ||
| 273 | |||
| 274 | // MARK: Lookup generation (Review Focus #4) | ||
| 275 | |||
| 276 | /// A second Run while the first lookup's sections are still in flight. Each | ||
| 277 | /// section applier checks `isCurrentLookup` before writing, so the first | ||
| 278 | /// lookup's generation must stop being current the moment the second | ||
| 279 | /// begins — otherwise a late DNS or TLS result lands on the new snapshot. | ||
| 280 | func testASecondLookupInvalidatesTheFirstOnesGeneration() { | ||
| 281 | let viewModel = makeViewModel() | ||
| 282 | |||
| 283 | let first = viewModel.beginLookup(for: "first.example") | ||
| 284 | XCTAssertTrue(viewModel.isCurrentLookup(first)) | ||
| 285 | |||
| 286 | let second = viewModel.beginLookup(for: "second.example") | ||
| 287 | |||
| 288 | XCTAssertFalse( | ||
| 289 | viewModel.isCurrentLookup(first), | ||
| 290 | "a section result from the first lookup would still be applied" | ||
| 291 | ) | ||
| 292 | XCTAssertTrue(viewModel.isCurrentLookup(second)) | ||
| 293 | XCTAssertEqual(viewModel.searchedDomain, "second.example") | ||
| 294 | } | ||
| 295 | |||
| 296 | /// Two lookups of the same domain are still separate generations. Keying | ||
| 297 | /// the guard on the domain rather than the run would let a stale result | ||
| 298 | /// through on a re-run. | ||
| 299 | func testRerunningTheSameDomainStillStartsANewGeneration() { | ||
| 300 | let viewModel = makeViewModel() | ||
| 301 | |||
| 302 | let first = viewModel.beginLookup(for: "same.example") | ||
| 303 | let second = viewModel.beginLookup(for: "same.example") | ||
| 304 | |||
| 305 | XCTAssertNotEqual(first, second) | ||
| 306 | XCTAssertFalse(viewModel.isCurrentLookup(first)) | ||
| 307 | } | ||
| 308 | |||
| 309 | /// Starting a lookup clears the previous run's snapshot identity, so the | ||
| 310 | /// history row the UI is pointing at cannot be mistaken for the new one. | ||
| 311 | func testBeginningALookupClearsThePreviousSnapshotIdentity() { | ||
| 312 | let viewModel = makeViewModel() | ||
| 313 | viewModel.history = makeEntries(1) | ||
| 314 | |||
| 315 | _ = viewModel.beginLookup(for: "fresh.example") | ||
| 316 | |||
| 317 | XCTAssertNil(viewModel.currentHistoryEntryID) | ||
| 318 | XCTAssertTrue(viewModel.hasRun) | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||