//! The ONE derivation from a chat message's attachments to photo and video //! rows (spec §D4). Pure — rows in, rows out — so this file can assert the //! exact rows the server's sweep writes and its refusal of malformed picture keys. use super::*; fn media_items_for(message: &MediaItemMessage, now: &str) -> MediaItems { super::media_items_for(message, now).expect("valid current media") } const WS: &str = "ws-1"; const SHA: &str = "a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f"; const SHA2: &str = "b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd"; const SENT: &str = "2026-03-14T09:30:00.000Z"; const NOW: &str = "2026-09-04T12:00:00.000Z"; fn message(media: Vec) -> MediaItemMessage { MediaItemMessage { id: "msg-1".to_string(), thread_id: "th-1".to_string(), workspace_id: WS.to_string(), sent_at: SENT.to_string(), media, } } fn image_entry(sha: &str) -> MessageMedia { MessageMedia { key: format!("{WS}/messages/th-1/{sha}.jpg"), content_type: "image/jpeg".to_string(), kind: Some(crate::constants::MessageMediaKind::Image), width: Some(3024.0), height: Some(4032.0), size_bytes: Some(2_400_000.0), ..MessageMedia::default() } } fn video_entry(sha: &str) -> MessageMedia { MessageMedia { key: format!("{WS}/messages/th-1/{sha}.mp4"), content_type: "video/mp4".to_string(), kind: Some(crate::constants::MessageMediaKind::Video), duration_ms: Some(12_500.0), size_bytes: Some(18_000_000.0), ..MessageMedia::default() } } // ── sha256FromKey ──────────────────────────────────────────────────────────── #[test] fn reads_the_digest_out_of_a_content_addressed_key() { assert_eq!( sha256_from_key(Some(&format!("{WS}/messages/th-1/{SHA}.jpg"))).as_deref(), Some(SHA) ); // The poster shape, and a key with no extension at all. assert_eq!( sha256_from_key(Some(&format!("{WS}/videos/{SHA}.poster.jpg"))).as_deref(), Some(SHA) ); assert_eq!( sha256_from_key(Some(&format!("{WS}/photos/{SHA}"))).as_deref(), Some(SHA) ); } #[test] fn answers_none_for_anything_that_is_not_a_digest() { // The id IS the hash, so a near-miss would mint a row the file refuses. assert_eq!(sha256_from_key(None), None); assert_eq!(sha256_from_key(Some("")), None); assert_eq!( sha256_from_key(Some(&format!("{WS}/messages/th-1/IMG_0421.jpg"))), None ); assert_eq!( sha256_from_key(Some(&format!("{WS}/messages/th-1/{}.jpg", &SHA[..63]))), None ); assert_eq!( sha256_from_key(Some(&format!( "{WS}/messages/th-1/{}.jpg", SHA.to_uppercase() ))), None ); } /// Vectors read out of Node (`npx tsx` over the TypeScript itself), not /// reasoned out. Three of them corrected a guess: a key ENDING in a dot still /// reads as a digest, a DOTFILE key reads as nothing at all (the stem before /// the first dot is empty), and a key that is nothing but a trailing slash /// answers null rather than reaching the pattern. #[test] fn sha256_from_key_matches_the_typescript_on_every_odd_shape() { let cases: &[(String, Option<&str>)] = &[ (format!("{WS}/messages/th-1/{SHA}.jpg"), Some(SHA)), (format!("{WS}/videos/{SHA}.poster.jpg"), Some(SHA)), (format!("{WS}/photos/{SHA}"), Some(SHA)), (SHA.to_string(), Some(SHA)), (format!("{SHA}."), Some(SHA)), (format!(".{SHA}.jpg"), None), (format!("a/b/c/{SHA}.tar.gz"), Some(SHA)), (format!("{WS}/messages/th-1/"), None), ( format!("{WS}/messages/th-1/{}.jpg", SHA.to_uppercase()), None, ), (format!("{WS}/messages/th-1/{}.jpg", &SHA[..63]), None), (format!("{WS}/messages/th-1/{SHA}0.jpg"), None), (String::new(), None), ("/".to_string(), None), (format!("{WS}/messages/th-1/IMG_0421.jpg"), None), ]; for (key, expected) in cases { assert_eq!( sha256_from_key(Some(key)).as_deref(), *expected, "sha256_from_key({key:?})" ); } } // ── mediaItemsFor ──────────────────────────────────────────────────────────── #[test] fn turns_an_image_attachment_into_a_photo_row_and_a_sighting_that_remembers_its_chat() { let items = media_items_for(&message(vec![image_entry(SHA)]), NOW); assert_eq!(items.videos, vec![]); assert_eq!( items.photos, vec![PhotoInsert { id: format!("photo:{SHA}"), workspace_id: WS.to_string(), // The MESSAGE's own key: nothing is copied, so the person is not // storing every chat photo twice. key: format!("{WS}/messages/th-1/{SHA}.jpg"), sha256: SHA.to_string(), content_type: "image/jpeg".to_string(), size_bytes: 2_400_000, width: Some(3024), height: Some(4032), // When it ARRIVED, not when the sweep noticed — a backfill must // not pile a decade of archive onto the day it ran. taken_at: SENT.to_string(), preview: None, created_at: NOW.to_string(), updated_at: NOW.to_string(), }] ); // WHERE it was seen is a row of its own (v39): the message, the chat, the // message's key, and when it arrived there. let sighting = &items.sightings[0]; assert_eq!(items.sightings.len(), 1); assert_eq!(sighting.workspace_id, WS); assert_eq!(sighting.record_kind, MediaFamily::Photo); assert_eq!(sighting.record_id, format!("photo:{SHA}")); assert_eq!(sighting.source, SightingSource::Message); assert_eq!(sighting.source_id, "msg-1"); assert_eq!(sighting.thread_id, "th-1"); assert_eq!(sighting.key, format!("{WS}/messages/th-1/{SHA}.jpg")); assert_eq!(sighting.seen_at, SENT); assert_eq!(sighting.created_at, NOW); assert_eq!(sighting.updated_at, NOW); // The id is derived, so the same message walked twice writes the same // sighting rather than a sibling. assert_eq!( media_items_for(&message(vec![image_entry(SHA)]), NOW).sightings[0].id, sighting.id ); } #[test] fn turns_a_video_attachment_into_a_video_row_with_how_long_it_runs() { let items = media_items_for(&message(vec![video_entry(SHA2)]), NOW); assert_eq!(items.photos, vec![]); assert_eq!(items.videos.len(), 1); let video = &items.videos[0]; assert_eq!(video.id, format!("video:{SHA2}")); assert_eq!(video.sha256, SHA2); assert_eq!(video.content_type, "video/mp4"); assert_eq!(video.duration_ms, Some(12_500)); assert_eq!(video.taken_at, SENT); } #[test] fn splits_a_message_carrying_both_in_the_order_they_were_attached() { let items = media_items_for(&message(vec![image_entry(SHA), video_entry(SHA2)]), NOW); assert_eq!( items .photos .iter() .map(|p| p.id.clone()) .collect::>(), vec![format!("photo:{SHA}")] ); assert_eq!( items .videos .iter() .map(|v| v.id.clone()) .collect::>(), vec![format!("video:{SHA2}")] ); assert_eq!( items .sightings .iter() .map(|s| (s.record_kind, s.record_id.clone())) .collect::>(), vec![ (MediaFamily::Photo, format!("photo:{SHA}")), (MediaFamily::Video, format!("video:{SHA2}")), ] ); } /// Photo/video derivation follows the MIME type, independently of optional display kind. #[test] fn derives_the_media_family_from_the_content_type() { let items = media_items_for( &message(vec![ MessageMedia { kind: None, content_type: "image/heic".to_string(), ..image_entry(SHA) }, MessageMedia { kind: None, content_type: "video/quicktime".to_string(), ..video_entry(SHA2) }, ]), NOW, ); assert_eq!(items.photos.len(), 1); assert_eq!(items.videos.len(), 1); assert_eq!(items.photos[0].content_type, "image/heic"); } #[test] fn skips_audio_voice_and_files_because_a_voice_note_is_not_a_picture() { let items = media_items_for( &message(vec![ MessageMedia { key: format!("{WS}/messages/th-1/{SHA}.m4a"), content_type: "audio/mp4".to_string(), kind: Some(crate::constants::MessageMediaKind::Voice), ..MessageMedia::default() }, MessageMedia { key: format!("{WS}/messages/th-1/{SHA2}.pdf"), content_type: "application/pdf".to_string(), kind: Some(crate::constants::MessageMediaKind::File), ..MessageMedia::default() }, ]), NOW, ); assert_eq!(items.photos, vec![]); assert_eq!(items.videos, vec![]); } // RULE: Cotton Dev accepts only keyed media; URL-only legacy attachments must fail decoding. #[test] fn refuses_url_media_and_requires_a_key() { for value in [ serde_json::json!({"url": "https://example.invalid/old.jpg", "contentType": "image/jpeg"}), serde_json::json!({"contentType": "image/jpeg"}), serde_json::json!({"key": "ws/messages/synthetic.jpg", "kind": "retired-kind", "contentType": "image/jpeg"}), serde_json::json!({"key": "ws/messages/synthetic.jpg", "url": "https://example.invalid/old.jpg", "contentType": "image/jpeg"}), ] { assert!(serde_json::from_value::(value.clone()).is_err(), "{value}"); assert!(serde_json::from_value::(value).is_err()); } let current = serde_json::json!({"key": "ws/messages/synthetic.jpg", "contentType": "image/jpeg"}); assert!(serde_json::from_value::(current.clone()).is_ok()); assert!(serde_json::from_value::(current).is_ok()); } // RULE: A picture without a content-addressed key is refused, never silently omitted from indexing. #[test] fn refuses_a_non_content_addressed_picture_key() { let bad = MessageMedia { key: format!("{WS}/messages/th-1/IMG_0421.jpg"), ..image_entry(SHA) }; let error = super::media_items_for(&message(vec![bad, image_entry(SHA)]), NOW).unwrap_err(); assert_eq!(error, super::UnaddressedPicture { key: format!("{WS}/messages/th-1/IMG_0421.jpg") }); } #[test] fn carries_the_inline_blur_across_so_a_grid_has_something_to_paint() { let items = media_items_for( &message(vec![MessageMedia { preview: Some("abc123".to_string()), ..image_entry(SHA) }]), NOW, ); assert_eq!(items.photos[0].preview.as_deref(), Some("abc123")); } /// `size_bytes` is NOT NULL because the Storage screen adds these up: one null /// would silently poison a total. A dimension has no total to poison, so an /// unknown one is honestly null — and a zero is not a width. #[test] fn records_an_unknown_size_as_zero_and_an_unknown_dimension_as_null() { let items = media_items_for( &message(vec![MessageMedia { size_bytes: None, width: None, height: Some(0.0), ..image_entry(SHA) }]), NOW, ); assert_eq!(items.photos[0].size_bytes, 0); assert_eq!(items.photos[0].width, None); assert_eq!(items.photos[0].height, None); } #[test] fn records_an_unknown_duration_as_null_and_refuses_a_negative_one_the_same_way() { let items = media_items_for( &message(vec![ MessageMedia { duration_ms: None, ..video_entry(SHA2) }, MessageMedia { duration_ms: Some(-5.0), ..video_entry(SHA) }, ]), NOW, ); assert_eq!( items .videos .iter() .map(|v| v.duration_ms) .collect::>(), vec![None, None] ); } /// The forwarded meme: one picture, one row, two sightings. The rows agree in /// identity, which is what makes the second row write a no-op at the sweep, /// and the sightings differ, which is what makes it a second row THERE. #[test] fn derives_the_same_id_for_the_same_bytes_in_two_different_chats() { let first = media_items_for(&message(vec![image_entry(SHA)]), NOW); let second = media_items_for( &MediaItemMessage { id: "msg-9".to_string(), thread_id: "th-9".to_string(), workspace_id: WS.to_string(), sent_at: "2026-05-01T00:00:00.000Z".to_string(), media: vec![MessageMedia { key: format!("{WS}/messages/th-9/{SHA}.jpg"), ..image_entry(SHA) }], }, NOW, ); assert_eq!(first.photos[0].id, second.photos[0].id); assert_ne!(first.sightings[0].id, second.sightings[0].id); assert_eq!(first.sightings[0].thread_id, "th-1"); assert_eq!(second.sightings[0].thread_id, "th-9"); assert_eq!( second.sightings[0].key, format!("{WS}/messages/th-9/{SHA}.jpg") ); } #[test] fn answers_empty_for_a_message_with_no_attachments_at_all() { assert_eq!( media_items_for(&message(vec![]), NOW), MediaItems::default() ); } /// The whole answer, byte for byte, as Node printed it — sighting ids /// included, which is the part no reasoning could have supplied. It also pins /// the three floors (`3024.7` → 3024, `2_400_000.9` → 2400000, `12_500.8` → /// 12500) and the ORDER a mixed message comes back in. #[test] fn the_whole_derivation_matches_a_golden_read_out_of_node() { let items = media_items_for( &message(vec![ MessageMedia { key: format!("{WS}/messages/th-1/{SHA}.jpg"), content_type: "image/jpeg".to_string(), kind: Some(crate::constants::MessageMediaKind::Image), width: Some(3024.7), height: Some(4032.0), size_bytes: Some(2_400_000.9), preview: Some("abc123".to_string()), ..MessageMedia::default() }, MessageMedia { key: format!("{WS}/messages/th-1/{SHA2}.mp4"), content_type: "video/mp4".to_string(), kind: Some(crate::constants::MessageMediaKind::Video), duration_ms: Some(12_500.8), size_bytes: Some(18_000_000.0), ..MessageMedia::default() }, MessageMedia { key: format!("{WS}/messages/th-1/{SHA}.m4a"), content_type: "audio/mp4".to_string(), kind: Some(crate::constants::MessageMediaKind::Voice), ..MessageMedia::default() }, ]), NOW, ); assert_eq!(items.photos[0].width, Some(3024)); assert_eq!(items.photos[0].size_bytes, 2_400_000); assert_eq!(items.videos[0].duration_ms, Some(12_500)); assert_eq!( items .sightings .iter() .map(|s| s.id.as_str()) .collect::>(), vec![ "3e75c08e-5b81-4b5c-014b-96c2546c1bc6", "955a5b8c-f42c-c92d-8699-3a0dc9b576f5", ] ); let expected: serde_json::Value = serde_json::from_str(GOLDEN).expect("golden JSON parses"); assert_eq!(serde_json::to_value(&items).unwrap(), expected); } /// Printed by `JSON.stringify(mediaItemsFor(…))` in Node. The Rust structs /// serialise to the same column names in the same order, which is what makes /// this comparison worth making at all. const GOLDEN: &str = r#"{ "photos": [ { "id": "photo:a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f", "workspace_id": "ws-1", "key": "ws-1/messages/th-1/a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f.jpg", "sha256": "a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f", "content_type": "image/jpeg", "size_bytes": 2400000, "width": 3024, "height": 4032, "taken_at": "2026-03-14T09:30:00.000Z", "preview": "abc123", "created_at": "2026-09-04T12:00:00.000Z", "updated_at": "2026-09-04T12:00:00.000Z" } ], "videos": [ { "id": "video:b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd", "workspace_id": "ws-1", "key": "ws-1/messages/th-1/b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd.mp4", "sha256": "b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd", "content_type": "video/mp4", "size_bytes": 18000000, "width": null, "height": null, "taken_at": "2026-03-14T09:30:00.000Z", "preview": null, "created_at": "2026-09-04T12:00:00.000Z", "updated_at": "2026-09-04T12:00:00.000Z", "duration_ms": 12500 } ], "sightings": [ { "id": "3e75c08e-5b81-4b5c-014b-96c2546c1bc6", "workspace_id": "ws-1", "record_kind": "photo", "record_id": "photo:a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f", "source": "message", "source_id": "msg-1", "thread_id": "th-1", "key": "ws-1/messages/th-1/a3f1c0d2e4b5968778695a4b3c2d1e0fa3f1c0d2e4b5968778695a4b3c2d1e0f.jpg", "seen_at": "2026-03-14T09:30:00.000Z", "created_at": "2026-09-04T12:00:00.000Z", "updated_at": "2026-09-04T12:00:00.000Z" }, { "id": "955a5b8c-f42c-c92d-8699-3a0dc9b576f5", "workspace_id": "ws-1", "record_kind": "video", "record_id": "video:b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd", "source": "message", "source_id": "msg-1", "thread_id": "th-1", "key": "ws-1/messages/th-1/b41ffe0913243546576879809aabbccddeeff00112233445566778899aabbccd.mp4", "seen_at": "2026-03-14T09:30:00.000Z", "created_at": "2026-09-04T12:00:00.000Z", "updated_at": "2026-09-04T12:00:00.000Z" } ] }"#;