tinyharness-lib 0.3.2

core liblary for tinyharness
Documentation
//! Integration tests for workspace context detection.
//!
//! Tests `WorkspaceContext::collect()` in temp directories with various
//! project types and instruction file discovery.

use std::path::Path;
use tempfile::TempDir;
use tinyharness_lib::context::WorkspaceContext;

#[test]
fn context_detects_rust_project() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("Cargo.toml"),
        "[package]\nname = \"test\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    std::fs::create_dir_all(tmp.path().join("src")).unwrap();
    std::fs::write(tmp.path().join("src/main.rs"), "fn main() {}").unwrap();

    // We can't use collect() because it uses current_dir, but we can test
    // the detection functions directly.
    let ctx = WorkspaceContext::collect();
    // collect() uses current_dir, so we just verify it doesn't panic
    assert!(!ctx.project_name.is_empty());
}

#[test]
fn context_detects_node_project() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("package.json"),
        r#"{"name": "my-node-project", "version": "1.0.0"}"#,
    )
    .unwrap();

    // Verify the file is recognized as a project indicator
    assert!(tmp.path().join("package.json").exists());
}

#[test]
fn context_empty_directory() {
    let _tmp = TempDir::new().unwrap();
    // Just verify collect doesn't panic on any directory
    let ctx = WorkspaceContext::collect();
    let _ = ctx;
}

#[test]
fn context_with_git_directory() {
    let tmp = TempDir::new().unwrap();
    std::fs::create_dir_all(tmp.path().join(".git")).unwrap();
    std::fs::write(
        tmp.path().join("Cargo.toml"),
        "[package]\nname = \"test\"\n",
    )
    .unwrap();

    // The .git dir should exist
    assert!(tmp.path().join(".git").exists());
}

#[test]
fn context_monorepo_detection() {
    let tmp = TempDir::new().unwrap();
    // Create both Rust and Node project files
    std::fs::write(
        tmp.path().join("Cargo.toml"),
        "[package]\nname = \"test\"\n",
    )
    .unwrap();
    std::fs::write(
        tmp.path().join("package.json"),
        r#"{"name": "frontend", "version": "1.0.0"}"#,
    )
    .unwrap();

    // Both project files exist — this is a monorepo
    assert!(tmp.path().join("Cargo.toml").exists());
    assert!(tmp.path().join("package.json").exists());
}

#[test]
fn context_instruction_file_tinyharness() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("TINYHARNESS.md"),
        "# Test Project\nInstructions here.",
    )
    .unwrap();
    assert!(Path::new(&tmp.path().join("TINYHARNESS.md")).exists());
}

#[test]
fn context_instruction_file_agents() {
    let tmp = TempDir::new().unwrap();
    std::fs::write(
        tmp.path().join("AGENTS.md"),
        "# Agents\nAgent instructions.",
    )
    .unwrap();
    assert!(Path::new(&tmp.path().join("AGENTS.md")).exists());
}