diff --git a/src/domain/appointment_slot.rs b/src/domain/appointment_slot.rs index 5dcad37..76ddcfc 100644 --- a/src/domain/appointment_slot.rs +++ b/src/domain/appointment_slot.rs @@ -565,17 +565,23 @@ mod tests { Err(AppError::ConflictOwned(_)) )); // Touching at the boundary (ends where the next starts) → allowed. - assert!(AppointmentSlot::create(&teacher, at(2_000), at(3_000), None, &db) - .await - .is_ok()); - assert!(AppointmentSlot::create(&teacher, at(0), at(1_000), None, &db) - .await - .is_ok()); + assert!( + AppointmentSlot::create(&teacher, at(2_000), at(3_000), None, &db) + .await + .is_ok() + ); + assert!( + AppointmentSlot::create(&teacher, at(0), at(1_000), None, &db) + .await + .is_ok() + ); // A different teacher sharing the same window is fine. let other = UserId::from_key("t2"); - assert!(AppointmentSlot::create(&other, at(1_000), at(2_000), None, &db) - .await - .is_ok()); + assert!( + AppointmentSlot::create(&other, at(1_000), at(2_000), None, &db) + .await + .is_ok() + ); } #[tokio::test] @@ -636,9 +642,11 @@ mod tests { .await, Err(AppError::ConflictOwned(_)) )); - assert!(AppointmentSlot::list_for_teacher(&teacher, &db) - .await - .unwrap() - .is_empty()); + assert!( + AppointmentSlot::list_for_teacher(&teacher, &db) + .await + .unwrap() + .is_empty() + ); } } diff --git a/src/domain/bank_question.rs b/src/domain/bank_question.rs index 6493ed5..c0221af 100644 --- a/src/domain/bank_question.rs +++ b/src/domain/bank_question.rs @@ -191,7 +191,11 @@ impl BankQuestion { /// hold — and a round-trip through `try_new` would mint *new* choice ids, /// detaching the copy's `correct` and option pictures from its choices. pub fn spec(&self) -> QuestionSpec { - QuestionSpec::from_stored(self.kind.clone(), self.choices.clone(), self.correct.clone()) + QuestionSpec::from_stored( + self.kind.clone(), + self.choices.clone(), + self.correct.clone(), + ) } pub async fn create( @@ -248,7 +252,10 @@ impl BankQuestion { created.ok_or_else(|| AppError::Internal("failed to create bank question".into())) } - pub async fn read(id: &BankQuestionId, db: &Database) -> Result, AppError> { + pub async fn read( + id: &BankQuestionId, + db: &Database, + ) -> Result, AppError> { Ok(db.select(id.record()).await?) } @@ -481,8 +488,14 @@ mod tests { QuestionSpec::try_new( QuestionKind::try_new("choice").unwrap(), Some(vec![ - ChoiceInput { id: Some("a".into()), text: "yes".into() }, - ChoiceInput { id: Some("b".into()), text: "no".into() }, + ChoiceInput { + id: Some("a".into()), + text: "yes".into(), + }, + ChoiceInput { + id: Some("b".into()), + text: "no".into(), + }, ]), Some("b".into()), &[], @@ -672,7 +685,10 @@ mod tests { // One statement, so a page costs one round trip — a `;` here would mean // the per-row N+1 crept back in. - assert!(!USAGE_COUNTS_SQL.contains(';'), "usage_counts must be one statement"); + assert!( + !USAGE_COUNTS_SQL.contains(';'), + "usage_counts must be one statement" + ); let db = crate::database::init_mem().await.unwrap(); let owner = UserId::generate(); @@ -725,15 +741,26 @@ mod tests { let counts = BankQuestion::usage_counts(&ids, &db).await.unwrap(); assert_eq!(counts.get(used_twice.get_id().key()).copied(), Some(2)); assert_eq!(counts.get(used_once.get_id().key()).copied(), Some(1)); - assert_eq!(counts.get(unused.get_id().key()), None, "unused stays absent"); + assert_eq!( + counts.get(unused.get_id().key()), + None, + "unused stays absent" + ); assert_eq!(counts.len(), 2); // A template outside the page is never counted into it. - let narrow = BankQuestion::usage_counts(&[used_once.get_id()], &db).await.unwrap(); + let narrow = BankQuestion::usage_counts(&[used_once.get_id()], &db) + .await + .unwrap(); assert_eq!(narrow.len(), 1); assert_eq!(narrow.get(used_once.get_id().key()).copied(), Some(1)); // An empty page asks nothing at all. - assert!(BankQuestion::usage_counts(&[], &db).await.unwrap().is_empty()); + assert!( + BankQuestion::usage_counts(&[], &db) + .await + .unwrap() + .is_empty() + ); } #[test] @@ -796,15 +823,24 @@ mod tests { .unwrap(); assert_eq!(tail[0].get_text().as_str(), "question 0"); // Text filter runs in SQL, case-insensitively, and narrows `total`. - let (hits, total) = BankQuestion::list(None, None, None, None, Some("QUESTION 3"), None, 0, &db) - .await - .unwrap(); - assert_eq!((hits.len(), total), (1, 1)); - // Owner filter with nobody's templates. - let (none, total) = - BankQuestion::list(None, Some(&UserId::generate()), None, None, None, None, 0, &db) + let (hits, total) = + BankQuestion::list(None, None, None, None, Some("QUESTION 3"), None, 0, &db) .await .unwrap(); + assert_eq!((hits.len(), total), (1, 1)); + // Owner filter with nobody's templates. + let (none, total) = BankQuestion::list( + None, + Some(&UserId::generate()), + None, + None, + None, + None, + 0, + &db, + ) + .await + .unwrap(); assert!(none.is_empty()); assert_eq!(total, 0); } @@ -823,15 +859,24 @@ mod tests { ) .await .unwrap(); - for needle in ["istanbul", "İSTANBUL", "İstanbul", "ıstanbul", "ilçe", "ILCE"] { - let (hits, total) = BankQuestion::list(None, None, None, None, Some(needle), None, 0, &db) - .await - .unwrap(); + for needle in [ + "istanbul", + "İSTANBUL", + "İstanbul", + "ıstanbul", + "ilçe", + "ILCE", + ] { + let (hits, total) = + BankQuestion::list(None, None, None, None, Some(needle), None, 0, &db) + .await + .unwrap(); assert_eq!((hits.len(), total), (1, 1), "needle {needle} missed"); } - let (miss, total) = BankQuestion::list(None, None, None, None, Some("ankara"), None, 0, &db) - .await - .unwrap(); + let (miss, total) = + BankQuestion::list(None, None, None, None, Some("ankara"), None, 0, &db) + .await + .unwrap(); assert!(miss.is_empty()); assert_eq!(total, 0); } diff --git a/src/domain/bank_question_image.rs b/src/domain/bank_question_image.rs index 7cc04fc..b583ba0 100644 --- a/src/domain/bank_question_image.rs +++ b/src/domain/bank_question_image.rs @@ -201,7 +201,10 @@ mod tests { Some( ["a", "b", "c"] .iter() - .map(|l| ChoiceInput { id: Some((*l).into()), text: (*l).into() }) + .map(|l| ChoiceInput { + id: Some((*l).into()), + text: (*l).into(), + }) .collect(), ), Some("a".into()), diff --git a/src/domain/chatbot_message.rs b/src/domain/chatbot_message.rs index 9726d91..9d5261a 100644 --- a/src/domain/chatbot_message.rs +++ b/src/domain/chatbot_message.rs @@ -213,7 +213,8 @@ impl ChatbotMessage { } async fn insert(message: ChatbotMessage, db: &Database) -> Result { - let created: Option = db.create(message.id.record()).content(message).await?; + let created: Option = + db.create(message.id.record()).content(message).await?; created.ok_or_else(|| AppError::Internal("failed to create chat message".into())) } @@ -469,9 +470,7 @@ mod tests { .unwrap(); } - let whole = ChatbotMessage::list_for_thread(&thread, &db) - .await - .unwrap(); + let whole = ChatbotMessage::list_for_thread(&thread, &db).await.unwrap(); let tail = ChatbotMessage::list_tail(&thread, TURNS * 2, &db) .await .unwrap(); diff --git a/src/domain/chatbot_thread.rs b/src/domain/chatbot_thread.rs index 14d0f20..b89fd19 100644 --- a/src/domain/chatbot_thread.rs +++ b/src/domain/chatbot_thread.rs @@ -108,10 +108,7 @@ impl ChatbotThread { created_at: now, updated_at: now, }; - let created: Option = db - .create(thread.id.record()) - .content(thread) - .await?; + let created: Option = db.create(thread.id.record()).content(thread).await?; created.ok_or_else(|| AppError::Internal("failed to create thread".into())) } @@ -227,7 +224,8 @@ impl ChatbotThread { .await? .check()?; // BEGIN is slot 0; the thread's own DELETE is slot 2. - let deleted: Option = result.take::>(2)?.into_iter().next(); + let deleted: Option = + result.take::>(2)?.into_iter().next(); deleted.ok_or(AppError::NotFound) } } diff --git a/src/domain/course.rs b/src/domain/course.rs index be5bb43..ff53034 100644 --- a/src/domain/course.rs +++ b/src/domain/course.rs @@ -264,11 +264,7 @@ impl Course { /// so a whole-row save from either side would revert the other. The /// `array::distinct` keeps the assignment idempotent even when two requests /// name the same teacher at once (the early return only sees a stale row). - pub async fn assign_teacher( - self, - teacher: &UserId, - db: &Database, - ) -> Result { + pub async fn assign_teacher(self, teacher: &UserId, db: &Database) -> Result { if self.is_assigned(teacher) { return Ok(self); } @@ -281,7 +277,11 @@ impl Course { .bind(("usr", teacher.record())) .await? .check()?; - result.take::>(0)?.into_iter().next().ok_or(AppError::NotFound) + result + .take::>(0)? + .into_iter() + .next() + .ok_or(AppError::NotFound) } /// Drop `teacher` from this course. `None` when they weren't assigned, so @@ -303,7 +303,11 @@ impl Course { .await? .check()?; Ok(Some( - result.take::>(0)?.into_iter().next().ok_or(AppError::NotFound)?, + result + .take::>(0)? + .into_iter() + .next() + .ok_or(AppError::NotFound)?, )) } diff --git a/src/domain/course_session.rs b/src/domain/course_session.rs index a2f2743..e293f0a 100644 --- a/src/domain/course_session.rs +++ b/src/domain/course_session.rs @@ -157,7 +157,11 @@ impl CourseSession { .bind(("ends_at", ends_at)) .await? .check()?; - result.take::>(0)?.into_iter().next().ok_or(AppError::NotFound) + result + .take::>(0)? + .into_iter() + .next() + .ok_or(AppError::NotFound) } /// Delete the session and cascade-remove its roll-call rows. diff --git a/src/domain/exam.rs b/src/domain/exam.rs index 777dc18..3056d1f 100644 --- a/src/domain/exam.rs +++ b/src/domain/exam.rs @@ -620,4 +620,3 @@ mod tests { assert!(ExamSchedule::try_new(mode("async"), at(3), at(2), dur).is_err()); } } - diff --git a/src/domain/exam_answer.rs b/src/domain/exam_answer.rs index 59b35ea..3ce16ad 100644 --- a/src/domain/exam_answer.rs +++ b/src/domain/exam_answer.rs @@ -312,7 +312,10 @@ mod tests { Some( labels .iter() - .map(|l| ChoiceInput { id: Some((*l).into()), text: (*l).into() }) + .map(|l| ChoiceInput { + id: Some((*l).into()), + text: (*l).into(), + }) .collect(), ), Some(labels[correct].into()), diff --git a/src/domain/exam_attempt.rs b/src/domain/exam_attempt.rs index efe37cd..0c85924 100644 --- a/src/domain/exam_attempt.rs +++ b/src/domain/exam_attempt.rs @@ -338,7 +338,10 @@ mod tests { /// The id of the question's second option — what these tests used to write /// as the index `1`. fn second_choice(question: &ExamQuestion) -> String { - question.get_choices().unwrap()[1].get_id().as_str().to_string() + question.get_choices().unwrap()[1] + .get_id() + .as_str() + .to_string() } use crate::domain::settings::Settings; @@ -367,8 +370,14 @@ mod tests { let spec = QuestionSpec::try_new( QuestionKind::try_new("choice").unwrap(), Some(vec![ - ChoiceInput { id: Some("a".into()), text: "5".into() }, - ChoiceInput { id: Some("b".into()), text: "6".into() }, + ChoiceInput { + id: Some("a".into()), + text: "5".into(), + }, + ChoiceInput { + id: Some("b".into()), + text: "6".into(), + }, ]), Some("b".into()), &[], @@ -400,9 +409,16 @@ mod tests { let (first, created) = ExamAttempt::start(&exam, &user, &db).await.unwrap(); assert!(created); assert_eq!(first.get_seq(), 1); - ExamAnswer::save(&question, &user, 1, Some(second_choice(&question)), None, &db) - .await - .unwrap(); + ExamAnswer::save( + &question, + &user, + 1, + Some(second_choice(&question)), + None, + &db, + ) + .await + .unwrap(); first.finish(&db).await.unwrap(); // The retake lands as sitting #2 without touching sitting #1's answers. @@ -434,9 +450,16 @@ mod tests { let (winner, created) = ExamAttempt::start(&exam, &user, &db).await.unwrap(); assert!(created); assert_eq!(winner.get_seq(), 2); - ExamAnswer::save(&question, &user, 2, Some(second_choice(&question)), None, &db) - .await - .unwrap(); + ExamAnswer::save( + &question, + &user, + 2, + Some(second_choice(&question)), + None, + &db, + ) + .await + .unwrap(); // A stale double-start races on the same seq and loses to the // composite id: the duplicate create is rejected, and with no wipe in diff --git a/src/domain/exam_question.rs b/src/domain/exam_question.rs index 6f032aa..c06c4dd 100644 --- a/src/domain/exam_question.rs +++ b/src/domain/exam_question.rs @@ -340,7 +340,11 @@ impl ExamQuestion { /// The stored kind-dependent fields as the validated bundle (for /// merge-on-update, and for copying into the bank). pub fn spec(&self) -> QuestionSpec { - QuestionSpec::from_stored(self.kind.clone(), self.choices.clone(), self.correct.clone()) + QuestionSpec::from_stored( + self.kind.clone(), + self.choices.clone(), + self.correct.clone(), + ) } pub async fn create( @@ -535,7 +539,10 @@ mod tests { ]) } - fn spec(choices: Option>, correct: Option<&str>) -> Result { + fn spec( + choices: Option>, + correct: Option<&str>, + ) -> Result { QuestionSpec::try_new(kind("choice"), choices, correct.map(str::to_string), &[]) } @@ -593,8 +600,14 @@ mod tests { // An unlabelled option cannot be marked correct — there is nothing to // name it by until the server's minted id comes back in the response. let unlabelled = Some(vec![ - ChoiceInput { id: None, text: "yes".into() }, - ChoiceInput { id: None, text: "no".into() }, + ChoiceInput { + id: None, + text: "yes".into(), + }, + ChoiceInput { + id: None, + text: "no".into(), + }, ]); assert!(spec(unlabelled, Some("a")).is_err()); } @@ -615,7 +628,11 @@ mod tests { /// option's identity, so reordering and deleting move nothing. #[tokio::test] async fn existing_ids_are_preserved_across_an_edit() { - let stored = spec(two_choices(), Some("a")).unwrap().into_parts().1.unwrap(); + let stored = spec(two_choices(), Some("a")) + .unwrap() + .into_parts() + .1 + .unwrap(); let (first, second) = (stored[0].get_id().clone(), stored[1].get_id().clone()); // Resubmit reordered, renaming the first option's text, and marking the @@ -623,8 +640,14 @@ mod tests { let edited = QuestionSpec::try_new( kind("choice"), Some(vec![ - ChoiceInput { id: Some(second.as_str().into()), text: "no".into() }, - ChoiceInput { id: Some(first.as_str().into()), text: "YES".into() }, + ChoiceInput { + id: Some(second.as_str().into()), + text: "no".into(), + }, + ChoiceInput { + id: Some(first.as_str().into()), + text: "YES".into(), + }, ]), Some(second.as_str().into()), &stored, @@ -641,8 +664,14 @@ mod tests { #[tokio::test] async fn spec_rejects_a_duplicate_choice_id() { let dupes = Some(vec![ - ChoiceInput { id: Some("a".into()), text: "yes".into() }, - ChoiceInput { id: Some("a".into()), text: "no".into() }, + ChoiceInput { + id: Some("a".into()), + text: "yes".into(), + }, + ChoiceInput { + id: Some("a".into()), + text: "no".into(), + }, ]); assert!(spec(dupes, Some("a")).is_err()); } @@ -652,7 +681,10 @@ mod tests { let n = |count: usize| { Some( (0..count) - .map(|i| ChoiceInput { id: Some(i.to_string()), text: "option".into() }) + .map(|i| ChoiceInput { + id: Some(i.to_string()), + text: "option".into(), + }) .collect::>(), ) }; @@ -676,7 +708,10 @@ mod tests { /// `try_new` on create, so the ids are real minted ULIDs. fn stored_set(n: usize) -> Vec { let inputs = (0..n) - .map(|i| ChoiceInput { id: Some(format!("s{i}")), text: format!("stored {i}") }) + .map(|i| ChoiceInput { + id: Some(format!("s{i}")), + text: format!("stored {i}"), + }) .collect(); QuestionSpec::try_new(kind("choice"), Some(inputs), Some("s0".into()), &[]) .unwrap() @@ -806,8 +841,14 @@ mod tests { async fn spec_rejects_blank_or_oversized_choices() { let with = |text: String| { Some(vec![ - ChoiceInput { id: Some("a".into()), text: "yes".into() }, - ChoiceInput { id: Some("b".into()), text }, + ChoiceInput { + id: Some("a".into()), + text: "yes".into(), + }, + ChoiceInput { + id: Some("b".into()), + text, + }, ]) }; assert!(spec(with(" ".into()), Some("a")).is_err()); diff --git a/src/domain/exam_result.rs b/src/domain/exam_result.rs index 8afe48b..5feea07 100644 --- a/src/domain/exam_result.rs +++ b/src/domain/exam_result.rs @@ -300,15 +300,20 @@ mod tests { .await .unwrap(); assert_eq!( - history.iter().map(|r| r.get_mark().as_i64()).collect::>(), + history + .iter() + .map(|r| r.get_mark().as_i64()) + .collect::>(), vec![40, 90] ); // Deleting the pair removes every sitting. ExamResult::remove(&exam, &user, &db).await.unwrap(); - assert!(ExamResult::list_all_for_exam_user(&exam, &user, &db) - .await - .unwrap() - .is_empty()); + assert!( + ExamResult::list_all_for_exam_user(&exam, &user, &db) + .await + .unwrap() + .is_empty() + ); } } diff --git a/src/domain/question_image.rs b/src/domain/question_image.rs index 0f42e26..3e4495b 100644 --- a/src/domain/question_image.rs +++ b/src/domain/question_image.rs @@ -217,7 +217,10 @@ mod tests { Some( ["a", "b", "c"] .iter() - .map(|l| ChoiceInput { id: Some((*l).into()), text: (*l).into() }) + .map(|l| ChoiceInput { + id: Some((*l).into()), + text: (*l).into(), + }) .collect(), ), Some("a".into()), @@ -312,9 +315,24 @@ mod tests { .unwrap(); // The question illustration plus the two surviving option pictures. assert_eq!(left.len(), 3); - assert!(QuestionImage::read_slot(&question, Some(&ids[0]), &db).await.unwrap().is_some()); - assert!(QuestionImage::read_slot(&question, Some(&ids[2]), &db).await.unwrap().is_some()); - assert!(QuestionImage::read_slot(&question, None, &db).await.unwrap().is_some()); + assert!( + QuestionImage::read_slot(&question, Some(&ids[0]), &db) + .await + .unwrap() + .is_some() + ); + assert!( + QuestionImage::read_slot(&question, Some(&ids[2]), &db) + .await + .unwrap() + .is_some() + ); + assert!( + QuestionImage::read_slot(&question, None, &db) + .await + .unwrap() + .is_some() + ); } #[tokio::test] diff --git a/src/domain/settings.rs b/src/domain/settings.rs index a7ab9bf..27d5a45 100644 --- a/src/domain/settings.rs +++ b/src/domain/settings.rs @@ -15,11 +15,11 @@ use surrealdb::types::{RecordId, SurrealValue}; use crate::constant::{ DEFAULT_ATTENDANCE_STATUSES, DEFAULT_CHATBOT_HISTORY_TURNS, DEFAULT_EXAM_KINDS, - DEFAULT_MAX_CHATBOT_THREADS, DEFAULT_MAX_CHATBOT_MESSAGE_LEN, DEFAULT_MAX_FILE_BYTES, - MAX_CHATBOT_HISTORY_TURNS, MAX_EXAM_KIND_WEIGHT, MAX_GRADE_BANDS, MAX_GRADE_LABEL_LEN, MAX_MARK, - MAX_MAX_CHATBOT_THREADS, MAX_MAX_CHATBOT_MESSAGE_LEN, MAX_MAX_FILE_BYTES, + DEFAULT_MAX_CHATBOT_MESSAGE_LEN, DEFAULT_MAX_CHATBOT_THREADS, DEFAULT_MAX_FILE_BYTES, + MAX_CHATBOT_HISTORY_TURNS, MAX_EXAM_KIND_WEIGHT, MAX_GRADE_BANDS, MAX_GRADE_LABEL_LEN, + MAX_MARK, MAX_MAX_CHATBOT_MESSAGE_LEN, MAX_MAX_CHATBOT_THREADS, MAX_MAX_FILE_BYTES, MAX_SETTINGS_ITEM_LEN, MAX_SETTINGS_LIST_LEN, MIN_CHATBOT_HISTORY_TURNS, MIN_EXAM_KIND_WEIGHT, - MIN_MARK, MIN_MAX_CHATBOT_THREADS, MIN_MAX_CHATBOT_MESSAGE_LEN, MIN_MAX_FILE_BYTES, + MIN_MARK, MIN_MAX_CHATBOT_MESSAGE_LEN, MIN_MAX_CHATBOT_THREADS, MIN_MAX_FILE_BYTES, }; use crate::database::{Database, SETTINGS_TABLE}; use crate::domain::text_fold; @@ -704,7 +704,10 @@ mod tests { Settings::defaults().save(&db).await.unwrap(); let loaded = Settings::load(&db).await.unwrap(); assert_eq!(loaded.get_max_file_bytes(), DEFAULT_MAX_FILE_BYTES); - assert_eq!(loaded.get_chatbot_history_turns(), DEFAULT_CHATBOT_HISTORY_TURNS); + assert_eq!( + loaded.get_chatbot_history_turns(), + DEFAULT_CHATBOT_HISTORY_TURNS + ); assert_eq!( loaded.get_max_chatbot_threads(), DEFAULT_MAX_CHATBOT_THREADS diff --git a/src/domain/subject.rs b/src/domain/subject.rs index 2d6eae6..822c99b 100644 --- a/src/domain/subject.rs +++ b/src/domain/subject.rs @@ -152,7 +152,11 @@ impl Subject { .bind(("description", description)) .await? .check()?; - result.take::>(0)?.into_iter().next().ok_or(AppError::NotFound) + result + .take::>(0)? + .into_iter() + .next() + .ok_or(AppError::NotFound) } /// Delete the subject and clear it off every bank template that carried it diff --git a/src/web/bank_questions.rs b/src/web/bank_questions.rs index 8022a80..00ddcc7 100644 --- a/src/web/bank_questions.rs +++ b/src/web/bank_questions.rs @@ -39,8 +39,8 @@ use crate::error::{AppError, ErrorResponse, ValidationError}; use crate::state::AppState; use super::dto::person_map; -use super::subjects::subject_must_exist; use super::exams::ChoiceResponse; +use super::subjects::subject_must_exist; use super::{ ChoiceBody, Page, PageParams, RequireTeacher, UploadFileForm, blob_path, image_content_type, read_upload, remove_blob, serve_inline_blob, set_or_clear, @@ -530,7 +530,10 @@ async fn list_questions( let buckets = bank_images_by_question(&ids, &st.db).await?; // One grouped query for the whole page — never a count per row. let used = BankQuestion::usage_counts(&ids, &st.db).await?; - let subject_ids: Vec<&SubjectId> = questions.iter().filter_map(BankQuestion::get_subject).collect(); + let subject_ids: Vec<&SubjectId> = questions + .iter() + .filter_map(BankQuestion::get_subject) + .collect(); let subject_names: HashMap = Subject::list_by_ids(&subject_ids, &st.db) .await? .iter() diff --git a/src/web/chatbot.rs b/src/web/chatbot.rs index 08cfa2a..4828312 100644 --- a/src/web/chatbot.rs +++ b/src/web/chatbot.rs @@ -85,9 +85,7 @@ impl ChatbotThreadResponse { fn new(thread: &ChatbotThread) -> Self { Self { id: thread.get_id().key().to_string(), - title: thread - .get_title() - .map(|title| title.as_str().to_string()), + title: thread.get_title().map(|title| title.as_str().to_string()), created_at: thread.get_created_at().as_millis(), updated_at: thread.get_updated_at().as_millis(), } @@ -232,11 +230,7 @@ async fn delete_thread( /// Read a thread the caller owns, or `404`. A foreign id is indistinguishable /// from a missing one. -async fn own_thread( - id: &str, - user: &UserId, - db: &Database, -) -> Result { +async fn own_thread(id: &str, user: &UserId, db: &Database) -> Result { ChatbotThread::read_for(&ChatbotThreadId::from_key(id), user, db) .await? .ok_or(AppError::NotFound) @@ -406,13 +400,9 @@ async fn send_message( return Ok(unavailable("no AI service is connected right now")); } - let prompt = ChatbotMessage::append_user( - thread.get_id(), - user.get_id(), - content.clone(), - &st.db, - ) - .await?; + let prompt = + ChatbotMessage::append_user(thread.get_id(), user.get_id(), content.clone(), &st.db) + .await?; let answer = ChatbotMessage::append_pending_assistant(thread.get_id(), user.get_id(), &st.db).await?; // Never fatal: both rows are already written, and failing here would strand diff --git a/src/web/etag.rs b/src/web/etag.rs index baa1137..49b5f5d 100644 --- a/src/web/etag.rs +++ b/src/web/etag.rs @@ -76,10 +76,9 @@ pub async fn etag(request: Request, next: Next) -> Response { // inert. `private`: these are per-user authenticated responses. But if the // handler set its own directive, it knows best — leave it untouched. if !parts.headers.contains_key(CACHE_CONTROL) { - parts.headers.insert( - CACHE_CONTROL, - HeaderValue::from_static("private, no-cache"), - ); + parts + .headers + .insert(CACHE_CONTROL, HeaderValue::from_static("private, no-cache")); } if matches(if_none_match.as_ref(), &tag) { @@ -117,5 +116,9 @@ fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { return false; }; let value = value.trim(); - value == "*" || value.split(',').map(str::trim).any(|candidate| candidate == tag) + value == "*" + || value + .split(',') + .map(str::trim) + .any(|candidate| candidate == tag) } diff --git a/src/web/exams.rs b/src/web/exams.rs index 64478c4..08cfd6b 100644 --- a/src/web/exams.rs +++ b/src/web/exams.rs @@ -1487,9 +1487,7 @@ impl QuestionResponse { kind: question.get_kind().as_str().to_string(), points: question.get_points().as_i64(), choices: ChoiceResponse::list(question.get_choices()), - correct: question - .get_correct() - .map(|id| id.as_str().to_string()), + correct: question.get_correct().map(|id| id.as_str().to_string()), image: image_meta(images, None), choice_images: choice_image_metas(question, images), from_bank: question.get_from_bank().map(|b| b.key().to_string()), @@ -2464,7 +2462,15 @@ async fn upload_choice_image( ensure_questions_editable(exam.get_id(), &st.db).await?; let question = question_of_exam(exam.get_id(), &qid, &st.db).await?; let slot = choice_slot(&question, &choice_id)?; - let stored = store_image(&st, &exam, &question, Some(&slot), content_type, &upload.data).await?; + let stored = store_image( + &st, + &exam, + &question, + Some(&slot), + content_type, + &upload.data, + ) + .await?; Ok((StatusCode::CREATED, Json(ImageMetaResponse::new(&stored)))) } @@ -3555,9 +3561,7 @@ async fn review_attempt_answers( Path((id, seq)): Path<(String, i64)>, ) -> Result, AppError> { let exam = reviewable_exam(&st, &user, &id).await?; - Ok(Json( - answer_sheet(&exam, user.get_id(), seq, &st.db).await?, - )) + Ok(Json(answer_sheet(&exam, user.get_id(), seq, &st.db).await?)) } /// The caller's own drawn-answer bytes for one of their sittings — the diff --git a/src/web/settings.rs b/src/web/settings.rs index 1f5a2f5..26c6599 100644 --- a/src/web/settings.rs +++ b/src/web/settings.rs @@ -239,7 +239,9 @@ async fn update_settings( params.attendance_statuses = attendance_statuses; params.grade_bands = grade_bands; params.max_file_bytes = req.max_file_bytes.unwrap_or(params.max_file_bytes); - params.chatbot_history_turns = req.chatbot_history_turns.unwrap_or(params.chatbot_history_turns); + params.chatbot_history_turns = req + .chatbot_history_turns + .unwrap_or(params.chatbot_history_turns); params.max_chatbot_threads = req .max_chatbot_threads .unwrap_or(params.max_chatbot_threads); diff --git a/src/web/subjects.rs b/src/web/subjects.rs index 8d9dc8b..118348c 100644 --- a/src/web/subjects.rs +++ b/src/web/subjects.rs @@ -14,8 +14,8 @@ use crate::domain::subject::{Subject, SubjectDescription, SubjectId, SubjectName use crate::error::{AppError, ErrorResponse, ValidationError}; use crate::state::AppState; -use super::courses::{can_manage_course, can_view_course}; use super::bank_questions::BANK_LOCK; +use super::courses::{can_manage_course, can_view_course}; use super::exams::EXAM_LOCK; use super::homework::HOMEWORK_LOCK; use super::{CurrentUser, RequireTeacher, SubjectResponse}; diff --git a/tests/e2e.rs b/tests/e2e.rs index 95416f2..903ed59 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -1841,9 +1841,7 @@ async fn chat_stream_and_poll_agree_that_an_answer_was_truncated() { // The same row, read the other way: identical verdict, field for field. let polled: Value = ali - .get(format!( - "{base}/chatbot/threads/{thread}/messages/{mid}" - )) + .get(format!("{base}/chatbot/threads/{thread}/messages/{mid}")) .send() .await .unwrap() @@ -1915,9 +1913,7 @@ async fn chat_stream_replays_an_answer_that_already_landed() { let mut settled = Value::Null; for _ in 0..500 { let turn: Value = ali - .get(format!( - "{base}/chatbot/threads/{thread}/messages/{mid}" - )) + .get(format!("{base}/chatbot/threads/{thread}/messages/{mid}")) .send() .await .unwrap() diff --git a/tests/etag.rs b/tests/etag.rs index 7f71da1..1767de8 100644 --- a/tests/etag.rs +++ b/tests/etag.rs @@ -93,7 +93,10 @@ async fn handler_cache_control_is_not_overwritten() { let (status, headers, _) = get(&app, "/x", None).await; assert_eq!(status, StatusCode::OK); // We still add our validator... - assert!(headers.get("etag").is_some(), "no handler ETag, so we set one"); + assert!( + headers.get("etag").is_some(), + "no handler ETag, so we set one" + ); // ...but the handler's own directive wins. assert_eq!(headers.get("cache-control").unwrap(), "public, max-age=60"); } diff --git a/tests/integration.rs b/tests/integration.rs index c2a4c19..373b4c1 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -7477,7 +7477,10 @@ async fn question_crud_validation_and_rbac() { .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!(res.body["points"], 15); - assert_eq!(res.body["correct"], res.body["choices"][1]["id"], "kind bundle kept"); + assert_eq!( + res.body["correct"], res.body["choices"][1]["id"], + "kind bundle kept" + ); assert_eq!(res.body["subject"], subject, "subject kept by omission"); // Re-tagging stays inside the course: another of its subjects is fine, a @@ -7735,7 +7738,10 @@ async fn student_question_view_hides_correct_and_embeds_answers() { let res = send(&app, "GET", &uri, Some(&student), None).await; let questions = res.body.as_array().expect("questions"); - assert_eq!(questions[0]["answer"]["selected"], choice_of(&choice_q_body, 0)); + assert_eq!( + questions[0]["answer"]["selected"], + choice_of(&choice_q_body, 0) + ); assert_eq!(questions[0]["answer"]["updated_at"], saved_at); assert!(questions[1]["answer"].is_null()); } @@ -7941,7 +7947,10 @@ async fn answer_saves_gate_on_attempt_state_and_kind() { "review after submit: {}", res.body ); - assert_eq!(res.body[0]["answer"]["selected"], choice_of(&choice_q_body, 2)); + assert_eq!( + res.body[0]["answer"]["selected"], + choice_of(&choice_q_body, 2) + ); assert_eq!(res.body[1]["answer"]["text"], "because"); } @@ -8140,7 +8149,10 @@ async fn answer_saves_stop_at_the_deadline() { ) .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); - assert_eq!(res.body["answers"][0]["selected"], choice_of(&question_body, 0)); + assert_eq!( + res.body["answers"][0]["selected"], + choice_of(&question_body, 0) + ); } #[tokio::test] @@ -8586,7 +8598,10 @@ async fn question_patch_revalidates_the_stale_kind_bundle() { .len(), 2 ); - assert_eq!(common::items(&res.body)[0]["correct"], common::items(&res.body)[0]["choices"][1]["id"]); + assert_eq!( + common::items(&res.body)[0]["correct"], + common::items(&res.body)[0]["choices"][1]["id"] + ); } #[tokio::test] @@ -10868,7 +10883,10 @@ async fn question_images_author_serve_and_cascade() { let (status, _) = post_image( &app, &teacher, - &format!("/exams/{exam}/questions/{essay}/choices/{}/image", choice_of(&question_body, 0)), + &format!( + "/exams/{exam}/questions/{essay}/choices/{}/image", + choice_of(&question_body, 0) + ), "image/png", b"x", ) @@ -11359,7 +11377,10 @@ async fn question_image_edits_follow_the_choices() { for (path, bytes) in [ (format!("{base}/image"), b"illustration".as_slice()), (format!("{base}/choices/{stone}/image"), b"stone".as_slice()), - (format!("{base}/choices/{bronze}/image"), b"bronze".as_slice()), + ( + format!("{base}/choices/{bronze}/image"), + b"bronze".as_slice(), + ), (format!("{base}/choices/{iron}/image"), b"iron".as_slice()), ] { let (status, body) = post_image(&app, &teacher, &path, "image/png", bytes).await; @@ -14841,12 +14862,7 @@ async fn new_thread(app: &axum::Router, cookie: &str) -> String { id_of(&res.body) } -async fn post_turn( - app: &axum::Router, - cookie: &str, - thread: &str, - content: &str, -) -> common::Res { +async fn post_turn(app: &axum::Router, cookie: &str, thread: &str, content: &str) -> common::Res { send( app, "POST", @@ -14859,17 +14875,10 @@ async fn post_turn( /// Open the SSE stream without draining it — a `pending` turn's stream stays /// open, so reading its body here would hang. -async fn open_stream( - app: &axum::Router, - cookie: &str, - thread: &str, - mid: &str, -) -> StatusCode { +async fn open_stream(app: &axum::Router, cookie: &str, thread: &str, mid: &str) -> StatusCode { let request = Request::builder() .method("GET") - .uri(format!( - "/chatbot/threads/{thread}/messages/{mid}/stream" - )) + .uri(format!("/chatbot/threads/{thread}/messages/{mid}/stream")) .header("cookie", cookie) .body(Body::empty()) .unwrap(); @@ -15086,24 +15095,15 @@ async fn chat_threads_are_invisible_to_everyone_else() { ] { let other = login_as(&app, &db, name, role).await; for (method, uri) in [ - ( - "GET", - format!("/chatbot/threads/{thread}/messages"), - ), - ( - "GET", - format!("/chatbot/threads/{thread}/messages/{mid}"), - ), + ("GET", format!("/chatbot/threads/{thread}/messages")), + ("GET", format!("/chatbot/threads/{thread}/messages/{mid}")), ( "GET", format!("/chatbot/threads/{thread}/messages/{mid}/stream"), ), ("DELETE", format!("/chatbot/threads/{thread}")), ("PATCH", format!("/chatbot/threads/{thread}")), - ( - "POST", - format!("/chatbot/threads/{thread}/messages"), - ), + ("POST", format!("/chatbot/threads/{thread}/messages")), ] { let body = match method { "POST" => Some(json!({ "content": "sızıntı" })), @@ -15286,9 +15286,7 @@ async fn chat_stream_stops_polling_when_the_client_hangs_up() { } let request = Request::builder() .method("GET") - .uri(format!( - "/chatbot/threads/{thread}/messages/{mid}/stream" - )) + .uri(format!("/chatbot/threads/{thread}/messages/{mid}/stream")) .header("cookie", &ali) .body(Body::empty()) .unwrap(); @@ -15812,7 +15810,14 @@ async fn approval_refuses_a_teacher_double_booking() { // Teacher counter-proposes a window overlapping the just-approved meeting; // accepting it is approval at the proposed time, and that collision is what // must be refused. - let res = propose(&app, &ali, &two, now + HOUR_MS + 600_000, now + 2 * HOUR_MS + 600_000).await; + let res = propose( + &app, + &ali, + &two, + now + HOUR_MS + 600_000, + now + 2 * HOUR_MS + 600_000, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); let res = decide(&app, &ayse, &two, "reschedule/accept").await; assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); @@ -16115,7 +16120,12 @@ async fn only_the_requester_cancels() { // a decider ends a booking by rejecting (pending) or rescheduling. let booking = book_ok(&app, &ayse, &slot).await; let res = decide(&app, &ali, &booking, "cancel").await; - assert_eq!(res.status, StatusCode::FORBIDDEN, "owner-teacher: {}", res.body); + assert_eq!( + res.status, + StatusCode::FORBIDDEN, + "owner-teacher: {}", + res.body + ); // An unrelated student may not either. let res = decide(&app, &veli, &booking, "cancel").await; assert_eq!(res.status, StatusCode::FORBIDDEN, "outsider: {}", res.body); @@ -16488,7 +16498,14 @@ async fn attempt_history_preserves_each_prior_sitting() { let own_image = format!("/exams/{exam}/attempt/answers/{question}/image"); // Seq 1: answer, draw, finish. - let res = send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "sit seq 1: {}", res.body); let res = send( &app, @@ -16501,9 +16518,23 @@ async fn attempt_history_preserves_each_prior_sitting() { assert_eq!(res.status, StatusCode::OK, "{}", res.body); let (status, _) = post_image(&app, &student, &own_image, "image/png", b"seq1-draw").await; assert_eq!(status, StatusCode::CREATED); - send(&app, "POST", &format!("/exams/{exam}/attempt/finish"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt/finish"), + Some(&student), + None, + ) + .await; // Seq 2: retake, answer + draw differently. - let res = send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; assert_eq!(res.body["attempt"], 2, "retake is the second sitting"); let res = send( &app, @@ -16548,11 +16579,15 @@ async fn attempt_history_preserves_each_prior_sitting() { .await; assert_eq!(seq1.body["answers"][0]["text"], "mitochondria"); assert_eq!(seq2.body["answers"][0]["text"], "chloroplast"); - assert_ne!(seq1.body["answers"][0]["text"], seq2.body["answers"][0]["text"]); + assert_ne!( + seq1.body["answers"][0]["text"], + seq2.body["answers"][0]["text"] + ); // Each sitting keeps its own drawing bytes. for (seq, want) in [(1, b"seq1-draw".as_slice()), (2, b"seq2-draw".as_slice())] { - let uri = format!("/exams/{exam}/students/{student_id}/attempts/{seq}/answers/{question}/image"); + let uri = + format!("/exams/{exam}/students/{student_id}/attempts/{seq}/answers/{question}/image"); let (status, _, bytes) = common::send_raw(&app, "GET", &uri, Some(&teacher), None, Vec::new()).await; assert_eq!(status, StatusCode::OK, "seq {seq} image"); @@ -16583,7 +16618,14 @@ async fn grade_of_record_is_latest_but_history_keeps_both() { let results_uri = format!("/exams/{exam}/results"); // Seq 1: sit, grade 40, finish. - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; let res = send( &app, "POST", @@ -16593,9 +16635,23 @@ async fn grade_of_record_is_latest_but_history_keeps_both() { ) .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); - send(&app, "POST", &format!("/exams/{exam}/attempt/finish"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt/finish"), + Some(&student), + None, + ) + .await; // Seq 2: retake, grade 90 — the mark lands on the new sitting. - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; let res = send( &app, "POST", @@ -16607,7 +16663,14 @@ async fn grade_of_record_is_latest_but_history_keeps_both() { assert_eq!(res.status, StatusCode::OK, "{}", res.body); // The roster (latest-per-pair) reports one row, at seq 2's mark. - let res = send(&app, "GET", &format!("/exams/{exam}/results"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/results"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); let rows = common::items(&res.body); assert_eq!(rows.len(), 1, "one grade-of-record per student"); @@ -16685,7 +16748,14 @@ async fn self_review_is_forbidden_until_the_teacher_enables_it() { ) .await; - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; let res = send( &app, "POST", @@ -16727,7 +16797,14 @@ async fn self_review_is_not_found_until_a_mark_exists() { .await; // Sat, but never graded — no ExamResult row. - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; let res = send( &app, @@ -16806,7 +16883,14 @@ async fn self_review_returns_own_seqs_and_per_sitting_answers() { let results_uri = format!("/exams/{exam}/results"); // Seq 1: answer, grade, finish. - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; send( &app, "POST", @@ -16823,9 +16907,23 @@ async fn self_review_returns_own_seqs_and_per_sitting_answers() { Some(json!({ "mark": 40, "user_id": student_id })), ) .await; - send(&app, "POST", &format!("/exams/{exam}/attempt/finish"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt/finish"), + Some(&student), + None, + ) + .await; // Seq 2: retake, answer differently, grade. - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; send( &app, "POST", @@ -16889,7 +16987,10 @@ async fn self_review_returns_own_seqs_and_per_sitting_answers() { assert_eq!(key.status, StatusCode::OK, "{}", key.body); assert_eq!(key.body["items"][0]["id"], question); assert!( - key.body["items"][0].as_object().unwrap().contains_key("correct"), + key.body["items"][0] + .as_object() + .unwrap() + .contains_key("correct"), "review/questions must expose `correct`: {}", key.body ); @@ -16933,7 +17034,14 @@ async fn self_review_is_own_scoped_between_students() { (&alice, &alice_id, "cicero", 70), (&bob, &bob_id, "cato", 30), ] { - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(who), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(who), + None, + ) + .await; send( &app, "POST", @@ -16971,7 +17079,10 @@ async fn self_review_is_own_scoped_between_students() { None, ) .await; - assert_eq!(sheet.body["answers"][0]["text"], "cicero", "A sees her own answer"); + assert_eq!( + sheet.body["answers"][0]["text"], "cicero", + "A sees her own answer" + ); assert_ne!(sheet.body["answers"][0]["text"], "cato", "never B's answer"); } @@ -17001,16 +17112,37 @@ async fn bank_question_create_get_list_and_instantiate() { let bid = id_of(&res.body); // School-wide read + list. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK); assert_eq!(res.body["text"], "2 + 2?"); let res = send(&app, "GET", "/bank-questions", Some(&teacher), None).await; assert_eq!(common::items(&res.body).len(), 1); // `?subject=` matches, and a foreign id excludes. - let res = send(&app, "GET", &format!("/bank-questions?subject={subject}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions?subject={subject}"), + Some(&teacher), + None, + ) + .await; assert_eq!(common::items(&res.body).len(), 1); - let res = send(&app, "GET", "/bank-questions?subject=nope", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?subject=nope", + Some(&teacher), + None, + ) + .await; assert_eq!(common::items(&res.body).len(), 0); // Instantiate into an exam under one of the course's subjects. @@ -17038,7 +17170,14 @@ async fn bank_question_create_get_list_and_instantiate() { assert_eq!(res.body["choices"][1]["text"], "4"); // The copy is independent — the template still stands. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK); } @@ -17062,8 +17201,10 @@ async fn bank_question_list_pages_filters_and_names() { "POST", "/bank-questions", Some(&teacher), - Some(json!({ "subject_id": subject, "text": format!("template {i:03}"), - "kind": "text", "points": 1 })), + Some( + json!({ "subject_id": subject, "text": format!("template {i:03}"), + "kind": "text", "points": 1 }), + ), ) .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); @@ -17074,19 +17215,35 @@ async fn bank_question_list_pages_filters_and_names() { "POST", "/bank-questions", Some(&other), - Some(json!({ "subject_id": other_subject, "text": "someone else's", - "kind": "text", "points": 1 })), + Some( + json!({ "subject_id": other_subject, "text": "someone else's", + "kind": "text", "points": 1 }), + ), ) .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); // Published, so it shows up in the first teacher's paging assertions below; // a private one would (correctly) be invisible to them. let foreign = id_of(&res.body); - let res = send(&app, "PATCH", &format!("/bank-questions/{foreign}"), Some(&other), Some(json!({ "visibility": "school" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{foreign}"), + Some(&other), + Some(json!({ "visibility": "school" })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); // Page 1: newest first, full window, honest total. - let res = send(&app, "GET", "/bank-questions?limit=100", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?limit=100", + Some(&teacher), + None, + ) + .await; assert_eq!(common::items(&res.body).len(), 100); assert_eq!(res.body["total"], 121); assert_eq!(common::items(&res.body)[0]["text"], "someone else's"); @@ -17365,7 +17522,14 @@ async fn bank_question_search_folds_turkish_casing() { assert_eq!(res.body["total"], 1, "needle {needle} missed: {}", res.body); } // Folding widens the match, it does not match everything. - let res = send(&app, "GET", "/bank-questions?q=ankara", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?q=ankara", + Some(&teacher), + None, + ) + .await; assert_eq!(res.body["total"], 0); } @@ -17389,18 +17553,53 @@ async fn bank_question_owner_gate_cross_course_and_freeze() { let bid = id_of(&res.body); // Published, so this test is about the *owner* gate alone — the visibility // gate (404 while private) has its own test below. - let res = send(&app, "PATCH", &format!("/bank-questions/{bid}"), Some(&owner), Some(json!({ "visibility": "school" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{bid}"), + Some(&owner), + Some(json!({ "visibility": "school" })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); // A non-owner teacher reads it, but may not edit or delete. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&other), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&other), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK); - let res = send(&app, "PATCH", &format!("/bank-questions/{bid}"), Some(&other), Some(json!({ "points": 7 }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{bid}"), + Some(&other), + Some(json!({ "points": 7 })), + ) + .await; assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); - let res = send(&app, "DELETE", &format!("/bank-questions/{bid}"), Some(&other), None).await; + let res = send( + &app, + "DELETE", + &format!("/bank-questions/{bid}"), + Some(&other), + None, + ) + .await; assert_eq!(res.status, StatusCode::FORBIDDEN); // Admin bypasses ownership. - let res = send(&app, "PATCH", &format!("/bank-questions/{bid}"), Some(&admin), Some(json!({ "points": 7 }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{bid}"), + Some(&admin), + Some(json!({ "points": 7 })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!(res.body["points"], 7); @@ -17430,7 +17629,14 @@ async fn bank_question_owner_gate_cross_course_and_freeze() { let student = login(&app, "sinem").await; let student_id = me_id(&app, &student).await; enroll(&app, &owner, &course, &student_id).await; - let res = send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let res = send( &app, @@ -17485,8 +17691,14 @@ async fn bank_private_template_is_invisible_until_published() { let illus = b"private-illustration".as_slice(); let pic = b"private-choice-pic".as_slice(); - let (status, _) = - post_image(&app, &owner, &format!("/bank-questions/{secret}/image"), "image/png", illus).await; + let (status, _) = post_image( + &app, + &owner, + &format!("/bank-questions/{secret}/image"), + "image/png", + illus, + ) + .await; assert_eq!(status, StatusCode::CREATED); let (status, _) = post_image( &app, @@ -17507,10 +17719,20 @@ async fn bank_private_template_is_invisible_until_published() { // --- the stranger sees nothing, and is told nothing --- let res = send(&app, "GET", "/bank-questions", Some(&other), None).await; assert!(common::items(&res.body).is_empty()); - assert_eq!(res.body["total"], 0, "total must count only visible templates"); - let res = send(&app, "GET", &format!("/bank-questions/{secret}"), Some(&other), None).await; - assert_eq!(res.status, StatusCode::NOT_FOUND, "{}", res.body); - let (status, _, _) = common::send_raw( + assert_eq!( + res.body["total"], 0, + "total must count only visible templates" + ); + let res = send( + &app, + "GET", + &format!("/bank-questions/{secret}"), + Some(&other), + None, + ) + .await; + assert_eq!(res.status, StatusCode::NOT_FOUND, "{}", res.body); + let (status, _, _) = common::send_raw( &app, "GET", &format!("/bank-questions/{secret}/image"), @@ -17540,13 +17762,27 @@ async fn bank_private_template_is_invisible_until_published() { .await; assert_eq!(res.status, StatusCode::NOT_FOUND, "{}", res.body); // Not even by asking for the owner's templates by name. - let res = send(&app, "GET", &format!("/bank-questions?owner={owner_id}"), Some(&other), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions?owner={owner_id}"), + Some(&other), + None, + ) + .await; assert_eq!(res.body["total"], 0); // --- the owner still has full use of their own template --- let res = send(&app, "GET", "/bank-questions", Some(&owner), None).await; assert_eq!(res.body["total"], 2); - let res = send(&app, "GET", &format!("/bank-questions/{secret}"), Some(&owner), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{secret}"), + Some(&owner), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!(res.body["correct"], res.body["choices"][0]["id"]); let (status, _, bytes) = common::send_raw( @@ -17581,13 +17817,27 @@ async fn bank_private_template_is_invisible_until_published() { assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); // --- publishing one template opens exactly that one --- - let res = send(&app, "PATCH", &format!("/bank-questions/{shared}"), Some(&owner), Some(json!({ "visibility": "school" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{shared}"), + Some(&owner), + Some(json!({ "visibility": "school" })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!(res.body["visibility"], "school"); let res = send(&app, "GET", "/bank-questions", Some(&other), None).await; assert_eq!(res.body["total"], 1, "the private one must still be hidden"); assert_eq!(common::items(&res.body)[0]["id"], shared); - let res = send(&app, "GET", &format!("/bank-questions/{shared}"), Some(&other), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{shared}"), + Some(&other), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); let res = send( &app, @@ -17599,7 +17849,14 @@ async fn bank_private_template_is_invisible_until_published() { .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); // The still-private one stays shut on every path. - let res = send(&app, "GET", &format!("/bank-questions/{secret}"), Some(&other), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{secret}"), + Some(&other), + None, + ) + .await; assert_eq!(res.status, StatusCode::NOT_FOUND); let res = send( &app, @@ -17611,12 +17868,33 @@ async fn bank_private_template_is_invisible_until_published() { .await; assert_eq!(res.status, StatusCode::NOT_FOUND, "{}", res.body); // Unpublishing closes it again. - let res = send(&app, "PATCH", &format!("/bank-questions/{shared}"), Some(&owner), Some(json!({ "visibility": "private" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{shared}"), + Some(&owner), + Some(json!({ "visibility": "private" })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); - let res = send(&app, "GET", &format!("/bank-questions/{shared}"), Some(&other), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{shared}"), + Some(&other), + None, + ) + .await; assert_eq!(res.status, StatusCode::NOT_FOUND); // A bogus value is a 400, not a silent publish. - let res = send(&app, "PATCH", &format!("/bank-questions/{shared}"), Some(&owner), Some(json!({ "visibility": "public" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{shared}"), + Some(&owner), + Some(json!({ "visibility": "public" })), + ) + .await; assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); } @@ -17639,7 +17917,14 @@ async fn bank_private_template_is_visible_to_admins() { .await; let bid = id_of(&res.body); - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&admin), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&admin), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); let res = send(&app, "GET", "/bank-questions", Some(&admin), None).await; assert_eq!(res.body["total"], 1); @@ -17661,7 +17946,14 @@ async fn exam_question_saves_to_bank() { ) .await; - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); assert_eq!(res.body["text"], "v=?"); assert_eq!(res.body["correct"], res.body["choices"][0]["id"]); @@ -17673,7 +17965,14 @@ async fn exam_question_saves_to_bank() { let bid = id_of(&res.body); // It now lives in their bank. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK); } @@ -17703,8 +18002,14 @@ async fn bank_instantiate_copies_images() { let illus = b"bank-illustration".as_slice(); let choice = b"bank-choice-pic".as_slice(); - let (status, _) = - post_image(&app, &teacher, &format!("/bank-questions/{bid}/image"), "image/png", illus).await; + let (status, _) = post_image( + &app, + &teacher, + &format!("/bank-questions/{bid}/image"), + "image/png", + illus, + ) + .await; assert_eq!(status, StatusCode::CREATED); let (status, _) = post_image( &app, @@ -17759,7 +18064,10 @@ async fn bank_instantiate_copies_images() { let exam_keys = image_blob_keys(&db).await; assert_eq!(exam_keys.len(), 2); for k in &exam_keys { - assert!(!src_bank_keys.contains(k), "copied blob reused a source file"); + assert!( + !src_bank_keys.contains(k), + "copied blob reused a source file" + ); } // Source bank images untouched — still 200 with the same bytes and keys. @@ -17785,7 +18093,11 @@ async fn bank_instantiate_copies_images() { .await; assert_eq!(status, StatusCode::OK); assert_eq!(bytes, choice); - assert_eq!(bank_image_blob_keys(&db).await, src_bank_keys, "source keys changed"); + assert_eq!( + bank_image_blob_keys(&db).await, + src_bank_keys, + "source keys changed" + ); } /// Saving an exam question to the bank copies its image blobs to fresh files @@ -17871,9 +18183,16 @@ async fn bank_save_copies_images() { let bank_keys = bank_image_blob_keys(&db).await; assert_eq!(bank_keys.len(), 2); for k in &bank_keys { - assert!(!src_exam_keys.contains(k), "copied blob reused a source file"); + assert!( + !src_exam_keys.contains(k), + "copied blob reused a source file" + ); } - assert_eq!(image_blob_keys(&db).await, src_exam_keys, "source keys changed"); + assert_eq!( + image_blob_keys(&db).await, + src_exam_keys, + "source keys changed" + ); let (status, _, bytes) = common::send_raw( &app, "GET", @@ -17975,8 +18294,14 @@ async fn bank_instantiate_rolls_back_on_missing_source_blob() { .await; let bid = id_of(&res.body); let opt = choice_of(&res.body, 0); - let (status, _) = - post_image(&app, &teacher, &format!("/bank-questions/{bid}/image"), "image/png", b"illus").await; + let (status, _) = post_image( + &app, + &teacher, + &format!("/bank-questions/{bid}/image"), + "image/png", + b"illus", + ) + .await; assert_eq!(status, StatusCode::CREATED); let (status, _) = post_image( &app, @@ -18005,12 +18330,24 @@ async fn bank_instantiate_rolls_back_on_missing_source_blob() { Some(json!({ "subject_id": subject })), ) .await; - assert_eq!(res.status, StatusCode::INTERNAL_SERVER_ERROR, "{}", res.body); + assert_eq!( + res.status, + StatusCode::INTERNAL_SERVER_ERROR, + "{}", + res.body + ); // No partial destination: no question row, no exam image rows (isolated // per-test DB is the race-safe source of truth; the shared parallel blob // dir rules out an exact on-disk diff). - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert!( common::items(&res.body).is_empty(), "orphan question survived rollback" @@ -18022,9 +18359,20 @@ async fn bank_instantiate_rolls_back_on_missing_source_blob() { // Source intact: the template still reads, and its surviving blob is still // on disk. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK); - assert_eq!(bank_image_blob_keys(&db).await, src_keys, "source bank rows changed"); + assert_eq!( + bank_image_blob_keys(&db).await, + src_keys, + "source bank rows changed" + ); assert!( common::files_dir().join(&src_keys[1]).exists(), "surviving source blob vanished" @@ -18101,15 +18449,36 @@ async fn subject_delete_clears_the_bank_templates_subject() { let (bid, kept) = (&ids[0], &ids[1]); // The manager owns none of these templates, yet the delete goes through. - let res = send(&app, "DELETE", &format!("/subjects/{subject}"), Some(&manager), None).await; + let res = send( + &app, + "DELETE", + &format!("/subjects/{subject}"), + Some(&manager), + None, + ) + .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); // The template survives with a cleared origin; the other one is untouched. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert_eq!(res.body["subject"], serde_json::Value::Null); assert_eq!(res.body["text"], "q"); - let res = send(&app, "GET", &format!("/bank-questions/{kept}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{kept}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.body["subject"], json!(other)); // The list join renders the cleared one with an empty subject name. @@ -18157,7 +18526,14 @@ async fn bank_owner_filter() { .await; // Published, so this test is about the owner filter, not visibility. let bid = id_of(&res.body); - let res = send(&app, "PATCH", &format!("/bank-questions/{bid}"), Some(who), Some(json!({ "visibility": "school" }))).await; + let res = send( + &app, + "PATCH", + &format!("/bank-questions/{bid}"), + Some(who), + Some(json!({ "visibility": "school" })), + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); } let alice_id = me_id(&app, &alice).await; @@ -18174,7 +18550,14 @@ async fn bank_owner_filter() { assert_eq!(items[0]["owner"], alice_id); // An explicit id filters to that owner. - let res = send(&app, "GET", &format!("/bank-questions?owner={bob_id}"), Some(&alice), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions?owner={bob_id}"), + Some(&alice), + None, + ) + .await; let items = common::items(&res.body); assert_eq!(items.len(), 1); assert_eq!(items[0]["owner"], bob_id); @@ -18204,8 +18587,14 @@ async fn bank_response_carries_image_metas() { assert!(res.body["image"].is_null()); assert_eq!(res.body["choice_images"], json!([null, null])); - let (status, _) = - post_image(&app, &teacher, &format!("/bank-questions/{bid}/image"), "image/png", b"illus").await; + let (status, _) = post_image( + &app, + &teacher, + &format!("/bank-questions/{bid}/image"), + "image/png", + b"illus", + ) + .await; assert_eq!(status, StatusCode::CREATED); let (status, _) = post_image( &app, @@ -18217,14 +18606,28 @@ async fn bank_response_carries_image_metas() { .await; assert_eq!(status, StatusCode::CREATED); - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.body["image"]["content_type"], "image/png"); assert_eq!(res.body["image"]["size"], 5); assert_eq!(res.body["choice_images"][0]["content_type"], "image/jpeg"); assert!(res.body["choice_images"][1].is_null()); // The list carries them too (batched, not per-row). - let res = send(&app, "GET", "/bank-questions?owner=me", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?owner=me", + Some(&teacher), + None, + ) + .await; let items = common::items(&res.body); assert_eq!(items[0]["image"]["content_type"], "image/png"); assert_eq!(items[0]["choice_images"][0]["content_type"], "image/jpeg"); @@ -18278,7 +18681,14 @@ async fn bank_provenance_both_directions() { .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let template = id_of(&res.body); - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; let question = common::items(&res.body) .iter() .find(|q| q["id"] == inserted) @@ -18298,12 +18708,26 @@ async fn bank_provenance_both_directions() { .await; // Exam -> bank: source_exam is the origin exam id. - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); assert_eq!(res.body["source_exam"], exam); // A directly authored template has no source_exam. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert!(res.body["source_exam"].is_null()); } @@ -18334,21 +18758,56 @@ async fn to_bank_links_the_question_at_the_new_template() { .expect("question in list")["banked_as"] .clone() }; - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert!(banked_as_of(&res.body).is_null()); - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let first = id_of(&res.body); - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert_eq!(banked_as_of(&res.body), json!(first)); // Second save: still 201, a distinct template, and the link follows it. - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let second = id_of(&res.body); assert_ne!(first, second); - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert_eq!(banked_as_of(&res.body), json!(second)); } @@ -18406,7 +18865,14 @@ async fn linking_the_banked_template_does_not_clobber_a_concurrent_edit() { .expect("link written"); // The edit survives and the link is set. - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; let question = common::items(&res.body) .iter() .find(|q| q["id"] == qid) @@ -18442,7 +18908,14 @@ async fn a_bank_patch_does_not_clobber_a_concurrent_write() { json!({ "text": "original", "kind": "text", "points": 5 }), ) .await; - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let bid = id_of(&res.body); assert_eq!(res.body["source_exam"], json!(exam)); @@ -18454,7 +18927,14 @@ async fn a_bank_patch_does_not_clobber_a_concurrent_write() { .unwrap() .expect("template exists"); assert!(stale.get_source_exam().is_some()); - let res = send(&app, "DELETE", &format!("/exams/{exam}"), Some(&teacher), None).await; + let res = send( + &app, + "DELETE", + &format!("/exams/{exam}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); // The stale struct writes its own fields only. @@ -18470,10 +18950,20 @@ async fn a_bank_patch_does_not_clobber_a_concurrent_write() { .await .expect("update written"); assert_eq!(updated.get_text().as_str(), "edited"); - assert!(updated.get_source_exam().is_none(), "the dead exam link came back"); + assert!( + updated.get_source_exam().is_none(), + "the dead exam link came back" + ); // The edit landed and the cleared provenance stayed cleared. - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.body["source_exam"], serde_json::Value::Null); assert_eq!(res.body["subject"], serde_json::Value::Null); assert_eq!(res.body["text"], "edited"); @@ -18502,7 +18992,14 @@ async fn deleting_a_template_clears_both_question_links() { json!({ "text": "plain", "kind": "text", "points": 5 }), ) .await; - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{authored}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{authored}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let saved = id_of(&res.body); @@ -18534,20 +19031,49 @@ async fn deleting_a_template_clears_both_question_links() { .expect("question in list")[field] .clone() }; - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert_eq!(link_of(&res.body, &authored, "banked_as"), json!(saved)); assert_eq!(link_of(&res.body, &inserted, "from_bank"), json!(origin)); for bid in [&saved, &origin] { - let res = send(&app, "DELETE", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "DELETE", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); } // Both questions survive their templates with no dangling link. - let res = send(&app, "GET", &format!("/exams/{exam}/questions"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/exams/{exam}/questions"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); - assert!(link_of(&res.body, &authored, "banked_as").is_null(), "{}", res.body); - assert!(link_of(&res.body, &inserted, "from_bank").is_null(), "{}", res.body); + assert!( + link_of(&res.body, &authored, "banked_as").is_null(), + "{}", + res.body + ); + assert!( + link_of(&res.body, &inserted, "from_bank").is_null(), + "{}", + res.body + ); } /// The mirror: deleting the origin exam clears `source_exam` on the templates @@ -18569,15 +19095,36 @@ async fn deleting_an_exam_clears_the_templates_source_exam() { ) .await; - let res = send(&app, "POST", &format!("/exams/{exam}/questions/{qid}/to-bank"), Some(&teacher), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{exam}/questions/{qid}/to-bank"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let bid = id_of(&res.body); assert_eq!(res.body["source_exam"], exam); - let res = send(&app, "DELETE", &format!("/exams/{exam}"), Some(&teacher), None).await; + let res = send( + &app, + "DELETE", + &format!("/exams/{exam}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); - let res = send(&app, "GET", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); assert!(res.body["source_exam"].is_null(), "{}", res.body); } @@ -18635,7 +19182,14 @@ async fn bank_list_counts_the_copies_made_from_each_template() { ) .await; - let res = send(&app, "GET", "/bank-questions?limit=100", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?limit=100", + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); let counts: std::collections::HashMap = common::items(&res.body) .iter() @@ -18663,7 +19217,14 @@ async fn bank_list_counts_the_copies_made_from_each_template() { ) .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); - let res = send(&app, "GET", "/bank-questions?limit=100", Some(&teacher), None).await; + let res = send( + &app, + "GET", + "/bank-questions?limit=100", + Some(&teacher), + None, + ) + .await; let after = common::items(&res.body) .iter() .find(|item| id_of(item) == used) @@ -18672,7 +19233,14 @@ async fn bank_list_counts_the_copies_made_from_each_template() { assert_eq!(after["used_count"], 2, "{}", res.body); // List-only, like the other joined fields: the single-template GET says 0. - let res = send(&app, "GET", &format!("/bank-questions/{used}"), Some(&teacher), None).await; + let res = send( + &app, + "GET", + &format!("/bank-questions/{used}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.body["used_count"], 0, "{}", res.body); } @@ -18802,13 +19370,21 @@ async fn refreshing_a_question_recopies_its_template() { assert_eq!(res.body["points"], 7); assert_eq!(res.body["subject"], subject, "the exam-side subject stays"); assert_eq!(res.body["from_bank"], bid, "provenance survives"); - assert_eq!(choice_of(&res.body, 0), keep_choice, "template choice ids adopted"); + assert_eq!( + choice_of(&res.body, 0), + keep_choice, + "template choice ids adopted" + ); assert_eq!(choice_of(&res.body, 1), fresh_choice); assert_eq!(res.body["choices"][1]["text"], "Marseille"); assert_eq!(res.body["correct"], keep_choice); // Pictures match the template: the new illustration landed, and the // dropped option's picture is gone rather than orphaned on a stale slot. - assert_eq!(res.body["image"]["content_type"], "image/png", "{}", res.body); + assert_eq!( + res.body["image"]["content_type"], "image/png", + "{}", + res.body + ); assert!(res.body["choice_images"][0].is_null(), "{}", res.body); assert!(res.body["choice_images"][1].is_null(), "{}", res.body); let (status, _, bytes) = common::send_raw( @@ -18821,7 +19397,10 @@ async fn refreshing_a_question_recopies_its_template() { ) .await; assert_eq!(status, StatusCode::OK); - assert_eq!(bytes, b"new-illustration", "the blob was re-copied, not kept"); + assert_eq!( + bytes, b"new-illustration", + "the blob was re-copied, not kept" + ); // No orphan blob rows left behind by the dropped option. assert_eq!(image_blob_keys(&db).await.len(), 1); @@ -18869,7 +19448,14 @@ async fn refreshing_a_question_recopies_its_template() { let student = login(&app, "bref_s").await; let student_id = me_id(&app, &student).await; enroll(&app, &teacher, &course, &student_id).await; - let res = send(&app, "POST", &format!("/exams/{live}/attempt"), Some(&student), None).await; + let res = send( + &app, + "POST", + &format!("/exams/{live}/attempt"), + Some(&student), + None, + ) + .await; assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let res = send( &app, @@ -18883,7 +19469,14 @@ async fn refreshing_a_question_recopies_its_template() { // Deleting the template clears `from_bank`, so the copy has nothing left to // refresh from — the action stops existing rather than half-working. - let res = send(&app, "DELETE", &format!("/bank-questions/{bid}"), Some(&teacher), None).await; + let res = send( + &app, + "DELETE", + &format!("/bank-questions/{bid}"), + Some(&teacher), + None, + ) + .await; assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); let res = send( &app, @@ -18955,10 +19548,21 @@ async fn a_course_patch_does_not_clobber_a_concurrent_teacher_assignment() { ) .await .expect("update written"); - assert_eq!(updated.get_teachers().len(), 1, "the assignment was reverted"); + assert_eq!( + updated.get_teachers().len(), + 1, + "the assignment was reverted" + ); // The rename landed and the assigned teacher stayed assigned. - let res = send(&app, "GET", &format!("/courses/{course}"), Some(&owner), None).await; + let res = send( + &app, + "GET", + &format!("/courses/{course}"), + Some(&owner), + None, + ) + .await; assert_eq!(res.body["title"], "renamed"); assert_eq!(res.body["teachers"][0]["username"], "cclob_h"); } @@ -19023,7 +19627,14 @@ async fn staffing_a_course_does_not_clobber_a_concurrent_edit() { assert_eq!(dropped.get_title().as_str(), "edited again"); assert!(dropped.get_teachers().is_empty()); - let res = send(&app, "GET", &format!("/courses/{course}"), Some(&owner), None).await; + let res = send( + &app, + "GET", + &format!("/courses/{course}"), + Some(&owner), + None, + ) + .await; assert_eq!(res.body["title"], "edited again"); assert_eq!(res.body["capacity"], 9); assert_eq!(res.body["teachers"].as_array().unwrap().len(), 0); diff --git a/tests/persistence.rs b/tests/persistence.rs index 996eaec..f1a5d49 100644 --- a/tests/persistence.rs +++ b/tests/persistence.rs @@ -357,7 +357,10 @@ async fn legacy_subjectless_questions_are_destroyed_on_boot() { assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); let question_id = common::id_of(&res.body); // Choice ids are minted server-side, so the answer names one off the response. - let picked = res.body["choices"][1]["id"].as_str().expect("choice id").to_string(); + let picked = res.body["choices"][1]["id"] + .as_str() + .expect("choice id") + .to_string(); let res = send( &app, "POST", @@ -748,14 +751,26 @@ async fn attempt_history_survives_remigration() { ); } set_role(&db, "ali", "teacher").await; - let teacher = send(&app, "POST", "/auth/login", None, Some(teacher_creds.clone())) - .await - .cookie - .unwrap(); - let student = send(&app, "POST", "/auth/login", None, Some(student_creds.clone())) - .await - .cookie - .unwrap(); + let teacher = send( + &app, + "POST", + "/auth/login", + None, + Some(teacher_creds.clone()), + ) + .await + .cookie + .unwrap(); + let student = send( + &app, + "POST", + "/auth/login", + None, + Some(student_creds.clone()), + ) + .await + .cookie + .unwrap(); let student_id = me_id(&app, &student).await; let course = create_course(&app, &teacher, "biology").await; @@ -786,9 +801,15 @@ async fn attempt_history_survives_remigration() { // Two sittings: distinct answer text + distinct mark on each. for (text, mark) in [("mitochondria", 40), ("chloroplast", 90)] { assert_eq!( - send(&app, "POST", &format!("/exams/{exam}/attempt"), Some(&student), None) - .await - .status, + send( + &app, + "POST", + &format!("/exams/{exam}/attempt"), + Some(&student), + None + ) + .await + .status, StatusCode::CREATED ); let res = send( @@ -809,7 +830,14 @@ async fn attempt_history_survives_remigration() { ) .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); - send(&app, "POST", &format!("/exams/{exam}/attempt/finish"), Some(&student), None).await; + send( + &app, + "POST", + &format!("/exams/{exam}/attempt/finish"), + Some(&student), + None, + ) + .await; } // Second boot: migration re-applied over the two sittings' live rows. @@ -859,9 +887,24 @@ async fn attempt_history_survives_remigration() { .iter() .map(|r| r["mark"].as_i64().unwrap()) .collect(); - assert_eq!(marks, vec![40, 90], "both sittings' marks durable, oldest first"); - let res = send(&app, "GET", &format!("/exams/{exam}/results"), Some(&teacher), None).await; - assert_eq!(common::items(&res.body)[0]["mark"], 90, "seq 2 is grade-of-record"); + assert_eq!( + marks, + vec![40, 90], + "both sittings' marks durable, oldest first" + ); + let res = send( + &app, + "GET", + &format!("/exams/{exam}/results"), + Some(&teacher), + None, + ) + .await; + assert_eq!( + common::items(&res.body)[0]["mark"], + 90, + "seq 2 is grade-of-record" + ); } /// A chatbot thread and every turn in it die together: deleting the