#[cfg(feature = "integration")]
#[test]
fn record_integration() -> anyhow::Result<()> {
    use futures::StreamExt;
    use image::Rgb;
    use pilatus::{DeviceConfig, Recipe, RelativeDirectoryPath};
    use pilatus_rt::{TempRuntime, TokioFileService};
    use serde_json::json;

    let mut recipe = Recipe::default();
    let player_id = recipe.add_device(DeviceConfig::new_unchecked(
        "pilatus-emulation-camera",
        "Player",
        json!({}),
    ));
    let recorder_id = recipe.add_device(DeviceConfig::new_unchecked(
        "pilatus-emulation-camera",
        "Recorder",
        json!({
            "permanent_recording": {
                "collection_name": "foo",
                "source_id": player_id,
            }
        }),
    ));

    let configured = TempRuntime::new()
        .config(serde_json::json!({
            "web": {
                "socket": "0.0.0.0:0"
            },
            "tracing": {
                "default_level": "debug",
                "filters": {
                  "tokio": "info",
                  "pilatus::image": "debug"
                }
            }
        }))
        .add_recipe(recipe)
        .register(pilatus_engineering_rt::register)
        .register(pilatus_axum_rt::register)
        .register(pilatus_emulation_camera_rt::register)
        .configure()?;
    let recipes_path = configured.path().join("recipes");
    let player_collection_path = recipes_path.join(player_id.to_string()).join("bar");
    std::fs::create_dir_all(&player_collection_path)?;

    for (i, color) in [[0u8, 0, 255], [0, 255, 0], [255, 0, 0]]
        .into_iter()
        .enumerate()
    {
        let image = image::ImageBuffer::from_pixel(2, 2, Rgb(color));
        image.save(player_collection_path.join(format!("imgage{i}.png")))?;
    }
    configured.run_until(|()| async move {
        let file_service = TokioFileService::builder(recipes_path).build(recorder_id);
        for _ in 0..3 {
            tokio::time::sleep(std::time::Duration::from_millis(500)).await;
            let all = file_service
                .stream_files_recursive(RelativeDirectoryPath::new("foo").unwrap())
                .filter_map(|x| async { x.ok() })
                .collect::<Vec<_>>()
                .await;

            if all.len() >= 2 {
                println!(
                    "Files: {:?}",
                    all.iter().map(|f| f.file_name()).collect::<Vec<_>>()
                );
                let data = file_service.get_file(all.get(0).unwrap()).await.unwrap();
                let image = image::load_from_memory(&data).unwrap();
                assert!(
                    matches!(image, image::DynamicImage::ImageRgb8(_)),
                    "Not rgb image: {image:?}"
                );
                return;
            }
        }
        panic!("Never got more than 1 file");
    });
    Ok(())
}
