forge
Explore
Log in

Don't you dare go Hollow

Report why sync rejects a duration

Browse files at this commit

0c594128bf62d97f9573a3d31352fa803fed2f4dAhmet Kaan GümüşParent: ea17828
  • fix
    • report why sync rejects a duration
Show full message
🐛 fix(exams): report why sync rejects a duration

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.rsmodified+24 -12
@@ -213,17 +213,6 @@ impl ExamSchedule {
213 if ends_at <= starts_at {213 if ends_at <= starts_at {
214 return Err(invalid("ends_at", "must be after starts_at"));214 return Err(invalid("ends_at", "must be after starts_at"));
215 }215 }
216- if let (Some(duration), Some(starts), Some(ends)) =
217- (duration_ms, starts_at, ends_at)
218- {
219- let window_ms = ends.as_millis().saturating_sub(starts.as_millis());
220- if duration.as_millis() > window_ms {
221- return Err(invalid(
222- "duration_ms",
223- "must fit within the starts_at..ends_at window",
224- ));
225- }
226- }
227 match (m.as_str(), duration_ms.is_some()) {216 match (m.as_str(), duration_ms.is_some()) {
228 ("async", false) => {217 ("async", false) => {
229 return Err(invalid("duration_ms", "required for an async exam"));218 return Err(invalid("duration_ms", "required for an async exam"));
@@ -236,6 +225,21 @@ impl ExamSchedule {
236 }225 }
237 _ => {}226 _ => {}
238 }227 }
228+ // Below the match on purpose: a `sync` exam carrying a
229+ // duration is answered by the match's root cause (it takes no
230+ // duration at all), not by how the duration compares to the
231+ // window. Only `async` reaches here with one to compare.
232+ if let (Some(duration), Some(starts), Some(ends)) =
233+ (duration_ms, starts_at, ends_at)
234+ {
235+ let window_ms = ends.as_millis().saturating_sub(starts.as_millis());
236+ if duration.as_millis() > window_ms {
237+ return Err(invalid(
238+ "duration_ms",
239+ "must fit within the starts_at..ends_at window",
240+ ));
241+ }
242+ }
239 }243 }
240 }244 }
241 Ok(Self {245 Ok(Self {
@@ -563,7 +567,15 @@ mod tests {
563 assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), None).is_ok());567 assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), None).is_ok());
564 assert!(ExamSchedule::try_new(mode("sync"), None, at(2), None).is_err());568 assert!(ExamSchedule::try_new(mode("sync"), None, at(2), None).is_err());
565 assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err());569 assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err());
566- assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), dur).is_err());570+ // Over-long `dur` in a 1ms window, but the root cause is that sync
571+ // takes no duration at all — that message wins over the window one.
572+ assert!(matches!(
573+ ExamSchedule::try_new(mode("sync"), at(1), at(2), dur),
574+ Err(ValidationError::Invalid {
575+ reason: "only async and open exams take a duration",
576+ ..
577+ })
578+ ));
567 579
568 // Async: window plus a per-student duration, which must fit inside it580 // Async: window plus a per-student duration, which must fit inside it
569 // (equal to the window is fine; the window `at(1), at(2)` above is581 // (equal to the window is fine; the window `at(1), at(2)` above is