refactor(backend): phase 3 - unify error handling and fix backup timestamp bug

Key improvements:
- Extract switch_provider_internal() returning AppError for better testability
- Fix backup mtime inheritance: use read+write instead of fs::copy to ensure latest backup survives cleanup
- Add 15+ integration tests covering provider commands, atomic writes, and rollback scenarios
- Expose write_codex_live_atomic, AppState, and test hooks in public API
- Extract tests/support.rs with isolated HOME and mutex utilities

Test coverage:
- Provider switching with live config backfill and MCP sync
- Codex atomic write success and failure rollback
- Backup retention policy with proper mtime ordering
- Negative cases: missing auth field, invalid provider ID
This commit is contained in:
Jason
2025-10-28 09:55:10 +08:00
parent 10abdfa096
commit 8e980e6974
7 changed files with 450 additions and 81 deletions
+2 -2
View File
@@ -28,8 +28,8 @@ pub fn create_backup(config_path: &PathBuf) -> Result<String, AppError> {
let backup_path = backup_dir.join(format!("{}.json", backup_id));
// 复制配置文件到备份
fs::copy(config_path, &backup_path).map_err(|e| AppError::io(&backup_path, e))?;
let contents = fs::read(config_path).map_err(|e| AppError::io(config_path, e))?;
fs::write(&backup_path, contents).map_err(|e| AppError::io(&backup_path, e))?;
// 备份完成后清理旧的备份文件(仅保留最近 MAX_BACKUPS 份)
cleanup_old_backups(&backup_dir, MAX_BACKUPS)?;