|
1
|
import { InferEntityType } from "@medusajs/framework/types" |
|
2
|
import { toMikroORMEntity } from "@medusajs/framework/utils" |
|
3
|
import { IndexData, IndexMetadata, IndexRelation, IndexSync } from "@models" |
|
4
|
import { IndexMetadataStatus } from "@utils" |
|
5
|
import { |
|
6
|
createIndexTestBed, |
|
7
|
IndexModuleTestBed, |
|
8
|
schema, |
|
9
|
} from "../__fixtures__" |
|
10
|
|
|
11
|
jest.setTimeout(300000) |
|
12
|
|
|
13
|
const productId = "prod_1" |
|
14
|
|
|
15
|
describe("sync management API", function () { |
|
16
|
describe("server mode", function () { |
|
17
|
let testBed: IndexModuleTestBed |
|
18
|
|
|
19
|
beforeAll(async () => { |
|
20
|
testBed = await createIndexTestBed({ schema, workerMode: "server" }) |
|
21
|
}) |
|
22
|
|
|
23
|
afterAll(async () => { |
|
24
|
await testBed.teardown() |
|
25
|
}) |
|
26
|
|
|
27
|
beforeEach(async () => { |
|
28
|
await testBed.truncateTables() |
|
29
|
testBed.eventBus.reset() |
|
30
|
}) |
|
31
|
|
|
32
|
describe("getInfo", function () { |
|
33
|
it("should return detailed index metadata with last synced keys", async () => { |
|
34
|
const manager = testBed.forkManager() |
|
35
|
const indexMetadataRepo = manager.getRepository( |
|
36
|
toMikroORMEntity(IndexMetadata) |
|
37
|
) |
|
38
|
const indexSyncRepo = manager.getRepository(toMikroORMEntity(IndexSync)) |
|
39
|
|
|
40
|
await indexMetadataRepo.upsertMany([ |
|
41
|
{ |
|
42
|
id: "metadata_1", |
|
43
|
entity: "product", |
|
44
|
status: IndexMetadataStatus.DONE, |
|
45
|
fields: ["id", "title"].sort().join(","), |
|
46
|
fields_hash: "hash_1", |
|
47
|
}, |
|
48
|
{ |
|
49
|
id: "metadata_2", |
|
50
|
entity: "product_variant", |
|
51
|
status: IndexMetadataStatus.PENDING, |
|
52
|
fields: ["id", "sku"].sort().join(","), |
|
53
|
fields_hash: "hash_2", |
|
54
|
}, |
|
55
|
]) |
|
56
|
|
|
57
|
await indexSyncRepo.upsertMany([ |
|
58
|
{ |
|
59
|
id: "sync_1", |
|
60
|
entity: "product", |
|
61
|
last_key: "prod_123", |
|
62
|
}, |
|
63
|
{ |
|
64
|
id: "sync_2", |
|
65
|
entity: "product_variant", |
|
66
|
last_key: null, |
|
67
|
}, |
|
68
|
]) |
|
69
|
|
|
70
|
const info = await testBed.module.getInfo() |
|
71
|
|
|
72
|
expect(info).toHaveLength(2) |
|
73
|
expect(info).toEqual( |
|
74
|
expect.arrayContaining([ |
|
75
|
expect.objectContaining({ |
|
76
|
id: "metadata_1", |
|
77
|
entity: "product", |
|
78
|
status: IndexMetadataStatus.DONE, |
|
79
|
fields: ["id", "title"], |
|
80
|
last_synced_key: "prod_123", |
|
81
|
}), |
|
82
|
expect.objectContaining({ |
|
83
|
id: "metadata_2", |
|
84
|
entity: "product_variant", |
|
85
|
status: IndexMetadataStatus.PENDING, |
|
86
|
fields: ["id", "sku"], |
|
87
|
last_synced_key: null, |
|
88
|
}), |
|
89
|
]) |
|
90
|
) |
|
91
|
}) |
|
92
|
|
|
93
|
it("should return empty array when no metadata exists", async () => { |
|
94
|
const info = await testBed.module.getInfo() |
|
95
|
expect(info).toBeDefined() |
|
96
|
expect(info).toHaveLength(0) |
|
97
|
}) |
|
98
|
|
|
99
|
it("should handle entities without sync records", async () => { |
|
100
|
const indexMetadataRepo = testBed |
|
101
|
.forkManager() |
|
102
|
.getRepository(toMikroORMEntity(IndexMetadata)) |
|
103
|
|
|
104
|
await indexMetadataRepo.upsertMany([ |
|
105
|
{ |
|
106
|
id: "metadata_test_1", |
|
107
|
entity: "test_product", |
|
108
|
status: IndexMetadataStatus.DONE, |
|
109
|
fields: "id", |
|
110
|
fields_hash: "hash_1", |
|
111
|
}, |
|
112
|
]) |
|
113
|
|
|
114
|
const info = await testBed.module.getInfo() |
|
115
|
|
|
116
|
expect(info).toBeDefined() |
|
117
|
expect(info).toHaveLength(1) |
|
118
|
expect(info[0]).toMatchObject({ |
|
119
|
entity: "test_product", |
|
120
|
last_synced_key: null, |
|
121
|
}) |
|
122
|
}) |
|
123
|
}) |
|
124
|
|
|
125
|
describe("sync with continue strategy", function () { |
|
126
|
it("should emit continue-sync event in server mode", async () => { |
|
127
|
await testBed.module.sync({}) |
|
128
|
|
|
129
|
expect(testBed.eventBus.emitted).toEqual([ |
|
130
|
expect.objectContaining({ |
|
131
|
name: "index.continue-sync", |
|
132
|
data: {}, |
|
133
|
options: { internal: true }, |
|
134
|
}), |
|
135
|
]) |
|
136
|
}) |
|
137
|
}) |
|
138
|
|
|
139
|
describe("sync with full strategy", function () { |
|
140
|
it("should reset metadata statuses and last_key, then emit event", async () => { |
|
141
|
const manager = testBed.forkManager() |
|
142
|
const indexMetadataRepo = manager.getRepository( |
|
143
|
toMikroORMEntity(IndexMetadata) |
|
144
|
) |
|
145
|
const indexSyncRepo = manager.getRepository(toMikroORMEntity(IndexSync)) |
|
146
|
|
|
147
|
await indexMetadataRepo.upsertMany([ |
|
148
|
{ |
|
149
|
id: "test_metadata_1", |
|
150
|
entity: "test_product_full", |
|
151
|
status: IndexMetadataStatus.DONE, |
|
152
|
fields: "id", |
|
153
|
fields_hash: "hash_1", |
|
154
|
}, |
|
155
|
{ |
|
156
|
id: "test_metadata_2", |
|
157
|
entity: "test_variant_full", |
|
158
|
status: IndexMetadataStatus.ERROR, |
|
159
|
fields: "id", |
|
160
|
fields_hash: "hash_2", |
|
161
|
}, |
|
162
|
{ |
|
163
|
id: "test_metadata_3", |
|
164
|
entity: "test_price_full", |
|
165
|
status: IndexMetadataStatus.PROCESSING, |
|
166
|
fields: "id", |
|
167
|
fields_hash: "hash_3", |
|
168
|
}, |
|
169
|
]) |
|
170
|
|
|
171
|
await indexSyncRepo.upsertMany([ |
|
172
|
{ |
|
173
|
id: "test_sync_1", |
|
174
|
entity: "test_product_full", |
|
175
|
last_key: "prod_123", |
|
176
|
}, |
|
177
|
{ |
|
178
|
id: "test_sync_2", |
|
179
|
entity: "test_variant_full", |
|
180
|
last_key: "var_456", |
|
181
|
}, |
|
182
|
]) |
|
183
|
|
|
184
|
await testBed.module.sync({ strategy: "full" }) |
|
185
|
|
|
186
|
const testMetadata: InferEntityType<typeof IndexMetadata>[] = |
|
187
|
await testBed.forkManager().find(toMikroORMEntity(IndexMetadata), {}) |
|
188
|
|
|
189
|
expect(testMetadata).toHaveLength(3) |
|
190
|
testMetadata.forEach((metadata) => { |
|
191
|
expect(metadata.status).toBe(IndexMetadataStatus.PENDING) |
|
192
|
}) |
|
193
|
|
|
194
|
const testSync: InferEntityType<typeof IndexSync>[] = await testBed |
|
195
|
.forkManager() |
|
196
|
.find(toMikroORMEntity(IndexSync), {}) |
|
197
|
|
|
198
|
testSync.forEach((sync) => { |
|
199
|
expect(sync.last_key).toBeNull() |
|
200
|
}) |
|
201
|
|
|
202
|
expect(testBed.eventBus.emitted).toEqual([ |
|
203
|
expect.objectContaining({ |
|
204
|
name: "index.full-sync", |
|
205
|
data: {}, |
|
206
|
options: { internal: true }, |
|
207
|
}), |
|
208
|
]) |
|
209
|
}) |
|
210
|
|
|
211
|
it("should not reset PENDING status metadata", async () => { |
|
212
|
const indexMetadataRepo = testBed |
|
213
|
.forkManager() |
|
214
|
.getRepository(toMikroORMEntity(IndexMetadata)) |
|
215
|
|
|
216
|
await indexMetadataRepo.upsertMany([ |
|
217
|
{ |
|
218
|
id: "test_pending_metadata", |
|
219
|
entity: "test_product_pending", |
|
220
|
status: IndexMetadataStatus.PENDING, |
|
221
|
fields: "id", |
|
222
|
fields_hash: "hash_1", |
|
223
|
}, |
|
224
|
]) |
|
225
|
|
|
226
|
await testBed.module.sync({ strategy: "full" }) |
|
227
|
|
|
228
|
const updatedMetadata: InferEntityType<typeof IndexMetadata>[] = |
|
229
|
await testBed.forkManager().find(toMikroORMEntity(IndexMetadata), {}) |
|
230
|
|
|
231
|
expect(updatedMetadata[0].status).toBe(IndexMetadataStatus.PENDING) |
|
232
|
}) |
|
233
|
}) |
|
234
|
|
|
235
|
describe("sync with reset strategy", function () { |
|
236
|
it("should truncate all index tables and emit event", async () => { |
|
237
|
const manager = testBed.forkManager() |
|
238
|
const indexDataRepo = manager.getRepository(toMikroORMEntity(IndexData)) |
|
239
|
const indexMetadataRepo = manager.getRepository( |
|
240
|
toMikroORMEntity(IndexMetadata) |
|
241
|
) |
|
242
|
const indexSyncRepo = manager.getRepository(toMikroORMEntity(IndexSync)) |
|
243
|
|
|
244
|
await indexDataRepo.upsertMany([ |
|
245
|
{ |
|
246
|
id: productId, |
|
247
|
name: "Product", |
|
248
|
data: { id: productId }, |
|
249
|
}, |
|
250
|
]) |
|
251
|
|
|
252
|
await indexMetadataRepo.upsertMany([ |
|
253
|
{ |
|
254
|
id: "metadata_1", |
|
255
|
entity: "product", |
|
256
|
status: IndexMetadataStatus.DONE, |
|
257
|
fields: "id", |
|
258
|
fields_hash: "hash_1", |
|
259
|
}, |
|
260
|
]) |
|
261
|
|
|
262
|
await indexSyncRepo.upsertMany([ |
|
263
|
{ |
|
264
|
id: "sync_1", |
|
265
|
entity: "product", |
|
266
|
last_key: "prod_123", |
|
267
|
}, |
|
268
|
]) |
|
269
|
|
|
270
|
await testBed.module.sync({ strategy: "reset" }) |
|
271
|
|
|
272
|
const freshManager = testBed.forkManager() |
|
273
|
const indexData = await freshManager.find( |
|
274
|
toMikroORMEntity(IndexData), |
|
275
|
{} |
|
276
|
) |
|
277
|
const indexRelations = await freshManager.find( |
|
278
|
toMikroORMEntity(IndexRelation), |
|
279
|
{} |
|
280
|
) |
|
281
|
const indexMetadata = await freshManager.find( |
|
282
|
toMikroORMEntity(IndexMetadata), |
|
283
|
{} |
|
284
|
) |
|
285
|
const indexSync = await freshManager.find( |
|
286
|
toMikroORMEntity(IndexSync), |
|
287
|
{} |
|
288
|
) |
|
289
|
|
|
290
|
expect(indexData).toHaveLength(0) |
|
291
|
expect(indexRelations).toHaveLength(0) |
|
292
|
expect(indexMetadata).toHaveLength(0) |
|
293
|
expect(indexSync).toHaveLength(0) |
|
294
|
|
|
295
|
expect(testBed.eventBus.emitted).toEqual([ |
|
296
|
expect.objectContaining({ |
|
297
|
name: "index.reset-sync", |
|
298
|
data: {}, |
|
299
|
options: { internal: true }, |
|
300
|
}), |
|
301
|
]) |
|
302
|
}) |
|
303
|
|
|
304
|
it("should handle empty tables gracefully", async () => { |
|
305
|
await expect( |
|
306
|
testBed.module.sync({ strategy: "reset" }) |
|
307
|
).resolves.not.toThrow() |
|
308
|
|
|
309
|
const indexData = await testBed |
|
310
|
.forkManager() |
|
311
|
.find(toMikroORMEntity(IndexData), {}) |
|
312
|
expect(indexData).toHaveLength(0) |
|
313
|
}) |
|
314
|
}) |
|
315
|
|
|
316
|
describe("sync strategy parameter validation", function () { |
|
317
|
it("should default to continue sync when no strategy provided", async () => { |
|
318
|
await testBed.module.sync({}) |
|
319
|
|
|
320
|
expect(testBed.eventBus.emitted).toEqual([ |
|
321
|
expect.objectContaining({ |
|
322
|
name: "index.continue-sync", |
|
323
|
}), |
|
324
|
]) |
|
325
|
}) |
|
326
|
|
|
327
|
it("should handle undefined strategy", async () => { |
|
328
|
await testBed.module.sync({ strategy: undefined }) |
|
329
|
|
|
330
|
expect(testBed.eventBus.emitted).toEqual([ |
|
331
|
expect.objectContaining({ |
|
332
|
name: "index.continue-sync", |
|
333
|
}), |
|
334
|
]) |
|
335
|
}) |
|
336
|
|
|
337
|
it("should reject an invalid strategy", async () => { |
|
338
|
await expect( |
|
339
|
testBed.module.sync({ strategy: "invalid" as any }) |
|
340
|
).rejects.toThrow(`Invalid sync strategy: invalid`) |
|
341
|
|
|
342
|
expect(testBed.eventBus.emitted).toHaveLength(0) |
|
343
|
}) |
|
344
|
}) |
|
345
|
}) |
|
346
|
}) |