Browse Source

Fix sorting by name in package search (#2198)

Slightly reorganize and add more tests for SourceQueryBuilder.
smolx 1 year ago
parent
commit
12282fb28a
4 changed files with 348 additions and 182 deletions
  1. 27 4
      pkg/query/query_builder.go
  2. 321 32
      pkg/query/query_builder_test.go
  3. 0 9
      pkg/query/source.go
  4. 0 137
      pkg/query/source_test.go

+ 27 - 4
pkg/query/query_builder.go

@@ -22,6 +22,15 @@ import (
 
 const sourceAUR = "aur"
 
+type SearchVerbosity int
+
+// Verbosity settings for search.
+const (
+	NumberMenu SearchVerbosity = iota
+	Detailed
+	Minimal
+)
+
 type Builder interface {
 	Len() int
 	Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string)
@@ -94,14 +103,28 @@ func (a *abstractResults) Less(i, j int) bool {
 	pkgA := a.results[i]
 	pkgB := a.results[j]
 
-	simA := a.calculateMetric(&pkgA)
-	simB := a.calculateMetric(&pkgB)
+	var cmpResult bool
+
+	switch a.sortBy {
+	case "name":
+		cmpResult = !text.LessRunes([]rune(pkgA.name), []rune(pkgB.name))
+		if a.separateSources {
+			cmpSources := strings.Compare(pkgA.source, pkgB.source)
+			if cmpSources != 0 {
+				cmpResult = cmpSources > 0
+			}
+		}
+	default:
+		simA := a.calculateMetric(&pkgA)
+		simB := a.calculateMetric(&pkgB)
+		cmpResult = simA > simB
+	}
 
 	if a.bottomUp {
-		return simA < simB
+		cmpResult = !cmpResult
 	}
 
-	return simA > simB
+	return cmpResult
 }
 
 func (s *SourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string) {

+ 321 - 32
pkg/query/query_builder_test.go

@@ -9,65 +9,354 @@ import (
 	"strings"
 	"testing"
 
-	"github.com/Jguer/aur/rpc"
+	"github.com/Jguer/aur"
 
+	"github.com/Jguer/yay/v12/pkg/db/mock"
+	mockaur "github.com/Jguer/yay/v12/pkg/dep/mock"
 	"github.com/Jguer/yay/v12/pkg/settings/parser"
 	"github.com/Jguer/yay/v12/pkg/text"
 
 	"github.com/stretchr/testify/assert"
-	"github.com/stretchr/testify/require"
 )
 
-func TestMixedSourceQueryBuilder(t *testing.T) {
+func TestSourceQueryBuilder(t *testing.T) {
 	t.Parallel()
 	type testCase struct {
-		desc     string
-		bottomUp bool
-		want     string
+		desc              string
+		search            []string
+		bottomUp          bool
+		separateSources   bool
+		sortBy            string
+		verbosity         SearchVerbosity
+		targetMode        parser.TargetMode
+		singleLineResults bool
+		searchBy          string
+		wantResults       []string
+		wantOutput        []string
 	}
 
 	testCases := []testCase{
 		{
-			desc: "bottomup", bottomUp: true,
-			want: "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+			desc:            "sort-by-votes bottomup separatesources",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "votes",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux-ck", "linux-zen", "linux"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+			},
 		},
 		{
-			desc: "topdown", bottomUp: false,
-			want: "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+			desc:            "sort-by-votes topdown separatesources",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: true,
+			sortBy:          "votes",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux", "linux-zen", "linux-ck"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+		{
+			desc:            "sort-by-votes bottomup noseparatesources",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: false,
+			sortBy:          "votes",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux-zen", "linux-ck", "linux"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-votes topdown noseparatesources",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: false,
+			sortBy:          "votes",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux", "linux-ck", "linux-zen"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-name bottomup separatesources",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux-ck", "linux", "linux-zen"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-name topdown separatesources",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux-zen", "linux", "linux-ck"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+		{
+			desc:            "sort-by-name bottomup noseparatesources",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: false,
+			sortBy:          "name",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux", "linux-ck", "linux-zen"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-name topdown noseparatesources",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: false,
+			sortBy:          "name",
+			verbosity:       Detailed,
+			wantResults:     []string{"linux-zen", "linux-ck", "linux"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-votes bottomup separatesources number-menu",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "votes",
+			verbosity:       NumberMenu,
+			wantResults:     []string{"linux-ck", "linux-zen", "linux"},
+			wantOutput: []string{
+				"\x1b[35m3\x1b[0m \x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[35m2\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[35m1\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-votes topdown separatesources number-menu",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: true,
+			sortBy:          "votes",
+			verbosity:       NumberMenu,
+			wantResults:     []string{"linux", "linux-zen", "linux-ck"},
+			wantOutput: []string{
+				"\x1b[35m1\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[35m2\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[35m3\x1b[0m \x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+		{
+			desc:            "sort-by-name bottomup separatesources number-menu",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       NumberMenu,
+			wantResults:     []string{"linux-ck", "linux", "linux-zen"},
+			wantOutput: []string{
+				"\x1b[35m3\x1b[0m \x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[35m2\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[35m1\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-name topdown separatesources number-menu",
+			search:          []string{"linux"},
+			bottomUp:        false,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       NumberMenu,
+			wantResults:     []string{"linux-zen", "linux", "linux-ck"},
+			wantOutput: []string{
+				"\x1b[35m1\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux ZEN kernel and modules\n",
+				"\x1b[35m2\x1b[0m \x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n    The Linux kernel and modules\n",
+				"\x1b[35m3\x1b[0m \x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+		{
+			desc:            "sort-by-name bottomup noseparatesources minimal",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: false,
+			sortBy:          "name",
+			verbosity:       Minimal,
+			wantResults:     []string{"linux", "linux-ck", "linux-zen"},
+			wantOutput: []string{
+				"linux\n",
+				"linux-ck\n",
+				"linux-zen\n",
+			},
+		},
+		{
+			desc:            "only-aur minimal",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       Minimal,
+			targetMode:      parser.ModeAUR,
+			wantResults:     []string{"linux-ck"},
+			wantOutput: []string{
+				"linux-ck\n",
+			},
+		},
+		{
+			desc:            "only-repo minimal",
+			search:          []string{"linux"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       Minimal,
+			targetMode:      parser.ModeRepo,
+			wantResults:     []string{"linux", "linux-zen"},
+			wantOutput: []string{
+				"linux\n",
+				"linux-zen\n",
+			},
+		},
+		{
+			desc:              "sort-by-name singleline",
+			search:            []string{"linux"},
+			bottomUp:          true,
+			separateSources:   true,
+			sortBy:            "name",
+			verbosity:         Detailed,
+			singleLineResults: true,
+			wantResults:       []string{"linux-ck", "linux", "linux-zen"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\tThe Linux-ck kernel and modules with ck's hrtimer patches\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux kernel and modules\n",
+				"\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux ZEN kernel and modules\n",
+			},
+		},
+		{
+			desc:            "sort-by-name search-by-name",
+			search:          []string{"linux-ck"},
+			bottomUp:        true,
+			separateSources: true,
+			sortBy:          "name",
+			verbosity:       Detailed,
+			searchBy:        "name",
+			targetMode:      parser.ModeAUR,
+			wantResults:     []string{"linux-ck"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+		{
+			desc:            "only-aur search-by-several-terms",
+			search:          []string{"linux-ck", "hrtimer"},
+			bottomUp:        true,
+			separateSources: true,
+			verbosity:       Detailed,
+			targetMode:      parser.ModeAUR,
+			wantResults:     []string{"linux-ck"},
+			wantOutput: []string{
+				"\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n    The Linux-ck kernel and modules with ck's hrtimer patches\n",
+			},
+		},
+	}
+
+	mockDB := &mock.DBExecutor{
+		SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
+			mockDB := mock.NewDB("core")
+			return []mock.IPackage{
+				&mock.Package{
+					PName:        "linux",
+					PVersion:     "5.16.0",
+					PDescription: "The Linux kernel and modules",
+					PSize:        1,
+					PISize:       1,
+					PDB:          mockDB,
+				},
+				&mock.Package{
+					PName:        "linux-zen",
+					PVersion:     "5.16.0",
+					PDescription: "The Linux ZEN kernel and modules",
+					PSize:        1,
+					PISize:       1,
+					PDB:          mockDB,
+				},
+			}
+		},
+		LocalPackageFn: func(string) mock.IPackage {
+			return nil
+		},
+	}
+
+	mockAUR := &mockaur.MockAUR{
+		GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
+			return []aur.Pkg{
+				{
+					Description:    "The Linux-ck kernel and modules with ck's hrtimer patches",
+					FirstSubmitted: 1311346274,
+					ID:             1045311,
+					LastModified:   1646250901,
+					Maintainer:     "graysky",
+					Name:           "linux-ck",
+					NumVotes:       450,
+					OutOfDate:      0,
+					PackageBase:    "linux-ck",
+					PackageBaseID:  50911,
+					Popularity:     1.511141,
+					URL:            "https://wiki.archlinux.org/index.php/Linux-ck",
+					URLPath:        "/cgit/aur.git/snapshot/linux-ck.tar.gz",
+					Version:        "5.16.12-1",
+				},
+			}, nil
 		},
 	}
 
 	for _, tc := range testCases {
 		t.Run(tc.desc, func(t *testing.T) {
-			client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
-
 			w := &strings.Builder{}
-			queryBuilder := NewSourceQueryBuilder(client,
+			queryBuilder := NewSourceQueryBuilder(mockAUR,
 				text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
-				"votes", parser.ModeAny, "", tc.bottomUp, false, false)
-			search := []string{"linux"}
-			mockStore := &mockDB{}
+				tc.sortBy, tc.targetMode, tc.searchBy, tc.bottomUp,
+				tc.singleLineResults, tc.separateSources)
 
-			require.NoError(t, err)
-			queryBuilder.Execute(context.Background(), mockStore, search)
-			assert.Len(t, queryBuilder.results, 3)
-			assert.Equal(t, 3, queryBuilder.Len())
+			queryBuilder.Execute(context.Background(), mockDB, tc.search)
 
-			if tc.bottomUp {
-				assert.Equal(t, "linux-zen", queryBuilder.results[0].name)
-				assert.Equal(t, "linux-ck", queryBuilder.results[1].name)
-				assert.Equal(t, "linux", queryBuilder.results[2].name)
-			} else {
-				assert.Equal(t, "linux-zen", queryBuilder.results[2].name)
-				assert.Equal(t, "linux-ck", queryBuilder.results[1].name)
-				assert.Equal(t, "linux", queryBuilder.results[0].name)
+			assert.Len(t, queryBuilder.results, len(tc.wantResults))
+			assert.Equal(t, len(tc.wantResults), queryBuilder.Len())
+			for i, name := range tc.wantResults {
+				assert.Equal(t, name, queryBuilder.results[i].name)
 			}
 
-			queryBuilder.Results(mockStore, Detailed)
+			queryBuilder.Results(mockDB, tc.verbosity)
 
-			wString := w.String()
-			require.GreaterOrEqual(t, len(wString), 1, wString)
-			assert.Equal(t, tc.want, wString)
+			assert.Equal(t, strings.Join(tc.wantOutput, ""), w.String())
 		})
 	}
 }

+ 0 - 9
pkg/query/source.go

@@ -7,15 +7,6 @@ import (
 	"github.com/hashicorp/go-multierror"
 )
 
-type SearchVerbosity int
-
-// Verbosity settings for search.
-const (
-	NumberMenu SearchVerbosity = iota
-	Detailed
-	Minimal
-)
-
 // queryAUR searches AUR and narrows based on subarguments.
 func queryAUR(ctx context.Context,
 	aurClient aur.QueryClient,

File diff suppressed because it is too large
+ 0 - 137
pkg/query/source_test.go