Report why sync rejects a duration
Browse files at this commit0c594128bf62d97f9573a3d31352fa803fed2f4dParent: ea17828- fix
- report why sync rejects a duration
1 changed file +24 -12
- src/domain/exam.rs +24 -12
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 it | 580 | // 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 is | 581 | // (equal to the window is fine; the window `at(1), at(2)` above is |