|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { SearchTypes } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
} from "../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
jest.setTimeout(120000) |
|
10
|
|
|
11
|
medusaIntegrationTestRunner({ |
|
12
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
13
|
let searchModule: SearchTypes.ISearchModuleService |
|
14
|
|
|
15
|
const groupFor = (data: any, entity: string) => |
|
16
|
data.results.find((group) => group.entity === entity) |
|
17
|
|
|
18
|
// Reindexing runs in the background, so tests that trigger it have to |
|
19
|
// poll for completion rather than assume it's done when the request |
|
20
|
// returns. |
|
21
|
const waitForIndexReady = async (name: string) => { |
|
22
|
const deadline = Date.now() + 10000 |
|
23
|
while (Date.now() < deadline) { |
|
24
|
const listed = await api.get("/admin/search-indexes", adminHeaders) |
|
25
|
const index = listed.data.search_indexes.find( |
|
26
|
(i: any) => i.name === name |
|
27
|
) |
|
28
|
if (index?.status === "ready") { |
|
29
|
return index |
|
30
|
} |
|
31
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
32
|
} |
|
33
|
throw new Error(`Index "${name}" did not become ready in time`) |
|
34
|
} |
|
35
|
|
|
36
|
const indexNamed = async (name: string) => { |
|
37
|
const listed = await api.get("/admin/search-indexes", adminHeaders) |
|
38
|
return listed.data.search_indexes.find((i: any) => i.name === name) |
|
39
|
} |
|
40
|
|
|
41
|
const planFor = async (name: string) => { |
|
42
|
const plan = await searchModule.createIndexMigrationPlan() |
|
43
|
return plan.find((action) => action.index === name) |
|
44
|
} |
|
45
|
|
|
46
|
// Soft deleted rows included — what a delete must leave behind is nothing. |
|
47
|
const rowCounts = async (name: string) => { |
|
48
|
const { rows } = await dbConnection.raw( |
|
49
|
`select |
|
50
|
(select count(*) from search_index where name = ?) as indexes, |
|
51
|
(select count(*) from search_index_version v |
|
52
|
join search_index i on i.id = v.search_index_id |
|
53
|
where i.name = ?) as versions, |
|
54
|
(select count(*) from search_index_sync s |
|
55
|
join search_index_version v on v.id = s.search_index_version_id |
|
56
|
join search_index i on i.id = v.search_index_id |
|
57
|
where i.name = ?) as syncs`, |
|
58
|
[name, name, name] |
|
59
|
) |
|
60
|
|
|
61
|
return { |
|
62
|
indexes: Number(rows[0].indexes), |
|
63
|
versions: Number(rows[0].versions), |
|
64
|
syncs: Number(rows[0].syncs), |
|
65
|
} |
|
66
|
} |
|
67
|
|
|
68
|
beforeAll(async () => { |
|
69
|
const container = getContainer() |
|
70
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
71
|
|
|
72
|
searchModule = container.resolve(Modules.SEARCH) |
|
73
|
|
|
74
|
await api.post( |
|
75
|
"/admin/products", |
|
76
|
{ |
|
77
|
title: "Zephyr Shirt", |
|
78
|
handle: "zephyr-shirt", |
|
79
|
status: "published", |
|
80
|
options: [{ title: "Size", values: ["M"] }], |
|
81
|
}, |
|
82
|
adminHeaders |
|
83
|
) |
|
84
|
|
|
85
|
await api.post( |
|
86
|
"/admin/products", |
|
87
|
{ |
|
88
|
title: "Aurora Shoe", |
|
89
|
handle: "aurora-shoe", |
|
90
|
status: "published", |
|
91
|
options: [{ title: "Size", values: ["42"] }], |
|
92
|
}, |
|
93
|
adminHeaders |
|
94
|
) |
|
95
|
|
|
96
|
await api.post( |
|
97
|
"/admin/customers", |
|
98
|
{ |
|
99
|
email: "zephyr.tester@example.com", |
|
100
|
first_name: "Zephyr", |
|
101
|
last_name: "Tester", |
|
102
|
}, |
|
103
|
adminHeaders |
|
104
|
) |
|
105
|
|
|
106
|
await api.post( |
|
107
|
"/admin/customers", |
|
108
|
{ |
|
109
|
email: "aurora.buyer@example.com", |
|
110
|
first_name: "Aurora", |
|
111
|
last_name: "Buyer", |
|
112
|
}, |
|
113
|
adminHeaders |
|
114
|
) |
|
115
|
|
|
116
|
// Fills both indexes from their seeds. Ingestion has already indexed the |
|
117
|
// products off their events; rebuilding is idempotent and covers the |
|
118
|
// customer index, which declares no events. |
|
119
|
await searchModule.reindex() |
|
120
|
}) |
|
121
|
|
|
122
|
describe("GET /admin/search", () => { |
|
123
|
it("searches every indexed entity and groups the results", async () => { |
|
124
|
const response = await api.get("/admin/search?q=zephyr", adminHeaders) |
|
125
|
|
|
126
|
expect(response.status).toEqual(200) |
|
127
|
expect(response.data.results.map((g) => g.entity).sort()).toEqual([ |
|
128
|
"customer", |
|
129
|
"product", |
|
130
|
]) |
|
131
|
|
|
132
|
expect(groupFor(response.data, "product").data).toEqual([ |
|
133
|
expect.objectContaining({ |
|
134
|
title: "Zephyr Shirt", |
|
135
|
handle: "zephyr-shirt", |
|
136
|
status: "published", |
|
137
|
}), |
|
138
|
]) |
|
139
|
|
|
140
|
expect(groupFor(response.data, "customer").data).toEqual([ |
|
141
|
expect.objectContaining({ |
|
142
|
email: "zephyr.tester@example.com", |
|
143
|
first_name: "Zephyr", |
|
144
|
}), |
|
145
|
]) |
|
146
|
}) |
|
147
|
|
|
148
|
it("returns a group with no hits rather than dropping the entity", async () => { |
|
149
|
const response = await api.get("/admin/search?q=shoe", adminHeaders) |
|
150
|
|
|
151
|
expect(response.status).toEqual(200) |
|
152
|
expect(groupFor(response.data, "product").data).toEqual([ |
|
153
|
expect.objectContaining({ title: "Aurora Shoe" }), |
|
154
|
]) |
|
155
|
expect(groupFor(response.data, "customer")).toEqual( |
|
156
|
expect.objectContaining({ entity: "customer", data: [], count: 0 }) |
|
157
|
) |
|
158
|
}) |
|
159
|
|
|
160
|
it("matches a prefix of the last query term", async () => { |
|
161
|
const prefix = await api.get("/admin/search?q=zep", adminHeaders) |
|
162
|
|
|
163
|
expect(groupFor(prefix.data, "product").data).toEqual([ |
|
164
|
expect.objectContaining({ title: "Zephyr Shirt" }), |
|
165
|
]) |
|
166
|
|
|
167
|
const twoTerms = await api.get( |
|
168
|
"/admin/search?q=zephyr sh", |
|
169
|
adminHeaders |
|
170
|
) |
|
171
|
|
|
172
|
expect(groupFor(twoTerms.data, "product").data).toEqual([ |
|
173
|
expect.objectContaining({ title: "Zephyr Shirt" }), |
|
174
|
]) |
|
175
|
}) |
|
176
|
|
|
177
|
it("restricts the search to the requested entities", async () => { |
|
178
|
const response = await api.get( |
|
179
|
"/admin/search?q=zephyr&entity=product", |
|
180
|
adminHeaders |
|
181
|
) |
|
182
|
|
|
183
|
expect(response.status).toEqual(200) |
|
184
|
expect(response.data.results.length).toEqual(1) |
|
185
|
expect(response.data.results[0]).toEqual( |
|
186
|
expect.objectContaining({ entity: "product", offset: 0, limit: 20 }) |
|
187
|
) |
|
188
|
}) |
|
189
|
|
|
190
|
it("accepts a comma-separated list of entities, in the order given", async () => { |
|
191
|
const response = await api.get( |
|
192
|
"/admin/search?entity=customer,product", |
|
193
|
adminHeaders |
|
194
|
) |
|
195
|
|
|
196
|
expect(response.status).toEqual(200) |
|
197
|
expect(response.data.results.map((g) => g.entity)).toEqual([ |
|
198
|
"customer", |
|
199
|
"product", |
|
200
|
]) |
|
201
|
}) |
|
202
|
|
|
203
|
it("returns every document of an entity when no query is given", async () => { |
|
204
|
const response = await api.get( |
|
205
|
"/admin/search?entity=product", |
|
206
|
adminHeaders |
|
207
|
) |
|
208
|
|
|
209
|
expect(response.status).toEqual(200) |
|
210
|
expect(groupFor(response.data, "product").data).toHaveLength(2) |
|
211
|
expect(groupFor(response.data, "product").count).toEqual(2) |
|
212
|
}) |
|
213
|
|
|
214
|
it("paginates each group on its own", async () => { |
|
215
|
const first = await api.get( |
|
216
|
"/admin/search?entity=product,customer&limit=1", |
|
217
|
adminHeaders |
|
218
|
) |
|
219
|
|
|
220
|
expect(first.data.results.map((g) => g.entity)).toEqual([ |
|
221
|
"product", |
|
222
|
"customer", |
|
223
|
]) |
|
224
|
|
|
225
|
for (const entity of ["product", "customer"]) { |
|
226
|
expect(groupFor(first.data, entity).data).toHaveLength(1) |
|
227
|
expect(groupFor(first.data, entity)).toEqual( |
|
228
|
expect.objectContaining({ limit: 1, offset: 0, count: 2 }) |
|
229
|
) |
|
230
|
} |
|
231
|
|
|
232
|
const second = await api.get( |
|
233
|
"/admin/search?entity=product,customer&limit=1&offset=1", |
|
234
|
adminHeaders |
|
235
|
) |
|
236
|
|
|
237
|
for (const entity of ["product", "customer"]) { |
|
238
|
expect(groupFor(second.data, entity).data).toHaveLength(1) |
|
239
|
expect(groupFor(second.data, entity).offset).toEqual(1) |
|
240
|
expect(groupFor(second.data, entity).data[0].id).not.toEqual( |
|
241
|
groupFor(first.data, entity).data[0].id |
|
242
|
) |
|
243
|
} |
|
244
|
}) |
|
245
|
|
|
246
|
it("returns only the fields the index holds", async () => { |
|
247
|
const response = await api.get( |
|
248
|
"/admin/search?entity=product&q=zephyr", |
|
249
|
adminHeaders |
|
250
|
) |
|
251
|
|
|
252
|
expect( |
|
253
|
Object.keys(groupFor(response.data, "product").data[0]).sort() |
|
254
|
).toEqual(["handle", "id", "sales_channel_ids", "status", "title"]) |
|
255
|
}) |
|
256
|
|
|
257
|
it("queries the database for an entity that has no index", async () => { |
|
258
|
const response = await api.get( |
|
259
|
"/admin/search?entity=region", |
|
260
|
adminHeaders |
|
261
|
) |
|
262
|
|
|
263
|
expect(response.status).toEqual(200) |
|
264
|
expect(response.data.results).toEqual([ |
|
265
|
expect.objectContaining({ entity: "region", data: [], count: 0 }), |
|
266
|
]) |
|
267
|
}) |
|
268
|
|
|
269
|
it("uses the search engine for indexed entities and the database for the rest", async () => { |
|
270
|
const response = await api.get( |
|
271
|
"/admin/search?q=zephyr&entity=product,region", |
|
272
|
adminHeaders |
|
273
|
) |
|
274
|
|
|
275
|
expect(response.status).toEqual(200) |
|
276
|
expect(response.data.results.map((g) => g.entity)).toEqual([ |
|
277
|
"product", |
|
278
|
"region", |
|
279
|
]) |
|
280
|
|
|
281
|
// Index fields for product; graph fields would be id/thumbnail/title. |
|
282
|
expect( |
|
283
|
Object.keys(groupFor(response.data, "product").data[0]).sort() |
|
284
|
).toEqual(["handle", "id", "sales_channel_ids", "status", "title"]) |
|
285
|
expect(groupFor(response.data, "product").data).toEqual([ |
|
286
|
expect.objectContaining({ |
|
287
|
title: "Zephyr Shirt", |
|
288
|
}), |
|
289
|
]) |
|
290
|
expect(groupFor(response.data, "region")).toEqual( |
|
291
|
expect.objectContaining({ entity: "region", data: [], count: 0 }) |
|
292
|
) |
|
293
|
}) |
|
294
|
|
|
295
|
it("fails on an entity that is neither indexed nor searchable in the database", async () => { |
|
296
|
const error = await api |
|
297
|
.get("/admin/search?entity=nope", adminHeaders) |
|
298
|
.catch((e) => e) |
|
299
|
|
|
300
|
expect(error.response.status).toEqual(404) |
|
301
|
expect(error.response.data.message).toContain("nope") |
|
302
|
}) |
|
303
|
|
|
304
|
it("rejects an unknown query parameter", async () => { |
|
305
|
const error = await api |
|
306
|
.get("/admin/search?q=zephyr&fields=id", adminHeaders) |
|
307
|
.catch((e) => e) |
|
308
|
|
|
309
|
expect(error.response.status).toEqual(400) |
|
310
|
}) |
|
311
|
|
|
312
|
it("requires authentication", async () => { |
|
313
|
const error = await api.get("/admin/search?q=zephyr").catch((e) => e) |
|
314
|
|
|
315
|
expect(error.response.status).toEqual(401) |
|
316
|
}) |
|
317
|
}) |
|
318
|
|
|
319
|
describe("GET /admin/search-indexes", () => { |
|
320
|
it("lists registered indexes with their fields and status", async () => { |
|
321
|
const response = await api.get("/admin/search-indexes", adminHeaders) |
|
322
|
|
|
323
|
expect(response.status).toEqual(200) |
|
324
|
expect(response.data.enabled).toEqual(true) |
|
325
|
expect(response.data.search_indexes.map((index) => index.name)).toEqual( |
|
326
|
["customer", "product"] |
|
327
|
) |
|
328
|
|
|
329
|
const product = response.data.search_indexes.find( |
|
330
|
(index) => index.name === "product" |
|
331
|
) |
|
332
|
const customer = response.data.search_indexes.find( |
|
333
|
(index) => index.name === "customer" |
|
334
|
) |
|
335
|
|
|
336
|
expect(product).toMatchObject({ |
|
337
|
name: "product", |
|
338
|
entity: "product", |
|
339
|
status: "ready", |
|
340
|
}) |
|
341
|
expect(product.fields.map((field) => field.name).sort()).toEqual( |
|
342
|
["handle", "id", "sales_channel_ids", "status", "title"].sort() |
|
343
|
) |
|
344
|
expect( |
|
345
|
product.fields.find((field) => field.name === "title") |
|
346
|
).toMatchObject({ |
|
347
|
type: "text", |
|
348
|
searchable: true, |
|
349
|
sortable: true, |
|
350
|
}) |
|
351
|
|
|
352
|
expect(customer).toMatchObject({ |
|
353
|
name: "customer", |
|
354
|
entity: "customer", |
|
355
|
status: "ready", |
|
356
|
}) |
|
357
|
expect(customer.fields.map((field) => field.name).sort()).toEqual( |
|
358
|
["email", "first_name", "id", "last_name"].sort() |
|
359
|
) |
|
360
|
}) |
|
361
|
|
|
362
|
it("requires authentication", async () => { |
|
363
|
const error = await api.get("/admin/search-indexes").catch((e) => e) |
|
364
|
|
|
365
|
expect(error.response.status).toEqual(401) |
|
366
|
}) |
|
367
|
}) |
|
368
|
|
|
369
|
describe("POST /admin/search-indexes/:id/reindex", () => { |
|
370
|
it("rebuilds a specific index from its seed", async () => { |
|
371
|
const response = await api.post( |
|
372
|
"/admin/search-indexes/product/reindex", |
|
373
|
{}, |
|
374
|
adminHeaders |
|
375
|
) |
|
376
|
|
|
377
|
// The reindex runs in the background, so the route responds as soon |
|
378
|
// as it's triggered rather than waiting for the rebuild to finish. |
|
379
|
expect(response.status).toEqual(202) |
|
380
|
expect(response.data).toEqual({ |
|
381
|
job_id: expect.any(String), |
|
382
|
indexes: ["product"], |
|
383
|
}) |
|
384
|
|
|
385
|
const product = await waitForIndexReady("product") |
|
386
|
expect(product.status).toBe("ready") |
|
387
|
|
|
388
|
const search = await api.get( |
|
389
|
"/admin/search?q=zephyr&entity=product", |
|
390
|
adminHeaders |
|
391
|
) |
|
392
|
expect(groupFor(search.data, "product").data).toEqual([ |
|
393
|
expect.objectContaining({ title: "Zephyr Shirt" }), |
|
394
|
]) |
|
395
|
}) |
|
396
|
|
|
397
|
it("rejects a since value that is not a date", async () => { |
|
398
|
const error = await api |
|
399
|
.post( |
|
400
|
"/admin/search-indexes/product/reindex", |
|
401
|
{ since: "yesterday" }, |
|
402
|
adminHeaders |
|
403
|
) |
|
404
|
.catch((e) => e) |
|
405
|
|
|
406
|
expect(error.response.status).toEqual(400) |
|
407
|
}) |
|
408
|
|
|
409
|
it("rejects an unknown field in the body", async () => { |
|
410
|
const error = await api |
|
411
|
.post( |
|
412
|
"/admin/search-indexes/product/reindex", |
|
413
|
{ nope: true }, |
|
414
|
adminHeaders |
|
415
|
) |
|
416
|
.catch((e) => e) |
|
417
|
|
|
418
|
expect(error.response.status).toEqual(400) |
|
419
|
}) |
|
420
|
|
|
421
|
it("fails when the index is not registered", async () => { |
|
422
|
const error = await api |
|
423
|
.post("/admin/search-indexes/nope/reindex", {}, adminHeaders) |
|
424
|
.catch((e) => e) |
|
425
|
|
|
426
|
expect(error.response.status).toEqual(404) |
|
427
|
expect(error.response.data.message).toContain("nope") |
|
428
|
}) |
|
429
|
}) |
|
430
|
|
|
431
|
// Destructive, but each test restores the snapshot taken after `beforeAll`, |
|
432
|
// so every one does its own deleting. |
|
433
|
describe("DELETE /admin/search-indexes/:id", () => { |
|
434
|
it("fails when the index is not registered", async () => { |
|
435
|
const error = await api |
|
436
|
.delete("/admin/search-indexes/nope", adminHeaders) |
|
437
|
.catch((e) => e) |
|
438
|
|
|
439
|
expect(error.response.status).toEqual(404) |
|
440
|
expect(error.response.data.message).toContain("nope") |
|
441
|
}) |
|
442
|
|
|
443
|
it("requires authentication", async () => { |
|
444
|
const error = await api |
|
445
|
.delete("/admin/search-indexes/customer") |
|
446
|
.catch((e) => e) |
|
447
|
|
|
448
|
expect(error.response.status).toEqual(401) |
|
449
|
}) |
|
450
|
|
|
451
|
it("drops every version and the rows behind them", async () => { |
|
452
|
const before = await rowCounts("customer") |
|
453
|
|
|
454
|
// The boot seed built one version and `beforeAll`'s reindex another, so |
|
455
|
// this covers more than the one serving reads. |
|
456
|
expect(before.versions).toBeGreaterThan(1) |
|
457
|
|
|
458
|
const response = await api.delete( |
|
459
|
"/admin/search-indexes/customer", |
|
460
|
adminHeaders |
|
461
|
) |
|
462
|
|
|
463
|
expect(response.status).toEqual(200) |
|
464
|
expect(response.data).toEqual({ |
|
465
|
id: "customer", |
|
466
|
object: "search_index", |
|
467
|
deleted: true, |
|
468
|
deleted_versions: before.versions, |
|
469
|
}) |
|
470
|
|
|
471
|
expect(await rowCounts("customer")).toEqual({ |
|
472
|
indexes: 0, |
|
473
|
versions: 0, |
|
474
|
syncs: 0, |
|
475
|
}) |
|
476
|
|
|
477
|
// Still listed, since the definition comes from code. |
|
478
|
expect(await indexNamed("customer")).toMatchObject({ |
|
479
|
name: "customer", |
|
480
|
status: "pending", |
|
481
|
}) |
|
482
|
|
|
483
|
// The point of deleting: numbering restarts rather than carrying on. |
|
484
|
expect(await planFor("customer")).toMatchObject({ |
|
485
|
action: "create", |
|
486
|
version: 1, |
|
487
|
}) |
|
488
|
|
|
489
|
expect(await planFor("product")).toMatchObject({ action: "noop" }) |
|
490
|
expect((await rowCounts("product")).versions).toBeGreaterThan(0) |
|
491
|
}) |
|
492
|
|
|
493
|
it("is a no-op when the index has nothing built", async () => { |
|
494
|
await api.delete("/admin/search-indexes/customer", adminHeaders) |
|
495
|
|
|
496
|
const response = await api.delete( |
|
497
|
"/admin/search-indexes/customer", |
|
498
|
adminHeaders |
|
499
|
) |
|
500
|
|
|
501
|
expect(response.status).toEqual(200) |
|
502
|
expect(response.data).toEqual({ |
|
503
|
id: "customer", |
|
504
|
object: "search_index", |
|
505
|
deleted: true, |
|
506
|
deleted_versions: 0, |
|
507
|
}) |
|
508
|
}) |
|
509
|
|
|
510
|
it("rebuilds into a working index after a migration", async () => { |
|
511
|
await api.delete("/admin/search-indexes/customer", adminHeaders) |
|
512
|
|
|
513
|
const plan = await searchModule.createIndexMigrationPlan() |
|
514
|
await searchModule.executeIndexMigrationPlan(plan) |
|
515
|
|
|
516
|
await searchModule.reindex({ index: "customer" }) |
|
517
|
await waitForIndexReady("customer") |
|
518
|
|
|
519
|
const search = await api.get( |
|
520
|
"/admin/search?q=zephyr&entity=customer", |
|
521
|
adminHeaders |
|
522
|
) |
|
523
|
|
|
524
|
expect(groupFor(search.data, "customer").data).toEqual([ |
|
525
|
expect.objectContaining({ email: "zephyr.tester@example.com" }), |
|
526
|
]) |
|
527
|
}) |
|
528
|
}) |
|
529
|
}, |
|
530
|
}) |