fix: close expired test attempts at the attempt limit

This commit is contained in:
Jakub Zych
2026-11-09 15:30:51 +01:00
parent 9c29bfd9cf
commit ef38b81fd9
2 changed files with 40 additions and 1 deletions

View File

@@ -286,6 +286,38 @@ VALUES ('Stary', 'stary', true, 50, false, NOW(), NOW())`).Error)
}
})
t.Run("the last expired attempt is closed at the limit", func(t *testing.T) {
f := seedQuiz(t, gdb, "test-wygasly-limit", test(func(q *models.Quiz) {
q.TimeLimitSeconds = intPtr(60)
q.MaxAttempts = intPtr(1)
}))
u, _ := newUser(t, gdb, "lazy-limit@example.com")
t0 := time.Now().UTC().Truncate(time.Second)
clock := t0
svc := &classes.Service{DB: gdb, Now: func() time.Time { return clock }, Grace: 5 * time.Second}
st, err := svc.Start(t.Context(), u.ID, "test-wygasly-limit", "")
must(t, err)
clock = t0.Add(10 * time.Second)
_, err = svc.Answer(t.Context(), u.ID, st.Attempt.ID, classes.AnswerInput{QuestionID: f.single.ID, AnswerIDs: []uint{f.singleRight.ID}})
must(t, err)
clock = t0.Add(70 * time.Second)
if _, err := svc.Start(t.Context(), u.ID, "test-wygasly-limit", ""); !errors.Is(err, classes.ErrAttemptLimit) {
t.Errorf("start after the last attempt expired = %v, want ErrAttemptLimit", err)
}
var old models.Attempt
must(t, gdb.Take(&old, st.Attempt.ID).Error)
if old.CompletedAt == nil || !old.CompletedAt.Equal(t0.Add(60*time.Second)) {
t.Errorf("expired attempt completed_at = %v, want %v (closed despite the limit)", old.CompletedAt, t0.Add(60*time.Second))
}
if old.ScorePoints != 1 || old.MaxPoints != 4 {
t.Errorf("expired attempt score = %v / %d, want 1 / 4", old.ScorePoints, old.MaxPoints)
}
if n := countAttempts(t, f.quiz.ID, u.ID); n != 1 {
t.Errorf("attempts after the refused start = %d, want 1", n)
}
})
t.Run("a shuffled test keeps its order", func(t *testing.T) {
c := &client{t: t, h: h}
seedQuiz(t, gdb, "test-losowy", test(func(q *models.Quiz) { q.ShuffleQuestions = true }))