1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { IndexTypes } from "@medusajs/types"
3 import { defaultCurrencies, Modules } from "@medusajs/utils"
4 import { setTimeout } from "timers/promises"
5 import {
6 adminHeaders,
7 createAdminUser,
8 } from "../../../helpers/create-admin-user"
9 import { fetchAndRetry } from "../../../helpers/retry"
10
11 jest.setTimeout(100000)
12
13 process.env.ENABLE_INDEX_MODULE = "true"
14
15 async function populateData(
16 api: any,
17 {
18 productCount = 50,
19 variantCount = 10,
20 priceCount = 10,
21 }: {
22 productCount?: number
23 variantCount?: number
24 priceCount?: number
25 } = {}
26 ) {
27 const shippingProfile = (
28 await api.post(
29 `/admin/shipping-profiles`,
30 { name: "Test", type: "default" },
31 adminHeaders
32 )
33 ).data.shipping_profile
34
35 const payloads = new Array(productCount).fill(0).map((_, a) => ({
36 title: "Test Giftcard-" + a,
37 is_giftcard: true,
38 shipping_profile_id: shippingProfile.id,
39 description: "test-giftcard-description" + a,
40 options: [{ title: "Denominations", values: ["100"] }],
41 variants: new Array(variantCount).fill(0).map((_, i) => ({
42 title: `Test variant ${i}`,
43 sku: `test-variant-${i}${a}`,
44 prices: new Array(priceCount).fill(0).map((_, j) => ({
45 currency_code: Object.values(defaultCurrencies)[j].code,
46 amount: 10 * j,
47 })),
48 options: {
49 Denominations: "100",
50 },
51 })),
52 }))
53
54 for (const payload of payloads) {
55 await api.post("/admin/products", payload, adminHeaders)
56 }
57
58 await setTimeout(4000 * (productCount / 10))
59 }
60
61 medusaIntegrationTestRunner({
62 testSuite: ({ getContainer, dbConnection, api, dbUtils, utils }) => {
63 let indexEngine: IndexTypes.IIndexService
64 let appContainer
65
66 beforeAll(async () => {
67 appContainer = getContainer()
68 indexEngine = appContainer.resolve(Modules.INDEX)
69 })
70
71 afterAll(() => {
72 process.env.ENABLE_INDEX_MODULE = "false"
73 })
74
75 beforeAll(async () => {
76 await createAdminUser(dbConnection, adminHeaders, appContainer)
77
78 await dbUtils.snapshot()
79 })
80
81 describe("Index engine", () => {
82 it("should search through the indexed data and return the correct results ordered and filtered [1]", async () => {
83 await populateData(api, {
84 productCount: 1,
85 variantCount: 10,
86 priceCount: 10,
87 })
88
89 const { data: results } = await fetchAndRetry(
90 async () =>
91 indexEngine.query<"product">({
92 fields: [
93 "product.*",
94 "product.variants.*",
95 "product.variants.prices.*",
96 ],
97 filters: {
98 product: {
99 variants: {
100 prices: {
101 amount: { $gt: 50 },
102 },
103 },
104 },
105 },
106 pagination: {
107 order: {
108 product: {
109 variants: {
110 prices: {
111 amount: "DESC",
112 },
113 },
114 },
115 },
116 },
117 }),
118 ({ data }) => data.length > 0
119 )
120
121 expect(results.length).toBe(1)
122
123 const variants = results[0].variants
124
125 expect(variants.length).toBe(10)
126
127 for (const variant of variants) {
128 expect(variant.prices.length).toBe(4)
129 for (const price of variant.prices) {
130 expect(price.amount).toBeGreaterThan(50)
131 }
132 }
133 })
134
135 it("should search through the indexed data and return the correct results ordered and filtered [2]", async () => {
136 await populateData(api, {
137 productCount: 1,
138 variantCount: 10,
139 priceCount: 10,
140 })
141
142 const { data: results } = await fetchAndRetry(
143 async () =>
144 indexEngine.query<"product">({
145 fields: [
146 "product.*",
147 "product.variants.*",
148 "product.variants.prices.*",
149 ],
150 filters: {
151 product: {
152 variants: {
153 prices: {
154 amount: { $gt: 50 },
155 currency_code: { $eq: "aud" },
156 },
157 },
158 },
159 },
160 pagination: {
161 order: {
162 product: {
163 variants: {
164 prices: {
165 amount: "DESC",
166 },
167 },
168 },
169 },
170 },
171 }),
172 ({ data }) => data.length > 0
173 )
174
175 expect(results.length).toBe(1)
176
177 const variants = results[0].variants
178
179 expect(variants.length).toBe(10)
180
181 for (const variant of variants) {
182 expect(variant.prices.length).toBe(1)
183 expect(variant.prices[0].amount).toBeGreaterThan(50)
184 expect(variant.prices[0].currency_code).toBe("aud")
185 }
186 })
187
188 it("should search through the indexed data and return the correct results ordered and filtered [3]", async () => {
189 await populateData(api)
190
191 const queryArgs = {
192 fields: [
193 "product.*",
194 "product.variants.*",
195 "product.variants.prices.*",
196 ],
197 filters: {
198 product: {
199 variants: {
200 prices: {
201 amount: { $gt: 50 },
202 currency_code: { $eq: "AUD" },
203 },
204 },
205 },
206 },
207 pagination: {
208 order: {
209 product: {
210 variants: {
211 prices: {
212 amount: "DESC",
213 },
214 },
215 },
216 },
217 },
218 }
219
220 await indexEngine.query<"product">(queryArgs)
221
222 const { data: results, metadata } = await indexEngine.query<"product">(
223 queryArgs
224 )
225 })
226 })
227 },
228 })