fix(ui): add missing translation for code search when keyword is empty string (#10964)

- `CodeSearchMode` should now be set when keyword is empty
- The default value for search mode should be exact, use fuzzy ONLY when fuzziness is enabled in settings

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10964
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
Co-committed-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
This commit is contained in:
Shiny Nematoda 2026-01-21 16:42:18 +01:00 committed by 0ko
parent 7894e1bb8f
commit da7ce17533
2 changed files with 50 additions and 4 deletions

View file

@ -68,8 +68,9 @@ func Search(ctx *context.Context) {
mode := ExactSearchMode
if modeStr := ctx.FormString("mode"); len(modeStr) > 0 {
mode = searchModeFromString(modeStr)
} else if ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) { // for backward compatibility in links
mode = UnionSearchMode
} else if ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) &&
setting.Indexer.RepoIndexerEnableFuzzy { // for backward compatibility in links
mode = FuzzySearchMode
}
ctx.Data["PageIsViewCode"] = true
@ -81,6 +82,11 @@ func Search(ctx *context.Context) {
}
if opts.Keyword == "" {
if setting.Indexer.RepoIndexerEnabled {
ctx.Data["CodeSearchMode"] = mode.ToIndexer().String()
} else {
ctx.Data["CodeSearchMode"] = mode.ToGitGrep().String()
}
ctx.HTML(http.StatusOK, tplSearch)
return
}

View file

@ -83,6 +83,9 @@ func testSearchRepo(t *testing.T, indexer bool) {
code_indexer.UpdateRepoIndexer(repo)
}
testEmptySearch(t, indexer, true)
testEmptySearch(t, indexer, false)
testSearch(t, "/user2/glob/search?q=", []string{}, indexer)
testSearch(t, "/user2/glob/search?q=loren&page=1", []string{"a.txt"}, indexer)
testSearch(t, "/user2/glob/search?q=loren&page=1&mode=exact", []string{"a.txt"}, indexer)
@ -97,6 +100,30 @@ func testSearchRepo(t *testing.T, indexer bool) {
testSearch(t, "/user2/glob/search?q=file5&page=1&mode=exact", []string{}, indexer)
}
func testEmptySearch(t *testing.T, indexer, withFuzzy bool) {
defer test.MockVariableValue(&setting.Indexer.RepoIndexerEnableFuzzy, withFuzzy)()
req := NewRequest(t, "GET", "/user2/glob/search")
resp := MakeRequest(t, req, http.StatusOK)
container := NewHTMLParser(t, resp.Body).
Find(".repository").
Find(".ui.container")
key := "search.exact"
if withFuzzy && indexer {
key = "search.fuzzy"
}
expected := translation.NewLocale("en-US").TrString(key)
menu := container.Find(".menu[data-test-tag=fuzzy-dropdown]")
defaultOpt := menu.
Parent().
Find(".text").
Text()
assert.Equal(t, expected, strings.TrimSpace(defaultOpt))
}
func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) {
req := NewRequest(t, "GET", rawURL)
resp := MakeRequest(t, req, http.StatusOK)
@ -131,10 +158,23 @@ func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) {
// testDropdownOptions verifies additional properties of dropdown options
func testDropdownOptions(t *testing.T, container *goquery.Selection, options []string, locale translation.Locale) {
for _, option := range options {
tr := make([]string, len(options))
for i, option := range options {
tr[i] = locale.TrString(fmt.Sprintf("search.%s", option))
}
// assert that the default value (in a .text adjacent to the menu) is a valid option
defaultOpt := container.
Find(".menu[data-test-tag=fuzzy-dropdown]").
Parent().
Find(".text").
Text()
assert.Contains(t, tr, strings.TrimSpace(defaultOpt))
for i, option := range options {
label := container.Find(fmt.Sprintf("label.item:has(input[value='%s'])", option))
name := strings.TrimSpace(label.Text())
assert.Equal(t, name, locale.TrString(fmt.Sprintf("search.%s", option)))
assert.Equal(t, name, tr[i])
tooltip, exists := label.Attr("data-tooltip-content")
assert.True(t, exists)