Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

util.generation.test.ts 3.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  1. import { retryWithDeduplication, sampleArray } from '../src/util/generation';
  2. jest.mock('../src/logger');
  3. describe('retryWithDeduplication', () => {
  4. it('should collect unique items until target count is reached', async () => {
  5. const operation = jest
  6. .fn()
  7. .mockResolvedValueOnce([1, 2, 3])
  8. .mockResolvedValueOnce([3, 4, 5])
  9. .mockResolvedValueOnce([5, 6, 7]);
  10. const result = await retryWithDeduplication(operation, 5);
  11. expect(result).toEqual([1, 2, 3, 4, 5]);
  12. expect(operation).toHaveBeenCalledTimes(2);
  13. });
  14. it('should stop after max consecutive retries', async () => {
  15. const operation = jest
  16. .fn()
  17. .mockResolvedValueOnce([1, 2])
  18. .mockResolvedValueOnce([1, 2])
  19. .mockResolvedValueOnce([1, 2]);
  20. const result = await retryWithDeduplication(operation, 5, 2);
  21. expect(result).toEqual([1, 2]);
  22. expect(operation).toHaveBeenCalledTimes(4);
  23. });
  24. it('should use custom deduplication function', async () => {
  25. const operation = jest
  26. .fn()
  27. .mockResolvedValueOnce([{ id: 1 }, { id: 2 }])
  28. .mockResolvedValueOnce([{ id: 2 }, { id: 3 }]);
  29. const customDedupFn = (items: { id: number }[]) =>
  30. Array.from(new Set(items.map((item) => item.id))).map((id) => ({ id }));
  31. const result = await retryWithDeduplication(operation, 3, 2, customDedupFn);
  32. expect(result).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]);
  33. expect(operation).toHaveBeenCalledTimes(2);
  34. });
  35. it('should handle empty results from operation', async () => {
  36. const operation = jest
  37. .fn()
  38. .mockResolvedValueOnce([1, 2])
  39. .mockResolvedValueOnce([])
  40. .mockResolvedValueOnce([3]);
  41. const result = await retryWithDeduplication(operation, 3);
  42. expect(result).toEqual([1, 2, 3]);
  43. expect(operation).toHaveBeenCalledTimes(3);
  44. });
  45. it('should return all unique items even if target count is not reached', async () => {
  46. const operation = jest
  47. .fn()
  48. .mockResolvedValueOnce([1, 2])
  49. .mockResolvedValueOnce([2, 3])
  50. .mockResolvedValueOnce([3, 4]);
  51. const result = await retryWithDeduplication(operation, 10, 2);
  52. expect(result).toEqual([1, 2, 3, 4]);
  53. expect(operation).toHaveBeenCalledTimes(6);
  54. });
  55. });
  56. describe('sampleArray', () => {
  57. it('should return n random items when n is less than array length', () => {
  58. const array = [1, 2, 3, 4, 5];
  59. const result = sampleArray(array, 3);
  60. expect(result).toHaveLength(3);
  61. expect(new Set(result).size).toBe(3); // All items are unique
  62. result.forEach((item) => expect(array).toContain(item));
  63. });
  64. it('should return all items when n is equal to array length', () => {
  65. const array = [1, 2, 3, 4, 5];
  66. const result = sampleArray(array, 5);
  67. expect(result).toHaveLength(5);
  68. expect(new Set(result).size).toBe(5);
  69. expect(result).toEqual(expect.arrayContaining(array));
  70. });
  71. it('should return all items when n is greater than array length', () => {
  72. const array = [1, 2, 3];
  73. const result = sampleArray(array, 5);
  74. expect(result).toHaveLength(3);
  75. expect(result).toEqual(expect.arrayContaining(array));
  76. });
  77. it('should return an empty array when input array is empty', () => {
  78. const result = sampleArray([], 3);
  79. expect(result).toEqual([]);
  80. });
  81. it('should return a new array, not modifying the original', () => {
  82. const array = [1, 2, 3, 4, 5];
  83. const originalArray = [...array];
  84. sampleArray(array, 3);
  85. expect(array).toEqual(originalArray);
  86. });
  87. it('should return random samples across multiple calls', () => {
  88. const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  89. const samples = new Set();
  90. for (let i = 0; i < 100; i++) {
  91. const result = sampleArray(array, 5);
  92. samples.add(result.join(','));
  93. }
  94. // With 100 samples, it's extremely unlikely to get the same sample every time
  95. // unless the randomization is not working
  96. expect(samples.size).toBeGreaterThan(1);
  97. });
  98. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...