diff --git a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_image_payload.rs b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_image_payload.rs index 2521692a30..eb9815d64d 100644 --- a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_image_payload.rs +++ b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_image_payload.rs @@ -12,62 +12,6 @@ mod tests { use crate::webhook_api::hydrated::hydrated_webhook_contents::HydratedWebhookContents; use crate::webhook_api::raw::raw_webhook_payload::{RawWebhookPayload, RawWebhookStatus}; - fn load_test_webhook(filename: &str) -> RawWebhookPayload { - let path = format!("test_data/webhooks/{}", filename); - let json = std::fs::read_to_string(&path) - .unwrap_or_else(|e| panic!("Failed to read {}: {}", path, e)); - serde_json::from_str(&json) - .unwrap_or_else(|e| panic!("Failed to parse {}: {}", path, e)) - } - - #[test] - fn images_payload_populates_images_field() { - let webhook = load_test_webhook("success/images_payload_1.json"); - let result = hydrate_webhook_contents(&webhook); - - let HydratedWebhookContents::Success(data) = result else { - panic!("Expected Success, got {:?}", result); - }; - - // The raw payload should have "images". - assert!(data.payload.get("images").is_some()); - - // extracted_contents should be populated. - let contents = data.extracted_contents - .expect("extracted_contents should be Some for an images payload"); - - // The "images" field should be an array with 2 items. - let images = contents.images.expect("images should be Some"); - let images_arr = images.as_array().expect("images should be an array"); - assert_eq!(images_arr.len(), 2); - - // First image should have a url. - let first = &images_arr[0]; - assert_eq!( - first.get("url").and_then(|v| v.as_str()), - Some("https://v3b.fal.media/files/b/01234567/name1.png"), - ); - assert_eq!( - first.get("content_type").and_then(|v| v.as_str()), - Some("image/png"), - ); - - // Second image. - let second = &images_arr[1]; - assert_eq!( - second.get("url").and_then(|v| v.as_str()), - Some("https://v3b.fal.media/files/b/01234567/name2.png"), - ); - - // The "image" (singular) field should be None since the payload has "images" not "image". - assert!(contents.image.is_none()); - - // The video/model fields should be None. - assert!(contents.video.is_none()); - assert!(contents.model_glb.is_none()); - assert!(contents.model_mesh.is_none()); - } - #[test] fn payload_without_known_keys_has_none_extracted_contents() { let webhook = RawWebhookPayload { diff --git a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_images_payload.rs b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_images_payload.rs index a824165837..3b95fd2008 100644 --- a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_images_payload.rs +++ b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_images_payload.rs @@ -5,3 +5,61 @@ use serde_json::{Map, Value}; pub fn extract_images(obj: &Map) -> Option { obj.get("images").cloned() } + +#[cfg(test)] +mod tests { + use crate::webhook_api::hydrate_webhook_contents::hydrate_webhook_contents; + use crate::webhook_api::hydrated::hydrated_webhook_contents::HydratedWebhookContents; + use crate::webhook_api::raw::raw_webhook_payload::RawWebhookPayload; + + fn load_test_webhook(filename: &str) -> RawWebhookPayload { + let path = format!("test_data/webhooks/{}", filename); + let json = std::fs::read_to_string(&path) + .unwrap_or_else(|e| panic!("Failed to read {}: {}", path, e)); + serde_json::from_str(&json) + .unwrap_or_else(|e| panic!("Failed to parse {}: {}", path, e)) + } + + #[test] + fn images_payload_populates_images_field() { + let webhook = load_test_webhook("success/images_payload_1.json"); + let result = hydrate_webhook_contents(&webhook); + + let HydratedWebhookContents::Success(data) = result else { + panic!("Expected Success, got {:?}", result); + }; + + assert!(data.payload.get("images").is_some()); + + let contents = data.extracted_contents + .expect("extracted_contents should be Some for an images payload"); + + let images = contents.images.expect("images should be Some"); + let images_arr = images.as_array().expect("images should be an array"); + assert_eq!(images_arr.len(), 2); + + // First image. + let first = &images_arr[0]; + assert_eq!( + first.get("url").and_then(|v| v.as_str()), + Some("https://v3b.fal.media/files/b/01234567/name1.png"), + ); + assert_eq!( + first.get("content_type").and_then(|v| v.as_str()), + Some("image/png"), + ); + + // Second image. + let second = &images_arr[1]; + assert_eq!( + second.get("url").and_then(|v| v.as_str()), + Some("https://v3b.fal.media/files/b/01234567/name2.png"), + ); + + // Other fields should be None. + assert!(contents.image.is_none()); + assert!(contents.video.is_none()); + assert!(contents.model_glb.is_none()); + assert!(contents.model_mesh.is_none()); + } +} diff --git a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_video_payload.rs b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_video_payload.rs index 466fbef2af..c5b34aa032 100644 --- a/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_video_payload.rs +++ b/crates/api_clients/fal_client/src/webhook_api/hydrated/success_case_extractors/extract_video_payload.rs @@ -5,3 +5,59 @@ use serde_json::{Map, Value}; pub fn extract_video(obj: &Map) -> Option { obj.get("video").cloned() } + +#[cfg(test)] +mod tests { + use crate::webhook_api::hydrate_webhook_contents::hydrate_webhook_contents; + use crate::webhook_api::hydrated::hydrated_webhook_contents::HydratedWebhookContents; + use crate::webhook_api::raw::raw_webhook_payload::RawWebhookPayload; + + fn load_test_webhook(filename: &str) -> RawWebhookPayload { + let path = format!("test_data/webhooks/{}", filename); + let json = std::fs::read_to_string(&path) + .unwrap_or_else(|e| panic!("Failed to read {}: {}", path, e)); + serde_json::from_str(&json) + .unwrap_or_else(|e| panic!("Failed to parse {}: {}", path, e)) + } + + #[test] + fn video_payload_populates_video_field() { + let webhook = load_test_webhook("success/video_payload_1.json"); + let result = hydrate_webhook_contents(&webhook); + + let HydratedWebhookContents::Success(data) = result else { + panic!("Expected Success, got {:?}", result); + }; + + assert!(data.payload.get("video").is_some()); + + let contents = data.extracted_contents + .expect("extracted_contents should be Some for a video payload"); + + let video = contents.video.expect("video should be Some"); + + // The video should be an object with url, content_type, file_name, file_size. + assert_eq!( + video.get("url").and_then(|v| v.as_str()), + Some("https://v3b.fal.media/files/b/0abcdef0/AB-CDE_123456789abcde_output.mp4"), + ); + assert_eq!( + video.get("content_type").and_then(|v| v.as_str()), + Some("video/mp4"), + ); + assert_eq!( + video.get("file_name").and_then(|v| v.as_str()), + Some("output.mp4"), + ); + assert_eq!( + video.get("file_size").and_then(|v| v.as_u64()), + Some(6226845), + ); + + // Other fields should be None. + assert!(contents.image.is_none()); + assert!(contents.images.is_none()); + assert!(contents.model_glb.is_none()); + assert!(contents.model_mesh.is_none()); + } +} diff --git a/crates/api_clients/fal_client/test_data/webhooks/success/video_payload_1.json b/crates/api_clients/fal_client/test_data/webhooks/success/video_payload_1.json new file mode 100644 index 0000000000..413f59db96 --- /dev/null +++ b/crates/api_clients/fal_client/test_data/webhooks/success/video_payload_1.json @@ -0,0 +1,14 @@ +{ + "error": null, + "gateway_request_id": "01234560-1230-1230-1230-122233344455", + "payload": { + "video": { + "content_type": "video/mp4", + "file_name": "output.mp4", + "file_size": 6226845, + "url": "https://v3b.fal.media/files/b/0abcdef0/AB-CDE_123456789abcde_output.mp4" + } + }, + "request_id": "01234560-1230-1230-1230-122233344455", + "status": "OK" +} \ No newline at end of file