Open History to see the last commit
1import { describe, expect, test } from "vitest"
2import { D2 } from "../../src/d2.js"
3import { MultiSet } from "../../src/multiset.js"
4import {
5 avg,
6 count,
7 groupBy,
8 max,
9 median,
10 min,
11 mode,
12 sum,
13} from "../../src/operators/groupBy.js"
14import { output } from "../../src/operators/index.js"
15
16describe(`Operators`, () => {
17 describe(`GroupBy operation`, () => {
18 test(`with no aggregate`, () => {
19 const graph = new D2()
20 const input = graph.newInput<{
21 category: string
22 amount: number
23 }>()
24 let latestMessage: any = null
25
26 input.pipe(
27 groupBy((data) => ({ category: data.category })),
28 output((message) => {
29 latestMessage = message
30 })
31 )
32
33 graph.finalize()
34
35 // Initial data
36 input.sendData(
37 new MultiSet([
38 [{ category: `A`, amount: 10 }, 1],
39 [{ category: `A`, amount: 20 }, 1],
40 [{ category: `B`, amount: 30 }, 1],
41 ])
42 )
43 graph.run()
44
45 // Verify we have the latest message
46 expect(latestMessage).not.toBeNull()
47
48 const result = latestMessage.getInner()
49
50 const expectedResult = [
51 [
52 [
53 `{"category":"A"}`,
54 {
55 category: `A`,
56 },
57 ],
58 1,
59 ],
60 [
61 [
62 `{"category":"B"}`,
63 {
64 category: `B`,
65 },
66 ],
67 1,
68 ],
69 ]
70
71 expect(result).toEqual(expectedResult)
72 })
73
74 test(`with single sum aggregate`, () => {
75 const graph = new D2()
76 const input = graph.newInput<{
77 category: string
78 amount: number
79 }>()
80 let latestMessage: any = null
81
82 input.pipe(
83 groupBy((data) => ({ category: data.category }), {
84 total: sum((data) => data.amount),
85 }),
86 output((message) => {
87 latestMessage = message
88 })
89 )
90
91 graph.finalize()
92
93 // Initial data
94 input.sendData(
95 new MultiSet([
96 [{ category: `A`, amount: 10 }, 1],
97 [{ category: `A`, amount: 20 }, 1],
98 [{ category: `B`, amount: 30 }, 1],
99 ])
100 )
101 graph.run()
102
103 // Verify we have the latest message
104 expect(latestMessage).not.toBeNull()
105
106 const result = latestMessage.getInner()
107
108 const expectedResult = [
109 [
110 [
111 `{"category":"A"}`,
112 {
113 total: 30,
114 category: `A`,
115 },
116 ],
117 1,
118 ],
119 [
120 [
121 `{"category":"B"}`,
122 {
123 total: 30,
124 category: `B`,
125 },
126 ],
127 1,
128 ],
129 ]
130
131 expect(result).toEqual(expectedResult)
132 })
133
134 test(`with sum and count aggregates`, () => {
135 const graph = new D2()
136 const input = graph.newInput<{
137 category: string
138 region: string
139 amount: number
140 }>()
141 let latestMessage: any = null
142 const messages: Array<MultiSet<any>> = []
143
144 input.pipe(
145 groupBy(
146 (data) => ({
147 category: data.category,
148 region: data.region,
149 }),
150 {
151 total: sum((data) => data.amount),
152 count: count(),
153 }
154 ),
155 output((message) => {
156 latestMessage = message
157 messages.push(message)
158 })
159 )
160
161 graph.finalize()
162
163 // Initial data
164 input.sendData(
165 new MultiSet([
166 [{ category: `A`, region: `East`, amount: 10 }, 1],
167 [{ category: `A`, region: `East`, amount: 20 }, 1],
168 [{ category: `A`, region: `West`, amount: 30 }, 1],
169 [{ category: `B`, region: `East`, amount: 40 }, 1],
170 ])
171 )
172 graph.run()
173
174 // Verify we have the latest message
175 expect(latestMessage).not.toBeNull()
176
177 const expectedResult = [
178 [
179 [
180 `{"category":"A","region":"East"}`,
181 {
182 total: 30,
183 count: 2,
184 category: `A`,
185 region: `East`,
186 },
187 ],
188 1,
189 ],
190 [
191 [
192 `{"category":"A","region":"West"}`,
193 {
194 total: 30,
195 count: 1,
196 category: `A`,
197 region: `West`,
198 },
199 ],
200 1,
201 ],
202 [
203 [
204 `{"category":"B","region":"East"}`,
205 {
206 total: 40,
207 count: 1,
208 category: `B`,
209 region: `East`,
210 },
211 ],
212 1,
213 ],
214 ]
215
216 expect(latestMessage.getInner()).toEqual(expectedResult)
217
218 // --- Add a new record ---
219 input.sendData(
220 new MultiSet([
221 [{ category: `A`, region: `East`, amount: 15 }, 1],
222 [{ category: `B`, region: `West`, amount: 25 }, 1],
223 ])
224 )
225
226 graph.run()
227
228 const expectedAddResult = [
229 [
230 [
231 `{"category":"A","region":"East"}`,
232 {
233 category: `A`,
234 region: `East`,
235 total: 30,
236 count: 2,
237 },
238 ],
239 -1,
240 ],
241 [
242 [
243 `{"category":"A","region":"East"}`,
244 {
245 category: `A`,
246 region: `East`,
247 total: 45,
248 count: 3,
249 },
250 ],
251 1,
252 ],
253 [
254 [
255 `{"category":"B","region":"West"}`,
256 {
257 category: `B`,
258 region: `West`,
259 total: 25,
260 count: 1,
261 },
262 ],
263 1,
264 ],
265 ]
266
267 expect(latestMessage.getInner()).toEqual(expectedAddResult)
268
269 // --- Delete a record ---
270 input.sendData(
271 new MultiSet([
272 [{ category: `A`, region: `East`, amount: 20 }, -1], // Remove one of the A/East records
273 ])
274 )
275 graph.run()
276
277 const expectedDeleteResult = [
278 [
279 [
280 `{"category":"A","region":"East"}`,
281 {
282 category: `A`,
283 region: `East`,
284 total: 45,
285 count: 3,
286 },
287 ],
288 -1,
289 ],
290 [
291 [
292 `{"category":"A","region":"East"}`,
293 {
294 category: `A`,
295 region: `East`,
296 total: 25,
297 count: 2,
298 },
299 ],
300 1,
301 ],
302 ]
303
304 expect(latestMessage.getInner()).toEqual(expectedDeleteResult)
305 })
306
307 test(`with avg and count aggregates`, () => {
308 const graph = new D2()
309 const input = graph.newInput<{
310 category: string
311 amount: number
312 }>()
313 let latestMessage: any = null
314 const messages: Array<MultiSet<any>> = []
315
316 input.pipe(
317 groupBy((data) => ({ category: data.category }), {
318 average: avg((data) => data.amount),
319 count: count(),
320 }),
321 output((message) => {
322 latestMessage = message
323 messages.push(message)
324 })
325 )
326
327 graph.finalize()
328
329 // Initial data
330 input.sendData(
331 new MultiSet([
332 [{ category: `A`, amount: 10 }, 1],
333 [{ category: `A`, amount: 20 }, 1],
334 [{ category: `B`, amount: 30 }, 1],
335 ])
336 )
337 graph.run()
338
339 // Verify we have the latest message
340 expect(latestMessage).not.toBeNull()
341
342 const expectedResult = [
343 [
344 [
345 `{"category":"A"}`,
346 {
347 category: `A`,
348 average: 15,
349 count: 2,
350 },
351 ],
352 1,
353 ],
354 [
355 [
356 `{"category":"B"}`,
357 {
358 category: `B`,
359 average: 30,
360 count: 1,
361 },
362 ],
363 1,
364 ],
365 ]
366
367 expect(latestMessage.getInner()).toEqual(expectedResult)
368
369 // --- Add a new record ---
370 input.sendData(
371 new MultiSet([
372 [{ category: `A`, amount: 30 }, 1],
373 [{ category: `C`, amount: 50 }, 1],
374 ])
375 )
376 graph.run()
377
378 const expectedAddResult = [
379 [
380 [
381 `{"category":"A"}`,
382 {
383 category: `A`,
384 average: 15,
385 count: 2,
386 },
387 ],
388 -1,
389 ],
390 [
391 [
392 `{"category":"A"}`,
393 {
394 category: `A`,
395 average: 20,
396 count: 3,
397 },
398 ],
399 1,
400 ],
401 [
402 [
403 `{"category":"C"}`,
404 {
405 category: `C`,
406 average: 50,
407 count: 1,
408 },
409 ],
410 1,
411 ],
412 ]
413
414 expect(latestMessage.getInner()).toEqual(expectedAddResult)
415
416 // --- Delete a record ---
417 input.sendData(
418 new MultiSet([
419 [{ category: `A`, amount: 10 }, -1], // Remove the first A record
420 ])
421 )
422 graph.run()
423
424 const expectedDeleteResult = [
425 [
426 [
427 `{"category":"A"}`,
428 {
429 category: `A`,
430 average: 20,
431 count: 3,
432 },
433 ],
434 -1,
435 ],
436 [
437 [
438 `{"category":"A"}`,
439 {
440 category: `A`,
441 average: 25,
442 count: 2,
443 },
444 ],
445 1,
446 ],
447 ]
448
449 expect(latestMessage.getInner()).toEqual(expectedDeleteResult)
450 })
451
452 test(`with min and max aggregates`, () => {
453 const graph = new D2()
454 const input = graph.newInput<{
455 category: string
456 amount: number
457 }>()
458 let latestMessage: any = null
459
460 input.pipe(
461 groupBy((data) => ({ category: data.category }), {
462 minimum: min((data) => data.amount),
463 maximum: max((data) => data.amount),
464 }),
465 output((message) => {
466 latestMessage = message
467 })
468 )
469
470 graph.finalize()
471
472 // Initial data
473 input.sendData(
474 new MultiSet([
475 [{ category: `A`, amount: 10 }, 1],
476 [{ category: `A`, amount: 20 }, 1],
477 [{ category: `A`, amount: 5 }, 1],
478 [{ category: `B`, amount: 30 }, 1],
479 [{ category: `B`, amount: 15 }, 1],
480 ])
481 )
482
483 // Run the graph to process all messages
484 graph.run()
485
486 expect(latestMessage).not.toBeNull()
487
488 const expectedResult = [
489 [
490 [
491 `{"category":"A"}`,
492 {
493 category: `A`,
494 minimum: 5,
495 maximum: 20,
496 },
497 ],
498 1,
499 ],
500 [
501 [
502 `{"category":"B"}`,
503 {
504 category: `B`,
505 minimum: 15,
506 maximum: 30,
507 },
508 ],
509 1,
510 ],
511 ]
512
513 expect(latestMessage.getInner()).toEqual(expectedResult)
514 })
515
516 test(`with median and mode aggregates`, () => {
517 const graph = new D2()
518 const input = graph.newInput<{
519 category: string
520 amount: number
521 }>()
522 let latestMessage: any = null
523
524 input.pipe(
525 groupBy((data) => ({ category: data.category }), {
526 middle: median((data) => data.amount),
527 mostFrequent: mode((data) => data.amount),
528 }),
529 output((message) => {
530 latestMessage = message
531 })
532 )
533
534 graph.finalize()
535
536 // Initial data with pattern designed to test median and mode
537 input.sendData(
538 new MultiSet([
539 // Category A: [10, 20, 20, 30, 50]
540 // Median: 20, Mode: 20
541 [{ category: `A`, amount: 10 }, 1],
542 [{ category: `A`, amount: 20 }, 2], // Added twice to test mode
543 [{ category: `A`, amount: 30 }, 1],
544 [{ category: `A`, amount: 50 }, 1],
545
546 // Category B: [5, 10, 15, 20]
547 // Median: 12.5 (average of 10 and 15), Mode: 5, 10, 15, 20 (all appear once)
548 [{ category: `B`, amount: 5 }, 1],
549 [{ category: `B`, amount: 10 }, 1],
550 [{ category: `B`, amount: 15 }, 1],
551 [{ category: `B`, amount: 20 }, 1],
552 ])
553 )
554
555 // Run the graph to process all messages
556 graph.run()
557
558 expect(latestMessage).not.toBeNull()
559
560 const expectedResult = [
561 [
562 [
563 `{"category":"A"}`,
564 {
565 category: `A`,
566 middle: 20,
567 mostFrequent: 20,
568 },
569 ],
570 1,
571 ],
572 [
573 [
574 `{"category":"B"}`,
575 {
576 category: `B`,
577 middle: 12.5,
578 mostFrequent: 5, // First encountered value with highest frequency (all values appear once)
579 },
580 ],
581 1,
582 ],
583 ]
584
585 expect(latestMessage.getInner()).toEqual(expectedResult)
586 })
587
588 test(`complete group removal with sum aggregate`, () => {
589 const graph = new D2()
590 const input = graph.newInput<{
591 category: string
592 amount: number
593 }>()
594 let latestMessage: any = null
595
596 input.pipe(
597 groupBy((data) => ({ category: data.category }), {
598 total: sum((data) => data.amount),
599 }),
600 output((message) => {
601 latestMessage = message
602 })
603 )
604
605 graph.finalize()
606
607 // Initial data
608 input.sendData(
609 new MultiSet([
610 [{ category: `A`, amount: 10 }, 1],
611 [{ category: `A`, amount: 20 }, 1],
612 [{ category: `B`, amount: 30 }, 1],
613 [{ category: `C`, amount: 40 }, 1],
614 ])
615 )
616 graph.run()
617
618 // Verify initial state
619 expect(latestMessage).not.toBeNull()
620 let result = latestMessage.getInner()
621 expect(result).toHaveLength(3) // Should have 3 groups
622
623 // Find the group for category A
624 const categoryAGroup = result.find(
625 ([key]: any) => key[0] === `{"category":"A"}`
626 )
627 expect(categoryAGroup).toBeDefined()
628 expect(categoryAGroup[0][1].total).toBe(30) // Sum of 10 + 20
629
630 // Now remove ALL records from category A
631 input.sendData(
632 new MultiSet([
633 [{ category: `A`, amount: 10 }, -1],
634 [{ category: `A`, amount: 20 }, -1],
635 ])
636 )
637 graph.run()
638
639 // After removing all A records, the group should be completely removed
640 // NOT return a group with total: 0
641 result = latestMessage.getInner()
642
643 // The result should contain the removal of the old group
644 // but NOT the creation of a new group with total: 0
645 const expectedResult = [
646 [
647 [
648 `{"category":"A"}`,
649 {
650 category: `A`,
651 total: 30,
652 },
653 ],
654 -1, // This should be removed
655 ],
656 ]
657
658 expect(result).toEqual(expectedResult)
659
660 // Verify no new group with total: 0 was created by checking that
661 // we don't have any positive weight entries for category A
662 const positiveCategoryAEntries = result.filter(
663 ([key, , weight]: any) => key[0] === `{"category":"A"}` && weight > 0
664 )
665 expect(positiveCategoryAEntries).toHaveLength(0)
666 })
667
668 test(`complete group removal with multiple aggregates`, () => {
669 const graph = new D2()
670 const input = graph.newInput<{
671 category: string
672 region: string
673 amount: number
674 }>()
675 let latestMessage: any = null
676
677 input.pipe(
678 groupBy(
679 (data) => ({
680 category: data.category,
681 region: data.region,
682 }),
683 {
684 total: sum((data) => data.amount),
685 count: count(),
686 average: avg((data) => data.amount),
687 }
688 ),
689 output((message) => {
690 latestMessage = message
691 })
692 )
693
694 graph.finalize()
695
696 // Initial data
697 input.sendData(
698 new MultiSet([
699 [{ category: `A`, region: `East`, amount: 10 }, 1],
700 [{ category: `A`, region: `East`, amount: 20 }, 1],
701 [{ category: `A`, region: `West`, amount: 30 }, 1],
702 [{ category: `B`, region: `East`, amount: 40 }, 1],
703 ])
704 )
705 graph.run()
706
707 // Verify initial state
708 expect(latestMessage).not.toBeNull()
709 let result = latestMessage.getInner()
710 expect(result).toHaveLength(3) // Should have 3 groups
711
712 // Find the group for category A, region East
713 const categoryAEastGroup = result.find(
714 ([key]: any) => key[0] === `{"category":"A","region":"East"}`
715 )
716 expect(categoryAEastGroup).toBeDefined()
717 expect(categoryAEastGroup[0][1]).toEqual({
718 category: `A`,
719 region: `East`,
720 total: 30, // 10 + 20
721 count: 2,
722 average: 15, // 30 / 2
723 })
724
725 // Now remove ALL records from category A, region East
726 input.sendData(
727 new MultiSet([
728 [{ category: `A`, region: `East`, amount: 10 }, -1],
729 [{ category: `A`, region: `East`, amount: 20 }, -1],
730 ])
731 )
732 graph.run()
733
734 // After removing all A/East records, that group should be completely removed
735 // NOT return a group with total: 0, count: 0, average: 0 (or NaN)
736 result = latestMessage.getInner()
737
738 // The result should contain the removal of the old group
739 const expectedResult = [
740 [
741 [
742 `{"category":"A","region":"East"}`,
743 {
744 category: `A`,
745 region: `East`,
746 total: 30,
747 count: 2,
748 average: 15,
749 },
750 ],
751 -1, // This should be removed
752 ],
753 ]
754
755 expect(result).toEqual(expectedResult)
756
757 // Verify no new group with zero/empty values was created
758 const positiveCategoryAEastEntries = result.filter(
759 ([key, , weight]: any) =>
760 key[0] === `{"category":"A","region":"East"}` && weight > 0
761 )
762 expect(positiveCategoryAEastEntries).toHaveLength(0)
763 })
764
765 test(`group removal and re-addition with sum aggregate`, () => {
766 const graph = new D2()
767 const input = graph.newInput<{
768 category: string
769 amount: number
770 }>()
771 let latestMessage: any = null
772
773 input.pipe(
774 groupBy((data) => ({ category: data.category }), {
775 total: sum((data) => data.amount),
776 }),
777 output((message) => {
778 latestMessage = message
779 })
780 )
781
782 graph.finalize()
783
784 // Step 1: Initial data
785 input.sendData(
786 new MultiSet([
787 [{ category: `A`, amount: 10 }, 1],
788 [{ category: `A`, amount: 20 }, 1],
789 [{ category: `B`, amount: 30 }, 1],
790 ])
791 )
792 graph.run()
793
794 // Verify initial state
795 expect(latestMessage).not.toBeNull()
796 let result = latestMessage.getInner()
797 expect(result).toHaveLength(2) // Should have 2 groups
798
799 // Find the group for category A
800 const categoryAGroup = result.find(
801 ([key]: any) => key[0] === `{"category":"A"}`
802 )
803 expect(categoryAGroup).toBeDefined()
804 expect(categoryAGroup[0][1].total).toBe(30) // Sum of 10 + 20
805
806 // Step 2: Remove ALL records from category A
807 input.sendData(
808 new MultiSet([
809 [{ category: `A`, amount: 10 }, -1],
810 [{ category: `A`, amount: 20 }, -1],
811 ])
812 )
813 graph.run()
814
815 // Verify group A is completely removed
816 result = latestMessage.getInner()
817 const expectedRemovalResult = [
818 [
819 [
820 `{"category":"A"}`,
821 {
822 category: `A`,
823 total: 30,
824 },
825 ],
826 -1, // Group should be removed
827 ],
828 ]
829 expect(result).toEqual(expectedRemovalResult)
830
831 // Step 3: Re-add records to category A with different values
832 input.sendData(
833 new MultiSet([
834 [{ category: `A`, amount: 50 }, 1],
835 [{ category: `A`, amount: 25 }, 1],
836 ])
837 )
838 graph.run()
839
840 // Verify group A is recreated with correct new aggregate values
841 result = latestMessage.getInner()
842 const expectedReAdditionResult = [
843 [
844 [
845 `{"category":"A"}`,
846 {
847 category: `A`,
848 total: 75, // 50 + 25 (new values, not the old 30)
849 },
850 ],
851 1, // New group should be added
852 ],
853 ]
854 expect(result).toEqual(expectedReAdditionResult)
855
856 // Step 4: Verify no lingering effects by adding more data
857 input.sendData(new MultiSet([[{ category: `A`, amount: 15 }, 1]]))
858 graph.run()
859
860 // Verify aggregate is updated correctly from the new baseline
861 result = latestMessage.getInner()
862 const expectedUpdateResult = [
863 [
864 [
865 `{"category":"A"}`,
866 {
867 category: `A`,
868 total: 75, // Previous total
869 },
870 ],
871 -1, // Remove old state
872 ],
873 [
874 [
875 `{"category":"A"}`,
876 {
877 category: `A`,
878 total: 90, // 75 + 15
879 },
880 ],
881 1, // Add new state
882 ],
883 ]
884 expect(result).toEqual(expectedUpdateResult)
885 })
886
887 test(`group removal and re-addition with multiple aggregates`, () => {
888 const graph = new D2()
889 const input = graph.newInput<{
890 category: string
891 region: string
892 amount: number
893 }>()
894 let latestMessage: any = null
895
896 input.pipe(
897 groupBy(
898 (data) => ({
899 category: data.category,
900 region: data.region,
901 }),
902 {
903 total: sum((data) => data.amount),
904 count: count(),
905 average: avg((data) => data.amount),
906 minimum: min((data) => data.amount),
907 maximum: max((data) => data.amount),
908 }
909 ),
910 output((message) => {
911 latestMessage = message
912 })
913 )
914
915 graph.finalize()
916
917 // Step 1: Initial data
918 input.sendData(
919 new MultiSet([
920 [{ category: `A`, region: `East`, amount: 10 }, 1],
921 [{ category: `A`, region: `East`, amount: 20 }, 1],
922 [{ category: `A`, region: `East`, amount: 30 }, 1],
923 [{ category: `B`, region: `West`, amount: 100 }, 1],
924 ])
925 )
926 graph.run()
927
928 // Verify initial state
929 expect(latestMessage).not.toBeNull()
930 let result = latestMessage.getInner()
931 expect(result).toHaveLength(2) // Should have 2 groups
932
933 // Find the group for category A, region East
934 const categoryAEastGroup = result.find(
935 ([key]: any) => key[0] === `{"category":"A","region":"East"}`
936 )
937 expect(categoryAEastGroup).toBeDefined()
938 expect(categoryAEastGroup[0][1]).toEqual({
939 category: `A`,
940 region: `East`,
941 total: 60, // 10 + 20 + 30
942 count: 3,
943 average: 20, // 60 / 3
944 minimum: 10,
945 maximum: 30,
946 })
947
948 // Step 2: Remove ALL records from category A, region East
949 input.sendData(
950 new MultiSet([
951 [{ category: `A`, region: `East`, amount: 10 }, -1],
952 [{ category: `A`, region: `East`, amount: 20 }, -1],
953 [{ category: `A`, region: `East`, amount: 30 }, -1],
954 ])
955 )
956 graph.run()
957
958 // Verify group is completely removed
959 result = latestMessage.getInner()
960 const expectedRemovalResult = [
961 [
962 [
963 `{"category":"A","region":"East"}`,
964 {
965 category: `A`,
966 region: `East`,
967 total: 60,
968 count: 3,
969 average: 20,
970 minimum: 10,
971 maximum: 30,
972 },
973 ],
974 -1, // Group should be removed
975 ],
976 ]
977 expect(result).toEqual(expectedRemovalResult)
978
979 // Step 3: Re-add records to category A, region East with completely different values
980 input.sendData(
981 new MultiSet([
982 [{ category: `A`, region: `East`, amount: 5 }, 1],
983 [{ category: `A`, region: `East`, amount: 15 }, 1],
984 [{ category: `A`, region: `East`, amount: 40 }, 1],
985 [{ category: `A`, region: `East`, amount: 40 }, 1], // Duplicate to test aggregates properly
986 ])
987 )
988 graph.run()
989
990 // Verify group is recreated with correct new aggregate values
991 result = latestMessage.getInner()
992 const expectedReAdditionResult = [
993 [
994 [
995 `{"category":"A","region":"East"}`,
996 {
997 category: `A`,
998 region: `East`,
999 total: 100, // 5 + 15 + 40 + 40 (completely new calculation)
1000 count: 4,
1001 average: 25, // 100 / 4
1002 minimum: 5, // New minimum
1003 maximum: 40, // New maximum
1004 },
1005 ],
1006 1, // New group should be added
1007 ],
1008 ]
1009 expect(result).toEqual(expectedReAdditionResult)
1010
1011 // Step 4: Remove some records and verify aggregates update correctly
1012 input.sendData(
1013 new MultiSet([
1014 [{ category: `A`, region: `East`, amount: 40 }, -1], // Remove one of the 40s
1015 ])
1016 )
1017 graph.run()
1018
1019 // Verify aggregates are updated correctly from the new baseline
1020 result = latestMessage.getInner()
1021 const expectedPartialRemovalResult = [
1022 [
1023 [
1024 `{"category":"A","region":"East"}`,
1025 {
1026 category: `A`,
1027 region: `East`,
1028 total: 100,
1029 count: 4,
1030 average: 25,
1031 minimum: 5,
1032 maximum: 40,
1033 },
1034 ],
1035 -1, // Remove old state
1036 ],
1037 [
1038 [
1039 `{"category":"A","region":"East"}`,
1040 {
1041 category: `A`,
1042 region: `East`,
1043 total: 60, // 5 + 15 + 40 (one 40 removed)
1044 count: 3,
1045 average: 20, // 60 / 3
1046 minimum: 5, // Still 5
1047 maximum: 40, // Still 40 (one remains)
1048 },
1049 ],
1050 1, // Add new state
1051 ],
1052 ]
1053 expect(result).toEqual(expectedPartialRemovalResult)
1054 })
1055 })
1056})