-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch-engine.test.ts
More file actions
250 lines (219 loc) · 7.26 KB
/
search-engine.test.ts
File metadata and controls
250 lines (219 loc) · 7.26 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { describe, it, expect } from 'vitest';
import {
SearchProviderSchema,
AnalyzerConfigSchema,
SearchIndexConfigSchema,
FacetConfigSchema,
SearchConfigSchema,
} from './search-engine.zod';
describe('SearchProviderSchema', () => {
it('should accept valid providers', () => {
const providers = ['elasticsearch', 'algolia', 'meilisearch', 'typesense', 'opensearch'];
providers.forEach((provider) => {
expect(() => SearchProviderSchema.parse(provider)).not.toThrow();
});
});
it('should reject invalid providers', () => {
expect(() => SearchProviderSchema.parse('solr')).toThrow();
expect(() => SearchProviderSchema.parse('lucene')).toThrow();
});
});
describe('AnalyzerConfigSchema', () => {
it('should accept minimal analyzer', () => {
const analyzer = AnalyzerConfigSchema.parse({ type: 'standard' });
expect(analyzer.type).toBe('standard');
});
it('should accept all analyzer types', () => {
const types = ['standard', 'simple', 'whitespace', 'keyword', 'pattern', 'language'];
types.forEach((type) => {
expect(() => AnalyzerConfigSchema.parse({ type })).not.toThrow();
});
});
it('should accept full analyzer config', () => {
const analyzer = AnalyzerConfigSchema.parse({
type: 'language',
language: 'english',
stopwords: ['the', 'a', 'an'],
customFilters: ['lowercase', 'stemmer'],
});
expect(analyzer.language).toBe('english');
expect(analyzer.stopwords).toEqual(['the', 'a', 'an']);
expect(analyzer.customFilters).toEqual(['lowercase', 'stemmer']);
});
it('should reject invalid type', () => {
expect(() => AnalyzerConfigSchema.parse({ type: 'fuzzy' })).toThrow();
});
it('should reject missing type', () => {
expect(() => AnalyzerConfigSchema.parse({})).toThrow();
});
});
describe('SearchIndexConfigSchema', () => {
it('should accept minimal index config with defaults', () => {
const index = SearchIndexConfigSchema.parse({
indexName: 'accounts_idx',
objectName: 'account',
fields: [{ name: 'name', type: 'text' }],
});
expect(index.indexName).toBe('accounts_idx');
expect(index.objectName).toBe('account');
expect(index.fields).toHaveLength(1);
expect(index.fields[0].searchable).toBe(true);
expect(index.fields[0].filterable).toBe(false);
expect(index.fields[0].sortable).toBe(false);
expect(index.fields[0].boost).toBe(1);
expect(index.replicas).toBe(1);
expect(index.shards).toBe(1);
});
it('should accept all field types', () => {
const types = ['text', 'keyword', 'number', 'date', 'boolean', 'geo'];
types.forEach((type) => {
expect(() =>
SearchIndexConfigSchema.parse({
indexName: 'test',
objectName: 'obj',
fields: [{ name: 'f', type }],
}),
).not.toThrow();
});
});
it('should accept full index config', () => {
const index = SearchIndexConfigSchema.parse({
indexName: 'contacts_idx',
objectName: 'contact',
fields: [
{
name: 'full_name',
type: 'text',
analyzer: 'standard',
searchable: true,
filterable: true,
sortable: true,
boost: 2.5,
},
{
name: 'status',
type: 'keyword',
searchable: false,
filterable: true,
sortable: true,
boost: 1,
},
],
replicas: 3,
shards: 5,
});
expect(index.fields).toHaveLength(2);
expect(index.fields[0].boost).toBe(2.5);
expect(index.replicas).toBe(3);
expect(index.shards).toBe(5);
});
it('should reject missing required fields', () => {
expect(() => SearchIndexConfigSchema.parse({})).toThrow();
expect(() =>
SearchIndexConfigSchema.parse({ indexName: 'test', objectName: 'obj' }),
).toThrow();
});
it('should reject invalid field type', () => {
expect(() =>
SearchIndexConfigSchema.parse({
indexName: 'test',
objectName: 'obj',
fields: [{ name: 'f', type: 'vector' }],
}),
).toThrow();
});
});
describe('FacetConfigSchema', () => {
it('should accept minimal facet with defaults', () => {
const facet = FacetConfigSchema.parse({ field: 'status' });
expect(facet.field).toBe('status');
expect(facet.maxValues).toBe(10);
expect(facet.sort).toBe('count');
});
it('should accept full facet config', () => {
const facet = FacetConfigSchema.parse({
field: 'category',
maxValues: 25,
sort: 'alpha',
});
expect(facet.maxValues).toBe(25);
expect(facet.sort).toBe('alpha');
});
it('should accept all sort options', () => {
const sorts = ['count', 'alpha'];
sorts.forEach((sort) => {
expect(() => FacetConfigSchema.parse({ field: 'f', sort })).not.toThrow();
});
});
it('should reject missing field', () => {
expect(() => FacetConfigSchema.parse({})).toThrow();
});
it('should reject invalid sort', () => {
expect(() => FacetConfigSchema.parse({ field: 'f', sort: 'relevance' })).toThrow();
});
});
describe('SearchConfigSchema', () => {
const minimalIndex = {
indexName: 'test_idx',
objectName: 'test',
fields: [{ name: 'title', type: 'text' as const }],
};
it('should accept minimal config with defaults', () => {
const config = SearchConfigSchema.parse({
provider: 'elasticsearch',
indexes: [minimalIndex],
});
expect(config.provider).toBe('elasticsearch');
expect(config.indexes).toHaveLength(1);
expect(config.typoTolerance).toBe(true);
});
it('should accept full configuration', () => {
const config = SearchConfigSchema.parse({
provider: 'meilisearch',
indexes: [minimalIndex],
analyzers: {
custom: { type: 'language', language: 'french' },
},
facets: [{ field: 'status' }, { field: 'category', maxValues: 20 }],
typoTolerance: false,
synonyms: {
phone: ['telephone', 'mobile'],
laptop: ['notebook', 'computer'],
},
ranking: ['typo', 'words', 'proximity', 'attribute', 'exact'],
});
expect(config.provider).toBe('meilisearch');
expect(config.analyzers?.custom.type).toBe('language');
expect(config.facets).toHaveLength(2);
expect(config.typoTolerance).toBe(false);
expect(config.synonyms?.phone).toEqual(['telephone', 'mobile']);
expect(config.ranking).toHaveLength(5);
});
it('should accept all ranking rules', () => {
const rules = ['typo', 'geo', 'words', 'filters', 'proximity', 'attribute', 'exact', 'custom'];
const config = SearchConfigSchema.parse({
provider: 'algolia',
indexes: [minimalIndex],
ranking: rules,
});
expect(config.ranking).toEqual(rules);
});
it('should reject missing required fields', () => {
expect(() => SearchConfigSchema.parse({})).toThrow();
expect(() => SearchConfigSchema.parse({ provider: 'elasticsearch' })).toThrow();
});
it('should reject invalid provider', () => {
expect(() =>
SearchConfigSchema.parse({ provider: 'solr', indexes: [minimalIndex] }),
).toThrow();
});
it('should reject invalid ranking rule', () => {
expect(() =>
SearchConfigSchema.parse({
provider: 'elasticsearch',
indexes: [minimalIndex],
ranking: ['invalid_rule'],
}),
).toThrow();
});
});