from pathlib import Path

from osx_backup.config import UserConfig, config_path, load_user_config, save_user_config


def test_roundtrip(fake_home: Path) -> None:
    assert load_user_config(fake_home) == UserConfig()
    cfg = UserConfig(dest="/Volumes/Backup", last_selection=["brew", "github"])
    path = save_user_config(fake_home, cfg)
    assert path == config_path(fake_home) == fake_home / ".config" / "osx-backup" / "config.toml"
    assert load_user_config(fake_home) == cfg


def test_corrupt_file_gives_default(fake_home: Path) -> None:
    p = config_path(fake_home)
    p.parent.mkdir(parents=True)
    p.write_text("not = [toml")
    assert load_user_config(fake_home) == UserConfig()
