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

evaluator.progress.test.ts 5.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  1. import type { RunEvalOptions } from '../src/types';
  2. // Mock dependencies
  3. jest.mock('cli-progress', () => {
  4. const mockBar = {
  5. increment: jest.fn(),
  6. update: jest.fn(),
  7. getTotal: jest.fn().mockImplementation(function (this: any) {
  8. return this._total || 10;
  9. }),
  10. };
  11. return {
  12. default: {
  13. MultiBar: jest.fn().mockImplementation(() => ({
  14. create: jest.fn().mockImplementation((total: number) => {
  15. const bar = { ...mockBar, _total: total };
  16. return bar;
  17. }),
  18. stop: jest.fn(),
  19. })),
  20. Presets: {
  21. shades_classic: {},
  22. },
  23. },
  24. };
  25. });
  26. jest.mock('../src/logger', () => ({
  27. __esModule: true,
  28. default: {
  29. warn: jest.fn(),
  30. debug: jest.fn(),
  31. info: jest.fn(),
  32. error: jest.fn(),
  33. },
  34. logger: {
  35. warn: jest.fn(),
  36. debug: jest.fn(),
  37. info: jest.fn(),
  38. error: jest.fn(),
  39. },
  40. setLogLevel: jest.fn(),
  41. }));
  42. // Import after mocking - we need to extract ProgressBarManager from evaluator
  43. // Since it's a private class, we'll test it through its usage patterns
  44. import { calculateThreadsPerBar } from '../src/evaluator';
  45. describe('Progress Bar Management', () => {
  46. afterEach(() => {
  47. jest.clearAllMocks();
  48. });
  49. describe('calculateThreadsPerBar', () => {
  50. it('should distribute threads evenly when divisible', () => {
  51. expect(calculateThreadsPerBar(12, 4, 0)).toBe(3);
  52. expect(calculateThreadsPerBar(12, 4, 1)).toBe(3);
  53. expect(calculateThreadsPerBar(12, 4, 2)).toBe(3);
  54. expect(calculateThreadsPerBar(12, 4, 3)).toBe(3);
  55. });
  56. it('should distribute extra threads to early bars', () => {
  57. expect(calculateThreadsPerBar(13, 4, 0)).toBe(4); // Gets extra thread
  58. expect(calculateThreadsPerBar(13, 4, 1)).toBe(3);
  59. expect(calculateThreadsPerBar(13, 4, 2)).toBe(3);
  60. expect(calculateThreadsPerBar(13, 4, 3)).toBe(3);
  61. });
  62. it('should handle concurrency less than bars', () => {
  63. expect(calculateThreadsPerBar(2, 4, 0)).toBe(1);
  64. expect(calculateThreadsPerBar(2, 4, 1)).toBe(1);
  65. expect(calculateThreadsPerBar(2, 4, 2)).toBe(0);
  66. expect(calculateThreadsPerBar(2, 4, 3)).toBe(0);
  67. });
  68. it('should handle single thread', () => {
  69. expect(calculateThreadsPerBar(1, 1, 0)).toBe(1);
  70. expect(calculateThreadsPerBar(1, 4, 0)).toBe(1);
  71. expect(calculateThreadsPerBar(1, 4, 1)).toBe(0);
  72. });
  73. });
  74. describe('ProgressBarManager Work Distribution', () => {
  75. it('should correctly separate serial and group (concurrent) tasks', () => {
  76. const runEvalOptions: Partial<RunEvalOptions>[] = [
  77. { test: { options: { runSerially: true } } },
  78. { test: {} },
  79. { test: { options: { runSerially: true } } },
  80. { test: {} },
  81. { test: {} },
  82. ];
  83. let serialCount = 0;
  84. let groupCount = 0;
  85. for (const option of runEvalOptions) {
  86. if (option.test?.options?.runSerially) {
  87. serialCount++;
  88. } else {
  89. groupCount++;
  90. }
  91. }
  92. expect(serialCount).toBe(2);
  93. expect(groupCount).toBe(3);
  94. });
  95. it('should map indices correctly to execution contexts', () => {
  96. const indexToContext = new Map();
  97. const runEvalOptions: Partial<RunEvalOptions>[] = [
  98. { test: { options: { runSerially: true } } }, // index 0 -> serial
  99. { test: {} }, // index 1 -> concurrent bar 0
  100. { test: { options: { runSerially: true } } }, // index 2 -> serial
  101. { test: {} }, // index 3 -> concurrent bar 1
  102. { test: {} }, // index 4 -> concurrent bar 2
  103. ];
  104. let concurrentCount = 0;
  105. const concurrency = 3;
  106. const maxBars = Math.min(concurrency, 20);
  107. for (let i = 0; i < runEvalOptions.length; i++) {
  108. const option = runEvalOptions[i];
  109. if (option.test?.options?.runSerially) {
  110. indexToContext.set(i, { phase: 'serial', barIndex: 0 });
  111. } else {
  112. indexToContext.set(i, {
  113. phase: 'concurrent',
  114. barIndex: concurrentCount % maxBars,
  115. });
  116. concurrentCount++;
  117. }
  118. }
  119. expect(indexToContext.get(0)).toEqual({ phase: 'serial', barIndex: 0 });
  120. expect(indexToContext.get(1)).toEqual({ phase: 'concurrent', barIndex: 0 });
  121. expect(indexToContext.get(2)).toEqual({ phase: 'serial', barIndex: 0 });
  122. expect(indexToContext.get(3)).toEqual({ phase: 'concurrent', barIndex: 1 });
  123. expect(indexToContext.get(4)).toEqual({ phase: 'concurrent', barIndex: 2 });
  124. });
  125. it('should calculate correct totals for each progress bar', () => {
  126. const concurrentCount = 10;
  127. const numBars = 3;
  128. const perBar = Math.floor(concurrentCount / numBars);
  129. const remainder = concurrentCount % numBars;
  130. const barTotals = [];
  131. for (let i = 0; i < numBars; i++) {
  132. const total = i < remainder ? perBar + 1 : perBar;
  133. barTotals.push(total);
  134. }
  135. expect(barTotals).toEqual([4, 3, 3]);
  136. expect(barTotals.reduce((a, b) => a + b, 0)).toBe(concurrentCount);
  137. });
  138. it('should handle dynamic comparison bar creation correctly', () => {
  139. // This test validates that comparison bars are created with the correct total
  140. // after we know the actual count of comparisons needed
  141. const cliProgress = require('cli-progress').default;
  142. const mockMultibar = new cliProgress.MultiBar();
  143. // Simulate creating progress bars without knowing comparison count initially
  144. mockMultibar.create(2, 0); // 2 serial tasks
  145. mockMultibar.create(3, 0); // 3 concurrent tasks
  146. // Later, when we know we need 5 comparisons, create the comparison bar
  147. mockMultibar.create(5, 0);
  148. // Verify the create method was called with correct totals
  149. expect(mockMultibar.create).toHaveBeenCalledTimes(3);
  150. expect(mockMultibar.create).toHaveBeenNthCalledWith(1, 2, 0);
  151. expect(mockMultibar.create).toHaveBeenNthCalledWith(2, 3, 0);
  152. expect(mockMultibar.create).toHaveBeenNthCalledWith(3, 5, 0);
  153. });
  154. });
  155. });
Tip!

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

Comments

Loading...