use crate::integration::test_helpers::{create_test_repo_with_commits, run_cc_with_timeout};
use std::env;
use tempfile::TempDir;
#[tokio::test]
async fn test_force_push_safety_with_backup() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 3).expect("Failed to create test repository");
let feature_branch = "feature/test-branch";
let head_commit = repo.head().unwrap().peel_to_commit().unwrap();
repo.branch(feature_branch, &head_commit, false)
.expect("Failed to create feature branch");
env::set_var("FORCE_PUSH_NO_CONFIRM", "1");
use cascade_cli::git::GitRepository;
let _git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
env::remove_var("FORCE_PUSH_NO_CONFIRM");
}
#[tokio::test]
async fn test_force_push_safety_ci_environment() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 2).expect("Failed to create test repository");
env::set_var("CI", "true");
use cascade_cli::git::GitRepository;
let _git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
assert!(env::var("CI").is_ok());
env::remove_var("CI");
}
#[tokio::test]
async fn test_force_push_safety_no_data_loss() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 2).expect("Failed to create test repository");
use cascade_cli::git::GitRepository;
let _git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
}
#[tokio::test]
async fn test_backup_branch_creation() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (repo, commit_oids) =
create_test_repo_with_commits(repo_path, 3).expect("Failed to create test repository");
use cascade_cli::git::GitRepository;
let _git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
let test_branch_name = "test-backup-branch";
let commit_oid = git2::Oid::from_str(&commit_oids[1]).expect("Valid commit OID");
let commit = repo.find_commit(commit_oid).expect("Should find commit");
let result = repo.branch(test_branch_name, &commit, false);
assert!(result.is_ok(), "Should be able to create backup branch");
let branch_ref = repo.find_reference(&format!("refs/heads/{test_branch_name}"));
assert!(branch_ref.is_ok(), "Backup branch should exist");
}
#[tokio::test]
async fn test_force_push_safety_platform_behavior() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 2).expect("Failed to create test repository");
use cascade_cli::git::GitRepository;
let git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
assert!(git_repo.path().exists());
}
#[tokio::test]
async fn test_force_push_safety_error_handling() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 2).expect("Failed to create test repository");
use cascade_cli::git::GitRepository;
let _git_repo = GitRepository::open(repo_path).expect("Failed to open repository");
}
#[tokio::test]
async fn test_cli_force_push_safety_integration() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 3).expect("Failed to create test repository");
let original_dir = env::current_dir().expect("Failed to get current directory");
env::set_current_dir(repo_path).expect("Failed to change directory");
let init_result = run_cc_with_timeout(&["init", "--force"], 30000).await;
assert!(init_result.status.success(), "Cascade init should succeed");
env::set_current_dir(original_dir).expect("Failed to restore directory");
}
#[tokio::test]
async fn test_concurrent_force_push_safety() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let repo_path = temp_dir.path();
let (_repo, _commit_oids) =
create_test_repo_with_commits(repo_path, 2).expect("Failed to create test repository");
use cascade_cli::git::GitRepository;
let git_repo1 = GitRepository::open(repo_path).expect("Failed to open repository 1");
let git_repo2 = GitRepository::open(repo_path).expect("Failed to open repository 2");
assert!(git_repo1.path().exists());
assert!(git_repo2.path().exists());
}