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 maxSize: 1000,
55 },
56 },
57 testSuite: ({ service }) => {
58 describe("Memory Cache maxSize Integration", () => {
59 beforeEach(async () => {
60 await service.clear({ tags: ["*"] }).catch(() => {})
61 })
62
63 it("should respect maxSize limit and stop caching when exceeded", async () => {
64 // Create test data that will use most of the 1000 byte limit
65 const largeData = {
66 id: "large-test",
67 name: "Large Test Item",
68 description: "A".repeat(400), // 400 characters
69 metadata: { info: "B".repeat(300) }, // Another 300 characters
70 }
71
72 await service.set({
73 key: "first-key",
74 data: largeData,
75 tags: ["large-data"],
76 })
77
78 let result = await service.get({ key: "first-key" })
79 expect(result).toEqual(largeData)
80
81 const anotherLargeData = {
82 id: "another-large",
83 content: "C".repeat(500), // This should push us over the 1000 byte limit but accepted because we are still bellow
84 }
85
86 await service.set({
87 key: "second-key",
88 data: anotherLargeData,
89 tags: ["overflow-data"],
90 })
91
92 result = await service.get({ key: "second-key" })
93 expect(result).toEqual(anotherLargeData)
94
95 const anotherLargeData2 = {
96 id: "another-large-2",
97 content: "D".repeat(500), // This should push us over the 1000 byte limit but rejected because we are over the limit
98 }
99
100 await service.set({
101 key: "third-key",
102 data: anotherLargeData2,
103 tags: ["overflow-data-2"],
104 })
105
106 result = await service.get({ key: "third-key" })
107 expect(result).toBeNull()
108
109 const firstResult = await service.get({ key: "first-key" })
110 expect(firstResult).toEqual(largeData)
111 })
112
113 it("should allow small entries after reaching the limit", async () => {
114 const largeData = {
115 id: "large-test",
116 description: "A".repeat(800), // Use most of the 1000 byte limit
117 }
118
119 await service.set({
120 key: "large-key",
121 data: largeData,
122 tags: ["large"],
123 })
124
125 let result = await service.get({ key: "large-key" })
126 expect(result).toEqual(largeData)
127
128 await service.set({
129 key: "overflow-key",
130 data: { content: "B".repeat(500) },
131 })
132
133 result = await service.get({ key: "overflow-key" })
134 expect(result).toEqual({ content: "B".repeat(500) })
135
136 await service.set({
137 key: "overflow-key-2",
138 data: { content: "C".repeat(500) },
139 })
140
141 result = await service.get({ key: "overflow-key-2" })
142 expect(result).toBeNull()
143
144 // Remove the large entry to free up space
145 await service.clear({ key: "large-key" })
146
147 // Now small entries should work again
148 const smallData = { id: "small", name: "Small Item" }
149 await service.set({
150 key: "small-key",
151 data: smallData,
152 })
153
154 result = await service.get({ key: "small-key" })
155 expect(result).toEqual(smallData)
156 })
157 })
158 },
159 })