diff options
| author | Christian Cleberg <[email protected]> | 2026-04-02 00:27:34 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-02 00:27:34 -0500 |
| commit | f820109a956c627e510880a7cac88cb35032203b (patch) | |
| tree | 942bfad93efbb8a2a98e98b0a9590e2631613b3a /HutchTests/LookupHistoryStoreTests.swift | |
| parent | 7f79cadf99ecf014834855e2e4a5c7d60dd709ac (diff) | |
| download | hutch-f820109a956c627e510880a7cac88cb35032203b.tar.gz hutch-f820109a956c627e510880a7cac88cb35032203b.tar.bz2 hutch-f820109a956c627e510880a7cac88cb35032203b.zip | |
Implement search history for Look Up page
Diffstat (limited to 'HutchTests/LookupHistoryStoreTests.swift')
| -rw-r--r-- | HutchTests/LookupHistoryStoreTests.swift | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/HutchTests/LookupHistoryStoreTests.swift b/HutchTests/LookupHistoryStoreTests.swift new file mode 100644 index 0000000..414d016 --- /dev/null +++ b/HutchTests/LookupHistoryStoreTests.swift @@ -0,0 +1,75 @@ +import Foundation +import Testing +@testable import Hutch + +struct LookupHistoryStoreTests { + + @Test + func recordsMostRecentUniqueSearchFirst() { + let suiteName = "LookupHistoryStoreTests-\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: suiteName)! + defer { defaults.removePersistentDomain(forName: suiteName) } + + LookupHistoryStore.record( + type: .user, + query: "~alice", + defaults: defaults, + now: Date(timeIntervalSince1970: 100) + ) + LookupHistoryStore.record( + type: .gitRepo, + query: "~alice/hutch", + defaults: defaults, + now: Date(timeIntervalSince1970: 200) + ) + LookupHistoryStore.record( + type: .user, + query: "~alice", + defaults: defaults, + now: Date(timeIntervalSince1970: 300) + ) + + let history = LookupHistoryStore.load(defaults: defaults) + + #expect(history.count == 2) + #expect(history.map(\.type) == [.user, .gitRepo]) + #expect(history.map(\.query) == ["~alice", "~alice/hutch"]) + #expect(history.first?.createdAt == Date(timeIntervalSince1970: 300)) + } + + @Test + func trimsHistoryToMaximumSize() { + let suiteName = "LookupHistoryStoreTests-\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: suiteName)! + defer { defaults.removePersistentDomain(forName: suiteName) } + + for index in 0..<25 { + LookupHistoryStore.record( + type: .buildJob, + query: "\(index)", + defaults: defaults, + now: Date(timeIntervalSince1970: TimeInterval(index)) + ) + } + + let history = LookupHistoryStore.load(defaults: defaults) + + #expect(history.count == 20) + #expect(history.first?.query == "24") + #expect(history.last?.query == "5") + } + + @Test + func clearsPersistedHistory() { + let suiteName = "LookupHistoryStoreTests-\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: suiteName)! + defer { defaults.removePersistentDomain(forName: suiteName) } + + LookupHistoryStore.record(type: .tracker, query: "~owner/todo", defaults: defaults) + #expect(!LookupHistoryStore.load(defaults: defaults).isEmpty) + + LookupHistoryStore.clear(defaults: defaults) + + #expect(LookupHistoryStore.load(defaults: defaults).isEmpty) + } +} |
