From ddc09d765fe277ab49b23ba3d265d8867bb96917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 1 Aug 2026 23:20:17 +0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=A7=AA=20pin=20the=20class=20pump?= =?UTF-8?q?=20at=20the=20router,=20wire,=20and=20volume?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/e2e.rs | 106 ++++++++++ tests/integration.rs | 484 +++++++++++++++++++++++++++++++++++++++++++ tests/pagination.rs | 129 ++++++++++++ tests/persistence.rs | 122 +++++++++++ 4 files changed, 841 insertions(+) diff --git a/tests/e2e.rs b/tests/e2e.rs index 6e0529e..67a3227 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -3002,3 +3002,109 @@ async fn a_roster_change_drops_only_the_socket_it_removed() { vec!["still mine".to_string()] ); } + +/// The class layer over real HTTP with three cookie jars: the office builds the +/// class, the teacher hands over their own course, and the student — who was +/// never enrolled by anyone — finds it on `GET /courses/me`. Taking them out of +/// the class takes the seat back with them. +#[tokio::test] +async fn a_class_seats_its_roster_and_gives_the_seat_back() { + let (base, db) = spawn_server().await; + let manager = client(); + let teacher = client(); + let student = client(); + + for (c, name) in [(&manager, "mgr"), (&teacher, "tch"), (&student, "veli")] { + assert_eq!(register(c, &base, name).await.status(), StatusCode::CREATED); + } + promote(&db, "mgr", "manager").await; + promote(&db, "tch", "teacher").await; + for (c, name) in [(&manager, "mgr"), (&teacher, "tch"), (&student, "veli")] { + login(c, &base, name).await; + } + + let id_of = |v: &Value| v["id"].as_str().unwrap().to_string(); + let student_id = id_of( + &student + .get(format!("{base}/auth/me")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(), + ); + + // The office opens the class and puts the student in it. + let res = manager + .post(format!("{base}/classes")) + .json(&json!({ "name": "9-A", "grade": "9" })) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::CREATED); + let class = id_of(&res.json::().await.unwrap()); + let res = manager + .post(format!("{base}/classes/{class}/members")) + .json(&json!({ "user_id": student_id })) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::CREATED); + + // The teacher hands their own course to the class … + let res = teacher + .post(format!("{base}/courses")) + .json(&json!({ "title": "Cebir" })) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::CREATED); + let course = id_of(&res.json::().await.unwrap()); + let res = teacher + .post(format!("{base}/classes/{class}/courses")) + .json(&json!({ "course_id": course })) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::CREATED); + + // … and the student, whom nobody ever enrolled, is in it. + let mine: Value = student + .get(format!("{base}/courses/me")) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + assert_eq!(mine["total"], 1, "the class seated them: {mine}"); + assert_eq!(mine["items"][0]["id"], course); + assert_eq!(mine["items"][0]["title"], "Cebir"); + + // Out of the class, out of the course — the seat comes back. + let res = manager + .delete(format!("{base}/classes/{class}/members/{student_id}")) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::NO_CONTENT); + let mine: Value = student + .get(format!("{base}/courses/me")) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + assert_eq!(mine["total"], 0, "the seat went with them: {mine}"); + let roster: Value = teacher + .get(format!("{base}/courses/{course}/enrollments")) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + assert_eq!(roster["total"], 0, "and off the teacher's roster: {roster}"); +} diff --git a/tests/integration.rs b/tests/integration.rs index da44ae8..862144a 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -401,6 +401,17 @@ async fn protected_routes_require_session() { // The board room authenticates before it upgrades, so a rejection is // an HTTP status here and not an instant close. ("GET", "/boards/x/ws"), + ("GET", "/classes"), + ("POST", "/classes"), + ("GET", "/classes/x"), + ("PATCH", "/classes/x"), + ("DELETE", "/classes/x"), + ("POST", "/classes/x/members"), + ("GET", "/classes/x/members"), + ("DELETE", "/classes/x/members/u"), + ("POST", "/classes/x/courses"), + ("GET", "/classes/x/courses"), + ("DELETE", "/classes/x/courses/c"), ] { let res = send(&app, method, uri, None, None).await; assert_eq!(res.status, StatusCode::UNAUTHORIZED, "{method} {uri}"); @@ -25153,3 +25164,476 @@ async fn a_class_pumps_enrollments_and_guards_each_axis_separately() { StatusCode::NOT_FOUND ); } + +/// Who may look at a class, and the one write a manager holds without teaching +/// anything. Every `/classes` read is teacher+, so the student on the roster — +/// and the parent watching them — is refused their own row; and attaching takes +/// management of the course, which a manager has over every course in the +/// school whether or not they run it. +#[tokio::test] +async fn class_reads_are_staff_only_and_a_manager_manages_every_course() { + let (app, db) = app_and_db().await; + let manager = login_as(&app, &db, "mgr", "manager").await; + let teacher = login_as(&app, &db, "tch", "teacher").await; + let parent = login_as(&app, &db, "anne", "parent").await; + let student = login(&app, "stu").await; + let student_id = me_id(&app, &student).await; + let course = create_course(&app, &teacher, "algebra").await; + + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": "9-A" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let class = id_of(&res.body); + // The student really is on this roster, so the 403s below are the gate + // talking and not an empty database. + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(&manager), + Some(json!({ "user_id": student_id })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + + for (who, cookie) in [("a student", &student), ("a parent", &parent)] { + for uri in [ + "/classes".to_string(), + format!("/classes/{class}"), + format!("/classes/{class}/members"), + format!("/classes/{class}/courses"), + ] { + let res = send(&app, "GET", &uri, Some(cookie), None).await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "GET {uri} for {who}"); + } + // …and neither of them writes, on either axis. + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(cookie), + Some(json!({ "user_id": student_id })), + ) + .await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "member add for {who}"); + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(cookie), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "attach for {who}"); + } + + // The manager neither created this course nor teaches it, and still + // attaches it: the bar is management of the course, not authorship of it. + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&manager), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + // Stored, not merely answered: the link is on the class and the roster moved. + let res = send( + &app, + "GET", + &format!("/classes/{class}/courses"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 1); + assert_eq!(common::items(&res.body)[0]["course"], course); + let res = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 1, "{}", res.body); +} + +/// A student promoted out of studenthood leaves their class as well as their +/// rosters. Two sweeps run on that path and each owns exactly half the state — +/// the memberships and the class counter, the enrollment rows and the course +/// counter — so both halves are asserted stored, and then the class is deleted, +/// which only a *released* member counter permits. +#[tokio::test] +async fn a_demotion_sweeps_the_class_membership_and_what_it_pumped() { + let (app, db) = app_and_db().await; + let admin = login_as(&app, &db, "boss", "admin").await; + let manager = login_as(&app, &db, "mgr", "manager").await; + let teacher = login_as(&app, &db, "tch", "teacher").await; + let student = login(&app, "veli").await; + let student_id = me_id(&app, &student).await; + let course = create_course(&app, &teacher, "algebra").await; + + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": "9-A" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let class = id_of(&res.body); + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&teacher), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(&manager), + Some(json!({ "user_id": student_id })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let res = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 1, "the class seated them first"); + + // Out of studenthood, through the endpoint an admin actually uses. + let res = send( + &app, + "PATCH", + &format!("/users/{student_id}/role"), + Some(&admin), + Some(json!({ "role": "teacher" })), + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + + let res = send( + &app, + "GET", + &format!("/classes/{class}/members"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 0, "the roster let them go"); + let res = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 0, "and so did the course"); + + // Both tables and both counters, read off the store itself. + let mut result = db + .query( + "SELECT VALUE id FROM class_member; \ + SELECT VALUE id FROM enrollment; \ + SELECT VALUE class_member_count FROM class_group; \ + SELECT VALUE enrollment_count FROM course;", + ) + .await + .expect("read the swept state") + .check() + .expect("read the swept state"); + assert!( + result + .take::>(0) + .expect("class_member rows") + .is_empty(), + "the membership row is deleted, not merely filtered out" + ); + assert!( + result + .take::>(1) + .expect("enrollment rows") + .is_empty(), + "the pumped enrollment is deleted too" + ); + assert_eq!( + result.take::>(2).expect("class_member_count"), + vec![0], + "the class counter came back" + ); + assert_eq!( + result.take::>(3).expect("enrollment_count"), + vec![0], + "the course counter came back" + ); + + // The bite: a membership left behind (or a counter never released) makes + // the class undeletable forever. + assert_eq!( + send( + &app, + "DELETE", + &format!("/classes/{class}/courses/{course}"), + Some(&teacher), + None, + ) + .await + .status, + StatusCode::NO_CONTENT + ); + let res = send( + &app, + "DELETE", + &format!("/classes/{class}"), + Some(&manager), + None, + ) + .await; + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); +} + +/// A class files itself under a term exactly as a course does, so the term's +/// delete guard has to count classes too — otherwise deleting the calendar +/// entry would leave the class pointing at nothing. +#[tokio::test] +async fn a_term_a_class_points_at_refuses_to_delete() { + let (app, db) = app_and_db().await; + let manager = login_as(&app, &db, "mgr", "manager").await; + + let res = send( + &app, + "POST", + "/terms", + Some(&manager), + Some(json!({ + "name": "2025 Fall", + "starts_at": 1_600_000_000_000_i64, + "ends_at": 1_610_000_000_000_i64, + })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let term = id_of(&res.body); + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": "9-A", "term_id": term })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + assert_eq!(res.body["term"].as_str(), Some(term.as_str())); + let class = id_of(&res.body); + + let res = send( + &app, + "DELETE", + &format!("/terms/{term}"), + Some(&manager), + None, + ) + .await; + assert_eq!( + res.status, + StatusCode::CONFLICT, + "a class still points at it: {}", + res.body + ); + + // Unlinking gives the reference back — an explicit `null` on the named + // field, because a `{}` body short-circuits before any guard runs. + let res = send( + &app, + "PATCH", + &format!("/classes/{class}"), + Some(&manager), + Some(json!({ "term_id": null })), + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert!(res.body["term"].is_null()); + let res = send( + &app, + "DELETE", + &format!("/terms/{term}"), + Some(&manager), + None, + ) + .await; + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); + // And the class outlives the term it was filed under. + assert_eq!( + send( + &app, + "GET", + &format!("/classes/{class}"), + Some(&manager), + None + ) + .await + .status, + StatusCode::OK + ); +} + +/// All-or-nothing where a client can see it: a course with one free seat and a +/// class of two takes *neither* of them. The 409 names the course so a manager +/// knows whose capacity to raise, and — the half only stored state can show — +/// the seats spent on the way to that refusal are all given back. +#[tokio::test] +async fn a_course_that_cannot_seat_the_whole_class_seats_none_of_it() { + let (app, db) = app_and_db().await; + let manager = login_as(&app, &db, "mgr", "manager").await; + let teacher = login_as(&app, &db, "tch", "teacher").await; + let hand = login(&app, "hand").await; + let ali = login(&app, "ali").await; + let veli = login(&app, "veli").await; + let hand_id = me_id(&app, &hand).await; + let ali_id = me_id(&app, &ali).await; + let veli_id = me_id(&app, &veli).await; + + // Two seats, one of them already spent by hand: the class of two needs two. + let res = send( + &app, + "POST", + "/courses", + Some(&teacher), + Some(json!({ "title": "small", "capacity": 2 })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let course = id_of(&res.body); + enroll(&app, &teacher, &course, &hand_id).await; + + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": "9-A" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let class = id_of(&res.body); + for student in [&ali_id, &veli_id] { + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(&manager), + Some(json!({ "user_id": student })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + } + + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&teacher), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + let message = res.body["error"].as_str().unwrap_or_default(); + assert!( + message.contains(&format!("course:{course}")), + "the refusal must name the full course: {message}" + ); + + // Nothing at all was written: not the seat that did fit, not the link. + let res = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 1, "{}", res.body); + assert_eq!( + common::items(&res.body)[0]["user"]["id"], + hand_id, + "only the hand-placed row survives" + ); + let res = send( + &app, + "GET", + &format!("/classes/{class}/courses"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 0); + assert!(common::items(&res.body).is_empty(), "no link row"); + let mut result = db + .query("SELECT VALUE enrollment_count FROM course; SELECT VALUE id FROM class_course;") + .await + .expect("read the refused state") + .check() + .expect("read the refused state"); + assert_eq!( + result.take::>(0).expect("enrollment_count"), + vec![1], + "the counter must not have moved" + ); + assert!( + result + .take::>(1) + .expect("class_course rows") + .is_empty(), + ); + + // It really was a seat shortfall: a class of one fits, and lands. + assert_eq!( + send( + &app, + "DELETE", + &format!("/classes/{class}/members/{veli_id}"), + Some(&manager), + None, + ) + .await + .status, + StatusCode::NO_CONTENT + ); + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&teacher), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let res = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(common::total(&res.body), 2, "{}", res.body); +} diff --git a/tests/pagination.rs b/tests/pagination.rs index d9a2155..5144ea7 100644 --- a/tests/pagination.rs +++ b/tests/pagination.rs @@ -529,3 +529,132 @@ async fn board_lists_are_paged() { assert_eq!(total(&res.body), 3, "the scoped total is the scoped count"); assert_eq!(items(&res.body).len(), 1, "offset 2 of 3 leaves 1"); } + +/// The class layer's three lists speak the same envelope: the class index, one +/// class's roster and the courses it carries. All three join people onto the +/// page, so the window is checked with the refs still riding on the rows. +#[tokio::test] +async fn class_lists_are_paged() { + let (app, db) = app_and_db().await; + let manager = login_as(&app, &db, "mgr", "manager").await; + + // Three classes; the first carries three students and three courses. + let mut classes = Vec::new(); + for n in 0..3 { + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": format!("9-{n}") })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + classes.push(res.body["id"].as_str().unwrap().to_string()); + } + let class = classes[0].clone(); + for n in 0..3 { + let course = create_course(&app, &manager, &format!("ders{n}")).await; + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&manager), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + + let student = login(&app, &format!("stud{n}")).await; + let sid = me_id(&app, &student).await; + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(&manager), + Some(json!({ "user_id": sid })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + } + + for uri in [ + "/classes".to_string(), + format!("/classes/{class}/members"), + format!("/classes/{class}/courses"), + ] { + // Unpaged: everything, `limit` echoes null. + let res = send(&app, "GET", &uri, Some(&manager), None).await; + assert_eq!(res.status, StatusCode::OK, "GET {uri}: {}", res.body); + assert_eq!(total(&res.body), 3, "GET {uri} total"); + assert_eq!(items(&res.body).len(), 3, "GET {uri} items"); + assert!(res.body["limit"].is_null(), "GET {uri} echoes a null limit"); + assert_eq!(res.body["offset"], 0); + + // A window of 1: the total stays the unpaged count, consecutive windows + // are disjoint, and past the end is an empty page rather than an error. + let res = send( + &app, + "GET", + &format!("{uri}?limit=1&offset=0"), + Some(&manager), + None, + ) + .await; + assert_eq!(total(&res.body), 3, "GET {uri} windowed total"); + assert_eq!(items(&res.body).len(), 1, "GET {uri} window size"); + assert_eq!(res.body["limit"], 1); + let first = items(&res.body)[0]["id"].clone(); + let res = send( + &app, + "GET", + &format!("{uri}?limit=1&offset=1"), + Some(&manager), + None, + ) + .await; + assert_eq!(res.body["offset"], 1); + assert_ne!(items(&res.body)[0]["id"], first, "GET {uri} pages overlap"); + let res = send( + &app, + "GET", + &format!("{uri}?limit=1&offset=99"), + Some(&manager), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK); + assert!(items(&res.body).is_empty(), "GET {uri} past the end"); + assert_eq!(total(&res.body), 3, "GET {uri} total stays honest"); + + // The shared bounds bite on every one of them. + for bad in ["limit=0", "limit=501", "offset=-1"] { + let res = send(&app, "GET", &format!("{uri}?{bad}"), Some(&manager), None).await; + assert_eq!( + res.status, + StatusCode::BAD_REQUEST, + "GET {uri}?{bad} should be 400" + ); + } + } + + // The joins run over the page, not the table: each windowed row still + // carries the people it names. + let res = send( + &app, + "GET", + "/classes?limit=1&offset=0", + Some(&manager), + None, + ) + .await; + assert_eq!(items(&res.body)[0]["creator"]["username"], "mgr"); + let uri = format!("/classes/{class}/members?limit=1&offset=0"); + let res = send(&app, "GET", &uri, Some(&manager), None).await; + let row = &items(&res.body)[0]; + assert!(row["user"]["username"].is_string(), "member ref: {row}"); + assert_eq!(row["added_by"]["username"], "mgr"); + let uri = format!("/classes/{class}/courses?limit=1&offset=0"); + let res = send(&app, "GET", &uri, Some(&manager), None).await; + assert_eq!(items(&res.body)[0]["attached_by"]["username"], "mgr"); +} diff --git a/tests/persistence.rs b/tests/persistence.rs index 5e9ff7c..8fc17c4 100644 --- a/tests/persistence.rs +++ b/tests/persistence.rs @@ -2100,3 +2100,125 @@ async fn boards_and_their_strokes_survive_remigration() { "drawing resumed after the boot" ); } + +/// Every course's `enrollment_count`, straight off the store. +async fn seats_taken(db: &Database) -> Vec { + db.query("SELECT VALUE enrollment_count FROM course;") + .await + .expect("read the counter") + .check() + .expect("read the counter") + .take::>(0) + .expect("enrollment_count") +} + +/// An `enrollment` row written before the class layer (2026-08-01) carries no +/// `source` key at all, and that absence *is* the meaning — "a person placed +/// this student" — so there is nothing to backfill and everything to preserve. +/// Such a row must still decode once the column exists, still count towards its +/// course's cap, and never be adopted by a class that later attaches the same +/// course: the class skips a pair that already has a row, so the sweep on its +/// way out must find nothing of its own to take. +#[tokio::test] +async fn pre_class_enrollments_keep_their_absent_source() { + let (app, db) = common::app_and_db().await; + let manager = common::login_as(&app, &db, "mgr", "manager").await; + let teacher = common::login_as(&app, &db, "teacher", "teacher").await; + let ali = common::login(&app, "ali").await; + let veli = common::login(&app, "veli").await; + let (ali_id, veli_id) = (me_id(&app, &ali).await, me_id(&app, &veli).await); + + let course = create_course(&app, &teacher, "algebra").await; + enroll(&app, &teacher, &course, &ali_id).await; + enroll(&app, &teacher, &course, &veli_id).await; + + // Age both rows into the pre-class shape — no `source` key — and the course + // into the pre-counter shape a volume that old also carries. + db.query("UPDATE enrollment UNSET source; UPDATE course UNSET enrollment_count;") + .await + .expect("age the rows") + .check() + .expect("age the rows"); + + let app = reboot(&db).await; + + // They decode, and the counter is seeded from them. + let roster = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!(roster.status, StatusCode::OK, "{}", roster.body); + assert_eq!( + common::total(&roster.body), + 2, + "a sourceless row must still read: {}", + roster.body + ); + assert_eq!(seats_taken(&db).await, vec![2], "seeded from the roster"); + + // A class attaching that same course finds ali already seated: no second + // seat is charged, and their row keeps its absent `source`… + let res = send( + &app, + "POST", + "/classes", + Some(&manager), + Some(json!({ "name": "9-A" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let class = common::id_of(&res.body); + let res = send( + &app, + "POST", + &format!("/classes/{class}/members"), + Some(&manager), + Some(json!({ "user_id": ali_id })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let res = send( + &app, + "POST", + &format!("/classes/{class}/courses"), + Some(&manager), + Some(json!({ "course_id": course })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + assert_eq!(seats_taken(&db).await, vec![2], "no second seat charged"); + + // …so the detach sweeps nothing, and the pre-class rows outlive the class. + let res = send( + &app, + "DELETE", + &format!("/classes/{class}/courses/{course}"), + Some(&manager), + None, + ) + .await; + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); + let roster = send( + &app, + "GET", + &format!("/courses/{course}/enrollments"), + Some(&teacher), + None, + ) + .await; + assert_eq!( + common::total(&roster.body), + 2, + "hand-placed rows are never swept by a class: {}", + roster.body + ); + assert_eq!( + seats_taken(&db).await, + vec![2], + "and no seat was given back" + ); +}