All files / core/application/use-cases EvaluateAnswer.ts

100% Statements 9/9
100% Branches 3/3
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                        18x               22x 1x     21x 21x   21x 21x   21x             21x                
import { QuizSession } from '@/core/domain/QuizSession'
 
/**
 * Result of evaluating an answer
 */
export interface EvaluateAnswerResult {
  /** Whether the answer was correct */
  isCorrect: boolean
  /** The correct word for the question */
  correctWord: string
  /** The word the user selected */
  selectedWord: string
  /** Updated quiz session with the new state */
  updatedSession: QuizSession
}
 
/**
 * EvaluateQuizAnswerUseCase
 * Application use case that evaluates user answers and updates the quiz session.
 */
export class EvaluateQuizAnswerUseCase {
  /**
   * Evaluates the user's answer for the current question
   * @param session - Current quiz session
   * @param selectedWord - The English word selected by the user
   * @returns Result containing correctness and updated session
   */
  execute(session: QuizSession, selectedWord: string): EvaluateAnswerResult {
    if (session.currentStep >= session.vocabularyItems.length) {
      throw new Error('Quiz is already complete')
    }
 
    const currentQuestion = session.vocabularyItems[session.currentStep]
    const isCorrect = currentQuestion.word.toLowerCase() === selectedWord.toLowerCase()
 
    const updatedAnswers = [...session.answers]
    updatedAnswers[session.currentStep] = isCorrect
 
    const updatedSession: QuizSession = {
      ...session,
      currentStep: session.currentStep + 1,
      score: isCorrect ? session.score + 1 : session.score,
      answers: updatedAnswers,
    }
 
    return {
      isCorrect,
      correctWord: currentQuestion.word,
      selectedWord,
      updatedSession,
    }
  }
}