Skip to content

Commit

Permalink
Fix an issue that causes URLs to be shown in the search term popover
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandroboron committed Apr 4, 2024
1 parent 43f2cf6 commit 5a6b0de
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Sources/Suggestions/Suggestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public enum Suggestion: Equatable {
}
}

var phrase: String? {
switch self {
case .phrase(let phrase):
return phrase
case .website, .bookmark, .historyEntry, .unknown:
return nil
}
}

public var allowedInTopHits: Bool {
switch self {
case .website:
Expand Down
18 changes: 17 additions & 1 deletion Sources/Suggestions/SuggestionProcessing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ final class SuggestionProcessing {
navigationalSuggestions = merge(navigationalSuggestions,
maximum: maximumOfNavigationalSuggestions)

// Filter out navigational link from initial suggestions.
let filteredDuckDuckGoSuggestions = remove(navigationalSuggestions: navigationalSuggestions, fromDuckDuckGoSuggestions: duckDuckGoSuggestions)

// Split the Top Hits and the History and Bookmarks section
let topHits = topHits(from: navigationalSuggestions)
let historyAndBookmarkSuggestions = Array(navigationalSuggestions.dropFirst(topHits.count).filter { suggestion in
Expand All @@ -73,7 +76,7 @@ final class SuggestionProcessing {
})

return makeResult(topHits: topHits,
duckduckgoSuggestions: duckDuckGoSuggestions,
duckduckgoSuggestions: filteredDuckDuckGoSuggestions,
historyAndBookmarks: historyAndBookmarkSuggestions)
}

Expand Down Expand Up @@ -273,4 +276,17 @@ final class SuggestionProcessing {
historyAndBookmarks: historyAndBookmarks)
}

// MARK: - Filter out search term URL that are Navigational suggestion

private func remove(navigationalSuggestions: [Suggestion], fromDuckDuckGoSuggestions suggestions: [Suggestion]) -> [Suggestion] {
let navigationalSuggestionsURLs = Set(navigationalSuggestions.map(\.url?.naked))
return suggestions.filter { suggestion in
guard let phrase = suggestion.phrase else { return false }
// If the URL can't be built return true as this means that the `phrase` is a search term.
guard let suggestionURL = urlFactory(phrase) else { return true }
// Compare the URL and return false if it's a match
return !navigationalSuggestionsURLs.contains(suggestionURL.naked)
}
}

}
40 changes: 37 additions & 3 deletions Tests/SuggestionsTests/SuggestionProcessingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ final class SuggestionProcessingTests: XCTestCase {
XCTAssertEqual(result!.topHits.first!.title, "DuckDuckGo")
}

func testWhenDuckDuckGoSuggestionContainsURLThenDoNotShowAsSearchTerm() throws {
// GIVEN
let processing = SuggestionProcessing(urlFactory: URL.makeURL(fromSuggestionPhrase:))
let facebookURLSearchTermSuggestion = Suggestion(key: Suggestion.phraseKey, value: "www.acer.com/ac/en/US/content/home")

// WHEN
let result = processing.result(
for: "ace",
from: [],
bookmarks: [],
apiResult: .aceAPIResult
)

// THEN
let duckduckGoSuggestions = try XCTUnwrap(result?.duckduckgoSuggestions)
XCTAssertEqual(duckduckGoSuggestions.count, 4)
XCTAssertFalse(duckduckGoSuggestions.contains(facebookURLSearchTermSuggestion))
}

}

extension HistoryEntryMock {
Expand All @@ -55,9 +74,12 @@ extension HistoryEntryMock {
extension BookmarkMock {

static var someBookmarks: [Bookmark] {
[ BookmarkMock(url: "http://duckduckgo.com", title: "DuckDuckGo", isFavorite: true),
BookmarkMock(url: "spreadprivacy.com", title: "Test 2", isFavorite: true),
BookmarkMock(url: "wikipedia.org", title: "Wikipedia", isFavorite: false) ]
[
BookmarkMock(url: "http://duckduckgo.com", title: "DuckDuckGo", isFavorite: true),
BookmarkMock(url: "spreadprivacy.com", title: "Test 2", isFavorite: true),
BookmarkMock(url: "wikipedia.org", title: "Wikipedia", isFavorite: false),
BookmarkMock(url: "www.facebook.com", title: "Facebook", isFavorite: true),
]
}

}
Expand All @@ -74,4 +96,16 @@ extension APIResult {
return result
}

static let aceAPIResult: APIResult = {
var result = APIResult()
result.items = [
[ "phrase": "acecqa" ],
[ "phrase": "acer" ],
[ "phrase": "www.acer.com/ac/en/US/content/home" ],
[ "phrase": "ace hotel sydney" ],
[ "phrase": "acer drivers" ],
]
return result
}()

}

0 comments on commit 5a6b0de

Please sign in to comment.