Say which bulk entries were skipped !45
4 files changed, +174 −10
Layout: unified · split
DomainDig/ContentView.swift +8
| @@ -190,6 +190,7 @@ struct ContentView: View { | ||
| 190 | 190 | viewModel.clearPresentedResults() |
| 191 | 191 | } |
| 192 | 192 | .onChange(of: viewModel.bulkInput) { _, newValue in |
| 193 | viewModel.bulkInputError = nil | |
| 193 | 194 | guard inputMode == .bulk else { return } |
| 194 | 195 | let normalized = newValue.trimmingCharacters(in: .whitespacesAndNewlines) |
| 195 | 196 | guard !normalized.isEmpty || viewModel.hasRun || !viewModel.batchResults.isEmpty else { return } |
| @@ -335,6 +336,13 @@ struct ContentView: View { | ||
| 335 | 336 | .clipShape(RoundedRectangle(cornerRadius: appDensity.metrics.cardCornerRadius)) |
| 336 | 337 | .focused($focusedInputField, equals: .bulkDomains) |
| 337 | 338 | |
| 339 | if let bulkInputError = viewModel.bulkInputError { | |
| 340 | Text(bulkInputError) | |
| 341 | .font(appDensity.font(.caption)) | |
| 342 | .foregroundStyle(Color(.statusCritical)) | |
| 343 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 344 | } | |
| 345 | ||
| 338 | 346 | Button { |
| 339 | 347 | focusedInputField = nil |
| 340 | 348 | viewModel.runBulkLookup() |
DomainDig/DomainViewModel.swift +34 −10
| @@ -116,6 +116,9 @@ final class DomainViewModel { | ||
| 116 | 116 | var domain: String = "" |
| 117 | 117 | var bulkInput: String = "" |
| 118 | 118 | var domainInputError: String? |
| 119 | /// What the bulk path skipped, for the same reason `domainInputError` | |
| 120 | /// exists for the single one. | |
| 121 | var bulkInputError: String? | |
| 119 | 122 | |
| 120 | 123 | // MARK: Section state |
| 121 | 124 | |
| @@ -1234,13 +1237,15 @@ final class DomainViewModel { | ||
| 1234 | 1237 | } |
| 1235 | 1238 | |
| 1236 | 1239 | func runBulkLookup() { |
| 1237 | let domains = parsedDomains(from: bulkInput) | |
| 1238 | guard !domains.isEmpty else { return } | |
| 1239 | guard FeatureAccessService.canRunBatch(domainCount: domains.count) else { | |
| 1240 | upgradePrompt = FeatureAccessService.upgradePromptForBatch(domainCount: domains.count) | |
| 1240 | let parsed = parsedBulkInput(from: bulkInput) | |
| 1241 | bulkInputError = parsed.skippedMessage | |
| 1242 | ||
| 1243 | guard !parsed.domains.isEmpty else { return } | |
| 1244 | guard FeatureAccessService.canRunBatch(domainCount: parsed.domains.count) else { | |
| 1245 | upgradePrompt = FeatureAccessService.upgradePromptForBatch(domainCount: parsed.domains.count) | |
| 1241 | 1246 | return |
| 1242 | 1247 | } |
| 1243 | startBatchLookup(domains: domains, source: .manual) | |
| 1248 | startBatchLookup(domains: parsed.domains, source: .manual) | |
| 1244 | 1249 | } |
| 1245 | 1250 | |
| 1246 | 1251 | func refreshAllTrackedDomains() { |
| @@ -2118,14 +2123,33 @@ final class DomainViewModel { | ||
| 2118 | 2123 | batchTask = nil |
| 2119 | 2124 | } |
| 2120 | 2125 | |
| 2121 | private func parsedDomains(from input: String) -> [String] { | |
| 2126 | /// Splits bulk input into the domains that validate and the entries that do | |
| 2127 | /// not. | |
| 2128 | /// | |
| 2129 | /// The rejected entries are returned rather than dropped: they used to go | |
| 2130 | /// missing silently, so pasting ten domains with two typos ran eight and | |
| 2131 | /// said nothing about the other two (U5). Before !8 those entries ran and | |
| 2132 | /// failed visibly per section, and the single-lookup path has reported its | |
| 2133 | /// own error since — the asymmetry was the defect. | |
| 2134 | func parsedBulkInput(from input: String) -> ParsedBulkInput { | |
| 2122 | 2135 | let separators = CharacterSet(charactersIn: ",").union(.whitespacesAndNewlines) |
| 2123 | 2136 | var seen = Set<String>() |
| 2137 | var domains: [String] = [] | |
| 2138 | var skipped: [String] = [] | |
| 2139 | ||
| 2140 | for token in input.components(separatedBy: separators) { | |
| 2141 | // Whitespace between entries is not an entry. | |
| 2142 | guard !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } | |
| 2143 | guard let domain = try? DomainName.validate(token).get() else { | |
| 2144 | skipped.append(token) | |
| 2145 | continue | |
| 2146 | } | |
| 2147 | if seen.insert(domain).inserted { | |
| 2148 | domains.append(domain) | |
| 2149 | } | |
| 2150 | } | |
| 2124 | 2151 | |
| 2125 | return input | |
| 2126 | .components(separatedBy: separators) | |
| 2127 | .compactMap { try? DomainName.validate($0).get() } | |
| 2128 | .filter { seen.insert($0).inserted } | |
| 2152 | return ParsedBulkInput(domains: domains, skipped: skipped) | |
| 2129 | 2153 | } |
| 2130 | 2154 | |
| 2131 | 2155 | private func runBatchLookup(domains: [String], source: BatchLookupSource) async { |
DomainDig/LookupSnapshot.swift +31
| @@ -322,3 +322,34 @@ struct InspectionMetadata: Equatable { | ||
| 322 | 322 | previousSnapshotID = snapshot.previousSnapshotID |
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | ||
| 326 | /// Bulk input split into what will run and what will not. | |
| 327 | struct ParsedBulkInput: Equatable { | |
| 328 | /// Valid domains, deduplicated, in the order they were written. | |
| 329 | var domains: [String] | |
| 330 | /// Entries that failed validation, as the user typed them. | |
| 331 | var skipped: [String] | |
| 332 | ||
| 333 | /// How many entries to name before falling back to a count. Naming them is | |
| 334 | /// the point — "two were skipped" does not tell anyone which to fix — but a | |
| 335 | /// pasted list of fifty typos should not become a wall of text. | |
| 336 | private static let namedLimit = 5 | |
| 337 | ||
| 338 | /// What to tell the user, or `nil` when nothing was skipped. | |
| 339 | var skippedMessage: String? { | |
| 340 | guard !skipped.isEmpty else { return nil } | |
| 341 | ||
| 342 | let named = skipped.prefix(Self.namedLimit).joined(separator: ", ") | |
| 343 | let remainder = skipped.count - min(skipped.count, Self.namedLimit) | |
| 344 | let list = remainder > 0 ? "\(named) and \(remainder) more" : named | |
| 345 | ||
| 346 | if domains.isEmpty { | |
| 347 | return skipped.count == 1 | |
| 348 | ? "\(list) is not a valid domain, so there is nothing to run." | |
| 349 | : "None of these is a valid domain, so there is nothing to run: \(list)." | |
| 350 | } | |
| 351 | return skipped.count == 1 | |
| 352 | ? "Skipped \(list): not a valid domain." | |
| 353 | : "Skipped \(skipped.count) entries that are not valid domains: \(list)." | |
| 354 | } | |
| 355 | } | |
DomainDigTests/DomainNameTests.swift +101
| @@ -141,6 +141,107 @@ final class DomainViewModelInputTests: XCTestCase { | ||
| 141 | 141 | defaults: UserDefaults(suiteName: "net.cleberg.DomainDig.tests.input") ?? .standard |
| 142 | 142 | ) |
| 143 | 143 | |
| 144 | // MARK: Bulk input (U5) | |
| 145 | ||
| 146 | /// The defect: ten domains with two typos ran eight and said nothing about | |
| 147 | /// the other two. | |
| 148 | func testBulkInputKeepsTheValidEntriesAndNamesTheRest() { | |
| 149 | let parsed = Self.viewModel.parsedBulkInput(from: "example.com, not valid!!, apple.com") | |
| 150 | ||
| 151 | XCTAssertEqual(parsed.domains, ["example.com", "apple.com"]) | |
| 152 | XCTAssertEqual(parsed.skipped, ["not", "valid!!"], "each rejected token is reported as typed") | |
| 153 | XCTAssertEqual( | |
| 154 | parsed.skippedMessage, | |
| 155 | "Skipped 2 entries that are not valid domains: not, valid!!." | |
| 156 | ) | |
| 157 | } | |
| 158 | ||
| 159 | func testBulkInputDeduplicatesAndIgnoresBlankSeparators() { | |
| 160 | let parsed = Self.viewModel.parsedBulkInput(from: "example.com,\n\n example.com ,\napple.com\n\n") | |
| 161 | ||
| 162 | XCTAssertEqual(parsed.domains, ["example.com", "apple.com"]) | |
| 163 | XCTAssertTrue(parsed.skipped.isEmpty, "whitespace between entries is not an entry") | |
| 164 | XCTAssertNil(parsed.skippedMessage) | |
| 165 | } | |
| 166 | ||
| 167 | func testValidBulkInputReportsNothing() { | |
| 168 | let parsed = Self.viewModel.parsedBulkInput(from: "example.com\napple.com") | |
| 169 | ||
| 170 | XCTAssertEqual(parsed.domains.count, 2) | |
| 171 | XCTAssertNil(parsed.skippedMessage) | |
| 172 | } | |
| 173 | ||
| 174 | /// One bad entry reads as a sentence about that entry, not as a count. | |
| 175 | func testASingleSkippedEntryIsNamedInTheSingular() { | |
| 176 | let parsed = ParsedBulkInput(domains: ["example.com"], skipped: ["nope!"]) | |
| 177 | ||
| 178 | XCTAssertEqual(parsed.skippedMessage, "Skipped nope!: not a valid domain.") | |
| 179 | } | |
| 180 | ||
| 181 | /// The worst case before this change: every entry invalid meant the button | |
| 182 | /// did nothing at all, with no explanation. | |
| 183 | func testAllEntriesInvalidSaysThereIsNothingToRun() { | |
| 184 | let many = ParsedBulkInput(domains: [], skipped: ["a!", "b!"]) | |
| 185 | XCTAssertEqual( | |
| 186 | many.skippedMessage, | |
| 187 | "None of these is a valid domain, so there is nothing to run: a!, b!." | |
| 188 | ) | |
| 189 | ||
| 190 | let one = ParsedBulkInput(domains: [], skipped: ["a!"]) | |
| 191 | XCTAssertEqual(one.skippedMessage, "a! is not a valid domain, so there is nothing to run.") | |
| 192 | } | |
| 193 | ||
| 194 | /// A pasted list of typos names the first few and counts the rest, rather | |
| 195 | /// than becoming a wall of text. | |
| 196 | func testManySkippedEntriesAreTruncated() { | |
| 197 | let parsed = ParsedBulkInput( | |
| 198 | domains: ["example.com"], | |
| 199 | skipped: ["a!", "b!", "c!", "d!", "e!", "f!", "g!"] | |
| 200 | ) | |
| 201 | ||
| 202 | XCTAssertEqual( | |
| 203 | parsed.skippedMessage, | |
| 204 | "Skipped 7 entries that are not valid domains: a!, b!, c!, d!, e! and 2 more." | |
| 205 | ) | |
| 206 | } | |
| 207 | ||
| 208 | /// The whole point: running a bulk lookup leaves the message behind for the | |
| 209 | /// view to show. | |
| 210 | func testRunningABulkLookupReportsWhatItSkipped() { | |
| 211 | let viewModel = Self.viewModel | |
| 212 | viewModel.bulkInputError = nil | |
| 213 | viewModel.bulkInput = "example.com, localhost" | |
| 214 | ||
| 215 | viewModel.runBulkLookup() | |
| 216 | ||
| 217 | XCTAssertEqual(viewModel.bulkInputError, "Skipped localhost: not a valid domain.") | |
| 218 | } | |
| 219 | ||
| 220 | func testABulkLookupWithNothingValidStillReports() { | |
| 221 | let viewModel = Self.viewModel | |
| 222 | viewModel.bulkInputError = nil | |
| 223 | viewModel.bulkInput = "localhost" | |
| 224 | ||
| 225 | viewModel.runBulkLookup() | |
| 226 | ||
| 227 | XCTAssertEqual( | |
| 228 | viewModel.bulkInputError, | |
| 229 | "localhost is not a valid domain, so there is nothing to run." | |
| 230 | ) | |
| 231 | XCTAssertFalse(viewModel.batchLookupRunning) | |
| 232 | } | |
| 233 | ||
| 234 | func testAValidBulkLookupLeavesNoMessage() { | |
| 235 | let viewModel = Self.viewModel | |
| 236 | viewModel.bulkInputError = "stale" | |
| 237 | viewModel.bulkInput = "example.com" | |
| 238 | ||
| 239 | viewModel.runBulkLookup() | |
| 240 | ||
| 241 | XCTAssertNil(viewModel.bulkInputError) | |
| 242 | viewModel.cancelBatchLookup() | |
| 243 | } | |
| 244 | ||
| 144 | 245 | func testAnInvalidDomainDoesNotStartALookup() { |
| 145 | 246 | let viewModel = Self.viewModel |
| 146 | 247 | viewModel.domain = "localhost" |