diff --git a/pkg/query/query_builder.go b/pkg/query/query_builder.go index d371c706..99885415 100644 --- a/pkg/query/query_builder.go +++ b/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 - if a.bottomUp { - return simA < simB + 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 } - return simA > simB + if a.bottomUp { + cmpResult = !cmpResult + } + + return cmpResult } func (s *SourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string) { diff --git a/pkg/query/query_builder_test.go b/pkg/query/query_builder_test.go index 1ef90cf8..6d87bfed 100644 --- a/pkg/query/query_builder_test.go +++ b/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()) }) } } diff --git a/pkg/query/source.go b/pkg/query/source.go index c7692b56..54ec1423 100644 --- a/pkg/query/source.go +++ b/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, diff --git a/pkg/query/source_test.go b/pkg/query/source_test.go deleted file mode 100644 index 131d8b08..00000000 --- a/pkg/query/source_test.go +++ /dev/null @@ -1,137 +0,0 @@ -//go:build !integration -// +build !integration - -package query - -import ( - "bytes" - "context" - "io" - "net/http" - "testing" - - "github.com/Jguer/aur/rpc" - - "github.com/Jguer/yay/v12/pkg/db" - "github.com/Jguer/yay/v12/pkg/db/mock" - "github.com/Jguer/yay/v12/pkg/settings/parser" - "github.com/Jguer/yay/v12/pkg/text" - - "github.com/Jguer/go-alpm/v2" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -const validPayload = `{ - "resultcount": 1, - "results": [ - { - "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": null, - "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" - } - ], - "type": "search", - "version": 5 -} -` - -type mockDB struct { - db.Executor -} - -func (m *mockDB) LocalPackage(string) alpm.IPackage { - return nil -} - -func (m *mockDB) PackageGroups(pkg alpm.IPackage) []string { - return []string{} -} - -func (m *mockDB) SyncPackages(...string) []alpm.IPackage { - mockDB := mock.NewDB("core") - linuxRepo := &mock.Package{ - PName: "linux", - PVersion: "5.16.0", - PDescription: "The Linux kernel and modules", - PSize: 1, - PISize: 1, - PDB: mockDB, - } - - linuxZen := &mock.Package{ - PName: "linux-zen", - PVersion: "5.16.0", - PDescription: "The Linux ZEN kernel and modules", - PSize: 1, - PISize: 1, - PDB: mockDB, - } - - return []alpm.IPackage{linuxRepo, linuxZen} -} - -type mockDoer struct{} - -func (m *mockDoer) Do(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: io.NopCloser(bytes.NewBufferString(validPayload)), - }, nil -} - -func TestSourceQueryBuilder(t *testing.T) { - t.Parallel() - type testCase struct { - desc string - bottomUp bool - want string - } - - testCases := []testCase{ - {desc: "bottomup", bottomUp: true, want: "\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[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", - }, - } - - for _, tc := range testCases { - t.Run(tc.desc, func(t *testing.T) { - client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{})) - require.NoError(t, err) - - queryBuilder := NewSourceQueryBuilder(client, - text.NewLogger(io.Discard, io.Discard, bytes.NewBufferString(""), false, "test"), - "votes", parser.ModeAny, "", tc.bottomUp, false, true) - search := []string{"linux"} - mockStore := &mockDB{} - - queryBuilder.Execute(context.Background(), mockStore, search) - assert.Equal(t, 3, queryBuilder.Len()) - - if tc.bottomUp { - assert.Equal(t, "linux-ck", queryBuilder.results[0].name) - assert.Equal(t, "linux-zen", queryBuilder.results[1].name) - assert.Equal(t, "linux", queryBuilder.results[2].name) - } else { - assert.Equal(t, "linux-ck", queryBuilder.results[2].name) - assert.Equal(t, "linux-zen", queryBuilder.results[1].name) - assert.Equal(t, "linux", queryBuilder.results[0].name) - } - - queryBuilder.Results(mockStore, Detailed) - }) - } -}