| @@ -0,0 +1,189 @@ |
| 1 | import XCTest |
| 2 | @testable import DomainDig |
| 3 | |
| 4 | /// Review Focus #2: a history store at its tier cap must not drop or duplicate |
| 5 | /// rows across a save, an auto-prune or a backup import, and a restore must |
| 6 | /// round-trip every field the UI reads. Pro+ at 2000 snapshots is the worst |
| 7 | /// case, not the 250 the store caps at today. |
| 8 | /// |
| 9 | /// These run against the store as it is. W5 moves the substrate underneath |
| 10 | /// `DomainDataPortabilityService`'s API, and this is the gate that says the |
| 11 | /// move lost nothing — so it comes before the switch, not after. |
| 12 | final class HistoryStoreTests: XCTestCase { |
| 13 | |
| 14 | /// The Pro+ retention tier D1 settled on, and the size every assertion here |
| 15 | /// is about. |
| 16 | private static let proPlusCap = 2000 |
| 17 | |
| 18 | private var suiteName = "" |
| 19 | private var scratch = UserDefaults.standard |
| 20 | |
| 21 | override func setUpWithError() throws { |
| 22 | try super.setUpWithError() |
| 23 | suiteName = "net.cleberg.DomainDig.tests.\(UUID().uuidString)" |
| 24 | scratch = try XCTUnwrap(UserDefaults(suiteName: suiteName)) |
| 25 | } |
| 26 | |
| 27 | override func tearDown() { |
| 28 | UserDefaults.standard.removePersistentDomain(forName: suiteName) |
| 29 | super.tearDown() |
| 30 | } |
| 31 | |
| 32 | private func entries(_ count: Int) -> [HistoryEntry] { |
| 33 | (0..<count).map { SnapshotFixture.historyEntry(index: $0) } |
| 34 | } |
| 35 | |
| 36 | // MARK: Round trip at the cap |
| 37 | |
| 38 | func testTwoThousandEntriesSurviveASaveAndLoad() { |
| 39 | let saved = entries(Self.proPlusCap) |
| 40 | |
| 41 | DomainDataPortabilityService.saveHistoryEntries(saved, defaults: scratch) |
| 42 | let loaded = DomainDataPortabilityService.loadHistoryEntries(defaults: scratch) |
| 43 | |
| 44 | XCTAssertEqual(loaded.count, Self.proPlusCap, "the store dropped or added rows") |
| 45 | XCTAssertEqual(Set(loaded.map(\.id)), Set(saved.map(\.id)), "the store changed which rows exist") |
| 46 | } |
| 47 | |
| 48 | /// The store sorts on load, newest first. Every reader downstream — the |
| 49 | /// history list, the dashboard's recent activity, the diff view's |
| 50 | /// "previous snapshot" — takes that for granted. |
| 51 | func testTheStoreReturnsRowsNewestFirst() { |
| 52 | DomainDataPortabilityService.saveHistoryEntries(entries(Self.proPlusCap), defaults: scratch) |
| 53 | |
| 54 | let loaded = DomainDataPortabilityService.loadHistoryEntries(defaults: scratch) |
| 55 | |
| 56 | XCTAssertEqual(loaded.map(\.timestamp), loaded.map(\.timestamp).sorted(by: >)) |
| 57 | XCTAssertEqual(loaded.first?.snapshotIndex, Self.proPlusCap - 1) |
| 58 | XCTAssertEqual(loaded.last?.snapshotIndex, 0) |
| 59 | } |
| 60 | |
| 61 | func testNoRowIsDuplicatedAcrossRepeatedSaves() { |
| 62 | let saved = entries(Self.proPlusCap) |
| 63 | |
| 64 | DomainDataPortabilityService.saveHistoryEntries(saved, defaults: scratch) |
| 65 | let once = DomainDataPortabilityService.loadHistoryEntries(defaults: scratch) |
| 66 | DomainDataPortabilityService.saveHistoryEntries(once, defaults: scratch) |
| 67 | let twice = DomainDataPortabilityService.loadHistoryEntries(defaults: scratch) |
| 68 | |
| 69 | XCTAssertEqual(twice.count, Self.proPlusCap) |
| 70 | XCTAssertEqual(Set(twice.map(\.id)).count, Self.proPlusCap, "a row was duplicated") |
| 71 | XCTAssertEqual(twice.map(\.id), once.map(\.id), "a second save reordered the store") |
| 72 | } |
| 73 | |
| 74 | /// Every field the UI reads, on an entry that has all of them set. A |
| 75 | /// substrate change that silently lost one section would still pass a |
| 76 | /// count assertion. |
| 77 | func testEveryPopulatedFieldSurvivesTheRoundTrip() throws { |
| 78 | let original = SnapshotFixture.historyEntry(index: 7) |
| 79 | |
| 80 | DomainDataPortabilityService.saveHistoryEntries([original], defaults: scratch) |
| 81 | let restored = try XCTUnwrap( |
| 82 | DomainDataPortabilityService.loadHistoryEntries(defaults: scratch).first |
| 83 | ) |
| 84 | |
| 85 | XCTAssertEqual(restored.id, original.id) |
| 86 | XCTAssertEqual(restored.domain, original.domain) |
| 87 | XCTAssertEqual(restored.timestamp, original.timestamp) |
| 88 | XCTAssertEqual(restored.trackedDomainID, original.trackedDomainID) |
| 89 | XCTAssertEqual(restored.note, original.note) |
| 90 | |
| 91 | XCTAssertEqual(restored.dnsSections.count, original.dnsSections.count) |
| 92 | XCTAssertEqual(restored.dnsSections.map(\.recordType), original.dnsSections.map(\.recordType)) |
| 93 | XCTAssertEqual( |
| 94 | restored.dnsSections.flatMap { $0.records.map(\.value) }, |
| 95 | original.dnsSections.flatMap { $0.records.map(\.value) } |
| 96 | ) |
| 97 | |
| 98 | XCTAssertEqual(restored.sslInfo?.commonName, original.sslInfo?.commonName) |
| 99 | XCTAssertEqual(restored.sslInfo?.issuer, original.sslInfo?.issuer) |
| 100 | XCTAssertEqual(restored.sslInfo?.daysUntilExpiry, original.sslInfo?.daysUntilExpiry) |
| 101 | XCTAssertEqual(restored.sslInfo?.subjectAltNames, original.sslInfo?.subjectAltNames) |
| 102 | |
| 103 | XCTAssertEqual(restored.httpHeaders.map(\.name), original.httpHeaders.map(\.name)) |
| 104 | XCTAssertEqual(restored.httpHeaders.map(\.value), original.httpHeaders.map(\.value)) |
| 105 | XCTAssertEqual(restored.reachabilityResults.map(\.port), original.reachabilityResults.map(\.port)) |
| 106 | XCTAssertEqual(restored.reachabilityResults.map(\.latencyMs), original.reachabilityResults.map(\.latencyMs)) |
| 107 | |
| 108 | XCTAssertEqual(restored.ipGeolocation?.ip, original.ipGeolocation?.ip) |
| 109 | XCTAssertEqual(restored.ipGeolocation?.countryName, original.ipGeolocation?.countryName) |
| 110 | XCTAssertEqual(restored.ipGeolocation?.org, original.ipGeolocation?.org) |
| 111 | |
| 112 | XCTAssertEqual(restored.ptrRecord, original.ptrRecord) |
| 113 | XCTAssertEqual(restored.redirectChain.map(\.url), original.redirectChain.map(\.url)) |
| 114 | XCTAssertEqual(restored.redirectChain.map(\.isFinal), original.redirectChain.map(\.isFinal)) |
| 115 | XCTAssertEqual(restored.subdomains.map(\.hostname), original.subdomains.map(\.hostname)) |
| 116 | XCTAssertEqual(restored.hstsPreloaded, original.hstsPreloaded) |
| 117 | |
| 118 | XCTAssertEqual(restored.priorProviders, original.priorProviders) |
| 119 | XCTAssertEqual(restored.riskSignals.map(\.id), original.riskSignals.map(\.id)) |
| 120 | XCTAssertEqual(restored.riskSignals.map(\.severity), original.riskSignals.map(\.severity)) |
| 121 | |
| 122 | XCTAssertEqual(restored.dataSources, original.dataSources) |
| 123 | XCTAssertEqual(restored.resolverDisplayName, original.resolverDisplayName) |
| 124 | XCTAssertEqual(restored.resolverURLString, original.resolverURLString) |
| 125 | XCTAssertEqual(restored.totalLookupDurationMs, original.totalLookupDurationMs) |
| 126 | |
| 127 | XCTAssertEqual(restored.primaryIP, original.primaryIP) |
| 128 | XCTAssertEqual(restored.finalRedirectURL, original.finalRedirectURL) |
| 129 | XCTAssertEqual(restored.tlsStatusSummary, original.tlsStatusSummary) |
| 130 | XCTAssertEqual(restored.snapshotIndex, original.snapshotIndex) |
| 131 | XCTAssertEqual(restored.changeCount, original.changeCount) |
| 132 | } |
| 133 | |
| 134 | // MARK: Backup and restore at the cap |
| 135 | |
| 136 | func testABackupAtTheCapRestoresEveryRow() throws { |
| 137 | let saved = entries(Self.proPlusCap) |
| 138 | DomainDataPortabilityService.saveHistoryEntries(saved, defaults: scratch) |
| 139 | |
| 140 | let backup = try DomainDataPortabilityService.backupData(defaults: scratch) |
| 141 | |
| 142 | let restoreName = "net.cleberg.DomainDig.tests.\(UUID().uuidString)" |
| 143 | defer { UserDefaults.standard.removePersistentDomain(forName: restoreName) } |
| 144 | let restoreStore = try XCTUnwrap(UserDefaults(suiteName: restoreName)) |
| 145 | |
| 146 | let preview = try DomainDataPortabilityService.prepareImport( |
| 147 | data: backup, |
| 148 | fileName: "backup.json", |
| 149 | mode: .replace, |
| 150 | defaults: restoreStore |
| 151 | ) |
| 152 | _ = try DomainDataPortabilityService.applyImport(preview, mode: .replace, defaults: restoreStore) |
| 153 | |
| 154 | let restored = DomainDataPortabilityService.loadHistoryEntries(defaults: restoreStore) |
| 155 | XCTAssertEqual(restored.count, Self.proPlusCap, "the backup round trip changed the row count") |
| 156 | XCTAssertEqual(Set(restored.map(\.id)), Set(saved.map(\.id)), "the backup round trip changed which rows exist") |
| 157 | } |
| 158 | |
| 159 | /// A merge import of a backup of the same store must not double every row. |
| 160 | func testMergingABackupOfTheSameStoreDoesNotDuplicateRows() throws { |
| 161 | let saved = entries(500) |
| 162 | DomainDataPortabilityService.saveHistoryEntries(saved, defaults: scratch) |
| 163 | |
| 164 | let backup = try DomainDataPortabilityService.backupData(defaults: scratch) |
| 165 | let preview = try DomainDataPortabilityService.prepareImport( |
| 166 | data: backup, |
| 167 | fileName: "backup.json", |
| 168 | mode: .merge, |
| 169 | defaults: scratch |
| 170 | ) |
| 171 | _ = try DomainDataPortabilityService.applyImport(preview, mode: .merge, defaults: scratch) |
| 172 | |
| 173 | let merged = DomainDataPortabilityService.loadHistoryEntries(defaults: scratch) |
| 174 | XCTAssertEqual(merged.count, 500, "merging a backup of the same store duplicated rows") |
| 175 | XCTAssertEqual(Set(merged.map(\.id)).count, 500) |
| 176 | } |
| 177 | |
| 178 | // MARK: Validation at the cap |
| 179 | |
| 180 | func testValidationAcceptsAStoreAtTheCap() throws { |
| 181 | let saved = entries(Self.proPlusCap) |
| 182 | DomainDataPortabilityService.saveHistoryEntries(saved, defaults: scratch) |
| 183 | |
| 184 | let backup = try DomainDataPortabilityService.backupData(defaults: scratch) |
| 185 | let report = try DomainDataPortabilityService.validateBackup(data: backup) |
| 186 | |
| 187 | XCTAssertTrue(report.errors.isEmpty, "a store at the Pro+ cap failed validation: \(report.errors)") |
| 188 | } |
| 189 | } |