Skip to content

Commit

Permalink
Fix interval logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 17, 2024
1 parent cadf45f commit 703439a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion backend/pkg/usecases/swipe_manager/interval_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ func (il *intervalLogic) UpdateInterval(card *repository.Card, swipe *repository
card.ReviewDate = time.Now().AddDate(0, 0, card.IntervalDays)
}

// increaseInterval advances the current interval to the next step.
func (il *intervalLogic) increaseInterval(card *repository.Card) {
currentIndex := il.findIntervalIndex(card.IntervalDays)

// Check if the current interval is out of the expected range and reset if necessary
if currentIndex == 0 && card.IntervalDays != il.intervals[0] {
card.IntervalDays = il.intervals[0]
return
}

if currentIndex < len(il.intervals)-1 {
card.IntervalDays = il.intervals[currentIndex+1]
} else {
Expand Down
10 changes: 10 additions & 0 deletions backend/pkg/usecases/swipe_manager/interval_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestIncreaseInterval(t *testing.T) {
il := NewIntervalLogic().(*intervalLogic)

t.Run("Normal Range Value 1 to 3", func(t *testing.T) {
t.Parallel()
card := &repository.Card{IntervalDays: 1}
il.increaseInterval(card)
if card.IntervalDays != 3 {
Expand All @@ -19,6 +20,7 @@ func TestIncreaseInterval(t *testing.T) {
})

t.Run("Normal Range Value 3 to 7", func(t *testing.T) {
t.Parallel()
card := &repository.Card{IntervalDays: 3}
il.increaseInterval(card)
if card.IntervalDays != 7 {
Expand All @@ -27,6 +29,7 @@ func TestIncreaseInterval(t *testing.T) {
})

t.Run("Edge Case: Max Interval", func(t *testing.T) {
t.Parallel()
card := &repository.Card{IntervalDays: 30}
il.increaseInterval(card)
if card.IntervalDays != 30 {
Expand All @@ -35,6 +38,7 @@ func TestIncreaseInterval(t *testing.T) {
})

t.Run("Abnormal Range Value", func(t *testing.T) {
t.Parallel()
card := &repository.Card{IntervalDays: 100}
il.increaseInterval(card)
if card.IntervalDays != 1 {
Expand All @@ -48,6 +52,7 @@ func TestUpdateInterval(t *testing.T) {
card := &repository.Card{IntervalDays: 1}

t.Run("Normal Value with Direction Known", func(t *testing.T) {
t.Parallel()
swipe := &repository.SwipeRecord{Direction: services.KNOWN}
il.UpdateInterval(card, swipe)
expectedDate := time.Now().AddDate(0, 0, 3) // because interval should increase to 3
Expand All @@ -57,6 +62,7 @@ func TestUpdateInterval(t *testing.T) {
})

t.Run("Reset Interval with Unknown Direction", func(t *testing.T) {
t.Parallel()
swipe := &repository.SwipeRecord{Direction: services.DONTKNOW}
il.UpdateInterval(card, swipe)
expectedDate := time.Now().AddDate(0, 0, 1) // reset to 1 day
Expand All @@ -66,6 +72,7 @@ func TestUpdateInterval(t *testing.T) {
})

t.Run("Edge Case with Maxed Out Interval", func(t *testing.T) {
t.Parallel()
card.IntervalDays = 30
swipe := &repository.SwipeRecord{Direction: services.KNOWN}
il.UpdateInterval(card, swipe)
Expand All @@ -80,20 +87,23 @@ func TestFindIntervalIndex(t *testing.T) {
il := NewIntervalLogic().(*intervalLogic)

t.Run("Find Index for Interval 1", func(t *testing.T) {
t.Parallel()
index := il.findIntervalIndex(1)
if index != 0 {
t.Errorf("Expected index to be 0, got %d", index)
}
})

t.Run("Find Index for Interval 7", func(t *testing.T) {
t.Parallel()
index := il.findIntervalIndex(7)
if index != 2 {
t.Errorf("Expected index to be 2, got %d", index)
}
})

t.Run("Find Index for Abnormal Interval", func(t *testing.T) {
t.Parallel()
index := il.findIntervalIndex(100)
if index != 0 {
t.Errorf("Expected index to be 0 for abnormal value, got %d", index)
Expand Down

0 comments on commit 703439a

Please sign in to comment.