From 0c594128bf62d97f9573a3d31352fa803fed2f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 22 Jul 2026 06:58:24 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(exams):=20report=20why=20syn?= =?UTF-8?q?c=20rejects=20a=20duration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The duration-vs-window check ran before the mode match, so a sync exam sent with an over-long duration_ms was told "must fit within the starts_at..ends_at window" — advice that would not have fixed the request, since a sync exam takes no duration at all whatever its size. Moving the check below the match lets the root cause answer first. Only async can reach it with a duration to compare, so nothing else changes: same inclusive bound, same duration_ms field, same reason string, same 400. The sync case in schedule_invariants_hold now pins which message wins instead of only asserting is_err. --- src/domain/exam.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/domain/exam.rs b/src/domain/exam.rs index 56b368a..6029c3e 100644 --- a/src/domain/exam.rs +++ b/src/domain/exam.rs @@ -213,17 +213,6 @@ impl ExamSchedule { if ends_at <= starts_at { return Err(invalid("ends_at", "must be after starts_at")); } - if let (Some(duration), Some(starts), Some(ends)) = - (duration_ms, starts_at, ends_at) - { - let window_ms = ends.as_millis().saturating_sub(starts.as_millis()); - if duration.as_millis() > window_ms { - return Err(invalid( - "duration_ms", - "must fit within the starts_at..ends_at window", - )); - } - } match (m.as_str(), duration_ms.is_some()) { ("async", false) => { return Err(invalid("duration_ms", "required for an async exam")); @@ -236,6 +225,21 @@ impl ExamSchedule { } _ => {} } + // Below the match on purpose: a `sync` exam carrying a + // duration is answered by the match's root cause (it takes no + // duration at all), not by how the duration compares to the + // window. Only `async` reaches here with one to compare. + if let (Some(duration), Some(starts), Some(ends)) = + (duration_ms, starts_at, ends_at) + { + let window_ms = ends.as_millis().saturating_sub(starts.as_millis()); + if duration.as_millis() > window_ms { + return Err(invalid( + "duration_ms", + "must fit within the starts_at..ends_at window", + )); + } + } } } Ok(Self { @@ -563,7 +567,15 @@ mod tests { assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), None).is_ok()); assert!(ExamSchedule::try_new(mode("sync"), None, at(2), None).is_err()); assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err()); - assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), dur).is_err()); + // Over-long `dur` in a 1ms window, but the root cause is that sync + // takes no duration at all — that message wins over the window one. + assert!(matches!( + ExamSchedule::try_new(mode("sync"), at(1), at(2), dur), + Err(ValidationError::Invalid { + reason: "only async and open exams take a duration", + .. + }) + )); // Async: window plus a per-student duration, which must fit inside it // (equal to the window is fine; the window `at(1), at(2)` above is