-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-suite.html
More file actions
496 lines (438 loc) · 20.9 KB
/
Copy pathtest-suite.html
File metadata and controls
496 lines (438 loc) · 20.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spliterator - Test Suite</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background-color: #f5f5f7;
padding: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1 {
font-size: 2rem;
margin-bottom: 1rem;
color: #1d1d1f;
}
.test-section {
background: white;
border-radius: 12px;
padding: 1.5rem;
margin-bottom: 1.5rem;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.test-section h2 {
font-size: 1.5rem;
margin-bottom: 1rem;
color: #1d1d1f;
}
.test-case {
padding: 0.75rem;
margin-bottom: 0.5rem;
border-left: 4px solid #ccc;
background: #f9f9f9;
}
.test-case.pass {
border-left-color: #34c759;
background: #f0fdf4;
}
.test-case.fail {
border-left-color: #ff3b30;
background: #fef2f2;
}
.test-case.running {
border-left-color: #007aff;
background: #eff6ff;
}
.test-name {
font-weight: 600;
margin-bottom: 0.25rem;
}
.test-result {
font-size: 0.875rem;
color: #666;
}
.test-error {
color: #ff3b30;
font-family: monospace;
font-size: 0.875rem;
margin-top: 0.5rem;
padding: 0.5rem;
background: #fff;
border-radius: 4px;
}
.summary {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-bottom: 2rem;
}
.summary-card {
background: white;
padding: 1.5rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.summary-value {
font-size: 2rem;
font-weight: 700;
margin-bottom: 0.25rem;
}
.summary-label {
font-size: 0.875rem;
color: #666;
text-transform: uppercase;
}
.run-button {
background: #007aff;
color: white;
border: none;
padding: 1rem 2rem;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
margin-bottom: 2rem;
}
.run-button:hover {
background: #0051d5;
}
.run-button:disabled {
background: #ccc;
cursor: not-allowed;
}
</style>
</head>
<body>
<div class="container">
<h1>Spliterator - Test Suite</h1>
<button id="runTests" class="run-button">Run All Tests</button>
<div class="summary">
<div class="summary-card">
<div class="summary-value" id="totalTests">0</div>
<div class="summary-label">Total Tests</div>
</div>
<div class="summary-card">
<div class="summary-value" id="passedTests" style="color: #34c759;">0</div>
<div class="summary-label">Passed</div>
</div>
<div class="summary-card">
<div class="summary-value" id="failedTests" style="color: #ff3b30;">0</div>
<div class="summary-label">Failed</div>
</div>
<div class="summary-card">
<div class="summary-value" id="duration">0ms</div>
<div class="summary-label">Duration</div>
</div>
</div>
<div id="testResults"></div>
</div>
<script src="split-engine.js"></script>
<script>
// Test Suite Framework
const TestRunner = (() => {
let tests = [];
let results = {
total: 0,
passed: 0,
failed: 0,
duration: 0
};
function describe(sectionName, testFn) {
tests.push({ section: sectionName, cases: [] });
testFn();
}
function it(testName, testFn) {
const currentSection = tests[tests.length - 1];
currentSection.cases.push({ name: testName, fn: testFn });
}
async function runAll() {
const startTime = performance.now();
results = { total: 0, passed: 0, failed: 0, duration: 0 };
const container = document.getElementById('testResults');
container.innerHTML = '';
for (const section of tests) {
const sectionDiv = createSection(section.section);
container.appendChild(sectionDiv);
for (const testCase of section.cases) {
results.total++;
const testDiv = createTestCase(testCase.name);
sectionDiv.appendChild(testDiv);
try {
await testCase.fn();
markTestPass(testDiv, 'Test passed');
results.passed++;
} catch (error) {
markTestFail(testDiv, error.message);
results.failed++;
}
updateSummary();
}
}
results.duration = Math.round(performance.now() - startTime);
updateSummary();
}
function createSection(name) {
const div = document.createElement('div');
div.className = 'test-section';
div.innerHTML = `<h2>${name}</h2>`;
return div;
}
function createTestCase(name) {
const div = document.createElement('div');
div.className = 'test-case running';
div.innerHTML = `<div class="test-name">${name}</div><div class="test-result">Running...</div>`;
return div;
}
function markTestPass(testDiv, message) {
testDiv.className = 'test-case pass';
testDiv.querySelector('.test-result').textContent = message;
}
function markTestFail(testDiv, error) {
testDiv.className = 'test-case fail';
testDiv.querySelector('.test-result').textContent = 'Failed';
const errorDiv = document.createElement('div');
errorDiv.className = 'test-error';
errorDiv.textContent = error;
testDiv.appendChild(errorDiv);
}
function updateSummary() {
document.getElementById('totalTests').textContent = results.total;
document.getElementById('passedTests').textContent = results.passed;
document.getElementById('failedTests').textContent = results.failed;
document.getElementById('duration').textContent = `${results.duration}ms`;
}
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
function assertEqual(actual, expected, message) {
if (actual !== expected) {
throw new Error(message || `Expected ${expected}, got ${actual}`);
}
}
function assertApprox(actual, expected, tolerance, message) {
if (Math.abs(actual - expected) > tolerance) {
throw new Error(message || `Expected ~${expected}, got ${actual} (tolerance: ${tolerance})`);
}
}
return { describe, it, runAll, assert, assertEqual, assertApprox };
})();
const { describe, it, assert, assertEqual, assertApprox } = TestRunner;
// Split Calculation Tests
describe('Split Calculation Engine', () => {
it('should calculate correct lap count for 1500m on 400m track', () => {
const config = SplitEngine.calculateRaceConfig(400, '1500m', 270); // 4:30
assertEqual(config.totalLaps, 4, 'Should have 4 laps');
assertEqual(config.firstLapDistance, 300, 'First lap should be 300m');
});
it('should calculate correct lap count for 800m on 200m track', () => {
const config = SplitEngine.calculateRaceConfig(200, '800m', 120); // 2:00
assertEqual(config.totalLaps, 4, 'Should have 4 laps');
assertEqual(config.firstLapDistance, 200, 'First lap should be 200m');
});
it('should calculate even splits correctly for 1600m', () => {
const config = SplitEngine.calculateRaceConfig(400, '1600m', 300); // 5:00
const expectedSplit = 75; // 75 seconds per 400m
assertApprox(config.evenSplitPerLap, expectedSplit, 0.1, 'Even split should be 75s per lap');
});
it('should handle partial first lap for 1500m correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '1500m', 270);
const firstLapTarget = config.laps[0].targetSplit;
const expectedFirstLap = (270 / 1500) * 300; // Proportional to 300m
assertApprox(firstLapTarget, expectedFirstLap, 0.1, 'First lap target should match 300m proportion');
});
it('should calculate cumulative times correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
assertEqual(config.laps[0].targetCumulative, 60, 'First lap cumulative should be 60s');
assertEqual(config.laps[1].targetCumulative, 120, 'Second lap cumulative should be 120s');
});
});
// Lap Recording Tests
describe('Lap Recording', () => {
it('should record first lap split correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
const result = SplitEngine.recordLapSplit(config, 1, 59.5);
assertApprox(result.actualSplit, 59.5, 0.01, 'Actual split should be 59.5');
assertEqual(result.difference, -1, 'Difference should be -1 (rounded)');
});
it('should calculate split times from cumulative correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 60);
const result = SplitEngine.recordLapSplit(config, 2, 122);
assertApprox(result.actualSplit, 62, 0.01, 'Second lap split should be 62');
});
it('should identify fastest lap correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 62);
SplitEngine.recordLapSplit(config, 2, 120);
assert(config.laps[1].isFastest, 'Second lap should be fastest');
});
it('should identify slowest lap correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 62);
SplitEngine.recordLapSplit(config, 2, 124);
assert(config.laps[0].isSlowest, 'First lap should be slowest');
});
});
// Undo Functionality Tests
describe('Undo Functionality', () => {
it('should undo last lap correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 60);
const undone = SplitEngine.undoLastLap(config);
assertEqual(undone, 1, 'Should return lap number 1');
assertEqual(config.laps[0].actualSplit, null, 'Lap 1 split should be null after undo');
});
it('should return null when no laps to undo', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
const undone = SplitEngine.undoLastLap(config);
assertEqual(undone, null, 'Should return null when no laps to undo');
});
});
// Race Statistics Tests
describe('Race Statistics', () => {
it('should calculate final time correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 60);
SplitEngine.recordLapSplit(config, 2, 122);
const stats = SplitEngine.calculateRaceStats(config);
assertApprox(stats.finalTime, 122, 0.01, 'Final time should be 122');
});
it('should calculate time difference correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 60);
SplitEngine.recordLapSplit(config, 2, 125);
const stats = SplitEngine.calculateRaceStats(config);
assertApprox(stats.timeDifference, 5, 0.01, 'Time difference should be +5');
});
it('should calculate average lap pace correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
SplitEngine.recordLapSplit(config, 1, 60);
SplitEngine.recordLapSplit(config, 2, 120);
const stats = SplitEngine.calculateRaceStats(config);
assertApprox(stats.avgLapPace, 60, 0.1, 'Average lap pace should be 60s');
});
it('should calculate average mile pace correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, '1600m', 300);
config.laps.forEach((lap, i) => {
SplitEngine.recordLapSplit(config, i + 1, lap.targetCumulative);
});
const stats = SplitEngine.calculateRaceStats(config);
const expectedMilePace = (300 / 1600) * 1609.344;
assertApprox(stats.avgMilePace, expectedMilePace, 1, 'Average mile pace should match');
});
});
// Time Formatting Tests
describe('Time Formatting', () => {
it('should format time correctly with decimal', () => {
const formatted = SplitEngine.formatTime(125.5, true);
assertEqual(formatted, '2:05.5', 'Should format as 2:05.5');
});
it('should format time correctly without decimal', () => {
const formatted = SplitEngine.formatTime(125.5, false);
assertEqual(formatted, '2:05', 'Should format as 2:05');
});
it('should handle null values gracefully', () => {
const formatted = SplitEngine.formatTime(null, true);
assertEqual(formatted, '--', 'Should return -- for null');
});
it('should handle undefined values gracefully', () => {
const formatted = SplitEngine.formatTime(undefined, true);
assertEqual(formatted, '--', 'Should return -- for undefined');
});
it('should handle NaN values gracefully', () => {
const formatted = SplitEngine.formatTime(NaN, true);
assertEqual(formatted, '--', 'Should return -- for NaN');
});
it('should parse time correctly', () => {
const seconds = SplitEngine.parseTime(4, 30);
assertEqual(seconds, 270, 'Should parse to 270 seconds');
});
});
// Pace Category Tests
describe('Pace Categorization', () => {
it('should identify fast pace correctly', () => {
const category = SplitEngine.getPaceCategory(-3);
assertEqual(category, 'fast', 'Should be fast');
});
it('should identify target pace correctly', () => {
const category = SplitEngine.getPaceCategory(0);
assertEqual(category, 'target', 'Should be target');
});
it('should identify slow pace correctly', () => {
const category = SplitEngine.getPaceCategory(3);
assertEqual(category, 'slow', 'Should be slow');
});
});
// Performance Tests
describe('Performance Requirements', () => {
it('should calculate race config in under 10ms', () => {
const start = performance.now();
SplitEngine.calculateRaceConfig(400, '1500m', 270);
const duration = performance.now() - start;
assert(duration < 10, `Config calculation took ${duration.toFixed(2)}ms (should be < 10ms)`);
});
it('should record lap split in under 5ms', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 120);
const start = performance.now();
SplitEngine.recordLapSplit(config, 1, 60);
const duration = performance.now() - start;
assert(duration < 5, `Lap recording took ${duration.toFixed(2)}ms (should be < 5ms)`);
});
});
// Edge Cases
describe('Edge Cases', () => {
it('should handle Mile distance correctly', () => {
const config = SplitEngine.calculateRaceConfig(400, 'Mile', 240);
assertApprox(config.raceDistance, 1609.344, 0.01, 'Mile should be 1609.344m');
});
it('should handle 10000m with many laps', () => {
const config = SplitEngine.calculateRaceConfig(400, '10000m', 1800);
assertEqual(config.totalLaps, 25, 'Should have 25 laps');
});
it('should handle very fast goal times', () => {
const config = SplitEngine.calculateRaceConfig(400, '800m', 110);
assert(config.evenSplitPerLap < 60, 'Should handle fast splits');
});
it('should not have NaN in lap distances (variable shadowing bug fix)', () => {
const config = SplitEngine.calculateRaceConfig(200, 'Mile', 300);
config.laps.forEach((lap, i) => {
assert(!isNaN(lap.distance), `Lap ${i + 1} distance should not be NaN`);
assert(lap.distance > 0, `Lap ${i + 1} distance should be positive`);
});
});
it('should not have NaN in target splits', () => {
const config = SplitEngine.calculateRaceConfig(200, '1500m', 270);
config.laps.forEach((lap, i) => {
assert(!isNaN(lap.targetSplit), `Lap ${i + 1} target split should not be NaN`);
assert(lap.targetSplit > 0, `Lap ${i + 1} target split should be positive`);
});
});
it('should calculate correct lap distances for 200m track Mile race', () => {
const config = SplitEngine.calculateRaceConfig(200, 'Mile', 300);
// First lap should be 209.344m (200 + 9.344)
assertApprox(config.laps[0].distance, 209.344, 0.01, 'First lap should be 209.344m');
// Middle laps should be 200m
assertEqual(config.laps[1].distance, 200, 'Middle laps should be 200m');
// Last lap should handle remainder
assert(config.laps[config.totalLaps - 1].distance > 0, 'Last lap distance should be positive');
});
});
// Initialize
document.getElementById('runTests').addEventListener('click', async () => {
const button = document.getElementById('runTests');
button.disabled = true;
button.textContent = 'Running Tests...';
await TestRunner.runAll();
button.disabled = false;
button.textContent = 'Run All Tests';
});
</script>
</body>
</html>