|
1
|
import { Modules } from "@medusajs/framework/utils" |
|
2
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { ICachingModuleService } from "@medusajs/framework/types" |
|
4
|
import { MedusaModule } from "@medusajs/framework/modules-sdk" |
|
5
|
import { EventBusServiceMock } from "../__fixtures__/event-bus-mock" |
|
6
|
|
|
7
|
jest.setTimeout(30000) |
|
8
|
|
|
9
|
jest.spyOn(MedusaModule, "getAllJoinerConfigs").mockReturnValue([ |
|
10
|
{ |
|
11
|
schema: ` |
|
12
|
type Product { |
|
13
|
id: ID |
|
14
|
title: String |
|
15
|
handle: String |
|
16
|
status: String |
|
17
|
type_id: String |
|
18
|
collection_id: String |
|
19
|
is_giftcard: Boolean |
|
20
|
external_id: String |
|
21
|
created_at: DateTime |
|
22
|
updated_at: DateTime |
|
23
|
|
|
24
|
variants: [ProductVariant] |
|
25
|
sales_channels: [SalesChannel] |
|
26
|
} |
|
27
|
|
|
28
|
type ProductVariant { |
|
29
|
id: ID |
|
30
|
product_id: String |
|
31
|
sku: String |
|
32
|
|
|
33
|
prices: [Price] |
|
34
|
} |
|
35
|
|
|
36
|
type Price { |
|
37
|
id: ID |
|
38
|
amount: Float |
|
39
|
currency_code: String |
|
40
|
variant_id: String |
|
41
|
} |
|
42
|
|
|
43
|
type SalesChannel { |
|
44
|
id: ID |
|
45
|
is_disabled: Boolean |
|
46
|
} |
|
47
|
|
|
48
|
type ProductCollection { |
|
49
|
id: ID |
|
50
|
title: String |
|
51
|
handle: String |
|
52
|
} |
|
53
|
`, |
|
54
|
}, |
|
55
|
]) |
|
56
|
|
|
57
|
const mockEventBus = new EventBusServiceMock() |
|
58
|
|
|
59
|
moduleIntegrationTestRunner<ICachingModuleService>({ |
|
60
|
moduleName: Modules.CACHING, |
|
61
|
injectedDependencies: { |
|
62
|
[Modules.EVENT_BUS]: mockEventBus, |
|
63
|
}, |
|
64
|
moduleOptions: { |
|
65
|
in_memory: { |
|
66
|
enable: true, |
|
67
|
}, |
|
68
|
}, |
|
69
|
testSuite: ({ service }) => { |
|
70
|
describe("Cache Invalidation with Entity Relationships", () => { |
|
71
|
afterEach(async () => { |
|
72
|
await service.clear({ tags: ["*"] }).catch(() => {}) |
|
73
|
}) |
|
74
|
|
|
75
|
describe("Single Entity Caching", () => { |
|
76
|
it("should cache and retrieve a single product entity using computed keys", async () => { |
|
77
|
const product = { |
|
78
|
id: "prod_1", |
|
79
|
title: "Test Product", |
|
80
|
handle: "test-product", |
|
81
|
status: "published", |
|
82
|
created_at: new Date().toISOString(), |
|
83
|
updated_at: new Date().toISOString(), |
|
84
|
} |
|
85
|
|
|
86
|
const productKey = await service.computeKey(product) |
|
87
|
|
|
88
|
await service.set({ |
|
89
|
key: productKey, |
|
90
|
data: product, |
|
91
|
}) |
|
92
|
|
|
93
|
const cachedProduct = await service.get({ key: productKey }) |
|
94
|
expect(cachedProduct).toEqual(product) |
|
95
|
}) |
|
96
|
|
|
97
|
it("should auto-invalidate single entity when strategy clears computed tags", async () => { |
|
98
|
const product = { |
|
99
|
id: "prod_1", |
|
100
|
title: "Test Product", |
|
101
|
handle: "test-product", |
|
102
|
} |
|
103
|
|
|
104
|
const productKey = await service.computeKey(product) |
|
105
|
|
|
106
|
await service.set({ |
|
107
|
key: productKey, |
|
108
|
data: product, |
|
109
|
}) |
|
110
|
|
|
111
|
await mockEventBus.emit( |
|
112
|
[{ name: "product.updated", data: { id: product.id } }], |
|
113
|
{} |
|
114
|
) |
|
115
|
|
|
116
|
const result = await service.get({ key: productKey }) |
|
117
|
expect(result).toBeNull() |
|
118
|
}) |
|
119
|
|
|
120
|
it("should not auto-invalidate single entity with autoInvalidate=false", async () => { |
|
121
|
const product = { |
|
122
|
id: "prod_1", |
|
123
|
title: "Test Product", |
|
124
|
handle: "test-product", |
|
125
|
} |
|
126
|
|
|
127
|
const productKey = await service.computeKey(product) |
|
128
|
|
|
129
|
await service.set({ |
|
130
|
key: productKey, |
|
131
|
data: product, |
|
132
|
options: { autoInvalidate: false }, |
|
133
|
}) |
|
134
|
|
|
135
|
await mockEventBus.emit( |
|
136
|
[{ name: "product.updated", data: { id: product.id } }], |
|
137
|
{} |
|
138
|
) |
|
139
|
|
|
140
|
const result = await service.get({ key: productKey }) |
|
141
|
expect(result).toEqual(product) |
|
142
|
}) |
|
143
|
}) |
|
144
|
|
|
145
|
describe("Explicit vs computed tags", () => { |
|
146
|
const product = { id: "prod_1", title: "Test Product" } |
|
147
|
// A link row's own event. Nothing in `product` can produce this tag, so it |
|
148
|
// only ever comes from the explicit list. |
|
149
|
const linkTag = "LinkProductVariantInventoryItem:list:*" |
|
150
|
const emitLinkAttached = () => |
|
151
|
mockEventBus.emit( |
|
152
|
[ |
|
153
|
{ |
|
154
|
name: "link_product_variant_inventory_item.attached", |
|
155
|
data: { id: "pvitem_1" }, |
|
156
|
}, |
|
157
|
], |
|
158
|
{} |
|
159
|
) |
|
160
|
|
|
161
|
it("should use the given tags on their own by default", async () => { |
|
162
|
const key = await service.computeKey({ case: "tags-only" }) |
|
163
|
await service.set({ key, data: product, tags: [linkTag] }) |
|
164
|
|
|
165
|
await mockEventBus.emit( |
|
166
|
[{ name: "product.updated", data: { id: product.id } }], |
|
167
|
{} |
|
168
|
) |
|
169
|
expect(await service.get({ key })).toEqual(product) |
|
170
|
|
|
171
|
await emitLinkAttached() |
|
172
|
expect(await service.get({ key })).toBeNull() |
|
173
|
}) |
|
174
|
|
|
175
|
it("should apply the computed tags alongside the given ones when enabled", async () => { |
|
176
|
const key = await service.computeKey({ case: "both-computed" }) |
|
177
|
await service.set({ |
|
178
|
key, |
|
179
|
data: product, |
|
180
|
tags: [linkTag], |
|
181
|
computeAutomaticTags: true, |
|
182
|
}) |
|
183
|
|
|
184
|
// Computed from the data, which the explicit list never mentions. |
|
185
|
await mockEventBus.emit( |
|
186
|
[{ name: "product.updated", data: { id: product.id } }], |
|
187
|
{} |
|
188
|
) |
|
189
|
expect(await service.get({ key })).toBeNull() |
|
190
|
}) |
|
191
|
|
|
192
|
it("should keep the given tags when the computed ones are applied too", async () => { |
|
193
|
const key = await service.computeKey({ case: "both-explicit" }) |
|
194
|
await service.set({ |
|
195
|
key, |
|
196
|
data: product, |
|
197
|
tags: [linkTag], |
|
198
|
computeAutomaticTags: true, |
|
199
|
}) |
|
200
|
|
|
201
|
await emitLinkAttached() |
|
202
|
expect(await service.get({ key })).toBeNull() |
|
203
|
}) |
|
204
|
|
|
205
|
it("should compute tags when no explicit tags are provided", async () => { |
|
206
|
const key = await service.computeKey({ case: "no-tags" }) |
|
207
|
await service.set({ key, data: product }) |
|
208
|
|
|
209
|
await mockEventBus.emit( |
|
210
|
[{ name: "product.updated", data: { id: product.id } }], |
|
211
|
{} |
|
212
|
) |
|
213
|
expect(await service.get({ key })).toBeNull() |
|
214
|
}) |
|
215
|
}) |
|
216
|
|
|
217
|
describe("Entity List Caching", () => { |
|
218
|
it("should cache and retrieve lists of entities using computed keys", async () => { |
|
219
|
const publishedProductsQuery = { |
|
220
|
entity: "product", |
|
221
|
filters: { status: "published" }, |
|
222
|
fields: ["id", "title", "status"], |
|
223
|
} |
|
224
|
|
|
225
|
const allProductsQuery = { |
|
226
|
entity: "product", |
|
227
|
filters: {}, |
|
228
|
fields: ["id", "title", "status"], |
|
229
|
} |
|
230
|
|
|
231
|
const publishedProducts = [ |
|
232
|
{ id: "prod_1", title: "Product 1", status: "published" }, |
|
233
|
{ id: "prod_2", title: "Product 2", status: "published" }, |
|
234
|
] |
|
235
|
|
|
236
|
const allProducts = [ |
|
237
|
...publishedProducts, |
|
238
|
{ id: "prod_3", title: "Product 3", status: "draft" }, |
|
239
|
] |
|
240
|
|
|
241
|
const publishedProductsKey = await service.computeKey( |
|
242
|
publishedProductsQuery |
|
243
|
) |
|
244
|
const allProductsKey = await service.computeKey(allProductsQuery) |
|
245
|
|
|
246
|
await service.set({ |
|
247
|
key: publishedProductsKey, |
|
248
|
data: publishedProducts, |
|
249
|
}) |
|
250
|
|
|
251
|
await service.set({ |
|
252
|
key: allProductsKey, |
|
253
|
data: allProducts, |
|
254
|
}) |
|
255
|
|
|
256
|
const cachedPublished = await service.get({ |
|
257
|
key: publishedProductsKey, |
|
258
|
}) |
|
259
|
const cachedAll = await service.get({ key: allProductsKey }) |
|
260
|
|
|
261
|
expect(cachedPublished).toEqual(publishedProducts) |
|
262
|
expect(cachedAll).toEqual(allProducts) |
|
263
|
}) |
|
264
|
|
|
265
|
it("should invalidate related lists when individual product is updated", async () => { |
|
266
|
const listQuery = { |
|
267
|
entity: "product", |
|
268
|
filters: { status: "published" }, |
|
269
|
includes: ["id", "title"], |
|
270
|
} |
|
271
|
|
|
272
|
const products = [ |
|
273
|
{ id: "prod_1", title: "Product 1", status: "published" }, |
|
274
|
{ id: "prod_2", title: "Product 2", status: "published" }, |
|
275
|
] |
|
276
|
|
|
277
|
const listKey = await service.computeKey(listQuery) |
|
278
|
|
|
279
|
await service.set({ |
|
280
|
key: listKey, |
|
281
|
data: products, |
|
282
|
}) |
|
283
|
|
|
284
|
await mockEventBus.emit( |
|
285
|
[ |
|
286
|
{ |
|
287
|
name: "product.updated", |
|
288
|
data: { id: "prod_1", title: "Updated Product 1" }, |
|
289
|
}, |
|
290
|
], |
|
291
|
{} |
|
292
|
) |
|
293
|
|
|
294
|
const cachedList = await service.get({ key: listKey }) |
|
295
|
|
|
296
|
expect(cachedList).toBeNull() |
|
297
|
}) |
|
298
|
}) |
|
299
|
|
|
300
|
describe("Nested Entity Caching", () => { |
|
301
|
it("should cache products with nested variants and prices using computed keys", async () => { |
|
302
|
const productWithVariants = { |
|
303
|
id: "prod_1", |
|
304
|
title: "Complex Product", |
|
305
|
variants: [ |
|
306
|
{ |
|
307
|
id: "var_1", |
|
308
|
product_id: "prod_1", |
|
309
|
sku: "SKU-001", |
|
310
|
prices: [ |
|
311
|
{ |
|
312
|
id: "price_1", |
|
313
|
variant_id: "var_1", |
|
314
|
amount: 1000, |
|
315
|
currency_code: "USD", |
|
316
|
}, |
|
317
|
{ |
|
318
|
id: "price_2", |
|
319
|
variant_id: "var_1", |
|
320
|
amount: 900, |
|
321
|
currency_code: "EUR", |
|
322
|
}, |
|
323
|
], |
|
324
|
}, |
|
325
|
{ |
|
326
|
id: "var_2", |
|
327
|
product_id: "prod_1", |
|
328
|
sku: "SKU-002", |
|
329
|
prices: [ |
|
330
|
{ |
|
331
|
id: "price_3", |
|
332
|
variant_id: "var_2", |
|
333
|
amount: 1200, |
|
334
|
currency_code: "USD", |
|
335
|
}, |
|
336
|
], |
|
337
|
}, |
|
338
|
], |
|
339
|
} |
|
340
|
|
|
341
|
const productKey = await service.computeKey(productWithVariants) |
|
342
|
|
|
343
|
await service.set({ |
|
344
|
key: productKey, |
|
345
|
data: productWithVariants, |
|
346
|
}) |
|
347
|
|
|
348
|
const cached = await service.get<typeof productWithVariants>({ |
|
349
|
key: productKey, |
|
350
|
}) |
|
351
|
expect(cached).toEqual(productWithVariants) |
|
352
|
expect(cached!.variants).toHaveLength(2) |
|
353
|
expect(cached!.variants[0].prices).toHaveLength(2) |
|
354
|
}) |
|
355
|
|
|
356
|
it("should invalidate nested product when related variant is updated", async () => { |
|
357
|
const productWithVariants = { |
|
358
|
id: "prod_1", |
|
359
|
title: "Complex Product", |
|
360
|
variants: [{ id: "var_1", product_id: "prod_1", sku: "SKU-001" }], |
|
361
|
} |
|
362
|
|
|
363
|
const productKey = await service.computeKey(productWithVariants) |
|
364
|
|
|
365
|
await service.set({ |
|
366
|
key: productKey, |
|
367
|
data: productWithVariants, |
|
368
|
}) |
|
369
|
|
|
370
|
await mockEventBus.emit( |
|
371
|
[ |
|
372
|
{ |
|
373
|
name: "product_variant.updated", |
|
374
|
data: { |
|
375
|
id: "var_1", |
|
376
|
product_id: "prod_1", |
|
377
|
sku: "SKU-001-UPDATED", |
|
378
|
}, |
|
379
|
}, |
|
380
|
], |
|
381
|
{} |
|
382
|
) |
|
383
|
|
|
384
|
const result = await service.get({ key: productKey }) |
|
385
|
expect(result).toBeNull() |
|
386
|
}) |
|
387
|
|
|
388
|
it("should handle price updates affecting variant and product caches", async () => { |
|
389
|
const price = { |
|
390
|
id: "price_1", |
|
391
|
variant_id: "var_1", |
|
392
|
amount: 1000, |
|
393
|
currency_code: "USD", |
|
394
|
} |
|
395
|
|
|
396
|
const variant = { |
|
397
|
id: "var_1", |
|
398
|
product_id: "prod_1", |
|
399
|
sku: "SKU-001", |
|
400
|
prices: [price], |
|
401
|
} |
|
402
|
|
|
403
|
const product = { |
|
404
|
id: "prod_1", |
|
405
|
title: "Product", |
|
406
|
variants: [variant], |
|
407
|
} |
|
408
|
|
|
409
|
const priceKey = await service.computeKey(price) |
|
410
|
const variantKey = await service.computeKey(variant) |
|
411
|
const productKey = await service.computeKey(product) |
|
412
|
|
|
413
|
await service.set({ |
|
414
|
key: priceKey, |
|
415
|
data: price, |
|
416
|
}) |
|
417
|
await service.set({ |
|
418
|
key: variantKey, |
|
419
|
data: variant, |
|
420
|
}) |
|
421
|
await service.set({ |
|
422
|
key: productKey, |
|
423
|
data: product, |
|
424
|
}) |
|
425
|
|
|
426
|
await mockEventBus.emit( |
|
427
|
[ |
|
428
|
{ |
|
429
|
name: "price.updated", |
|
430
|
data: { id: "price_1", variant_id: "var_1", amount: 1100 }, |
|
431
|
}, |
|
432
|
], |
|
433
|
{} |
|
434
|
) |
|
435
|
|
|
436
|
const cachedPrice = await service.get({ key: priceKey }) |
|
437
|
const cachedVariant = await service.get({ key: variantKey }) |
|
438
|
const cachedProduct = await service.get({ key: productKey }) |
|
439
|
|
|
440
|
expect(cachedPrice).toBeNull() |
|
441
|
expect(cachedVariant).toBeNull() |
|
442
|
expect(cachedProduct).toBeNull() |
|
443
|
}) |
|
444
|
}) |
|
445
|
|
|
446
|
describe("Complex Query Caching", () => { |
|
447
|
it("should cache complex queries and invalidate based on entity relationships", async () => { |
|
448
|
const complexQuery = { |
|
449
|
entity: "product", |
|
450
|
filters: { status: "published", collection_id: "col_1" }, |
|
451
|
includes: { |
|
452
|
variants: { |
|
453
|
include: { |
|
454
|
prices: true, |
|
455
|
}, |
|
456
|
}, |
|
457
|
collection: true, |
|
458
|
}, |
|
459
|
pagination: { limit: 10, offset: 0 }, |
|
460
|
} |
|
461
|
|
|
462
|
const queryResult = { |
|
463
|
data: [ |
|
464
|
{ |
|
465
|
id: "prod_1", |
|
466
|
title: "Product 1", |
|
467
|
collection_id: "col_1", |
|
468
|
variants: [ |
|
469
|
{ |
|
470
|
id: "var_1", |
|
471
|
product_id: "prod_1", |
|
472
|
prices: [{ id: "price_1", amount: 1000 }], |
|
473
|
}, |
|
474
|
], |
|
475
|
collection: { id: "col_1", title: "Collection 1" }, |
|
476
|
}, |
|
477
|
], |
|
478
|
pagination: { total: 1, limit: 10, offset: 0 }, |
|
479
|
} |
|
480
|
|
|
481
|
const queryKey = await service.computeKey(complexQuery) |
|
482
|
|
|
483
|
await service.set({ |
|
484
|
key: queryKey, |
|
485
|
data: queryResult, |
|
486
|
}) |
|
487
|
|
|
488
|
const cached = await service.get({ key: queryKey }) |
|
489
|
expect(cached).toEqual(queryResult) |
|
490
|
|
|
491
|
await mockEventBus.emit( |
|
492
|
[ |
|
493
|
{ |
|
494
|
name: "price.updated", |
|
495
|
data: { id: "price_1", amount: 1100 }, |
|
496
|
}, |
|
497
|
], |
|
498
|
{} |
|
499
|
) |
|
500
|
|
|
501
|
const cachedAfterUpdate = await service.get({ key: queryKey }) |
|
502
|
expect(cachedAfterUpdate).toBeNull() |
|
503
|
}) |
|
504
|
}) |
|
505
|
}) |
|
506
|
}, |
|
507
|
}) |