1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package index_test
import (
"context"
"maps"
"math"
"slices"
"testing"
"time"
"go.alanpearce.eu/searchix/internal/config"
"go.alanpearce.eu/searchix/internal/index"
"go.alanpearce.eu/searchix/internal/nix"
"go.alanpearce.eu/x/log"
)
const dataRoot = "../../data"
func TestSearchGitPackagesFirst(t *testing.T) {
log := log.Configure(false)
cfg := config.DefaultConfig
read, _, exists, err := index.OpenOrCreate(dataRoot, false, &index.Options{
Logger: log.Named("index"),
LowMemory: false,
})
defer read.Close()
if err != nil {
t.Fatal(err)
}
if !exists {
t.Fatal("expected index to exist")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
source := cfg.Importer.Sources["nixpkgs"]
if source == nil || !source.Enable {
t.Fatal("expected source to exist and be enabled")
}
result, err := read.Search(
ctx,
source,
"git",
0,
100,
)
if err != nil {
t.Fatal(err)
}
if result.Total < 4 {
t.Errorf("Expected at least 4 results, got %d", result.Total)
}
important := map[string]int{
"git": 0,
"git-doc": 0,
"gitFull": 0,
"gitMinimal": 0,
"gitSVN": 0,
}
var i int
for hit := range result.Hits {
data := hit.Data.(nix.Package)
if _, found := important[data.Attribute]; found {
important[data.Attribute] = i
}
i++
}
if slices.Max(slices.Collect(maps.Values(important))) > len(important) {
t.Errorf(
"Expected all of %s to be the first %d matches, got %v",
slices.Collect(maps.Keys(important)),
len(important),
important,
)
}
}
func TestSearchJujutsuPackagesFirst(t *testing.T) {
log := log.Configure(false)
cfg := config.DefaultConfig
read, _, exists, err := index.OpenOrCreate(dataRoot, false, &index.Options{
Logger: log.Named("index"),
LowMemory: false,
})
defer read.Close()
if err != nil {
t.Fatal(err)
}
if !exists {
t.Fatal("expected index to exist")
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
source := cfg.Importer.Sources["nixpkgs"]
if source == nil || !source.Enable {
t.Fatal("expected source to exist and be enabled")
}
result, err := read.Search(
ctx,
source,
"jj",
0,
100,
)
if err != nil {
t.Fatal(err)
}
if result.Total < 4 {
t.Errorf("Expected at least 4 results, got %d", result.Total)
}
important := map[string]int{
"jj": 0,
"jujutsu": 0,
"lazyjj": 0,
"jjui": 0,
"jj-fzf": 0,
}
matches := []string{}
unwanted := "javacc"
unwantedIndex := math.MaxInt
var i int
for hit := range result.Hits {
data := hit.Data.(nix.Package)
if _, found := important[data.Attribute]; found {
matches = append(matches, data.Attribute)
} else if data.Attribute == unwanted {
unwantedIndex = i
matches = append(matches, data.Attribute)
}
i++
}
if slices.Max(slices.Collect(maps.Values(important))) > unwantedIndex {
t.Errorf(
"Expected all of %s to be above unwanted result %s at index %d. Results: %v",
slices.Collect(maps.Keys(important)),
unwanted,
unwantedIndex,
matches,
)
}
}
|