2024-04-11 11:42:59 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-12-17 07:51:48 -05:00
|
|
|
code_indexer "forgejo.org/modules/indexer/code"
|
2025-03-27 15:40:14 -04:00
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
|
"forgejo.org/modules/test"
|
|
|
|
|
"forgejo.org/tests"
|
2024-04-11 11:42:59 -04:00
|
|
|
|
2024-08-06 01:57:25 -04:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2024-04-11 11:42:59 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExploreCodeSearchIndexer(t *testing.T) {
|
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnabled, true)()
|
|
|
|
|
|
2025-12-17 07:51:48 -05:00
|
|
|
t.Run("Exact", func(t *testing.T) {
|
|
|
|
|
req := NewRequest(t, "GET", "/explore/code?q=file&mode=exact")
|
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
doc := NewHTMLParser(t, resp.Body).Find(".explore")
|
2024-04-11 11:42:59 -04:00
|
|
|
|
2025-12-17 07:51:48 -05:00
|
|
|
active, ok := doc.Find("[data-test-tag=fuzzy-dropdown] .active input").Attr("value")
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "exact", active)
|
|
|
|
|
|
|
|
|
|
doc.Find(".file-body").Each(func(i int, sel *goquery.Selection) {
|
|
|
|
|
assert.Positive(t, sel.Find(".code-inner").Find(".search-highlight").Length())
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Fuzzy", func(t *testing.T) {
|
|
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnableFuzzy, true)()
|
|
|
|
|
code_indexer.CodeSearchOptions = []string{"exact", "union", "fuzzy"} // usually set by Init
|
|
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/explore/code?q=file&mode=fuzzy")
|
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
doc := NewHTMLParser(t, resp.Body).Find(".explore")
|
|
|
|
|
|
|
|
|
|
active, ok := doc.Find("[data-test-tag=fuzzy-dropdown] .active input").Attr("value")
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "fuzzy", active)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("No Fuzzy", func(t *testing.T) {
|
|
|
|
|
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnableFuzzy, false)()
|
|
|
|
|
code_indexer.CodeSearchOptions = []string{"exact", "union"} // usually set by Init
|
|
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/explore/code?q=file&mode=fuzzy")
|
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
doc := NewHTMLParser(t, resp.Body).Find(".explore")
|
|
|
|
|
|
|
|
|
|
active, ok := doc.Find("[data-test-tag=fuzzy-dropdown] .active input").Attr("value")
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "union", active)
|
2024-08-06 01:57:25 -04:00
|
|
|
})
|
2024-04-11 11:42:59 -04:00
|
|
|
}
|