|
1
|
import { SearchTypes } from "@medusajs/framework/types" |
|
2
|
import { Modules } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { |
|
5
|
productIndex, |
|
6
|
products, |
|
7
|
} from "../__fixtures__/medusa-cloud-product-index" |
|
8
|
|
|
9
|
type SearchService = SearchTypes.ISearchModuleService |
|
10
|
|
|
11
|
const apiKey = process.env.MEDUSA_CLOUD_API_KEY |
|
12
|
const endpoint = process.env.MEDUSA_CLOUD_SEARCH_ENDPOINT |
|
13
|
const environmentHandle = process.env.MEDUSA_CLOUD_ENVIRONMENT_HANDLE |
|
14
|
|
|
15
|
if (apiKey && endpoint && environmentHandle) { |
|
16
|
jest.setTimeout(120000) |
|
17
|
|
|
18
|
const boot = (service: SearchService) => |
|
19
|
(service as any).onApplicationStart_() as Promise<void> |
|
20
|
|
|
21
|
moduleIntegrationTestRunner<SearchService>({ |
|
22
|
moduleName: Modules.SEARCH, |
|
23
|
resolve: __dirname + "/../../src", |
|
24
|
moduleOptions: { |
|
25
|
cloud: { |
|
26
|
api_key: apiKey, |
|
27
|
endpoint, |
|
28
|
environment_handle: environmentHandle, |
|
29
|
}, |
|
30
|
default_provider: "search-medusa", |
|
31
|
indexes: [productIndex], |
|
32
|
}, |
|
33
|
hooks: { |
|
34
|
afterModuleInit: async (_app: any, service: SearchService) => { |
|
35
|
await service.executeIndexMigrationPlan( |
|
36
|
await service.createIndexMigrationPlan() |
|
37
|
) |
|
38
|
await boot(service) |
|
39
|
}, |
|
40
|
}, |
|
41
|
testSuite: ({ service }) => |
|
42
|
describe("Medusa Cloud Search Provider", () => { |
|
43
|
afterAll(async () => { |
|
44
|
const provider = (service as any).context_.providers.retrieve( |
|
45
|
"search-medusa" |
|
46
|
) |
|
47
|
await provider.deleteIndex({ index: productIndex.name }) |
|
48
|
}) |
|
49
|
|
|
50
|
it("seeds and searches documents", async () => { |
|
51
|
const result = await service.search({ |
|
52
|
entity: productIndex.name, |
|
53
|
filters: { q: "running" }, |
|
54
|
pagination: { take: 10 }, |
|
55
|
}) |
|
56
|
|
|
57
|
expect(result.hits).toHaveLength(products.length) |
|
58
|
expect(result.metadata.count).toBe(products.length) |
|
59
|
|
|
60
|
const provider = (service as any).context_.providers.retrieve( |
|
61
|
"search-medusa" |
|
62
|
) |
|
63
|
const indexes = await provider.listIndexes() |
|
64
|
expect(indexes).toEqual( |
|
65
|
expect.arrayContaining([ |
|
66
|
expect.objectContaining({ |
|
67
|
name: productIndex.name, |
|
68
|
provider: "search-medusa", |
|
69
|
document_count: products.length, |
|
70
|
}), |
|
71
|
]) |
|
72
|
) |
|
73
|
}) |
|
74
|
|
|
75
|
it("filters and returns facets", async () => { |
|
76
|
const result = await service.search({ |
|
77
|
entity: productIndex.name, |
|
78
|
filters: { status: "published" }, |
|
79
|
search_options: { facets: ["tags"] }, |
|
80
|
}) |
|
81
|
|
|
82
|
expect(result.facets?.tags).toMatchObject({ |
|
83
|
type: "value", |
|
84
|
values: expect.arrayContaining([{ value: "sport", count: 2 }]), |
|
85
|
}) |
|
86
|
}) |
|
87
|
|
|
88
|
it("counts distinct values when results are deduplicated", async () => { |
|
89
|
const result = await service.search({ |
|
90
|
entity: productIndex.name, |
|
91
|
search_options: { distinct: "status" }, |
|
92
|
}) |
|
93
|
|
|
94
|
expect(result.hits).toHaveLength(1) |
|
95
|
expect(result.metadata.count).toBe(1) |
|
96
|
}) |
|
97
|
|
|
98
|
it("matches text case-insensitively", async () => { |
|
99
|
const result = await service.search({ |
|
100
|
entity: productIndex.name, |
|
101
|
filters: { q: "RED RUNNING" }, |
|
102
|
}) |
|
103
|
|
|
104
|
expect(result.hits.map((hit) => hit.id)).toContain("prod_medusa_1") |
|
105
|
}) |
|
106
|
|
|
107
|
it("tolerates typos through the Fuzzy fallback when typo_tolerance is on", async () => { |
|
108
|
const withTypo = await service.search({ |
|
109
|
entity: productIndex.name, |
|
110
|
filters: { q: "runing shurt" }, |
|
111
|
search_options: { typo_tolerance: true }, |
|
112
|
}) |
|
113
|
expect(withTypo.hits.map((hit) => hit.id)).toContain( |
|
114
|
"prod_medusa_2" |
|
115
|
) |
|
116
|
|
|
117
|
const withoutTypo = await service.search({ |
|
118
|
entity: productIndex.name, |
|
119
|
filters: { q: "runing shurt" }, |
|
120
|
}) |
|
121
|
expect(withoutTypo.hits).toHaveLength(0) |
|
122
|
}) |
|
123
|
|
|
124
|
it("returns highlighted fragments around the matched terms", async () => { |
|
125
|
const result = await service.search({ |
|
126
|
entity: productIndex.name, |
|
127
|
filters: { q: "running" }, |
|
128
|
search_options: { highlight: { fields: ["title"] } }, |
|
129
|
}) |
|
130
|
|
|
131
|
const hit = result.hits.find((h) => h.id === "prod_medusa_1") |
|
132
|
expect(hit?.highlights?.title?.[0]).toContain("<mark>") |
|
133
|
expect(hit?.highlights?.title?.[0].toLowerCase()).toContain( |
|
134
|
"<mark>running</mark>" |
|
135
|
) |
|
136
|
}) |
|
137
|
|
|
138
|
it("highlights searchable fields when highlight is true", async () => { |
|
139
|
const result = await service.search({ |
|
140
|
entity: productIndex.name, |
|
141
|
filters: { q: "running" }, |
|
142
|
search_options: { highlight: true }, |
|
143
|
}) |
|
144
|
|
|
145
|
const hit = result.hits.find((h) => h.id === "prod_medusa_1") |
|
146
|
expect(hit?.highlights?.title?.[0].toLowerCase()).toContain( |
|
147
|
"<mark>running</mark>" |
|
148
|
) |
|
149
|
}) |
|
150
|
|
|
151
|
it("ignores highlighting and typo tolerance when there is no text query", async () => { |
|
152
|
const result = await service.search({ |
|
153
|
entity: productIndex.name, |
|
154
|
search_options: { highlight: true, typo_tolerance: true }, |
|
155
|
}) |
|
156
|
|
|
157
|
expect(result.hits.length).toBeGreaterThan(0) |
|
158
|
expect(result.hits.every((hit) => !hit.highlights)).toBe(true) |
|
159
|
}) |
|
160
|
}), |
|
161
|
}) |
|
162
|
} else { |
|
163
|
describe.skip("Medusa Cloud Search Provider", () => { |
|
164
|
it("requires Medusa Cloud search credentials", () => {}) |
|
165
|
}) |
|
166
|
} |