use second_brain_core::machine::MachineIdentity;

#[test]
fn creates_identity_on_first_run() {
    let dir = tempfile::tempdir().unwrap();
    let identity = MachineIdentity::load_or_create(dir.path()).unwrap();

    assert!(!identity.id.is_empty());
    assert!(!identity.name.is_empty());
    assert!(dir.path().join("machine.toml").exists());
}

#[test]
fn returns_same_identity_on_subsequent_runs() {
    let dir = tempfile::tempdir().unwrap();
    let first = MachineIdentity::load_or_create(dir.path()).unwrap();
    let second = MachineIdentity::load_or_create(dir.path()).unwrap();

    assert_eq!(first.id, second.id);
    assert_eq!(first.name, second.name);
}

#[test]
fn preserves_id_when_name_is_edited() {
    let dir = tempfile::tempdir().unwrap();
    let original = MachineIdentity::load_or_create(dir.path()).unwrap();

    let toml_path = dir.path().join("machine.toml");
    let content = std::fs::read_to_string(&toml_path).unwrap();
    let edited = content.replace(&original.name, "custom-name");
    std::fs::write(&toml_path, edited).unwrap();

    let reloaded = MachineIdentity::load_or_create(dir.path()).unwrap();
    assert_eq!(reloaded.id, original.id);
    assert_eq!(reloaded.name, "custom-name");
}
