test(03-02): add failing tests for word mode typing engine

- Tests for createWordExerciseState, getCurrentWordIndex
- Backwards compatibility test for createExerciseState mode field
- handleKeyPress test with word-mode state

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 13:45:18 +02:00
co-authored by Claude Opus 4.6
parent 0bda51df7a
commit 958f4460a7
+82
View File
@@ -2,6 +2,8 @@ import { describe, it, expect } from "vitest";
import { import {
generateLetters, generateLetters,
createExerciseState, createExerciseState,
createWordExerciseState,
getCurrentWordIndex,
handleKeyPress, handleKeyPress,
getCurrentLetter, getCurrentLetter,
isExerciseComplete, isExerciseComplete,
@@ -172,3 +174,83 @@ describe("isExerciseComplete", () => {
expect(isExerciseComplete(state)).toBe(true); expect(isExerciseComplete(state)).toBe(true);
}); });
}); });
describe("createExerciseState backwards compatibility", () => {
it("sets mode to letters with no words/wordBoundaries", () => {
const state = createExerciseState(1);
expect(state.mode).toBe("letters");
expect(state.words).toBeUndefined();
expect(state.wordBoundaries).toBeUndefined();
});
});
describe("createWordExerciseState", () => {
it("creates state with letters from words joined", () => {
const state = createWordExerciseState(["eis", "die"]);
expect(state.letters).toEqual(["e", "i", "s", "d", "i", "e"]);
});
it("stores words and wordBoundaries in state", () => {
const state = createWordExerciseState(["eis", "die"]);
expect(state.words).toEqual(["eis", "die"]);
expect(state.wordBoundaries).toEqual([0, 3]);
});
it("sets mode to words", () => {
const state = createWordExerciseState(["eis"]);
expect(state.mode).toBe("words");
});
it("sets intervalMs to 0 for word mode", () => {
const state = createWordExerciseState(["eis"]);
expect(state.intervalMs).toBe(0);
});
it("starts with 0 correct, 0 errors, 0 streak, index 0", () => {
const state = createWordExerciseState(["eis"]);
expect(state.currentLetterIndex).toBe(0);
expect(state.totalCorrect).toBe(0);
expect(state.totalErrors).toBe(0);
expect(state.correctStreak).toBe(0);
});
});
describe("handleKeyPress with word-mode state", () => {
it("works the same way for word-mode state", () => {
const state = createWordExerciseState(["eis"]);
const result1 = handleKeyPress(state, "e");
expect(result1.correct).toBe(true);
expect(state.currentLetterIndex).toBe(1);
const result2 = handleKeyPress(state, "x");
expect(result2.correct).toBe(false);
expect(state.currentLetterIndex).toBe(1);
handleKeyPress(state, "i");
const result3 = handleKeyPress(state, "s");
expect(result3.exerciseComplete).toBe(true);
});
});
describe("getCurrentWordIndex", () => {
it("returns which word the letter belongs to", () => {
const state = createWordExerciseState(["eis", "die"]);
// Index 0 -> word 0 ("eis")
state.currentLetterIndex = 0;
expect(getCurrentWordIndex(state)).toBe(0);
// Index 2 -> word 0 ("eis")
state.currentLetterIndex = 2;
expect(getCurrentWordIndex(state)).toBe(0);
// Index 3 -> word 1 ("die")
state.currentLetterIndex = 3;
expect(getCurrentWordIndex(state)).toBe(1);
// Index 5 -> word 1 ("die")
state.currentLetterIndex = 5;
expect(getCurrentWordIndex(state)).toBe(1);
});
it("returns 0 for letters-mode state (no boundaries)", () => {
const state = createExerciseState(1);
expect(getCurrentWordIndex(state)).toBe(0);
});
});