use crate::config::Config;
use std::io::Error;
use std::io::ErrorKind;

#[test]
fn parse_config_with_old_fields() {
    let data = "{
  \"backend\": \"miniflux\",
  \"concurrent_downloads\": 12,
  \"sync_amount\": 300,
  \"last_sync\": \"2026-02-25T09:29:11.692636017+00:00\",
  \"keep_articles_days\": 30
}
";
    let config = serde_json::from_str::<Config>(data).map_err(|_| Error::new(ErrorKind::InvalidData, "Failed to parse Config"));
    assert_eq!(config.unwrap().concurrent_downloads, Some(12_u32));
}

#[test]
fn parse_current() {
    let data = "{
  \"backend\": \"miniflux\",
  \"concurrent_downloads\": 24,
  \"sync_amount\": 300,
  \"last_sync\": \"2026-02-25T09:29:11.692636017+00:00\",
  \"keep_articles_days\": 30
}";
    let config = serde_json::from_str::<Config>(data).map_err(|_| Error::new(ErrorKind::InvalidData, "Failed to parse Config"));
    assert_eq!(config.unwrap().concurrent_downloads, Some(24_u32));
}

#[test]
fn migrate_to_current() {
    let data = "{
  \"backend\": \"miniflux\",
  \"concurrent_downlaods\": 24,
  \"sync_amount\": 300,
  \"last_sync\": \"2026-02-25T09:29:11.692636017+00:00\",
  \"keep_articles_days\": 30
}";

    let config = serde_json::from_str::<Config>(data).map_err(|_| Error::new(ErrorKind::InvalidData, "Failed to parse Config"));
    assert!(config.is_ok());
    let written = serde_json::to_string_pretty(&config.unwrap()).map_err(|_| Error::new(ErrorKind::InvalidData, "Failed to serialize Config"));
    assert!(written.unwrap().contains("\"concurrent_downloads\":"));
}
