From 3d752619a08d74a174a3370a8098fee7211b73d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 9 Sep 2026 01:37:36 +0300 Subject: [PATCH] Verify-squared: drop dead test assignment; end-to-end profile-frame tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - im-client: the split-frame parser test initialized 'events' before the loop reassigned it — a value-assigned-never-read warning in the test profile. The initialization is gone. - im-web: two router-level tests pin the emit path end to end. They open GET /directory/live through the router, read the body's data stream frame by frame (topcoat Body::into_data_stream, Box-pinned), drive POST /api/profile_photo and POST /api/delete_profile_photo through the router with the session cookie, and assert the event:profile frame arrives carrying sub and photo_version:2 — upload and removal halves both. --- crates/im-client/src/directory.rs | 2 +- crates/im-web/src/photo.rs | 149 +++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/crates/im-client/src/directory.rs b/crates/im-client/src/directory.rs index 650f257..70bb31b 100644 --- a/crates/im-client/src/directory.rs +++ b/crates/im-client/src/directory.rs @@ -379,7 +379,7 @@ mod tests { fn a_frame_split_across_reads_still_parses() { let whole = frame("two", 7); let mut parser = SseParser::default(); - let mut events = Vec::new(); + let mut events; // Cut at every byte boundary in turn; no split may lose the event. for cut in 1..whole.len() { events = parser.feed(&whole.as_bytes()[..cut]); diff --git a/crates/im-web/src/photo.rs b/crates/im-web/src/photo.rs index dc766cb..937ff33 100644 --- a/crates/im-web/src/photo.rs +++ b/crates/im-web/src/photo.rs @@ -225,6 +225,7 @@ mod tests { use std::path::Path; use std::sync::Arc; + use futures_util::StreamExt as _; use im_core::accounts::{create_invite, create_user_from_invite}; use im_core::model::UserId; use im_core::oidc::create_client; @@ -232,7 +233,9 @@ mod tests { use im_core::sessions::{SessionMeta, create_session}; use im_core::store::Store; use topcoat::cookie::RouterBuilderCookieExt as _; - use topcoat::router::{Body, HeaderMap, Router, RouterBuilderDiscoverExt as _, StatusCode}; + use topcoat::router::{ + Body, BodyDataStream, HeaderMap, Router, RouterBuilderDiscoverExt as _, StatusCode, + }; use topcoat::router::{header, to_bytes}; use crate::config::Config; @@ -612,4 +615,148 @@ mod tests { assert_eq!(status, StatusCode::NOT_MODIFIED); assert!(bytes.is_empty()); } + + /// POSTs a photo the way the avatar script's autosubmit does — a + /// multipart form with one named file — through the router, with the + /// session cookie carrying who it is for. + async fn post_upload( + router: &Router, + cookie: &str, + bytes: &[u8], + ) -> (StatusCode, Option) { + const BOUNDARY: &str = "imwebtestboundary"; + let mut body = Vec::new(); + body.extend_from_slice( + format!( + "--{BOUNDARY}\r\n\ + Content-Disposition: form-data; name=\"file\"; filename=\"face.png\"\r\n\ + Content-Type: image/png\r\n\r\n" + ) + .as_bytes(), + ); + body.extend_from_slice(bytes); + body.extend_from_slice(format!("\r\n--{BOUNDARY}--\r\n").as_bytes()); + let response = router + .handle( + http::Request::builder() + .method(http::Method::POST) + .uri("/api/profile_photo") + .header( + header::CONTENT_TYPE, + format!("multipart/form-data; boundary={BOUNDARY}"), + ) + .header(header::COOKIE, cookie) + .body(Body::from(body)) + .unwrap(), + ) + .await; + let (parts, body) = response.into_parts(); + let location = parts + .headers + .get(header::LOCATION) + .and_then(|v| v.to_str().ok()) + .map(str::to_string); + let _ = to_bytes(body, usize::MAX).await.unwrap(); + (parts.status, location) + } + + /// Opens `/directory/live` as an app would and pins the body's data + /// stream. A single SSE frame may straddle two body frames, so + /// callers accumulate text and match on substrings, never on chunk + /// boundaries. + async fn open_live_stream( + router: &Router, + authorization: &str, + ) -> std::pin::Pin> { + let response = router + .handle( + http::Request::builder() + .uri("/directory/live") + .header(header::AUTHORIZATION, authorization) + .body(Body::empty()) + .unwrap(), + ) + .await; + let (parts, body) = response.into_parts(); + assert_eq!(parts.status, StatusCode::OK); + Box::pin(body.into_data_stream()) + } + + /// The next body frame off the stream, as text. + async fn next_wire_chunk(stream: &mut std::pin::Pin>) -> String { + let chunk = stream + .as_mut() + .next() + .await + .expect("stream stays open") + .expect("frames succeed"); + String::from_utf8_lossy(&chunk).into_owned() + } + + /// The whole emit path, end to end: a signed-in upload through the + /// router, the committed row's new version on the live bus, and an + /// `event: profile` frame off `/directory/live` carrying it. + #[tokio::test] + async fn a_photo_upload_through_the_router_emits_a_profile_frame() { + let setup = setup().await; + // Open the app stream first, the way a sibling sits on it, and + // read through the opening reconnection hint. + let mut stream = + open_live_stream(&setup.router, &basic(&setup.client_id, &setup.secret)).await; + let mut wire = String::new(); + while !wire.contains("retry: 5000") { + wire.push_str(&next_wire_chunk(&mut stream).await); + } + + // Setup's upload was version 1; this one must announce 2. + let (status, location) = post_upload(&setup.router, &setup.session_cookie, PHOTO).await; + assert_eq!(status, StatusCode::SEE_OTHER, "upload answers a 303"); + assert_eq!(location.as_deref(), Some("/?ok=photo_saved")); + + wire.clear(); + while !wire.contains("event: profile") { + wire.push_str(&next_wire_chunk(&mut stream).await); + } + assert!(wire.contains(&format!("\"sub\":\"{}\"", setup.user_id))); + assert!( + wire.contains("\"photo_version\":2"), + "the frame carries the bumped version: {wire}" + ); + } + + /// Same path, the removal half: a delete announces too, and its frame + /// moves the version forward even though the mime is gone. + #[tokio::test] + async fn a_photo_delete_through_the_router_emits_a_profile_frame() { + let setup = setup().await; + let mut stream = + open_live_stream(&setup.router, &basic(&setup.client_id, &setup.secret)).await; + let mut wire = String::new(); + while !wire.contains("retry: 5000") { + wire.push_str(&next_wire_chunk(&mut stream).await); + } + + let response = setup + .router + .handle( + http::Request::builder() + .method(http::Method::POST) + .uri("/api/delete_profile_photo") + .header(header::COOKIE, &setup.session_cookie) + .body(Body::empty()) + .unwrap(), + ) + .await; + let (parts, _) = response.into_parts(); + assert_eq!(parts.status, StatusCode::SEE_OTHER); + + wire.clear(); + while !wire.contains("event: profile") { + wire.push_str(&next_wire_chunk(&mut stream).await); + } + assert!( + wire.contains("\"photo_version\":2"), + "the removal bumps and announces the version: {wire}" + ); + } }