mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 01:18:34 +00:00
reoganize
This commit is contained in:
1 parent
d02ce726c6
commit
c5e9cf6bcf
4 files changed
+128
-56
No files matched your search
-56
@@ -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 {
|
||||
|
||||
+58
@@ -5,3 +5,61 @@ use serde_json::{Map, Value};
|
||||
pub fn extract_images(obj: &Map<String, Value>) -> Option<Value> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
+56
@@ -5,3 +5,59 @@ use serde_json::{Map, Value};
|
||||
pub fn extract_video(obj: &Map<String, Value>) -> Option<Value> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
Reference in new issue
Block a user