#![cfg(test)]
use tempfile::TempDir;
use thoughts_tool::{RepoConfig, RepoConfigManager, RequiredMount, SyncStrategy};
#[test]
fn test_repo_config_round_trip() {
let temp_dir = TempDir::new().unwrap();
let thoughts_dir = temp_dir.path().join(".thoughts");
std::fs::create_dir_all(&thoughts_dir).unwrap();
let manager = RepoConfigManager::new(temp_dir.path().to_path_buf());
let config = RepoConfig {
version: "1.0".to_string(),
mount_dirs: Default::default(),
requires: vec![RequiredMount {
remote: "git@github.com:test/repo.git".to_string(),
mount_path: "test_mount".to_string(),
subpath: None,
description: "Test mount".to_string(),
optional: false,
override_rules: None,
sync: SyncStrategy::Auto,
}],
rules: vec![],
};
manager.save(&config).unwrap();
let loaded = manager
.load()
.expect("Failed to load config")
.expect("Config file should exist after save");
assert_eq!(loaded.version, config.version);
assert_eq!(loaded.requires.len(), 1);
assert_eq!(loaded.requires[0].mount_path, "test_mount");
assert_eq!(loaded.requires[0].remote, "git@github.com:test/repo.git");
}
#[test]
fn test_config_create_detects_malformed_json_as_already_configured() {
let temp_dir = TempDir::new().unwrap();
let thoughts_dir = temp_dir.path().join(".thoughts");
std::fs::create_dir_all(&thoughts_dir).unwrap();
let config_path = thoughts_dir.join("config.json");
std::fs::write(&config_path, "{ invalid json !!!").unwrap();
assert!(config_path.exists());
let config_exists = config_path.exists();
assert!(
config_exists,
"Config file should exist and be detected as 'already configured'"
);
}
#[test]
fn test_mount_remove_does_not_create_config_when_absent() {
let temp_dir = TempDir::new().unwrap();
let thoughts_dir = temp_dir.path().join(".thoughts");
std::fs::create_dir_all(&thoughts_dir).unwrap();
let config_path = thoughts_dir.join("config.json");
assert!(!config_path.exists());
let manager = RepoConfigManager::new(temp_dir.path().to_path_buf());
let result = manager.load_v2_or_bail();
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("No repository configuration found")
);
assert!(
!config_path.exists(),
"Config file should not be created when loading fails"
);
}