fp-dotfiles-manager 0.2.5

Minimal, zero-dependency Chezmoi-based dotfiles manager
mod common;

use common::*;
use std::process::Command;

#[tokio::test]
async fn test_setup_brand_new_repository() {
    // 1. Spin up the test Fedora container
    let container = setup_fedora_container().await;
    let container_id = container.id();

    // 2. Generate a valid SSH key pair inside the container so ssh-keygen -y succeeds
    let status = Command::new("docker")
        .args(&[
            "exec",
            container_id,
            "mkdir",
            "-p",
            "/root/.config/fp-dotfiles-manager",
        ])
        .status()
        .expect("Failed to create config dir");
    assert!(status.success());

    let status = Command::new("docker")
        .args(&[
            "exec",
            container_id,
            "ssh-keygen",
            "-t",
            "ed25519",
            "-N",
            "",
            "-f",
            "/root/.config/fp-dotfiles-manager/ssh_gitlab",
        ])
        .status()
        .expect("Failed to generate ed25519 SSH keys");
    assert!(status.success(), "Failed to generate valid SSH keys inside container");

    // Configure global git user name and email so git commit works inside the container
    let status = Command::new("docker")
        .args(&[
            "exec",
            container_id,
            "git",
            "config",
            "--global",
            "user.name",
            "Test User",
        ])
        .status()
        .expect("Failed to configure git user.name");
    assert!(status.success());

    let status = Command::new("docker")
        .args(&[
            "exec",
            container_id,
            "git",
            "config",
            "--global",
            "user.email",
            "test@example.com",
        ])
        .status()
        .expect("Failed to configure git user.email");
    assert!(status.success());

    // 3. Run setup pointing to a non-existent remote repository to trigger Scenario B fallback
    let stdout = run_manager_success(
        &container,
        &[
            "setup",
            "--repo-url",
            "/tmp/non-existent-repo.git",
            "--ssh-key",
            "/root/.config/fp-dotfiles-manager/ssh_gitlab",
            "--force",
        ],
    ).await;

    // Verify key steps are executed
    assert!(stdout.contains("Initializing a brand new dotfiles repository locally"));
    assert!(stdout.contains("Generating brand new age encryption key pair"));
    assert!(stdout.contains("Generated default .chezmoi.toml.tmpl in the dotfiles repository"));
    assert!(stdout.contains("Generated ~/.config/chezmoi/chezmoi.toml for immediate use"));
    assert!(stdout.contains("Created initial commit in the local dotfiles repository"));

    // 4. Verify the files exist inside the container
    let age_key = read_file_in_container(&container, "/root/.config/chezmoi/.age-private-key.txt");
    assert!(age_key.contains("AGE-SECRET-KEY-"));

    let chezmoi_toml = read_file_in_container(&container, "/root/.config/chezmoi/chezmoi.toml");
    assert!(chezmoi_toml.contains("encryption = \"age\""));
    assert!(chezmoi_toml.contains("/root/.config/chezmoi/.age-private-key.txt"));

    let tmpl = read_file_in_container(&container, "/root/.local/share/chezmoi/.chezmoi.toml.tmpl");
    assert!(tmpl.contains("encryption = \"age\""));
    assert!(tmpl.contains(".chezmoi.homeDir"));

    // 5. Verify Git remote is registered
    let git_remote = Command::new("docker")
        .args(&[
            "exec",
            container_id,
            "git",
            "-C",
            "/root/.local/share/chezmoi",
            "remote",
            "get-url",
            "origin",
        ])
        .output()
        .expect("Failed to query git remote URL");
    assert!(git_remote.status.success());
    let remote_url = String::from_utf8_lossy(&git_remote.stdout).trim().to_string();
    assert_eq!(remote_url, "/tmp/non-existent-repo.git");
}