Files
markitect-main/tddai_cli.py
Bernd Worsch 5155a548eb feat: implement tddai Python library for TDD workspace management
- Create comprehensive tddai package with workspace, issue fetcher, and test generator modules
- Add Python CLI interface (tddai_cli.py) to replace complex Makefile shell logic
- Update Makefile targets to use Python CLI for better maintainability
- Implement proper behavior-based tests instead of file existence checks
- Add workspace lifecycle management (create, active, finish, cleanup)
- Add issue fetching from Gitea API with error handling
- Add comprehensive test coverage with 19 passing tests
- Support environment variable configuration for different deployments

This addresses issue #11: Setup TDD workspace infrastructure
All tests pass and the system achieves green state before commit.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-22 02:04:19 +02:00

331 lines
11 KiB
Python

#!/usr/bin/env python3
"""
CLI interface for tddai library.
"""
import sys
import argparse
from pathlib import Path
# Add current directory to path so we can import tddai
sys.path.insert(0, str(Path(__file__).parent))
from tddai import (
WorkspaceManager, IssueFetcher, TestGenerator,
WorkspaceStatus, TddaiError
)
def workspace_status():
"""Show current workspace status."""
try:
manager = WorkspaceManager()
status = manager.get_status()
if status == WorkspaceStatus.CLEAN:
print("📋 No active issue workspace")
print(" Use 'make start-issue NUM=X' to begin working on an issue")
return
if status == WorkspaceStatus.DIRTY:
print("⚠️ Workspace directory exists but no current issue file")
print(" Run 'make finish-issue' to clean up or 'make start-issue' to create new workspace")
return
workspace = manager.get_current_workspace()
if not workspace:
print("❌ Failed to load workspace")
return
print("📋 Active Issue Workspace")
print("========================")
print()
print(f"🎯 Issue #{workspace.issue_number}: {workspace.issue_title}")
print(f"📊 Status: {workspace.issue_state}")
print(f"📁 Workspace: {workspace.workspace_dir}/issue_{workspace.issue_number}/")
print()
if workspace.tests_dir.exists():
test_files = list(workspace.tests_dir.glob("*.py"))
print(f"🧪 Generated Tests ({len(test_files)}):")
if test_files:
for test_file in test_files:
print(f" - {test_file.name}")
else:
print(" - No tests generated yet")
print()
print("📋 Workspace Files:")
print(" - requirements.md (review and break down issue)")
print(" - test_plan.md (plan test scenarios)")
print(" - tests/ (generated test files)")
print()
print("💡 Commands:")
print(" - make add-test (generate another test)")
print(" - make finish-issue (complete and move tests to main)")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def start_issue(issue_number: int):
"""Start working on an issue."""
try:
manager = WorkspaceManager()
fetcher = IssueFetcher()
# Check if workspace already active
status = manager.get_status()
if status == WorkspaceStatus.ACTIVE:
current = manager.get_current_workspace()
print(f"⚠️ Already working on issue #{current.issue_number}")
print(" Run 'make finish-issue' first or 'make workspace-status' to see details")
sys.exit(1)
print(f"🔍 Starting work on issue #{issue_number}...")
print(f"📋 Fetching issue #{issue_number} details...")
# Fetch issue data
issue_data = fetcher.get_issue_data_dict(issue_number)
# Create workspace
workspace = manager.create_workspace(issue_data)
print(f"✅ Workspace created for issue #{issue_number}")
print(f"📁 Workspace: {workspace.workspace_dir}/issue_{issue_number}/")
print(f"📋 Requirements: {workspace.requirements_file}")
print(f"🧪 Test plan: {workspace.test_plan_file}")
print()
print("💡 Next steps:")
print(" 1. Review requirements.md and break down the issue")
print(" 2. Plan test scenarios in test_plan.md")
print(" 3. Use 'make add-test' to generate tests")
print(" 4. Use 'make finish-issue' when complete")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def finish_issue():
"""Finish current issue workspace."""
try:
manager = WorkspaceManager()
workspace = manager.get_current_workspace()
if not workspace:
print("❌ No active issue workspace")
print(" Nothing to finish")
sys.exit(1)
print(f"🏁 Finishing work on issue #{workspace.issue_number}")
print()
# Check for tests
if workspace.tests_dir.exists():
test_files = list(workspace.tests_dir.glob("*.py"))
if test_files:
print(f"📦 Moving {len(test_files)} test(s) to tests/ directory...")
print("✅ Tests moved to main tests/ directory")
else:
print("⚠️ No tests found in workspace")
# Finish workspace (moves tests and cleans up)
manager.finish_workspace()
print("🧹 Cleaning up workspace...")
print(f"✅ Issue #{workspace.issue_number} workspace cleaned up")
print()
print("💡 Next steps:")
print(" - Run 'make test' to verify tests fail (red state)")
print(" - Implement code to make tests pass (green state)")
print(" - Start next issue with 'make start-issue NUM=X'")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def add_test_guidance():
"""Show guidance for adding tests."""
try:
manager = WorkspaceManager()
workspace = manager.get_current_workspace()
if not workspace:
print("❌ No active issue workspace")
print(" Run 'make start-issue NUM=X' first")
sys.exit(1)
print(f"🧪 Adding test to issue #{workspace.issue_number} workspace")
print()
print(f"📋 Issue: {workspace.issue_title}")
print(f"📁 Workspace: {workspace.workspace_dir}/issue_{workspace.issue_number}/")
print()
print("🤖 Please ask Claude Code to generate a test:")
print()
print(" Command: 'Generate a test for the current workspace issue'")
print()
print("📝 Test Requirements:")
print(f" - Save test in: {workspace.tests_dir}/")
print(f" - Name format: test_issue_{workspace.issue_number}_<scenario>.py")
print(f" - Include docstring referencing issue #{workspace.issue_number}")
print(" - Follow TDD principles (test should fail initially)")
print(" - Review requirements.md and test_plan.md for context")
print()
print("📋 Issue Details:")
print(f" Title: {workspace.issue_title}")
print(f" Description: {workspace.issue_body}")
print()
print("💡 After generation: Use 'make workspace-status' to see all tests")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def list_issues():
"""List all issues."""
try:
fetcher = IssueFetcher()
print("📋 MarkiTect Issues")
print("==================")
print()
issues = fetcher.fetch_issues()
if not issues:
print("No issues found")
return
for issue in issues:
status_icon = "🟢" if issue.state == "open" else "🔴"
print(f"{status_icon} #{issue.number}: {issue.title}")
print(f" Status: {issue.state.upper()} | Created: {issue.created_at.strftime('%Y-%m-%d')}")
# Truncate body for list view
body_preview = issue.body[:80] + "..." if len(issue.body) > 80 else issue.body
if body_preview:
print(f" {body_preview}")
print()
print("💡 Tip: Use 'make show-issue NUM=X' for full details")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def list_open_issues():
"""List only open issues."""
try:
fetcher = IssueFetcher()
print("📋 Open MarkiTect Issues (Active Backlog)")
print("========================================")
print()
issues = fetcher.fetch_open_issues()
if not issues:
print("No open issues found")
return
for issue in issues:
print(f"[OPEN] #{issue.number}: {issue.title}")
print(f" Created: {issue.created_at.strftime('%Y-%m-%d')} | Updated: {issue.updated_at.strftime('%Y-%m-%d')}")
# Truncate body for list view
body_preview = issue.body[:80] + "..." if len(issue.body) > 80 else issue.body
if body_preview:
print(f" {body_preview}")
print()
print("💡 Tip: Use 'make show-issue NUM=X' for full details or 'make list-issues' for all issues")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def show_issue(issue_number: int):
"""Show detailed issue information."""
try:
fetcher = IssueFetcher()
print(f"🔍 Issue #{issue_number} Details")
print("=======================")
print()
issue = fetcher.fetch_issue(issue_number)
print(f"**Title:** {issue.title}")
print(f"**Status:** {issue.state.upper()}")
print(f"**Number:** #{issue.number}")
print(f"**Created:** {issue.created_at.strftime('%Y-%m-%d %H:%M')}")
print(f"**Updated:** {issue.updated_at.strftime('%Y-%m-%d %H:%M')}")
print(f"**URL:** {issue.html_url}")
if issue.assignee:
print(f"**Assignee:** {issue.assignee}")
if issue.labels:
print(f"**Labels:** {', '.join(issue.labels)}")
print()
print("**Description:**")
print(issue.body)
print()
print("💡 Tip: Use 'make list-issues' to see all issues")
except TddaiError as e:
print(f"❌ Error: {e}")
sys.exit(1)
def main():
"""Main CLI entry point."""
parser = argparse.ArgumentParser(description="tddai CLI tool")
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Workspace commands
subparsers.add_parser('workspace-status', help='Show workspace status')
start_parser = subparsers.add_parser('start-issue', help='Start working on issue')
start_parser.add_argument('issue_number', type=int, help='Issue number')
subparsers.add_parser('finish-issue', help='Finish current issue')
subparsers.add_parser('add-test', help='Show guidance for adding tests')
# Issue commands
subparsers.add_parser('list-issues', help='List all issues')
subparsers.add_parser('list-open-issues', help='List open issues')
show_parser = subparsers.add_parser('show-issue', help='Show issue details')
show_parser.add_argument('issue_number', type=int, help='Issue number')
args = parser.parse_args()
if not args.command:
parser.print_help()
return
try:
if args.command == 'workspace-status':
workspace_status()
elif args.command == 'start-issue':
start_issue(args.issue_number)
elif args.command == 'finish-issue':
finish_issue()
elif args.command == 'add-test':
add_test_guidance()
elif args.command == 'list-issues':
list_issues()
elif args.command == 'list-open-issues':
list_open_issues()
elif args.command == 'show-issue':
show_issue(args.issue_number)
except KeyboardInterrupt:
print("\n⚠️ Operation cancelled")
sys.exit(1)
if __name__ == '__main__':
main()