codetree 0.1.2

AI-native version control system for autonomous agents
Documentation
mod common;
use common::*;

#[tokio::test]
async fn test_import_from_git() -> Result<()> {
    let _serial = WORKDIR_LOCK.lock().await;
    let temp_dir = tempfile::TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();
    let _guard = DirGuard::new(&repo_path);

    // Skip if git is not available.
    if std::process::Command::new("git").arg("--version").output().is_err() {
        eprintln!("Skipping test_import_from_git: git not available");
        return Ok(());
    }

    // Initialize a Git repo.
    std::process::Command::new("git")
        .args(["init"])
        .current_dir(&repo_path)
        .output()
        .with_context(|| "git init failed")?;

    // Disable hooks for the test.
    std::process::Command::new("git")
        .args(["config", "core.hooksPath", "/dev/null"])
        .current_dir(&repo_path)
        .output()?;

    // Create commits.
    std::fs::write(repo_path.join("file1.txt"), "content 1\n")?;
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&repo_path)
        .output()?;
    std::process::Command::new("git")
        .args(["commit", "-m", "first commit"])
        .current_dir(&repo_path)
        .output()?;

    std::fs::write(repo_path.join("file2.txt"), "content 2\n")?;
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&repo_path)
        .output()?;
    std::process::Command::new("git")
        .args(["commit", "-m", "second commit"])
        .current_dir(&repo_path)
        .output()?;

    // Import into CodeTree.
    commands::import_command(false).await?;

    // Verify CodeTree has the commits.
    let git_manager = VcsManager::new(repo_path.clone())?;
    let head = git_manager.get_head_hash()?;
    let head_commit = git_manager.repo().read_commit(&head)?;
    assert_eq!(head_commit.message, "second commit");

    // Check parent commit.
    assert_eq!(head_commit.parents.len(), 1);
    let parent_commit = git_manager.repo().read_commit(&head_commit.parents[0])?;
    assert_eq!(parent_commit.message, "first commit");

    // Check files exist.
    assert!(repo_path.join("file1.txt").exists());
    assert!(repo_path.join("file2.txt").exists());

    Ok(())
}

#[tokio::test]
async fn test_export_to_git() -> Result<()> {
    let _serial = WORKDIR_LOCK.lock().await;
    let temp_dir = tempfile::TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();
    let _guard = DirGuard::new(&repo_path);

    // Skip if git is not available.
    if std::process::Command::new("git").arg("--version").output().is_err() {
        eprintln!("Skipping test_export_to_git: git not available");
        return Ok(());
    }

    // Initialize CodeTree.
    commands::init_command(false).await?;
    let git_manager = VcsManager::new(repo_path.clone())?;

    // Create commits in CodeTree.
    tokio::fs::write(repo_path.join("file1.txt"), "content 1\n").await?;
    git_manager.add_all().await?;
    git_manager.commit("first commit").await?;

    tokio::fs::write(repo_path.join("file2.txt"), "content 2\n").await?;
    git_manager.add_all().await?;
    git_manager.commit("second commit").await?;

    // Export to Git.
    commands::export_command().await?;

    // Verify Git has the commits.
    let log = std::process::Command::new("git")
        .args(["log", "--oneline"])
        .current_dir(&repo_path)
        .output()
        .context("git log failed")?;

    let log_str = String::from_utf8_lossy(&log.stdout);
    assert!(log_str.contains("second commit"), "Git should have 'second commit'");
    assert!(log_str.contains("first commit"), "Git should have 'first commit'");

    Ok(())
}

#[tokio::test]
async fn test_bundle_and_unbundle() -> Result<()> {
    let _serial = WORKDIR_LOCK.lock().await;
    let temp_dir = tempfile::TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();
    let _guard = DirGuard::new(&repo_path);

    // Skip if tar is not available.
    if std::process::Command::new("tar").arg("--version").output().is_err() {
        eprintln!("Skipping test_bundle_and_unbundle: tar not available");
        return Ok(());
    }

    // Initialize CodeTree.
    commands::init_command(false).await?;
    let git_manager = VcsManager::new(repo_path.clone())?;

    // Create a commit.
    tokio::fs::write(repo_path.join("file.txt"), "content\n").await?;
    git_manager.add_all().await?;
    git_manager.commit("test commit").await?;

    let head_before = git_manager.get_head_hash()?;

    // Create a bundle.
    let bundle_path = repo_path.join("test-bundle.tar.gz");
    commands::bundle_command(Some(bundle_path.to_string_lossy().to_string())).await?;

    // Verify the bundle file exists.
    assert!(bundle_path.exists(), "Bundle file should exist");

    // Unbundle into a new directory.
    let restore_dir = tempfile::TempDir::new()?;
    let restore_path = restore_dir.path().to_path_buf();

    // Copy the bundle to the restore directory.
    std::fs::copy(&bundle_path, restore_path.join("test-bundle.tar.gz"))?;

    // Change to the restore directory and unbundle.
    let original_dir = std::env::current_dir()?;
    std::env::set_current_dir(&restore_path)?;

    let result = commands::unbundle_command("test-bundle.tar.gz".to_string()).await;
    let _ = std::env::set_current_dir(&original_dir);
    result?;

    // Verify the restored repo has the same HEAD.
    let restored_manager = VcsManager::new(restore_path.clone())?;
    let head_after = restored_manager.get_head_hash()?;
    assert_eq!(head_before, head_after, "HEAD should match after unbundle");

    // Verify the file exists.
    assert!(restore_path.join("file.txt").exists(), "File should exist after unbundle");

    Ok(())
}