|
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
|
|
|
10
|
jest.setTimeout(300000) |
|
11
|
|
|
12
|
process.env.ENABLE_INDEX_MODULE = "true" |
|
13
|
|
|
14
|
async function populateData( |
|
15
|
api: any, |
|
16
|
{ productCount = 1, variantCount = 1, priceCount = 1 } = {} |
|
17
|
) { |
|
18
|
const shippingProfile = ( |
|
19
|
await api.post( |
|
20
|
`/admin/shipping-profiles`, |
|
21
|
{ name: "Test", type: "default" }, |
|
22
|
adminHeaders |
|
23
|
) |
|
24
|
).data.shipping_profile |
|
25
|
|
|
26
|
for (let i = 0; i < productCount; i++) { |
|
27
|
const payload = { |
|
28
|
title: "Test Giftcard " + i, |
|
29
|
shipping_profile_id: shippingProfile.id, |
|
30
|
description: "test-giftcard-description " + i, |
|
31
|
options: [{ title: "Denominations", values: ["100"] }], |
|
32
|
variants: new Array(variantCount).fill(0).map((_, j) => ({ |
|
33
|
title: `Test variant ${i} ${j}`, |
|
34
|
sku: `test-variant-${i}-${j}`, |
|
35
|
prices: new Array(priceCount).fill(0).map((_, k) => ({ |
|
36
|
currency_code: Object.values(defaultCurrencies)[k].code, |
|
37
|
amount: 10 * k, |
|
38
|
})), |
|
39
|
options: { |
|
40
|
Denominations: "100", |
|
41
|
}, |
|
42
|
})), |
|
43
|
} |
|
44
|
|
|
45
|
await api.post("/admin/products", payload, adminHeaders) |
|
46
|
} |
|
47
|
} |
|
48
|
|
|
49
|
medusaIntegrationTestRunner({ |
|
50
|
testSuite: ({ |
|
51
|
getContainer, |
|
52
|
dbConnection, |
|
53
|
api, |
|
54
|
dbConfig, |
|
55
|
dbUtils, |
|
56
|
utils, |
|
57
|
}) => { |
|
58
|
let indexEngine: IndexTypes.IIndexService |
|
59
|
let appContainer |
|
60
|
|
|
61
|
beforeAll(() => { |
|
62
|
appContainer = getContainer() |
|
63
|
indexEngine = appContainer.resolve(Modules.INDEX) |
|
64
|
}) |
|
65
|
|
|
66
|
afterAll(() => { |
|
67
|
process.env.ENABLE_INDEX_MODULE = "false" |
|
68
|
}) |
|
69
|
|
|
70
|
beforeAll(async () => { |
|
71
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
72
|
|
|
73
|
await dbUtils.snapshot() |
|
74
|
}) |
|
75
|
|
|
76
|
describe("Index engine syncing", () => { |
|
77
|
it("should sync the data to the index based on the indexation configuration", async () => { |
|
78
|
await populateData(api, { |
|
79
|
productCount: 2, |
|
80
|
variantCount: 2, |
|
81
|
priceCount: 2, |
|
82
|
}) |
|
83
|
|
|
84
|
await setTimeout(1000) |
|
85
|
await dbConnection.raw('TRUNCATE TABLE "index_data";') |
|
86
|
await dbConnection.raw('TRUNCATE TABLE "index_relation";') |
|
87
|
await dbConnection.raw('TRUNCATE TABLE "index_metadata";') |
|
88
|
await dbConnection.raw('TRUNCATE TABLE "index_sync";') |
|
89
|
|
|
90
|
const { data: indexedDataAfterCreation } = |
|
91
|
await indexEngine.query<"product">({ |
|
92
|
fields: [ |
|
93
|
"product.*", |
|
94
|
"product.variants.*", |
|
95
|
"product.variants.prices.*", |
|
96
|
], |
|
97
|
}) |
|
98
|
|
|
99
|
expect(indexedDataAfterCreation.length).toBe(0) |
|
100
|
|
|
101
|
// Prevent storage provider to be triggered though |
|
102
|
;(indexEngine as any).storageProvider_.onApplicationStart = jest.fn() |
|
103
|
|
|
104
|
// Trigger a sync |
|
105
|
;(indexEngine as any).schemaObjectRepresentation_ = null |
|
106
|
await (indexEngine as any).onApplicationStart_() |
|
107
|
|
|
108
|
// 28 ms - 6511 records |
|
109
|
const { data: results } = await indexEngine.query<"product">({ |
|
110
|
fields: [ |
|
111
|
"product.*", |
|
112
|
"product.variants.*", |
|
113
|
"product.variants.prices.*", |
|
114
|
], |
|
115
|
}) |
|
116
|
|
|
117
|
expect(results.length).toBe(2) |
|
118
|
for (const result of results) { |
|
119
|
expect(result.variants.length).toBe(2) |
|
120
|
for (const variant of result.variants) { |
|
121
|
expect(variant.prices.length).toBe(2) |
|
122
|
} |
|
123
|
} |
|
124
|
}) |
|
125
|
}) |
|
126
|
|
|
127
|
it("should sync the data to the index based on the updated indexation configuration", async () => { |
|
128
|
await populateData(api) |
|
129
|
|
|
130
|
await setTimeout(1000) |
|
131
|
await dbConnection.raw('TRUNCATE TABLE "index_data";') |
|
132
|
await dbConnection.raw('TRUNCATE TABLE "index_relation";') |
|
133
|
await dbConnection.raw('TRUNCATE TABLE "index_metadata";') |
|
134
|
await dbConnection.raw('TRUNCATE TABLE "index_sync";') |
|
135
|
|
|
136
|
const { data: indexedDataAfterCreation } = |
|
137
|
await indexEngine.query<"product">({ |
|
138
|
fields: [ |
|
139
|
"product.*", |
|
140
|
"product.variants.*", |
|
141
|
"product.variants.prices.*", |
|
142
|
], |
|
143
|
}) |
|
144
|
|
|
145
|
expect(indexedDataAfterCreation.length).toBe(0) |
|
146
|
|
|
147
|
// Prevent storage provider to be triggered though |
|
148
|
;(indexEngine as any).storageProvider_.onApplicationStart = jest.fn() |
|
149
|
|
|
150
|
// Trigger a sync |
|
151
|
;(indexEngine as any).schemaObjectRepresentation_ = null |
|
152
|
await (indexEngine as any).onApplicationStart_() |
|
153
|
|
|
154
|
const { data: results } = await indexEngine.query<"product">({ |
|
155
|
fields: [ |
|
156
|
"product.*", |
|
157
|
"product.variants.*", |
|
158
|
"product.variants.prices.*", |
|
159
|
], |
|
160
|
}) |
|
161
|
|
|
162
|
expect(results.length).toBe(1) |
|
163
|
expect(results[0].variants.length).toBe(1) |
|
164
|
expect(results[0].variants[0].prices.length).toBe(1) |
|
165
|
|
|
166
|
// Manually change the indexation configuration |
|
167
|
;(indexEngine as any).schemaObjectRepresentation_ = null |
|
168
|
;(indexEngine as any).moduleOptions_ = { |
|
169
|
...(indexEngine as any).moduleOptions_, |
|
170
|
schema: ` |
|
171
|
type Product @Listeners(values: ["product.created", "product.updated", "product.deleted"]) { |
|
172
|
id: ID |
|
173
|
title: String |
|
174
|
handle: String |
|
175
|
variants: [ProductVariant] |
|
176
|
} |
|
177
|
|
|
178
|
type ProductVariant @Listeners(values: ["variant.created", "variant.updated", "variant.deleted"]) { |
|
179
|
id: ID |
|
180
|
product_id: String |
|
181
|
sku: String |
|
182
|
description: String |
|
183
|
} |
|
184
|
`, |
|
185
|
} |
|
186
|
// Trigger a sync |
|
187
|
;(indexEngine as any).schemaObjectRepresentation_ = null |
|
188
|
await (indexEngine as any).onApplicationStart_() |
|
189
|
await setTimeout(3000) |
|
190
|
|
|
191
|
const { data: updatedResults } = await indexEngine.query<"product">({ |
|
192
|
fields: ["product.*", "product.variants.*"], |
|
193
|
}) |
|
194
|
|
|
195
|
expect(updatedResults.length).toBe(1) |
|
196
|
expect(updatedResults[0].variants.length).toBe(1) |
|
197
|
|
|
198
|
let staledRaws = await dbConnection.raw( |
|
199
|
'SELECT * FROM "index_data" WHERE "staled_at" IS NOT NULL' |
|
200
|
) |
|
201
|
|
|
202
|
expect(staledRaws.rows.length).toBe(0) |
|
203
|
|
|
204
|
staledRaws = await dbConnection.raw( |
|
205
|
'SELECT * FROM "index_relation" WHERE "staled_at" IS NOT NULL' |
|
206
|
) |
|
207
|
expect(staledRaws.rows.length).toBe(0) |
|
208
|
}) |
|
209
|
}, |
|
210
|
}) |