diff --git a/.claude/rules/code-style.md b/.claude/rules/code-style.md index c527a3411e..fee98b6edd 100644 --- a/.claude/rules/code-style.md +++ b/.claude/rules/code-style.md @@ -42,6 +42,16 @@ then the story, then the fine print. - A method that calls another method in the same file should appear *above* the method it calls. This lets readers follow the call chain downward. +### Ordering within test modules + +Put test cases first, helpers last. A reader opening a test file should immediately see +*what* is being tested before *how* the test machinery works. + +1. **Imports** +2. **Constants** (test IDs, URLs, fixture data) — small context that tests reference +3. **Test functions** grouped by category +4. **Helper functions** (builders, pipeline runners, assertions) + ## Naming - Handler functions: `{verb}_{noun}_handler` (e.g. `get_health_check_handler`, `login_handler`) diff --git a/.claude/rules/testing.md b/.claude/rules/testing.md index 2f6c92f9d2..56c0b120a6 100644 --- a/.claude/rules/testing.md +++ b/.claude/rules/testing.md @@ -14,6 +14,21 @@ - Enum tests should cover: serialization, deserialization, `to_str`/`from_str` round-trip, variant count - Password/crypto tests: be mindful of computation cost, don't add too many bcrypt test cases +## Test File Layout + +Put test cases first, helpers last. Engineers should see the actual tests before scrolling +to understand helper plumbing. + +Within a `mod tests { ... }` block: + +1. **Imports** at the top +2. **Constants** (test data IDs, URLs, etc.) — small context that tests reference +3. **Test functions** (`#[test]` / `#[tokio::test]`) grouped by category +4. **Helper functions** (builders, pipeline runners, etc.) at the very bottom + +If tests are grouped into sub-modules, the same rule applies within each sub-module: +constants first, then tests, then helpers at the end of that sub-module. + ## What to Test - New error types and their mappings diff --git a/CLAUDE.md b/CLAUDE.md index 8b91a4cb30..37ab0b9157 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -75,3 +75,4 @@ Organize for top-to-bottom reading. Important things first, details later. - **In impl blocks**: constructors first, then public methods, then private helpers - Private helpers go *below* the methods that call them - Among helpers: meatier logic above leaf-level formatters +- **In test modules**: constants first, then test cases, then helper functions last diff --git a/crates/CLAUDE.md b/crates/CLAUDE.md new file mode 100644 index 0000000000..b1ce7dd2d4 --- /dev/null +++ b/crates/CLAUDE.md @@ -0,0 +1,54 @@ +# Rust Workspace + +All Rust code lives under `crates/`. The workspace is defined in `Cargo.toml` at the repo root. + +## Code Style + +- Two spaces for indentation (not four) +- No minimum supported Rust version; use latest stable features freely +- Never use `println!` or `eprintln!` outside of tests; use `log` crate macros instead +- Use `anyhow::Result` / `AnyhowResult` for fallible functions in application code +- Fields that are optional use the `maybe_` prefix: `maybe_creator_user_token` +- When two crates export the same type name, alias with a suffix: `use foo::Bar as BarFoo;` + +## File Layout + +Organize for top-to-bottom reading. Important things first, details later. + +1. **Constants** at the top (after imports) +2. **Primary public type** (the main struct/enum the file defines) +3. **Supporting types** below the primary type +4. **API types** in order: Request, Response, Error +5. **`impl` blocks** after their type definitions + - Constructors first, then public methods, then private helpers + - Callers above callees — follow the call chain downward + +## Test Layout + +Within `#[cfg(test)] mod tests { ... }`: + +1. **Imports** +2. **Constants** (test IDs, URLs, fixture data) — small context that tests reference +3. **Test functions** (`#[test]` / `#[tokio::test]`) grouped by category +4. **Helper functions** (builders, pipeline runners, assertions) + +Constants provide small context up front. Test cases come next so engineers see *what* is +tested before *how* the helpers work. The same ordering applies within test sub-modules. + +## Building + +- Most crates: `cargo check -p {crate_name}` or `cargo test -p {crate_name}` +- Crates using SQLx: `SQLX_OFFLINE=true cargo check -p {crate_name}` +- `storyteller-web` and `mysql_queries` require `SQLX_OFFLINE=true` +- `storyteller-web` has ~370 pre-existing warnings — check the last few lines for actual errors + +## Key Crates + +- `storyteller_web` — main HTTP API monolith (actix-web) +- `artcraft` (desktop) — Tauri desktop app +- `artcraft_router` — provider routing for image/video generation (Artcraft, Fal, Seedance2Pro, Muapi) +- `artcraft_api_defs` — shared HTTP API type definitions +- `seedance2pro_client` — HTTP client for the Kinovi/Seedance2Pro video generation service +- `enums` — database-backed enums stored as VARCHAR fields +- `tokens` — primary key identifiers with Stripe-like prefixes (e.g. `user_`, `mf_`) +- `mysql_queries` — SQLx MySQL queries for the backend diff --git a/crates/api_clients/artcraft/artcraft_router/src/generate/generate_video_v2/providers/kinovi/seedance_2p0/request.rs b/crates/api_clients/artcraft/artcraft_router/src/generate/generate_video_v2/providers/kinovi/seedance_2p0/request.rs index c43a6158d0..f616f4ba74 100644 --- a/crates/api_clients/artcraft/artcraft_router/src/generate/generate_video_v2/providers/kinovi/seedance_2p0/request.rs +++ b/crates/api_clients/artcraft/artcraft_router/src/generate/generate_video_v2/providers/kinovi/seedance_2p0/request.rs @@ -53,12 +53,200 @@ mod tests { use crate::generate::generate_video_v2::video_generation_draft_or_request::VideoGenerationDraftOrRequest; use seedance2pro_client::creds::seedance2pro_session::Seedance2ProSession; - // ── Kinovi character IDs (pre-created on the Kinovi platform) ── - const STEAMPUNK_CLOWN_KINOVI_ID: &str = "char_1775176566518_sik0te"; const MOCHI_KINOVI_ID: &str = "char_1775177718294_g2pitx"; - // ── Test helpers ── + // ── Aspect ratio tests ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_landscape() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A corgi running through a field of wildflowers at sunset.".to_string()), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_portrait() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A cat sitting on a windowsill watching rain.".to_string()), + aspect_ratio: Some(CommonAspectRatio::TallNineBySixteen), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_square() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A hummingbird hovering near a flower.".to_string()), + aspect_ratio: Some(CommonAspectRatio::Square), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Resolution tests ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_480p() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A shiba inu playing in autumn leaves.".to_string()), + resolution: Some(CommonResolution::FourEightyP), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_720p() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A fox walking through a snowy forest.".to_string()), + resolution: Some(CommonResolution::SevenTwentyP), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video_1080p() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A golden retriever catching a frisbee on the beach.".to_string()), + resolution: Some(CommonResolution::TenEightyP), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Modality: text to video ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_text_to_video() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("A whale breaching in the open ocean at dawn, cinematic.".to_string()), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Modality: keyframe (start + end frame) ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_keyframe_start_and_end_frame() { + let start_token = MediaFileToken::new("mf_start".to_string()); + let end_token = MediaFileToken::new("mf_end".to_string()); + + let mut media_map = HashMap::new(); + media_map.insert(start_token.clone(), test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL.to_string()); + media_map.insert(end_token.clone(), test_data::web::image_urls::FOREST_BACKDROP_IMAGE_URL.to_string()); + + let response = run_pipeline_with_media_map(GenerateVideoRequestBuilder { + prompt: Some("The dog walks from the lake to the forest.".to_string()), + start_frame: Some(ImageRef::MediaFileToken(start_token)), + end_frame: Some(ImageRef::MediaFileToken(end_token)), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }, media_map).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Modality: image references ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_image_references() { + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("The dog in @2 runs through the scenery in @1 towards the building in @3.".to_string()), + reference_images: Some(ImageListRef::Urls(vec![ + test_data::web::image_urls::FOREST_BACKDROP_IMAGE_URL.to_string(), + test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL.to_string(), + test_data::web::image_urls::WHITE_HOUSE_SUNSET_IMAGE_URL.to_string(), + ])), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Modality: video reference ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_video_reference() { + // NB: This URL is a pre-uploaded video on the Seedance2Pro CDN from prior tests. + let response = run_pipeline(GenerateVideoRequestBuilder { + prompt: Some("Change @video1 to a nighttime scene with moonlight.".to_string()), + reference_videos: Some(VideoListRef::Urls(vec![ + "https://static.seedance2-pro.com/materials/20260315/1773594284659-3a46d231.mp4".to_string(), + ])), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // ── Modality: character references ── + + #[tokio::test] + #[ignore] // manually run — fires a real API request and incurs cost + async fn test_character_references() { + let char_clown = CharacterToken::new("char_clown".to_string()); + let char_mochi = CharacterToken::new("char_mochi".to_string()); + + let mut character_map = HashMap::new(); + character_map.insert(char_clown.clone(), STEAMPUNK_CLOWN_KINOVI_ID.to_string()); + character_map.insert(char_mochi.clone(), MOCHI_KINOVI_ID.to_string()); + + let response = run_pipeline_with_maps( + GenerateVideoRequestBuilder { + prompt: Some("@Steampunk Clown and @Mochi are playing fetch in a sunny park.".to_string()), + reference_character_tokens: Some(CharacterListRef::CharacterTokens(vec![ + char_clown, + char_mochi, + ])), + aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), + ..seedance2pro_builder() + }, + HashMap::new(), + character_map, + ).await; + assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); + assert_eq!(1, 2, "Inspect output above"); + } + + // TODO: Add audio reference test once a web-accessible audio URL is available in test_data. + + // ── Helpers ── + + fn seedance2pro_builder() -> GenerateVideoRequestBuilder { + GenerateVideoRequestBuilder { + provider: Provider::Seedance2Pro, + duration_seconds: Some(4), + video_batch_count: Some(1), + ..Default::default() + } + } fn get_seedance2pro_client() -> RouterClient { let cookies = std::fs::read_to_string("/Users/bt/Artcraft/credentials/seedance2pro_cookies.txt") @@ -159,194 +347,4 @@ mod tests { response } - - fn seedance2pro_builder() -> GenerateVideoRequestBuilder { - GenerateVideoRequestBuilder { - provider: Provider::Seedance2Pro, - duration_seconds: Some(4), - video_batch_count: Some(1), - ..Default::default() - } - } - - // ── Aspect ratio tests ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_landscape() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A corgi running through a field of wildflowers at sunset.".to_string()), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_portrait() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A cat sitting on a windowsill watching rain.".to_string()), - aspect_ratio: Some(CommonAspectRatio::TallNineBySixteen), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_square() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A hummingbird hovering near a flower.".to_string()), - aspect_ratio: Some(CommonAspectRatio::Square), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Resolution tests ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_480p() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A shiba inu playing in autumn leaves.".to_string()), - resolution: Some(CommonResolution::FourEightyP), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_720p() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A fox walking through a snowy forest.".to_string()), - resolution: Some(CommonResolution::SevenTwentyP), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video_1080p() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A golden retriever catching a frisbee on the beach.".to_string()), - resolution: Some(CommonResolution::TenEightyP), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Text to video (no references) ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_text_to_video() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("A whale breaching in the open ocean at dawn, cinematic.".to_string()), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Keyframe (start frame + end frame) ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_keyframe_start_and_end_frame() { - let start_token = MediaFileToken::new("mf_start".to_string()); - let end_token = MediaFileToken::new("mf_end".to_string()); - - let mut media_map = HashMap::new(); - media_map.insert(start_token.clone(), test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL.to_string()); - media_map.insert(end_token.clone(), test_data::web::image_urls::FOREST_BACKDROP_IMAGE_URL.to_string()); - - let response = run_pipeline_with_media_map(GenerateVideoRequestBuilder { - prompt: Some("The dog walks from the lake to the forest.".to_string()), - start_frame: Some(ImageRef::MediaFileToken(start_token)), - end_frame: Some(ImageRef::MediaFileToken(end_token)), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }, media_map).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Image references (multiple) ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_image_references() { - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("The dog in @2 runs through the scenery in @1 towards the building in @3.".to_string()), - reference_images: Some(ImageListRef::Urls(vec![ - test_data::web::image_urls::FOREST_BACKDROP_IMAGE_URL.to_string(), - test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL.to_string(), - test_data::web::image_urls::WHITE_HOUSE_SUNSET_IMAGE_URL.to_string(), - ])), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Video reference (single) ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_video_reference() { - // NB: This URL is a pre-uploaded video on the Seedance2Pro CDN from prior tests. - let response = run_pipeline(GenerateVideoRequestBuilder { - prompt: Some("Change @video1 to a nighttime scene with moonlight.".to_string()), - reference_videos: Some(VideoListRef::Urls(vec![ - "https://static.seedance2-pro.com/materials/20260315/1773594284659-3a46d231.mp4".to_string(), - ])), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // ── Character references (multiple) ── - - #[tokio::test] - #[ignore] // manually run — fires a real API request and incurs cost - async fn test_character_references() { - let char_clown = CharacterToken::new("char_clown".to_string()); - let char_mochi = CharacterToken::new("char_mochi".to_string()); - - let mut character_map = HashMap::new(); - character_map.insert(char_clown.clone(), STEAMPUNK_CLOWN_KINOVI_ID.to_string()); - character_map.insert(char_mochi.clone(), MOCHI_KINOVI_ID.to_string()); - - let response = run_pipeline_with_maps( - GenerateVideoRequestBuilder { - prompt: Some("@Steampunk Clown and @Mochi are playing fetch in a sunny park.".to_string()), - reference_character_tokens: Some(CharacterListRef::CharacterTokens(vec![ - char_clown, - char_mochi, - ])), - aspect_ratio: Some(CommonAspectRatio::WideSixteenByNine), - ..seedance2pro_builder() - }, - HashMap::new(), - character_map, - ).await; - assert!(matches!(response, GenerateVideoResponse::Seedance2Pro(_))); - assert_eq!(1, 2, "Inspect output above"); - } - - // TODO: Add audio reference test once a web-accessible audio URL is available in test_data. }