#![cfg(feature = "serde")]

use mount_fstab::*;
use std::path::PathBuf;

#[test]
fn spec_roundtrip() {
    let specs = vec![
        Spec::Device(PathBuf::from("/dev/sda1")),
        Spec::Label("Boot".into()),
        Spec::Uuid("abc-123".into()),
        Spec::PartLabel("System".into()),
        Spec::PartUuid("xyz".into()),
        Spec::Keyword("proc".into()),
        Spec::NetworkMount {
            host: "server".into(),
            path: PathBuf::from("/path"),
        },
    ];
    for spec in specs {
        let json = serde_json::to_string(&spec).unwrap();
        let back: Spec = serde_json::from_str(&json).unwrap();
        assert_eq!(spec, back);
    }
}

#[test]
fn fstab_roundtrip() {
    let input = "UUID=root / ext4 defaults 0 1\n";
    let fstab = Fstab::parse_str(input).unwrap();
    let json = serde_json::to_string(&fstab).unwrap();
    let back: Fstab = serde_json::from_str(&json).unwrap();
    assert_eq!(fstab, back);
}

#[test]
fn entry_roundtrip() {
    let entry = Entry::builder()
        .spec(Spec::Uuid("abc".into()))
        .file(MountPoint::new("/").unwrap())
        .vfstype(FsType::parse("ext4").unwrap())
        .options(Options::parse("defaults").unwrap())
        .freq(0)
        .passno(1)
        .build()
        .unwrap();
    let json = serde_json::to_string(&entry).unwrap();
    let back: Entry = serde_json::from_str(&json).unwrap();
    assert_eq!(entry, back);
}

#[test]
fn fstype_roundtrip() {
    let ft = FsType::parse("ext4").unwrap();
    let json = serde_json::to_string(&ft).unwrap();
    let back: FsType = serde_json::from_str(&json).unwrap();
    assert_eq!(ft, back);
}

#[test]
fn options_roundtrip() {
    let opts = Options::parse("rw,noatime,size=10G").unwrap();
    let json = serde_json::to_string(&opts).unwrap();
    let back: Options = serde_json::from_str(&json).unwrap();
    assert_eq!(opts.to_string(), back.to_string());
}

#[test]
fn mount_point_roundtrip() {
    let mp = MountPoint::new("/mnt/data").unwrap();
    let json = serde_json::to_string(&mp).unwrap();
    let back: MountPoint = serde_json::from_str(&json).unwrap();
    assert_eq!(mp, back);
}

#[test]
fn opt_item_roundtrip() {
    let item = OptItem::new("size", Some("10G".into())).unwrap();
    let json = serde_json::to_string(&item).unwrap();
    let back: OptItem = serde_json::from_str(&json).unwrap();
    assert_eq!(item, back);
}

#[test]
fn diagnostic_roundtrip() {
    let diag = Diagnostic {
        severity: Severity::Warning,
        message: "test warning".into(),
    };
    let json = serde_json::to_string(&diag).unwrap();
    let back: Diagnostic = serde_json::from_str(&json).unwrap();
    assert_eq!(diag, back);
}
