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

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

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

Comments

Loading...