fix #81 site search improved

This commit is contained in:
sergeych committed 2025-12-06 18:03:15 +01:00
1 parent a6085b11a1
commit e25fc95cbf
6 files changed
+298 -101

No files matched your search

@@ -0,0 +1,31 @@
import kotlinx.browser.window
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SearchHistoryTest {
@BeforeTest
fun resetStorage() {
window.localStorage.removeItem("lyng.search.history")
}
@Test
fun keepsOnlySevenMostRecentUnique() {
// Add 9 queries
for (i in 1..9) rememberSearchQuery("query $i")
val raw = window.localStorage.getItem("lyng.search.history") ?: ""
val list = raw.split('\n').filter { it.isNotBlank() }
assertEquals(7, list.size, "history should contain 7 items")
assertEquals("query 9", list[0], "most recent should be first")
assertEquals("query 3", list.last(), "oldest retained should be 3")
// Re-insert existing should move to front without duplicates
rememberSearchQuery("query 5")
val raw2 = window.localStorage.getItem("lyng.search.history") ?: ""
val list2 = raw2.split('\n').filter { it.isNotBlank() }
assertEquals(7, list2.size)
assertEquals("query 5", list2[0])
assertTrue(list2.count { it == "query 5" } == 1)
}
}
@@ -0,0 +1,29 @@
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SearchScoringTest {
private fun rec(text: String, title: String = "Doc") = DocRecord("docs/a.md", title, norm(text))
@Test
fun zeroWhenNoTerms() {
assertEquals(0, scoreQueryAdvanced(emptyList(), rec("hello world")))
}
@Test
fun coverageMatters() {
val r = rec("alpha beta gamma alpha beta")
val s1 = scoreQueryAdvanced(listOf("alp"), r)
val s2 = scoreQueryAdvanced(listOf("alp", "bet"), r)
assertTrue(s2 > s1, "two-term coverage should score higher than one-term")
}
@Test
fun proximityImprovesScore() {
val near = rec("alpha beta gamma")
val far = rec(("alpha "+"x ").repeat(50) + "beta")
val sNear = scoreQueryAdvanced(listOf("alp", "bet"), near)
val sFar = scoreQueryAdvanced(listOf("alp", "bet"), far)
assertTrue(sNear > sFar, "closer terms should have higher score")
}
}