All files / core/domain QuizSession.ts

100% Statements 23/23
100% Branches 10/10
100% Functions 5/5
100% Lines 23/23

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                        31x 31x 2x     29x                     22x 22x 3x   19x           34x 34x           12x 37x           25x 25x   25x 3x 22x 3x 19x 3x 16x 3x   13x      
import { VocabularyItem } from './VocabularyItem'
 
/**
 * QuizSession Entity
 * Represents the user's current quiz attempt with vocabulary items and progress tracking.
 */
export interface QuizSession {
  /** Array of 5 vocabulary items for the session */
  vocabularyItems: VocabularyItem[]
  /** Current step in the quiz (0-4 for questions, 5 for completed) */
  currentStep: number
  /** Number of correct answers */
  score: number
  /** Array tracking answers: true = correct, false = incorrect, null = not answered */
  answers: (boolean | null)[]
}
 
/**
 * Creates a new quiz session with the provided vocabulary items
 */
export function createQuizSession(vocabularyItems: VocabularyItem[]): QuizSession {
  if (vocabularyItems.length !== 5) {
    throw new Error('Quiz session requires exactly 5 vocabulary items')
  }
 
  return {
    vocabularyItems,
    currentStep: 0,
    score: 0,
    answers: [null, null, null, null, null],
  }
}
 
/**
 * Gets the current question's vocabulary item
 */
export function getCurrentQuestion(session: QuizSession): VocabularyItem | null {
  if (session.currentStep >= session.vocabularyItems.length) {
    return null
  }
  return session.vocabularyItems[session.currentStep]
}
 
/**
 * Checks if the quiz is complete
 */
export function isQuizComplete(session: QuizSession): boolean {
  return session.currentStep >= session.vocabularyItems.length
}
 
/**
 * Calculates the final score percentage
 */
export function calculateScorePercentage(session: QuizSession): number {
  return (session.score / session.vocabularyItems.length) * 100
}
 
/**
 * Gets the result message based on score
 */
export function getResultMessage(session: QuizSession): string {
  const percentage = calculateScorePercentage(session)
  
  if (percentage === 100) {
    return 'Excelente! Pontuação perfeita!'
  } else if (percentage >= 80) {
    return 'Ótimo trabalho! Continue praticando!'
  } else if (percentage >= 60) {
    return 'Bom esforço! Há espaço para melhorias.'
  } else if (percentage >= 40) {
    return 'Continue estudando! A prática leva à perfeição.'
  } else {
    return 'Não desista! Tente novamente para melhorar sua pontuação.'
  }
}