Files
helder/Tests/HelderUITests/Overlays/SearchComputeOnceTests.swift

107 lines
4.0 KiB
Swift

import Foundation
import HelderCore
@testable import HelderUI
import Testing
/// Counts how often the file-name ranking runs.
@MainActor
private final class CountingRanker {
var calls = 0
func rank(_ query: String, _ paths: [String], _ showHidden: Bool) -> [FileNameMatch] {
calls += 1
return Fuzzy.rankFileNames(query: query, paths: paths, showHidden: showHidden)
}
}
@MainActor
@Suite struct SearchComputeOnceTests {
private func makeModel(_ ranker: CountingRanker, results: [String: [ContentGroup]] = [:],
files: [String] = []) -> SearchModel {
let service = FakeSearch(results: results, files: files)
return SearchModel(search: service, activePath: "a.php", activeText: "user\nnone",
debounce: .milliseconds(20), ranker: { ranker.rank($0, $1, $2) })
}
@Test func aQueryChangeRanksTheFileNamesOnceHoweverOftenTheResultsAreRead() async {
let ranker = CountingRanker()
let model = makeModel(ranker, files: ["user.php", "user.js", "readme.md"])
await model.start()
ranker.calls = 0
model.setQuery("user")
for _ in 0..<5 {
_ = model.summary
_ = model.extensionChips
_ = model.fileMatches
_ = model.shownFileMatches
_ = model.emptyMessage(for: .files)
_ = model.target()
}
#expect(ranker.calls == 1)
}
@Test func choosingATypeFiltersWithoutRankingAgain() async {
let ranker = CountingRanker()
let model = makeModel(ranker, files: ["user.php", "user.js"])
await model.start()
model.setQuery("user")
ranker.calls = 0
model.toggleExtension("js")
#expect(model.fileMatches.map(\.path) == ["user.js"])
#expect(ranker.calls == 0)
}
@Test func arrivingContentResultsDoNotRankTheFileNamesAgain() async {
let ranker = CountingRanker()
let model = makeModel(ranker, results: ["user": [ContentGroup(path: "b.js", hits: [ContentHit(line: 1, text: "user", matchOffset: 0)])]],
files: ["user.php"])
await model.start()
model.setQuery("user")
ranker.calls = 0
await model.pendingSearch?.value
#expect(model.groups.map(\.path) == ["b.js"])
#expect(ranker.calls == 0)
}
@Test func theHiddenToggleRanksAgainOnce() async {
let ranker = CountingRanker()
let model = makeModel(ranker, files: [".env", "env.php"])
await model.start()
model.setQuery("env")
ranker.calls = 0
model.showHidden = true
#expect(ranker.calls == 1)
#expect(Set(model.fileMatches.map(\.path)) == [".env", "env.php"])
}
@Test func theThisFileHitsFollowTheQuery() {
let model = makeModel(CountingRanker())
model.setQuery("user")
#expect(model.inFileHits.map(\.line) == [1])
model.setQuery("zzz")
#expect(model.inFileHits.isEmpty)
}
}
@Suite struct SearchChipTooltipTests {
@Test func aChipSaysHowManyResultsItHasAndWhatAClickDoes() {
#expect(SearchModel.chipTooltip(count: 3, isChosen: false) == "3 results · click to show only this type")
#expect(SearchModel.chipTooltip(count: 1, isChosen: false) == "1 result · click to show only this type")
}
@Test func theChosenChipOffersEveryTypeBack() {
#expect(SearchModel.chipTooltip(count: 2, isChosen: true) == "2 results · click to show every type")
}
@MainActor
@Test func chipsCarryTheirResultCounts() async {
let service = FakeSearch(results: ["user": [ContentGroup(path: "a.php", hits: [ContentHit(line: 1, text: "user", matchOffset: 0)])]],
files: ["user.php", "user.js"])
let model = SearchModel(search: service, debounce: .milliseconds(20))
await model.start()
model.setQuery("user")
await model.pendingSearch?.value
#expect(model.chips == [SearchModel.ExtensionChip(ext: "php", count: 2), SearchModel.ExtensionChip(ext: "js", count: 1)])
}
}