Skip to content

[REQ-001] Add currentStreak and bestStreak pure functions with unit tests #1

@markoub

Description

@markoub

User story

As a user who tracks habits, I want the app to compute my current and best streaks so that I can see my momentum at a glance.

Acceptance criteria

  • currentStreak(habit: Habit, today: string): number is added to src/habits.ts.
    • Returns the count of consecutive completed days ending on today.
    • If today is not completed but yesterday (today − 1 day) is, the streak still counts up through yesterday.
    • Returns 0 when there is no active streak (no completion on today or yesterday).
  • bestStreak(habit: Habit): number is added to src/habits.ts.
    • Returns the length of the longest run of consecutive completed days ever recorded.
    • Returns 0 for a habit with no completions.
  • Both functions are pure — they accept today as a YYYY-MM-DD string parameter; they never read Date, localStorage, or the DOM.
  • Unit tests in src/habits.test.ts cover all of the following cases for each function:
    • Empty habit (no completions) → 0.
    • Single completed day (today) → 1.
    • Single completed day (yesterday only, today not done) → currentStreak returns 1; bestStreak returns 1.
    • Streak broken in the middle → correct count before and after the gap.
    • Active streak of several days ending today.
    • Best streak is a past run, not the current run.
  • npm test and npm run build both pass with no new TypeScript errors.

Definition of done

npm test and npm run build pass; all new logic lives in src/habits.ts (pure, no DOM/localStorage/window); every new branch is covered by a test in src/habits.test.ts.

Source: REQ-001 (vault/02 - Requirements/REQ-001 - Streaks and momentum.md)

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentCreated/handled by the autonomous pipeline

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions