Skip to content

[Feature]: Decouple and Unit Test the SM-2 Space Repetition Algorithm #1524

Description

@Dev1822

Feature/Project Proposal

Extract the calculateSM2 spaced repetition logic from the flashcard controller into a dedicated service utility, introduce unit tests, and improve timezone handling.

Problem Statement

The SuperMemo-2 (SM-2) algorithm used for flashcard spaced repetition is tightly coupled inside the flashcardController.js file. This mixes business logic with HTTP routing logic, making it difficult to unit test edge cases. Additionally, the calculation const nextDueDate = new Date(now.getTime() + newInterval ...) relies entirely on the server's local time. This can lead to inaccurate daily roll-overs for global users (e.g., a user in Australia might see their due dates roll over at an awkward time of day).

Proposed Solution

Move the calculateSM2 function to a dedicated file like backend/utils/srsAlgorithm.js. Write comprehensive Jest unit tests covering various ratings, repetition states, and fallback edge cases. Update the function signature to accept an optional user timezone parameter to ensure nextDueDate is calculated accurately for the user's local day boundary.

Alternatives Considered

Utilizing an external npm package for the SM-2 algorithm. However, keeping a custom implementation allows maximum flexibility for future tweaks or migrating to newer algorithms (like FSRS) later.

Benefits

Greatly improves code maintainability, ensures mathematical accuracy via automated tests, and paves the way for much better timezone support for international users.

Priority

Low

Additional Context

Located in backend/controllers/flashcardController.js. Currently, there are no tests for this function in the backend/tests directory.

Checklist

  • I have searched existing issues and discussions before creating this request.
  • I am willing to work on this feature if it is approved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions