toak-rs 6.0.3

A high-performance library and CLI tool for tokenizing git repositories, cleaning code, and generating embeddings
Documentation
//! Integration tests for the CLI commands

use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;

fn unique_test_dir(test_name: &str) -> std::path::PathBuf {
    let unique = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system time should be after unix epoch")
        .as_nanos();
    std::env::temp_dir().join(format!(
        "toak-rs-cli-{}-{}",
        test_name,
        unique
    ))
}

fn init_git_repo(dir: &std::path::Path) {
    std::fs::create_dir_all(dir).expect("failed to create temp repo");
    std::fs::write(dir.join("lib.rs"), "pub fn answer() -> i32 {\n    42\n}\n")
        .expect("failed to write tracked file");

    let status = std::process::Command::new("git")
        .args(["init", "-q"])
        .current_dir(dir)
        .status()
        .expect("failed to run git init");
    assert!(status.success());

    let status = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(dir)
        .status()
        .expect("failed to run git add");
    assert!(status.success());
}

#[test]
fn test_version_command() {
    let mut cmd = cargo_bin_cmd!("toak");
    cmd.arg("version");

    cmd.assert()
        .success()
        .stdout(predicate::str::starts_with("toak "));
}

#[test]
fn test_version_flag() {
    let mut cmd = cargo_bin_cmd!("toak");
    cmd.arg("--version");

    cmd.assert()
        .success()
        .stdout(predicate::str::starts_with("toak "));
}

#[test]
fn test_version_short_flag() {
    let mut cmd = cargo_bin_cmd!("toak");
    cmd.arg("-V");

    cmd.assert()
        .success()
        .stdout(predicate::str::starts_with("toak "));
}

#[test]
fn test_generate_does_not_create_embeddings_by_default() {
    let dir = unique_test_dir("generate-no-embeddings");
    init_git_repo(&dir);

    let mut cmd = cargo_bin_cmd!("toak");
    cmd.args([
        "generate",
        "--dir",
        dir.to_str().expect("temp dir should be utf-8"),
        "--output-file-path",
        dir.join("prompt.md")
            .to_str()
            .expect("output path should be utf-8"),
        "--quiet",
    ]);

    cmd.assert().success();
    assert!(dir.join("prompt.md").exists());
    assert!(!dir.join("embeddings.json").exists());
}