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
6 jest.setTimeout(10000)
7
8 jest.spyOn(MedusaModule, "getAllJoinerConfigs").mockReturnValue([
9 {
10 schema: `
11 type Product {
12 id: ID
13 title: String
14 handle: String
15 status: String
16 type_id: String
17 collection_id: String
18 is_giftcard: Boolean
19 external_id: String
20 created_at: DateTime
21 updated_at: DateTime
22
23 variants: [ProductVariant]
24 sales_channels: [SalesChannel]
25 }
26
27 type ProductVariant {
28 id: ID
29 product_id: String
30 sku: String
31
32 prices: [Price]
33 }
34
35 type Price {
36 id: ID
37 amount: Float
38 currency_code: String
39 }
40
41 type SalesChannel {
42 id: ID
43 is_disabled: Boolean
44 }
45 `,
46 },
47 ])
48
49 moduleIntegrationTestRunner<ICachingModuleService>({
50 moduleName: Modules.CACHING,
51 moduleOptions: {
52 in_memory: {
53 enable: true,
54 },
55 },
56 testSuite: ({ service }) => {
57 describe("Caching Module Service", () => {
58 beforeEach(async () => {
59 await service.clear({ tags: ["*"] }).catch(() => {})
60 })
61
62 describe("Basic Cache Operations", () => {
63 it("should set and get cache data with default memory provider", async () => {
64 const testData = { id: "test-id", name: "Test Item" }
65
66 await service.set({
67 key: "test-key",
68 data: testData,
69 ttl: 3600,
70 })
71
72 const result = await service.get({ key: "test-key" })
73 expect(result).toEqual(testData)
74 })
75
76 it("should return null for non-existent keys", async () => {
77 const result = await service.get({ key: "non-existent" })
78 expect(result).toBeNull()
79 })
80
81 it("should handle tags-based storage and retrieval", async () => {
82 const testData1 = { id: "1", name: "Item 1" }
83 const testData2 = { id: "2", name: "Item 2" }
84
85 await service.set({
86 key: "item-1",
87 data: testData1,
88 tags: ["product", "active"],
89 })
90
91 await service.set({
92 key: "item-2",
93 data: testData2,
94 tags: ["product", "inactive"],
95 })
96
97 const productResults = await service.get({ tags: ["product"] })
98 expect(productResults).toHaveLength(2)
99 expect(productResults).toContainEqual(testData1)
100 expect(productResults).toContainEqual(testData2)
101
102 const activeResults = await service.get<any[]>({ tags: ["active"] })
103 expect(activeResults).toHaveLength(1)
104 expect(activeResults?.[0]).toEqual(testData1)
105 })
106
107 it("should clear cache by key", async () => {
108 await service.set({
109 key: "test-key",
110 data: { value: "test" },
111 })
112
113 await service.clear({ key: "test-key" })
114
115 const result = await service.get({ key: "test-key" })
116 expect(result).toBeNull()
117 })
118
119 it("should clear cache by tags", async () => {
120 await service.set({
121 key: "item-1",
122 data: { id: "1" },
123 tags: ["category-a"],
124 })
125
126 await service.set({
127 key: "item-2",
128 data: { id: "2" },
129 tags: ["category-b"],
130 })
131
132 await service.clear({ tags: ["category-a"] })
133
134 const result1 = await service.get({ key: "item-1" })
135 const result2 = await service.get({ key: "item-2" })
136
137 expect(result1).toBeNull()
138 expect(result2).toEqual({ id: "2" })
139 })
140 })
141
142 describe("Provider Priority", () => {
143 it("should check providers in order of priority when specified", async () => {
144 const testData = { id: "priority-test", name: "Priority Test" }
145
146 await service.set({
147 key: "priority-key",
148 data: testData,
149 providers: ["cache-memory"],
150 })
151
152 const result = await service.get({
153 key: "priority-key",
154 providers: ["cache-memory"],
155 })
156
157 expect(result).toEqual(testData)
158 })
159
160 it("should return null when providers array is empty or invalid", async () => {
161 const result = await service.get({
162 key: "test-key",
163 providers: [],
164 })
165
166 expect(result).toBeNull()
167 })
168 })
169
170 describe("Promise Deduplication", () => {
171 it("should deduplicate concurrent get requests with same parameters", async () => {
172 const testData = { id: "concurrent-test", name: "Concurrent Test" }
173
174 await service.set({
175 key: "concurrent-key",
176 data: testData,
177 })
178
179 const promises = Array.from({ length: 5 }, () =>
180 service.get<any>({ key: "concurrent-key" })
181 )
182
183 const results = await Promise.all(promises)
184
185 results.forEach((result) => {
186 expect(result).toEqual(testData)
187 })
188 })
189
190 it("should deduplicate concurrent get requests with same tags", async () => {
191 const testData = { id: "tag-test", name: "Tag Test" }
192
193 await service.set({
194 key: "tag-key",
195 data: testData,
196 tags: ["concurrent-tag"],
197 })
198
199 const promises = Array.from({ length: 5 }, () =>
200 service.get<any[]>({ tags: ["concurrent-tag"] })
201 )
202
203 const results = await Promise.all(promises)
204
205 results.forEach((result) => {
206 expect(result).toHaveLength(1)
207 expect(result?.[0]).toEqual(testData)
208 })
209 })
210
211 it("should deduplicate concurrent clear requests", async () => {
212 await service.set({
213 key: "clear-test-1",
214 data: { id: "1" },
215 tags: ["clear-tag"],
216 })
217
218 await service.set({
219 key: "clear-test-2",
220 data: { id: "2" },
221 tags: ["clear-tag"],
222 })
223
224 const promises = Array.from({ length: 3 }, () =>
225 service.clear({ tags: ["clear-tag"] })
226 )
227
228 await Promise.all(promises)
229
230 const result1 = await service.get({ key: "clear-test-1" })
231 const result2 = await service.get({ key: "clear-test-2" })
232
233 expect(result1).toBeNull()
234 expect(result2).toBeNull()
235 })
236
237 it("should handle concurrent requests with different parameters separately", async () => {
238 const testData1 = { id: "1", name: "Item 1" }
239 const testData2 = { id: "2", name: "Item 2" }
240
241 await service.set({ key: "key-1", data: testData1 })
242 await service.set({ key: "key-2", data: testData2 })
243
244 const promises = [
245 service.get({ key: "key-1" }),
246 service.get({ key: "key-1" }),
247 service.get({ key: "key-2" }),
248 service.get({ key: "key-2" }),
249 ]
250
251 const results = await Promise.all(promises)
252
253 expect(results[0]).toEqual(testData1)
254 expect(results[1]).toEqual(testData1)
255 expect(results[2]).toEqual(testData2)
256 expect(results[3]).toEqual(testData2)
257 })
258 })
259
260 describe("Memory Cache Provider Integration", () => {
261 it("should respect TTL settings", async () => {
262 const testData = { id: "ttl-test", name: "TTL Test" }
263
264 await service.set({
265 key: "ttl-key",
266 data: testData,
267 ttl: 1,
268 })
269
270 let result = await service.get({ key: "ttl-key" })
271 expect(result).toEqual(testData)
272
273 await new Promise((resolve) => setTimeout(resolve, 1100))
274
275 result = await service.get({ key: "ttl-key" })
276 expect(result).toBeNull()
277 })
278
279 it("should handle autoInvalidate option", async () => {
280 const testData = { id: "no-auto-test", name: "No Auto Test" }
281
282 await service.set({
283 key: "no-auto-key",
284 data: testData,
285 tags: ["no-auto-tag"],
286 options: { autoInvalidate: false },
287 })
288
289 await service.clear({
290 tags: ["no-auto-tag"],
291 options: { autoInvalidate: true },
292 })
293
294 const result = await service.get({ key: "no-auto-key" })
295 expect(result).toEqual(testData)
296
297 await service.clear({
298 tags: ["no-auto-tag"],
299 })
300
301 const result2 = await service.get({ key: "no-auto-key" })
302 expect(result2).toBeNull()
303 })
304
305 it("should generate consistent cache keys", async () => {
306 const testInput = { userId: "123", action: "view" }
307
308 const key1 = await service.computeKey(testInput)
309 const key2 = await service.computeKey(testInput)
310
311 expect(key1).toBe(key2)
312 expect(typeof key1).toBe("string")
313 expect(key1.length).toBeGreaterThan(0)
314 })
315
316 it("should generate cache tags", async () => {
317 const testInput = { id: "prod_1", title: "123", description: "456" }
318
319 const tags = await service.computeTags(testInput)
320
321 expect(Array.isArray(tags)).toBe(true)
322 expect(tags.length).toBeGreaterThan(0)
323 })
324 })
325
326 describe("Error Handling", () => {
327 it("should throw error when neither key nor tags provided to get", async () => {
328 await expect(service.get({})).rejects.toThrow(
329 "Either key or tags must be provided"
330 )
331 })
332
333 it("should throw error when neither key nor tags provided to clear", async () => {
334 await expect(service.clear({})).rejects.toThrow(
335 "Either key or tags must be provided"
336 )
337 })
338 })
339 })
340 },
341 })