1 import { GraphQLSchema, buildSchema } from "graphql"
2 import { CacheInvalidationParser, EntityReference } from "../parser"
3
4 describe("CacheInvalidationParser", () => {
5 let parser: CacheInvalidationParser
6 let schema: GraphQLSchema
7
8 beforeEach(() => {
9 const schemaDefinition = `
10 type Product {
11 id: ID!
12 title: String
13 description: String
14 collection: ProductCollection
15 categories: [ProductCategory!]
16 variants: [ProductVariant!]
17 created_at: String
18 updated_at: String
19 }
20
21 type ProductCollection {
22 id: ID!
23 title: String
24 products: [Product!]
25 created_at: String
26 updated_at: String
27 }
28
29 type ProductCategory {
30 id: ID!
31 name: String
32 products: [Product!]
33 parent: ProductCategory
34 children: [ProductCategory!]
35 created_at: String
36 updated_at: String
37 }
38
39 type ProductVariant {
40 id: ID!
41 title: String
42 sku: String
43 product: Product!
44 prices: [Price!]
45 created_at: String
46 updated_at: String
47 }
48
49 type Price {
50 id: ID!
51 amount: Int
52 currency_code: String
53 variant: ProductVariant!
54 created_at: String
55 updated_at: String
56 }
57
58 type Order {
59 id: ID!
60 status: String
61 items: [OrderItem!]
62 customer: Customer
63 created_at: String
64 updated_at: String
65 }
66
67 type OrderItem {
68 id: ID!
69 quantity: Int
70 order: Order!
71 variant: ProductVariant!
72 created_at: String
73 updated_at: String
74 }
75
76 type Customer {
77 id: ID!
78 first_name: String
79 last_name: String
80 email: String
81 orders: [Order!]
82 created_at: String
83 updated_at: String
84 }
85 `
86
87 schema = buildSchema(schemaDefinition)
88 parser = new CacheInvalidationParser(schema, [
89 // Partially populate this record ro force the test to match from both id prefix or type
90 // detection
91 {
92 idPrefixToEntityName: {
93 prod: "Product",
94 col: "ProductCollection",
95 cat: "ProductCategory",
96 },
97 },
98 ])
99 })
100
101 describe("parseObjectForEntities", () => {
102 it("should identify a simple product entity", () => {
103 const product = {
104 id: "prod_123",
105 title: "Test Product",
106 description: "A test product",
107 }
108
109 const entities = parser.parseObjectForEntities(product)
110
111 expect(entities).toHaveLength(1)
112 expect(entities[0]).toEqual({
113 type: "Product",
114 id: "prod_123",
115 isInArray: false,
116 })
117 })
118
119 it("should identify nested entities in a product with collection", () => {
120 const product = {
121 id: "prod_123",
122 title: "Test Product",
123 collection: {
124 id: "col_456",
125 title: "Test Collection",
126 },
127 }
128
129 const entities = parser.parseObjectForEntities(product)
130
131 expect(entities).toHaveLength(2)
132 expect(entities).toContainEqual({
133 type: "Product",
134 id: "prod_123",
135 isInArray: false,
136 })
137 expect(entities).toContainEqual({
138 type: "ProductCollection",
139 id: "col_456",
140 isInArray: false,
141 })
142 })
143
144 it("should identify entities in arrays", () => {
145 const product = {
146 id: "prod_123",
147 title: "Test Product",
148 variants: [
149 {
150 id: "var_789",
151 title: "Variant 1",
152 sku: "SKU-001",
153 },
154 {
155 id: "var_790",
156 title: "Variant 2",
157 sku: "SKU-002",
158 },
159 ],
160 }
161
162 const entities = parser.parseObjectForEntities(product)
163
164 expect(entities).toHaveLength(3)
165 expect(entities).toContainEqual({
166 type: "Product",
167 id: "prod_123",
168 isInArray: false,
169 })
170 expect(entities).toContainEqual({
171 type: "ProductVariant",
172 id: "var_789",
173 isInArray: true,
174 })
175 expect(entities).toContainEqual({
176 type: "ProductVariant",
177 id: "var_790",
178 isInArray: true,
179 })
180 })
181
182 it("should handle deeply nested entities", () => {
183 const order = {
184 id: "order_123",
185 status: "completed",
186 items: [
187 {
188 id: "item_456",
189 quantity: 2,
190 variant: {
191 id: "var_789",
192 title: "Variant 1",
193 product: {
194 id: "prod_123",
195 title: "Test Product",
196 collection: {
197 id: "col_456",
198 title: "Test Collection",
199 },
200 },
201 },
202 },
203 ],
204 customer: {
205 id: "cus_789",
206 email: "test@example.com",
207 first_name: "John",
208 },
209 }
210
211 const entities = parser.parseObjectForEntities(order)
212
213 expect(entities).toHaveLength(6)
214 expect(entities).toContainEqual({
215 type: "Order",
216 id: "order_123",
217 isInArray: false,
218 })
219 expect(entities).toContainEqual({
220 type: "OrderItem",
221 id: "item_456",
222 isInArray: true,
223 })
224 expect(entities).toContainEqual({
225 type: "ProductVariant",
226 id: "var_789",
227 isInArray: false,
228 })
229 expect(entities).toContainEqual({
230 type: "Product",
231 id: "prod_123",
232 isInArray: false,
233 })
234 expect(entities).toContainEqual({
235 type: "ProductCollection",
236 id: "col_456",
237 isInArray: false,
238 })
239 expect(entities).toContainEqual({
240 type: "Customer",
241 id: "cus_789",
242 isInArray: false,
243 })
244 })
245
246 it("should return empty array for null or primitive values", () => {
247 expect(parser.parseObjectForEntities(null)).toEqual([])
248 expect(parser.parseObjectForEntities(undefined)).toEqual([])
249 expect(parser.parseObjectForEntities("string")).toEqual([])
250 expect(parser.parseObjectForEntities(123)).toEqual([])
251 expect(parser.parseObjectForEntities(true)).toEqual([])
252 })
253
254 it("should ignore objects without id field", () => {
255 const invalidObject = {
256 title: "No ID Object",
257 description: "This object has no ID",
258 }
259
260 const entities = parser.parseObjectForEntities(invalidObject)
261 expect(entities).toEqual([])
262 })
263
264 it("should handle objects with partial field matches", () => {
265 const partialProduct = {
266 id: "prod_123",
267 title: "Test Product",
268 unknown_field: "Should still work",
269 }
270
271 const entities = parser.parseObjectForEntities(partialProduct)
272
273 expect(entities).toHaveLength(1)
274 expect(entities[0]).toEqual({
275 type: "Product",
276 id: "prod_123",
277 isInArray: false,
278 })
279 })
280 })
281
282 describe("buildInvalidationEvents", () => {
283 it("should build invalidation events for a single entity", () => {
284 const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
285
286 const events = parser.buildInvalidationEvents(entities)
287
288 expect(events).toHaveLength(1)
289 expect(events[0]).toMatchObject({
290 entityType: "Product",
291 entityId: "prod_123",
292 })
293
294 expect(events[0].cacheKeys).toEqual([
295 "Product:prod_123",
296 "Product:list:*",
297 ])
298 })
299
300 it("should build invalidation events with related entities", () => {
301 const entities: EntityReference[] = [
302 { type: "Product", id: "prod_123" },
303 { type: "ProductCollection", id: "col_456" },
304 { type: "ProductVariant", id: "var_789" },
305 ]
306
307 const events = parser.buildInvalidationEvents(entities)
308
309 expect(events).toHaveLength(3)
310
311 const productEvent = events.find((e) => e.entityType === "Product")
312 expect(productEvent).toBeDefined()
313 expect(productEvent!.cacheKeys).toEqual([
314 "Product:prod_123",
315 "Product:list:*",
316 ])
317 })
318
319 it("should avoid duplicate entities in events", () => {
320 const entities: EntityReference[] = [
321 { type: "Product", id: "prod_123" },
322 { type: "Product", id: "prod_123" }, // Duplicate
323 { type: "ProductCollection", id: "col_456" },
324 ]
325
326 const events = parser.buildInvalidationEvents(entities)
327
328 expect(events).toHaveLength(2) // Should only have Product and ProductCollection events
329 expect(events.map((e) => e.entityType).sort()).toEqual([
330 "Product",
331 "ProductCollection",
332 ])
333 })
334
335 it("should generate comprehensive cache keys", () => {
336 const entities: EntityReference[] = [
337 { type: "Product", id: "prod_123" },
338 { type: "ProductCollection", id: "col_456" },
339 ]
340
341 const events = parser.buildInvalidationEvents(entities)
342 const productEvent = events.find((e) => e.entityType === "Product")!
343
344 expect(productEvent.cacheKeys).toEqual([
345 "Product:prod_123",
346 "Product:list:*",
347 ])
348 })
349 })
350
351 describe("integration scenarios", () => {
352 it("should handle a complete product updated scenario", () => {
353 const productData = {
354 id: "prod_123",
355 title: "Updated Product Title",
356 collection: {
357 id: "col_456",
358 title: "Fashion Collection",
359 },
360 categories: [
361 { id: "cat_789", name: "Shirts" },
362 { id: "cat_790", name: "Casual" },
363 ],
364 variants: [
365 {
366 id: "var_111",
367 title: "Size S",
368 prices: [{ id: "price_222", amount: 2999, currency_code: "USD" }],
369 },
370 ],
371 }
372
373 const entities = parser.parseObjectForEntities(productData)
374 const events = parser.buildInvalidationEvents(entities)
375
376 // Should identify all nested entities
377 expect(entities).toHaveLength(6) // Product, Collection, 2 Categories, Variant, Price
378
379 // Should created events for each entity type
380 expect(events).toHaveLength(6)
381
382 // Validate cache keys for each entity type
383 const productEvent = events.find((e) => e.entityType === "Product")!
384 expect(productEvent.cacheKeys).toEqual([
385 "Product:prod_123",
386 "Product:list:*",
387 ])
388
389 const collectionEvent = events.find(
390 (e) => e.entityType === "ProductCollection"
391 )!
392 expect(collectionEvent.cacheKeys).toEqual([
393 "ProductCollection:col_456",
394 "ProductCollection:list:*",
395 ])
396
397 const categoryEvents = events.filter(
398 (e) => e.entityType === "ProductCategory"
399 )
400 expect(categoryEvents).toHaveLength(2)
401 expect(categoryEvents[0].cacheKeys).toEqual([
402 "ProductCategory:cat_789",
403 "ProductCategory:list:*",
404 ])
405 expect(categoryEvents[1].cacheKeys).toEqual([
406 "ProductCategory:cat_790",
407 "ProductCategory:list:*",
408 ])
409
410 const variantEvent = events.find(
411 (e) => e.entityType === "ProductVariant"
412 )!
413 expect(variantEvent.cacheKeys).toEqual([
414 "ProductVariant:var_111",
415 "ProductVariant:list:*",
416 ])
417
418 const priceEvent = events.find((e) => e.entityType === "Price")!
419 expect(priceEvent.cacheKeys).toEqual(["Price:price_222", "Price:list:*"])
420 })
421
422 it("should handle order with customer and items scenario", () => {
423 const orderData = {
424 id: "order_123",
425 status: "completed",
426 customer: {
427 id: "cus_456",
428 email: "customer@example.com",
429 },
430 items: [
431 {
432 id: "item_789",
433 quantity: 2,
434 variant: {
435 id: "var_111",
436 sku: "SHIRT-S-BLUE",
437 },
438 },
439 ],
440 }
441
442 const entities = parser.parseObjectForEntities(orderData)
443 const events = parser.buildInvalidationEvents(entities)
444
445 expect(entities).toHaveLength(4) // Order, Customer, OrderItem, ProductVariant
446 expect(events).toHaveLength(4)
447
448 // Validate cache keys for each entity type
449 const orderEvent = events.find((e) => e.entityType === "Order")!
450 expect(orderEvent.cacheKeys).toEqual(["Order:order_123", "Order:list:*"])
451
452 const customerEvent = events.find((e) => e.entityType === "Customer")!
453 expect(customerEvent.cacheKeys).toEqual([
454 "Customer:cus_456",
455 "Customer:list:*",
456 ])
457
458 const itemEvent = events.find((e) => e.entityType === "OrderItem")!
459 expect(itemEvent.cacheKeys).toEqual([
460 "OrderItem:item_789",
461 "OrderItem:list:*",
462 ])
463
464 const variantEvent = events.find(
465 (e) => e.entityType === "ProductVariant"
466 )!
467 expect(variantEvent.cacheKeys).toEqual([
468 "ProductVariant:var_111",
469 "ProductVariant:list:*",
470 ])
471 })
472
473 it("should include simplified cache keys for created operation", () => {
474 const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
475
476 const events = parser.buildInvalidationEvents(entities, "created")
477
478 const productEvent = events[0]
479 expect(productEvent.cacheKeys).toEqual([
480 "Product:prod_123",
481 "Product:list:*",
482 ])
483 })
484
485 it("should include simplified cache keys for deleted operation", () => {
486 const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
487
488 const events = parser.buildInvalidationEvents(entities, "deleted")
489
490 const productEvent = events[0]
491 expect(productEvent.cacheKeys).toEqual([
492 "Product:prod_123",
493 "Product:list:*",
494 ])
495 })
496
497 it("should invalidate list caches for updated operation", () => {
498 const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
499
500 const events = parser.buildInvalidationEvents(entities, "updated")
501
502 const productEvent = events[0]
503 expect(productEvent.cacheKeys).toEqual([
504 "Product:prod_123",
505 "Product:list:*",
506 ])
507 })
508
509 it("should invalidate list caches for restored operation", () => {
510 // A restored entity re-enters every list it was soft deleted out of.
511 const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
512
513 const events = parser.buildInvalidationEvents(entities, "restored")
514
515 expect(events[0].cacheKeys).toEqual([
516 "Product:prod_123",
517 "Product:list:*",
518 ])
519 })
520
521 it.each(["attached", "detached"] as const)(
522 "should invalidate list caches for %s operation",
523 (operation) => {
524 const entities: EntityReference[] = [
525 { type: "LinkProductVariantInventoryItem", id: "pvitem_123" },
526 ]
527
528 const events = parser.buildInvalidationEvents(entities, operation)
529
530 expect(events[0].cacheKeys).toEqual([
531 "LinkProductVariantInventoryItem:pvitem_123",
532 "LinkProductVariantInventoryItem:list:*",
533 ])
534 }
535 )
536
537 it("should still invalidate the list cache when a link is attached without an id", () => {
538 // Link modules emit the attach payload before the row is persisted, so no
539 // id is available. The list key is the only usable tag in that case.
540 const entities: EntityReference[] = [
541 { type: "LinkProductVariantInventoryItem", id: undefined as any },
542 ]
543
544 const events = parser.buildInvalidationEvents(entities, "attached")
545
546 expect(events[0].cacheKeys).toContain(
547 "LinkProductVariantInventoryItem:list:*"
548 )
549 })
550 })
551 })