From 492c4e56f0c882e714a05fa2fefda0473568ff40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20H=C3=BCnniger?= Date: Sun, 21 Jan 2024 13:43:02 +0100 Subject: [PATCH] refactor: adjust all wpm based tests to work properly --- .../suite/keystroke_count_status_bar.test.ts | 2 +- .../suite/words_per_minute_calculator.test.ts | 23 ++++++++++++++----- .../suite/words_per_minute_status_bar.test.ts | 6 ++++- src/test/test_utils.ts | 11 ++++++--- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/test/suite/keystroke_count_status_bar.test.ts b/src/test/suite/keystroke_count_status_bar.test.ts index 138f52e..ebdfdf9 100644 --- a/src/test/suite/keystroke_count_status_bar.test.ts +++ b/src/test/suite/keystroke_count_status_bar.test.ts @@ -32,7 +32,7 @@ suite("KeystrokeCountStatusBar Test Suite", () => { }); test(`For 5 keys pressed the status bar text is set to '${KEYBOARD_ICON} 5'`, () => { - TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", 0), 5); + TestUtils.generateKeystrokesWithIncreasingTimestamps(repository, new Keystroke("a", 0), 5); statusBar.update(); diff --git a/src/test/suite/words_per_minute_calculator.test.ts b/src/test/suite/words_per_minute_calculator.test.ts index bdd8f31..8c0e84a 100644 --- a/src/test/suite/words_per_minute_calculator.test.ts +++ b/src/test/suite/words_per_minute_calculator.test.ts @@ -41,7 +41,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => { const now = Date.now(); const oneMinuteBefore: number = now - MINUTE_AS_MILLISECONDS; - TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", oneMinuteBefore), 300); + TestUtils.generateKeystrokesWithIncreasingTimestamps( + repository, + new Keystroke("a", oneMinuteBefore), + 300 + ); const wpm = calculator.getAverageWordsPerMinute(); assert.strictEqual(Math.round(wpm), 60); @@ -51,7 +55,12 @@ suite("WordsPerMinuteCalculator Test Suite", () => { const now = Date.now(); const twoMinutesBefore: number = now - 2 * MINUTE_AS_MILLISECONDS; - TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", twoMinutesBefore), 600); + TestUtils.generateKeystrokesWithIncreasingTimestamps( + repository, + new Keystroke("a", twoMinutesBefore), + 600, + 2 * MINUTE_AS_MILLISECONDS + ); const wpm = calculator.getAverageWordsPerMinute(); assert.strictEqual(Math.round(wpm), 60); @@ -61,10 +70,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => { const now = Date.now(); const oneAndAHalfMinutesBefore: number = now - 1.5 * MINUTE_AS_MILLISECONDS; - TestUtils.generateIdenticalKeystrokes( + TestUtils.generateKeystrokesWithIncreasingTimestamps( repository, new Keystroke("a", oneAndAHalfMinutesBefore), - 450 + 450, + 1.5 * MINUTE_AS_MILLISECONDS ); const wpm = calculator.getAverageWordsPerMinute(); @@ -75,10 +85,11 @@ suite("WordsPerMinuteCalculator Test Suite", () => { const now = Date.now(); const thirtySecondsBefore: number = now - 0.5 * MINUTE_AS_MILLISECONDS; - TestUtils.generateIdenticalKeystrokes( + TestUtils.generateKeystrokesWithIncreasingTimestamps( repository, new Keystroke("a", thirtySecondsBefore), - 150 + 150, + 0.5 * MINUTE_AS_MILLISECONDS ); const wpm = calculator.getAverageWordsPerMinute(); diff --git a/src/test/suite/words_per_minute_status_bar.test.ts b/src/test/suite/words_per_minute_status_bar.test.ts index 5423a59..443f664 100644 --- a/src/test/suite/words_per_minute_status_bar.test.ts +++ b/src/test/suite/words_per_minute_status_bar.test.ts @@ -39,7 +39,11 @@ suite("WordsPerMinuteStatusBar Test Suite", () => { const now = Date.now(); const oneMinuteBefore: number = now - MINUTE_AS_MILLISECONDS; - TestUtils.generateIdenticalKeystrokes(repository, new Keystroke("a", oneMinuteBefore), 300); + TestUtils.generateKeystrokesWithIncreasingTimestamps( + repository, + new Keystroke("a", oneMinuteBefore), + 300 + ); statusBar.update(); diff --git a/src/test/test_utils.ts b/src/test/test_utils.ts index 8a61e22..7df0ac4 100644 --- a/src/test/test_utils.ts +++ b/src/test/test_utils.ts @@ -3,15 +3,20 @@ import * as path from "path"; import { Keystroke } from "../libs/keystroke"; import { KeystrokeRepository } from "../libs/keystroke_repository"; +import { MINUTE_AS_MILLISECONDS } from "../libs/constants"; export class TestUtils { - public static generateIdenticalKeystrokes( + public static generateKeystrokesWithIncreasingTimestamps( repository: KeystrokeRepository, keystroke: Keystroke, - count: number + count: number, + fullTimeInMilliseconds: number = MINUTE_AS_MILLISECONDS ): void { + let timeIterator = keystroke.timestampInMilliseconds; + for (let i = 0; i < count; i++) { - repository.addKeystroke(keystroke.key, keystroke.timestampInMilliseconds); + repository.addKeystroke(keystroke.key, timeIterator); + timeIterator += fullTimeInMilliseconds / count; } }