Files
yay/pkg/query/metric_test.go
T
Pujitha PaladuguandPujitha Paladugu ce6bcbd775 fix(query): keep pacman.conf repo order for exact name matches (#2987)
Motivation
With separatesources enabled (the default), a package with the exact
searched name that exists in several sync repos can be listed in the
wrong order. For example, if [cachyos-extra-v3] comes before [extra] in
pacman.conf, extra/chromium can still show up as the top pick instead of
cachyos-extra-v3/chromium. pacman prefers the repo listed first.

separateSourceScore returned a flat 50 for every exact match, whatever
its source. As a result, all exact matches got the same metric
(50 + 1.0). The unstable slices.SortFunc then left their relative order
unspecified, so the ordering added for non-exact results did not apply
to exact ones.

Approach
Move the repo-order scoring into a repoOrderScore helper (same logic and
cache as before). For exact matches, add repoOrderScore/separateSourceMax
to the 50: a bonus between 0 and 1. Exact matches still outrank every
non-exact result (which scores at most 45 + a metric below 1). Among
exact matches, repos earlier in pacman.conf now come first. Exact AUR
matches keep 50, so an exact sync-repo match now ranks strictly above an
exact AUR match instead of tying with it. Scoring for non-exact results
is unchanged, and nothing changes when separatesources is disabled.

Validation
- go test -race ./pkg/query/... -count=1: passes.
- New tests TestSourceQueryBuilderExactMatchSortsByRepoOrder (top-down
  and bottom-up) and TestSeparateSourceScore_ExactMatchKeepsRepoOrder
  fail with the previous metric.go and pass with this change.
- golangci-lint run ./pkg/query/...: 0 issues; gofmt clean.
- go test ./... on macOS has failures that also occur on the unchanged
  base: Linux-only syscall fields in pkg/settings/exe, and a
  timezone-dependent test in pkg/text. Not tested against a real
  pacman setup with custom repos.

Report: https://github.com/Jguer/yay/issues/2986

Assisted-by: claude-opus-5-5 (via Claude Code)

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Co-authored-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
2026-09-26 23:56:01 +02:00

66 lines
2.1 KiB
Go

//go:build !integration
package query
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSeparateSourceScore_UsesRepoOrderEvenlyDistributed(t *testing.T) {
t.Parallel()
// Any non-1.0 score avoids the special-case 50 return.
const sim = 0.5
const delta = 1e-1
t.Run("arch repos (core/extra/community/multilib)", func(t *testing.T) {
a := &abstractResults{
separateSources: true,
repoOrder: []string{"core", "extra", "community", "multilib"},
separateSourceCache: map[string]float64{},
}
assert.InDelta(t, 45.0, a.separateSourceScore("core", sim), delta)
assert.InDelta(t, 31.6, a.separateSourceScore("extra", sim), delta)
assert.InDelta(t, 18.3, a.separateSourceScore("community", sim), delta)
assert.InDelta(t, 5.0, a.separateSourceScore("multilib", sim), delta)
assert.Equal(t, 0.0, a.separateSourceScore("aur", sim))
})
t.Run("arch arm repos (core/extra/alarm/aur)", func(t *testing.T) {
a := &abstractResults{
separateSources: true,
repoOrder: []string{"core", "extra", "alarm", "aur"},
separateSourceCache: map[string]float64{},
}
// Note: AUR is not a sync repository; it is always lowest priority (0) regardless of repo order.
assert.InDelta(t, 45.0, a.separateSourceScore("core", sim), delta)
assert.InDelta(t, 31.6, a.separateSourceScore("extra", sim), delta)
assert.InDelta(t, 18.3, a.separateSourceScore("alarm", sim), delta)
assert.Equal(t, 0.0, a.separateSourceScore("aur", sim))
})
}
func TestSeparateSourceScore_ExactMatchKeepsRepoOrder(t *testing.T) {
t.Parallel()
a := &abstractResults{
separateSources: true,
repoOrder: []string{"cachyos-extra-v3", "core", "extra"},
separateSourceCache: map[string]float64{},
}
custom := a.separateSourceScore("cachyos-extra-v3", 1.0)
extra := a.separateSourceScore("extra", 1.0)
aurScore := a.separateSourceScore("aur", 1.0)
assert.Greater(t, custom, extra)
assert.Greater(t, extra, aurScore)
assert.Equal(t, 50.0, aurScore)
// Exact matches must still outrank any non-exact match.
assert.Greater(t, extra+1.0, a.separateSourceScore("cachyos-extra-v3", 0.99)+0.99)
}