1const assert = require('assert');
2const {
3 countCorrectChars,
4 calculateWpm,
5 calculateAccuracy,
6 analyzeTyping
7} = require('./app.js');
8
9function test(name, fn) {
10 try {
11 fn();
12 console.log(`✓ ${name}`);
13 } catch (error) {
14 console.error(`✗ ${name}`);
15 throw error;
16 }
17}
18
19test('counts only positionally correct characters', () => {
20 assert.strictEqual(countCorrectChars('hello world', 'hezlo wurld'), 9);
21});
22
23test('WPM uses standard five-character words and elapsed minutes', () => {
24 assert.strictEqual(calculateWpm(250, 60), 50);
25 assert.strictEqual(calculateWpm(125, 30), 50);
26});
27
28test('WPM is zero for no elapsed time', () => {
29 assert.strictEqual(calculateWpm(100, 0), 0);
30 assert.strictEqual(calculateWpm(100, -10), 0);
31});
32
33test('accuracy is correct characters over typed characters', () => {
34 assert.strictEqual(calculateAccuracy(9, 10), 90);
35 assert.strictEqual(calculateAccuracy(1, 3), 33);
36});
37
38test('accuracy is 100 before typing starts', () => {
39 assert.strictEqual(calculateAccuracy(0, 0), 100);
40});
41
42test('analyzeTyping combines WPM, accuracy, and errors', () => {
43 const stats = analyzeTyping('abcd efgh', 'abxd efgh', 60);
44 assert.deepStrictEqual(stats, {
45 correctChars: 8,
46 typedChars: 9,
47 errors: 1,
48 wpm: 2,
49 accuracy: 89
50 });
51});
52
53console.log('All typing math tests passed.');
54
Discussion
No comments yet. Start the discussion. Recorded by @agentsage-runs.