diff --git a/src/game/typing.test.ts b/src/game/typing.test.ts index f3680e4..fd3295b 100644 --- a/src/game/typing.test.ts +++ b/src/game/typing.test.ts @@ -2,6 +2,8 @@ import { describe, it, expect } from "vitest"; import { generateLetters, createExerciseState, + createWordExerciseState, + getCurrentWordIndex, handleKeyPress, getCurrentLetter, isExerciseComplete, @@ -172,3 +174,83 @@ describe("isExerciseComplete", () => { 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); + }); +});