import XCTest @testable import RSSuper /// Unit tests for SearchHistoryStore final class SearchHistoryStoreTests: XCTestCase { private var historyStore: SearchHistoryStore! private var databaseManager: DatabaseManager! override func setUp() async throws { // Create in-memory database for testing databaseManager = try await DatabaseManager.inMemory() historyStore = SearchHistoryStore(databaseManager: databaseManager, maxHistoryCount: 10) try await historyStore.initialize() } override func tearDown() async throws { historyStore = nil databaseManager = nil } func testRecordSearch() async throws { try await historyStore.recordSearch("test query") let exists = try await historyStore.queryExists("test query") XCTAssertTrue(exists) } func testRecordSearchUpdatesExisting() async throws { try await historyStore.recordSearch("test query") let firstCount = try await historyStore.getTotalCount() try await historyStore.recordSearch("test query") let secondCount = try await historyStore.getTotalCount() XCTAssertEqual(firstCount, secondCount) // Should be same, updated not inserted } func testGetRecentQueries() async throws { try await historyStore.recordSearch("query 1") try await historyStore.recordSearch("query 2") try await historyStore.recordSearch("query 3") let queries = try await historyStore.getRecentQueries(limit: 2) XCTAssertEqual(queries.count, 2) XCTAssertEqual(queries[0], "query 3") // Most recent first XCTAssertEqual(queries[1], "query 2") } func testGetHistoryWithMetadata() async throws { try await historyStore.recordSearch("test query", resultCount: 42) let entries = try await historyStore.getHistoryWithMetadata(limit: 10) XCTAssertEqual(entries.count, 1) XCTAssertEqual(entries[0].query, "test query") XCTAssertEqual(entries[0].resultCount, 42) } func testRemoveQuery() async throws { try await historyStore.recordSearch("to remove") XCTAssertTrue(try await historyStore.queryExists("to remove")) try await historyStore.removeQuery("to remove") XCTAssertFalse(try await historyStore.queryExists("to remove")) } func testClearHistory() async throws { try await historyStore.recordSearch("query 1") try await historyStore.recordSearch("query 2") XCTAssertEqual(try await historyStore.getTotalCount(), 2) try await historyStore.clearHistory() XCTAssertEqual(try await historyStore.getTotalCount(), 0) } func testTrimHistory() async throws { // Insert more than maxHistoryCount for i in 1...15 { try await historyStore.recordSearch("query \(i)") } let count = try await historyStore.getTotalCount() XCTAssertEqual(count, 10) // Should be trimmed to maxHistoryCount } func testGetPopularQueries() async throws { // Record queries with different frequencies try await historyStore.recordSearch("popular") try await historyStore.recordSearch("popular") try await historyStore.recordSearch("popular") try await historyStore.recordSearch("less popular") try await historyStore.recordSearch("less popular") try await historyStore.recordSearch("once") let popular = try await historyStore.getPopularQueries(limit: 10) XCTAssertEqual(popular.count, 3) XCTAssertEqual(popular[0].query, "popular") XCTAssertEqual(popular[0].count, 3) } func testGetTodaysQueries() async throws { try await historyStore.recordSearch("today query 1") try await historyStore.recordSearch("today query 2") let todays = try await historyStore.getTodaysQueries() XCTAssertTrue(todays.contains("today query 1")) XCTAssertTrue(todays.contains("today query 2")) } func testEmptyQueryIgnored() async throws { try await historyStore.recordSearch("") try await historyStore.recordSearch(" ") let count = try await historyStore.getTotalCount() XCTAssertEqual(count, 0) } }