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 count (only not-null values)`, () => {
308 const graph = new D2()
309 const input = graph.newInput<{
310 category: string
311 amount: number | null
312 }>()
313 let latestMessage: any = null
314 const messages: Array<MultiSet<any>> = []
315
316 input.pipe(
317 groupBy(
318 (data) => ({
319 category: data.category,
320 }),
321 {
322 countNotNull: count((data) => data.amount),
323 count: count(),
324 }
325 ),
326 output((message) => {
327 latestMessage = message
328 messages.push(message)
329 })
330 )
331
332 graph.finalize()
333
334 // Initial data
335 input.sendData(
336 new MultiSet([
337 [{ category: `A`, amount: 10 }, 1],
338 [{ category: `B`, amount: 10 }, 1],
339 [{ category: `A`, amount: null }, 1],
340 [{ category: `B`, amount: null }, 1],
341 ])
342 )
343
344 graph.run()
345
346 // Verify we have the latest message
347 expect(latestMessage).not.toBeNull()
348
349 const expectedResult = [
350 [
351 [
352 `{"category":"A"}`,
353 {
354 category: `A`,
355 countNotNull: 1,
356 count: 2,
357 },
358 ],
359 1,
360 ],
361 [
362 [
363 `{"category":"B"}`,
364 {
365 category: `B`,
366 countNotNull: 1,
367 count: 2,
368 },
369 ],
370 1,
371 ],
372 ]
373
374 expect(latestMessage.getInner()).toEqual(expectedResult)
375 })
376
377 test(`with avg and count aggregates`, () => {
378 const graph = new D2()
379 const input = graph.newInput<{
380 category: string
381 amount: number
382 }>()
383 let latestMessage: any = null
384 const messages: Array<MultiSet<any>> = []
385
386 input.pipe(
387 groupBy((data) => ({ category: data.category }), {
388 average: avg((data) => data.amount),
389 count: count(),
390 }),
391 output((message) => {
392 latestMessage = message
393 messages.push(message)
394 })
395 )
396
397 graph.finalize()
398
399 // Initial data
400 input.sendData(
401 new MultiSet([
402 [{ category: `A`, amount: 10 }, 1],
403 [{ category: `A`, amount: 20 }, 1],
404 [{ category: `B`, amount: 30 }, 1],
405 ])
406 )
407 graph.run()
408
409 // Verify we have the latest message
410 expect(latestMessage).not.toBeNull()
411
412 const expectedResult = [
413 [
414 [
415 `{"category":"A"}`,
416 {
417 category: `A`,
418 average: 15,
419 count: 2,
420 },
421 ],
422 1,
423 ],
424 [
425 [
426 `{"category":"B"}`,
427 {
428 category: `B`,
429 average: 30,
430 count: 1,
431 },
432 ],
433 1,
434 ],
435 ]
436
437 expect(latestMessage.getInner()).toEqual(expectedResult)
438
439 // --- Add a new record ---
440 input.sendData(
441 new MultiSet([
442 [{ category: `A`, amount: 30 }, 1],
443 [{ category: `C`, amount: 50 }, 1],
444 ])
445 )
446 graph.run()
447
448 const expectedAddResult = [
449 [
450 [
451 `{"category":"A"}`,
452 {
453 category: `A`,
454 average: 15,
455 count: 2,
456 },
457 ],
458 -1,
459 ],
460 [
461 [
462 `{"category":"A"}`,
463 {
464 category: `A`,
465 average: 20,
466 count: 3,
467 },
468 ],
469 1,
470 ],
471 [
472 [
473 `{"category":"C"}`,
474 {
475 category: `C`,
476 average: 50,
477 count: 1,
478 },
479 ],
480 1,
481 ],
482 ]
483
484 expect(latestMessage.getInner()).toEqual(expectedAddResult)
485
486 // --- Delete a record ---
487 input.sendData(
488 new MultiSet([
489 [{ category: `A`, amount: 10 }, -1], // Remove the first A record
490 ])
491 )
492 graph.run()
493
494 const expectedDeleteResult = [
495 [
496 [
497 `{"category":"A"}`,
498 {
499 category: `A`,
500 average: 20,
501 count: 3,
502 },
503 ],
504 -1,
505 ],
506 [
507 [
508 `{"category":"A"}`,
509 {
510 category: `A`,
511 average: 25,
512 count: 2,
513 },
514 ],
515 1,
516 ],
517 ]
518
519 expect(latestMessage.getInner()).toEqual(expectedDeleteResult)
520 })
521
522 test(`with min and max aggregates`, () => {
523 const graph = new D2()
524 const input = graph.newInput<{
525 category: string
526 amount: number
527 date: Date
528 }>()
529 let latestMessage: any = null
530
531 input.pipe(
532 groupBy((data) => ({ category: data.category }), {
533 minimum: min((data) => data.amount),
534 maximum: max((data) => data.amount),
535 min_date: min((data) => data.date),
536 max_date: max((data) => data.date),
537 }),
538 output((message) => {
539 latestMessage = message
540 })
541 )
542
543 graph.finalize()
544
545 // Initial data
546 input.sendData(
547 new MultiSet([
548 [{ category: `A`, amount: 10, date: new Date(`2025/12/13`) }, 1],
549 [{ category: `A`, amount: 20, date: new Date(`2025/12/15`) }, 1],
550 [{ category: `A`, amount: 5, date: new Date(`2025/12/12`) }, 1],
551 [{ category: `B`, amount: 30, date: new Date(`2025/12/12`) }, 1],
552 [{ category: `B`, amount: 15, date: new Date(`2025/12/13`) }, 1],
553 ])
554 )
555
556 // Run the graph to process all messages
557 graph.run()
558
559 expect(latestMessage).not.toBeNull()
560
561 const expectedResult = [
562 [
563 [
564 `{"category":"A"}`,
565 {
566 category: `A`,
567 minimum: 5,
568 maximum: 20,
569 min_date: new Date(`2025/12/12`),
570 max_date: new Date(`2025/12/15`),
571 },
572 ],
573 1,
574 ],
575 [
576 [
577 `{"category":"B"}`,
578 {
579 category: `B`,
580 minimum: 15,
581 maximum: 30,
582 min_date: new Date(`2025/12/12`),
583 max_date: new Date(`2025/12/13`),
584 },
585 ],
586 1,
587 ],
588 ]
589
590 expect(latestMessage.getInner()).toEqual(expectedResult)
591 })
592
593 test(`with median and mode aggregates`, () => {
594 const graph = new D2()
595 const input = graph.newInput<{
596 category: string
597 amount: number
598 }>()
599 let latestMessage: any = null
600
601 input.pipe(
602 groupBy((data) => ({ category: data.category }), {
603 middle: median((data) => data.amount),
604 mostFrequent: mode((data) => data.amount),
605 }),
606 output((message) => {
607 latestMessage = message
608 })
609 )
610
611 graph.finalize()
612
613 // Initial data with pattern designed to test median and mode
614 input.sendData(
615 new MultiSet([
616 // Category A: [10, 20, 20, 30, 50]
617 // Median: 20, Mode: 20
618 [{ category: `A`, amount: 10 }, 1],
619 [{ category: `A`, amount: 20 }, 2], // Added twice to test mode
620 [{ category: `A`, amount: 30 }, 1],
621 [{ category: `A`, amount: 50 }, 1],
622
623 // Category B: [5, 10, 15, 20]
624 // Median: 12.5 (average of 10 and 15), Mode: 5, 10, 15, 20 (all appear once)
625 [{ category: `B`, amount: 5 }, 1],
626 [{ category: `B`, amount: 10 }, 1],
627 [{ category: `B`, amount: 15 }, 1],
628 [{ category: `B`, amount: 20 }, 1],
629 ])
630 )
631
632 // Run the graph to process all messages
633 graph.run()
634
635 expect(latestMessage).not.toBeNull()
636
637 const expectedResult = [
638 [
639 [
640 `{"category":"A"}`,
641 {
642 category: `A`,
643 middle: 20,
644 mostFrequent: 20,
645 },
646 ],
647 1,
648 ],
649 [
650 [
651 `{"category":"B"}`,
652 {
653 category: `B`,
654 middle: 12.5,
655 mostFrequent: 5, // First encountered value with highest frequency (all values appear once)
656 },
657 ],
658 1,
659 ],
660 ]
661
662 expect(latestMessage.getInner()).toEqual(expectedResult)
663 })
664
665 test(`complete group removal with sum aggregate`, () => {
666 const graph = new D2()
667 const input = graph.newInput<{
668 category: string
669 amount: number
670 }>()
671 let latestMessage: any = null
672
673 input.pipe(
674 groupBy((data) => ({ category: data.category }), {
675 total: sum((data) => data.amount),
676 }),
677 output((message) => {
678 latestMessage = message
679 })
680 )
681
682 graph.finalize()
683
684 // Initial data
685 input.sendData(
686 new MultiSet([
687 [{ category: `A`, amount: 10 }, 1],
688 [{ category: `A`, amount: 20 }, 1],
689 [{ category: `B`, amount: 30 }, 1],
690 [{ category: `C`, amount: 40 }, 1],
691 ])
692 )
693 graph.run()
694
695 // Verify initial state
696 expect(latestMessage).not.toBeNull()
697 let result = latestMessage.getInner()
698 expect(result).toHaveLength(3) // Should have 3 groups
699
700 // Find the group for category A
701 const categoryAGroup = result.find(
702 ([key]: any) => key[0] === `{"category":"A"}`
703 )
704 expect(categoryAGroup).toBeDefined()
705 expect(categoryAGroup[0][1].total).toBe(30) // Sum of 10 + 20
706
707 // Now remove ALL records from category A
708 input.sendData(
709 new MultiSet([
710 [{ category: `A`, amount: 10 }, -1],
711 [{ category: `A`, amount: 20 }, -1],
712 ])
713 )
714 graph.run()
715
716 // After removing all A records, the group should be completely removed
717 // NOT return a group with total: 0
718 result = latestMessage.getInner()
719
720 // The result should contain the removal of the old group
721 // but NOT the creation of a new group with total: 0
722 const expectedResult = [
723 [
724 [
725 `{"category":"A"}`,
726 {
727 category: `A`,
728 total: 30,
729 },
730 ],
731 -1, // This should be removed
732 ],
733 ]
734
735 expect(result).toEqual(expectedResult)
736
737 // Verify no new group with total: 0 was created by checking that
738 // we don't have any positive weight entries for category A
739 const positiveCategoryAEntries = result.filter(
740 ([key, , weight]: any) => key[0] === `{"category":"A"}` && weight > 0
741 )
742 expect(positiveCategoryAEntries).toHaveLength(0)
743 })
744
745 test(`complete group removal with multiple aggregates`, () => {
746 const graph = new D2()
747 const input = graph.newInput<{
748 category: string
749 region: string
750 amount: number
751 }>()
752 let latestMessage: any = null
753
754 input.pipe(
755 groupBy(
756 (data) => ({
757 category: data.category,
758 region: data.region,
759 }),
760 {
761 total: sum((data) => data.amount),
762 count: count(),
763 average: avg((data) => data.amount),
764 }
765 ),
766 output((message) => {
767 latestMessage = message
768 })
769 )
770
771 graph.finalize()
772
773 // Initial data
774 input.sendData(
775 new MultiSet([
776 [{ category: `A`, region: `East`, amount: 10 }, 1],
777 [{ category: `A`, region: `East`, amount: 20 }, 1],
778 [{ category: `A`, region: `West`, amount: 30 }, 1],
779 [{ category: `B`, region: `East`, amount: 40 }, 1],
780 ])
781 )
782 graph.run()
783
784 // Verify initial state
785 expect(latestMessage).not.toBeNull()
786 let result = latestMessage.getInner()
787 expect(result).toHaveLength(3) // Should have 3 groups
788
789 // Find the group for category A, region East
790 const categoryAEastGroup = result.find(
791 ([key]: any) => key[0] === `{"category":"A","region":"East"}`
792 )
793 expect(categoryAEastGroup).toBeDefined()
794 expect(categoryAEastGroup[0][1]).toEqual({
795 category: `A`,
796 region: `East`,
797 total: 30, // 10 + 20
798 count: 2,
799 average: 15, // 30 / 2
800 })
801
802 // Now remove ALL records from category A, region East
803 input.sendData(
804 new MultiSet([
805 [{ category: `A`, region: `East`, amount: 10 }, -1],
806 [{ category: `A`, region: `East`, amount: 20 }, -1],
807 ])
808 )
809 graph.run()
810
811 // After removing all A/East records, that group should be completely removed
812 // NOT return a group with total: 0, count: 0, average: 0 (or NaN)
813 result = latestMessage.getInner()
814
815 // The result should contain the removal of the old group
816 const expectedResult = [
817 [
818 [
819 `{"category":"A","region":"East"}`,
820 {
821 category: `A`,
822 region: `East`,
823 total: 30,
824 count: 2,
825 average: 15,
826 },
827 ],
828 -1, // This should be removed
829 ],
830 ]
831
832 expect(result).toEqual(expectedResult)
833
834 // Verify no new group with zero/empty values was created
835 const positiveCategoryAEastEntries = result.filter(
836 ([key, , weight]: any) =>
837 key[0] === `{"category":"A","region":"East"}` && weight > 0
838 )
839 expect(positiveCategoryAEastEntries).toHaveLength(0)
840 })
841
842 test(`group removal and re-addition with sum aggregate`, () => {
843 const graph = new D2()
844 const input = graph.newInput<{
845 category: string
846 amount: number
847 }>()
848 let latestMessage: any = null
849
850 input.pipe(
851 groupBy((data) => ({ category: data.category }), {
852 total: sum((data) => data.amount),
853 }),
854 output((message) => {
855 latestMessage = message
856 })
857 )
858
859 graph.finalize()
860
861 // Step 1: Initial data
862 input.sendData(
863 new MultiSet([
864 [{ category: `A`, amount: 10 }, 1],
865 [{ category: `A`, amount: 20 }, 1],
866 [{ category: `B`, amount: 30 }, 1],
867 ])
868 )
869 graph.run()
870
871 // Verify initial state
872 expect(latestMessage).not.toBeNull()
873 let result = latestMessage.getInner()
874 expect(result).toHaveLength(2) // Should have 2 groups
875
876 // Find the group for category A
877 const categoryAGroup = result.find(
878 ([key]: any) => key[0] === `{"category":"A"}`
879 )
880 expect(categoryAGroup).toBeDefined()
881 expect(categoryAGroup[0][1].total).toBe(30) // Sum of 10 + 20
882
883 // Step 2: Remove ALL records from category A
884 input.sendData(
885 new MultiSet([
886 [{ category: `A`, amount: 10 }, -1],
887 [{ category: `A`, amount: 20 }, -1],
888 ])
889 )
890 graph.run()
891
892 // Verify group A is completely removed
893 result = latestMessage.getInner()
894 const expectedRemovalResult = [
895 [
896 [
897 `{"category":"A"}`,
898 {
899 category: `A`,
900 total: 30,
901 },
902 ],
903 -1, // Group should be removed
904 ],
905 ]
906 expect(result).toEqual(expectedRemovalResult)
907
908 // Step 3: Re-add records to category A with different values
909 input.sendData(
910 new MultiSet([
911 [{ category: `A`, amount: 50 }, 1],
912 [{ category: `A`, amount: 25 }, 1],
913 ])
914 )
915 graph.run()
916
917 // Verify group A is recreated with correct new aggregate values
918 result = latestMessage.getInner()
919 const expectedReAdditionResult = [
920 [
921 [
922 `{"category":"A"}`,
923 {
924 category: `A`,
925 total: 75, // 50 + 25 (new values, not the old 30)
926 },
927 ],
928 1, // New group should be added
929 ],
930 ]
931 expect(result).toEqual(expectedReAdditionResult)
932
933 // Step 4: Verify no lingering effects by adding more data
934 input.sendData(new MultiSet([[{ category: `A`, amount: 15 }, 1]]))
935 graph.run()
936
937 // Verify aggregate is updated correctly from the new baseline
938 result = latestMessage.getInner()
939 const expectedUpdateResult = [
940 [
941 [
942 `{"category":"A"}`,
943 {
944 category: `A`,
945 total: 75, // Previous total
946 },
947 ],
948 -1, // Remove old state
949 ],
950 [
951 [
952 `{"category":"A"}`,
953 {
954 category: `A`,
955 total: 90, // 75 + 15
956 },
957 ],
958 1, // Add new state
959 ],
960 ]
961 expect(result).toEqual(expectedUpdateResult)
962 })
963
964 test(`group removal and re-addition with multiple aggregates`, () => {
965 const graph = new D2()
966 const input = graph.newInput<{
967 category: string
968 region: string
969 amount: number
970 }>()
971 let latestMessage: any = null
972
973 input.pipe(
974 groupBy(
975 (data) => ({
976 category: data.category,
977 region: data.region,
978 }),
979 {
980 total: sum((data) => data.amount),
981 count: count(),
982 average: avg((data) => data.amount),
983 minimum: min((data) => data.amount),
984 maximum: max((data) => data.amount),
985 }
986 ),
987 output((message) => {
988 latestMessage = message
989 })
990 )
991
992 graph.finalize()
993
994 // Step 1: Initial data
995 input.sendData(
996 new MultiSet([
997 [{ category: `A`, region: `East`, amount: 10 }, 1],
998 [{ category: `A`, region: `East`, amount: 20 }, 1],
999 [{ category: `A`, region: `East`, amount: 30 }, 1],
1000 [{ category: `B`, region: `West`, amount: 100 }, 1],
1001 ])
1002 )
1003 graph.run()
1004
1005 // Verify initial state
1006 expect(latestMessage).not.toBeNull()
1007 let result = latestMessage.getInner()
1008 expect(result).toHaveLength(2) // Should have 2 groups
1009
1010 // Find the group for category A, region East
1011 const categoryAEastGroup = result.find(
1012 ([key]: any) => key[0] === `{"category":"A","region":"East"}`
1013 )
1014 expect(categoryAEastGroup).toBeDefined()
1015 expect(categoryAEastGroup[0][1]).toEqual({
1016 category: `A`,
1017 region: `East`,
1018 total: 60, // 10 + 20 + 30
1019 count: 3,
1020 average: 20, // 60 / 3
1021 minimum: 10,
1022 maximum: 30,
1023 })
1024
1025 // Step 2: Remove ALL records from category A, region East
1026 input.sendData(
1027 new MultiSet([
1028 [{ category: `A`, region: `East`, amount: 10 }, -1],
1029 [{ category: `A`, region: `East`, amount: 20 }, -1],
1030 [{ category: `A`, region: `East`, amount: 30 }, -1],
1031 ])
1032 )
1033 graph.run()
1034
1035 // Verify group is completely removed
1036 result = latestMessage.getInner()
1037 const expectedRemovalResult = [
1038 [
1039 [
1040 `{"category":"A","region":"East"}`,
1041 {
1042 category: `A`,
1043 region: `East`,
1044 total: 60,
1045 count: 3,
1046 average: 20,
1047 minimum: 10,
1048 maximum: 30,
1049 },
1050 ],
1051 -1, // Group should be removed
1052 ],
1053 ]
1054 expect(result).toEqual(expectedRemovalResult)
1055
1056 // Step 3: Re-add records to category A, region East with completely different values
1057 input.sendData(
1058 new MultiSet([
1059 [{ category: `A`, region: `East`, amount: 5 }, 1],
1060 [{ category: `A`, region: `East`, amount: 15 }, 1],
1061 [{ category: `A`, region: `East`, amount: 40 }, 1],
1062 [{ category: `A`, region: `East`, amount: 40 }, 1], // Duplicate to test aggregates properly
1063 ])
1064 )
1065 graph.run()
1066
1067 // Verify group is recreated with correct new aggregate values
1068 result = latestMessage.getInner()
1069 const expectedReAdditionResult = [
1070 [
1071 [
1072 `{"category":"A","region":"East"}`,
1073 {
1074 category: `A`,
1075 region: `East`,
1076 total: 100, // 5 + 15 + 40 + 40 (completely new calculation)
1077 count: 4,
1078 average: 25, // 100 / 4
1079 minimum: 5, // New minimum
1080 maximum: 40, // New maximum
1081 },
1082 ],
1083 1, // New group should be added
1084 ],
1085 ]
1086 expect(result).toEqual(expectedReAdditionResult)
1087
1088 // Step 4: Remove some records and verify aggregates update correctly
1089 input.sendData(
1090 new MultiSet([
1091 [{ category: `A`, region: `East`, amount: 40 }, -1], // Remove one of the 40s
1092 ])
1093 )
1094 graph.run()
1095
1096 // Verify aggregates are updated correctly from the new baseline
1097 result = latestMessage.getInner()
1098 const expectedPartialRemovalResult = [
1099 [
1100 [
1101 `{"category":"A","region":"East"}`,
1102 {
1103 category: `A`,
1104 region: `East`,
1105 total: 100,
1106 count: 4,
1107 average: 25,
1108 minimum: 5,
1109 maximum: 40,
1110 },
1111 ],
1112 -1, // Remove old state
1113 ],
1114 [
1115 [
1116 `{"category":"A","region":"East"}`,
1117 {
1118 category: `A`,
1119 region: `East`,
1120 total: 60, // 5 + 15 + 40 (one 40 removed)
1121 count: 3,
1122 average: 20, // 60 / 3
1123 minimum: 5, // Still 5
1124 maximum: 40, // Still 40 (one remains)
1125 },
1126 ],
1127 1, // Add new state
1128 ],
1129 ]
1130 expect(result).toEqual(expectedPartialRemovalResult)
1131 })
1132 })
1133})