Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d4234378f | |||
| d1fbccde39 | |||
| ed93df698b | |||
| 8872d33e32 | |||
| 3b3ad75c1b | |||
| 8ece0009ee |
-12
@@ -1,12 +0,0 @@
|
||||
[run]
|
||||
parallel = true
|
||||
source = apps/backend/cli
|
||||
omit =
|
||||
*/tests/*
|
||||
*/__pycache__/*
|
||||
*/.venv/*
|
||||
|
||||
[report]
|
||||
exclude_lines =
|
||||
pragma: no cover
|
||||
if __name__ == "__main__":
|
||||
+20
-12
@@ -97,8 +97,9 @@ repos:
|
||||
- id: ruff-format
|
||||
files: ^apps/backend/
|
||||
|
||||
# Python tests (apps/backend/) - run full test suite from project root
|
||||
# Python tests (apps/backend/) - skip slow/integration tests for pre-commit speed
|
||||
# Tests to skip: graphiti (external deps), merge_file_tracker/service_orchestrator/worktree/workspace (Windows path/git issues)
|
||||
# NOTE: Skip this hook in worktrees (where .git is a file, not a directory)
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: pytest
|
||||
@@ -107,24 +108,31 @@ repos:
|
||||
args:
|
||||
- -c
|
||||
- |
|
||||
# Run pytest directly from project root
|
||||
if [ -f "apps/backend/.venv/bin/pytest" ]; then
|
||||
PYTEST_CMD="apps/backend/.venv/bin/pytest"
|
||||
elif [ -f "apps/backend/.venv/Scripts/pytest.exe" ]; then
|
||||
PYTEST_CMD="apps/backend/.venv/Scripts/pytest.exe"
|
||||
# Skip in worktrees - .git is a file pointing to main repo, not a directory
|
||||
# This prevents path resolution issues with ../../tests/ in worktree context
|
||||
if [ -f ".git" ]; then
|
||||
echo "Skipping pytest in worktree (path resolution would fail)"
|
||||
exit 0
|
||||
fi
|
||||
cd apps/backend
|
||||
if [ -f ".venv/bin/pytest" ]; then
|
||||
PYTEST_CMD=".venv/bin/pytest"
|
||||
elif [ -f ".venv/Scripts/pytest.exe" ]; then
|
||||
PYTEST_CMD=".venv/Scripts/pytest.exe"
|
||||
else
|
||||
PYTEST_CMD="python -m pytest"
|
||||
fi
|
||||
$PYTEST_CMD tests/ \
|
||||
PYTHONPATH=. $PYTEST_CMD \
|
||||
../../tests/ \
|
||||
-v \
|
||||
--tb=short \
|
||||
-x \
|
||||
-m "not slow and not integration" \
|
||||
--ignore=tests/test_graphiti.py \
|
||||
--ignore=tests/test_merge_file_tracker.py \
|
||||
--ignore=tests/test_service_orchestrator.py \
|
||||
--ignore=tests/test_worktree.py \
|
||||
--ignore=tests/test_workspace.py
|
||||
--ignore=../../tests/test_graphiti.py \
|
||||
--ignore=../../tests/test_merge_file_tracker.py \
|
||||
--ignore=../../tests/test_service_orchestrator.py \
|
||||
--ignore=../../tests/test_worktree.py \
|
||||
--ignore=../../tests/test_workspace.py
|
||||
language: system
|
||||
files: ^(apps/backend/.*\.py$|tests/.*\.py$)
|
||||
pass_filenames: false
|
||||
|
||||
@@ -35,18 +35,18 @@
|
||||
> ⚠️ Beta releases may contain bugs and breaking changes. [View all releases](https://github.com/AndyMik90/Auto-Claude/releases)
|
||||
|
||||
<!-- BETA_VERSION_BADGE -->
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.3)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.4)
|
||||
<!-- BETA_VERSION_BADGE_END -->
|
||||
|
||||
<!-- BETA_DOWNLOADS -->
|
||||
| Platform | Download |
|
||||
|----------|----------|
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.3-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.3-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.3-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-beta.3-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.3-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.3-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-x86_64.flatpak) |
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.4-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.4-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.4-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-beta.4-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.4-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.4-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.4/Auto-Claude-2.7.6-beta.4-linux-x86_64.flatpak) |
|
||||
<!-- BETA_DOWNLOADS_END -->
|
||||
|
||||
> All releases include SHA256 checksums and VirusTotal scan results for security verification.
|
||||
|
||||
@@ -67,10 +67,4 @@ tests/
|
||||
|
||||
# Auto Claude data directory
|
||||
.auto-claude/
|
||||
|
||||
# Auto Claude generated files
|
||||
.auto-claude-security.json
|
||||
.auto-claude-status
|
||||
.security-key
|
||||
logs/security/
|
||||
coverage.json
|
||||
|
||||
@@ -19,5 +19,5 @@ Quick Start:
|
||||
See README.md for full documentation.
|
||||
"""
|
||||
|
||||
__version__ = "2.7.6-beta.3"
|
||||
__version__ = "2.7.6-beta.4"
|
||||
__author__ = "Auto Claude Team"
|
||||
|
||||
@@ -292,6 +292,14 @@ AGENT_CONFIGS = {
|
||||
"auto_claude_tools": [],
|
||||
"thinking_default": "high",
|
||||
},
|
||||
"pr_followup_extraction": {
|
||||
# Lightweight extraction call for recovering data when structured output fails
|
||||
# Pure structured output extraction, no tools needed
|
||||
"tools": [],
|
||||
"mcp_servers": [],
|
||||
"auto_claude_tools": [],
|
||||
"thinking_default": "low",
|
||||
},
|
||||
"pr_finding_validator": {
|
||||
# Standalone validator for re-checking findings against actual code
|
||||
# Called separately from orchestrator to validate findings with fresh context
|
||||
|
||||
@@ -10,7 +10,6 @@ import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from qa.criteria import is_fixes_applied, is_qa_approved, is_qa_rejected
|
||||
from ui import highlight, print_status
|
||||
|
||||
|
||||
@@ -152,22 +151,13 @@ def handle_batch_status_command(project_dir: str) -> bool:
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# Determine status (highest priority first)
|
||||
# Use authoritative QA status check, not just file existence
|
||||
if is_qa_approved(spec_dir):
|
||||
status = "qa_approved"
|
||||
elif is_qa_rejected(spec_dir):
|
||||
status = "qa_rejected"
|
||||
elif is_fixes_applied(spec_dir):
|
||||
status = "fixes_applied"
|
||||
elif (spec_dir / "implementation_plan.json").exists():
|
||||
# Check if there's a qa_report.md but no approval yet (QA in progress)
|
||||
if (spec_dir / "qa_report.md").exists():
|
||||
status = "qa_in_progress"
|
||||
else:
|
||||
status = "building"
|
||||
elif (spec_dir / "spec.md").exists():
|
||||
# Determine status
|
||||
if (spec_dir / "spec.md").exists():
|
||||
status = "spec_created"
|
||||
elif (spec_dir / "implementation_plan.json").exists():
|
||||
status = "building"
|
||||
elif (spec_dir / "qa_report.md").exists():
|
||||
status = "qa_approved"
|
||||
else:
|
||||
status = "pending_spec"
|
||||
|
||||
@@ -175,10 +165,7 @@ def handle_batch_status_command(project_dir: str) -> bool:
|
||||
"pending_spec": "⏳",
|
||||
"spec_created": "📋",
|
||||
"building": "⚙️",
|
||||
"qa_in_progress": "🔍",
|
||||
"qa_approved": "✅",
|
||||
"qa_rejected": "❌",
|
||||
"fixes_applied": "🔧",
|
||||
"unknown": "❓",
|
||||
}.get(status, "❓")
|
||||
|
||||
@@ -205,10 +192,10 @@ def handle_batch_cleanup_command(project_dir: str, dry_run: bool = True) -> bool
|
||||
print_status("No specs directory found", "info")
|
||||
return True
|
||||
|
||||
# Find completed specs (only QA-approved, matching status display logic)
|
||||
# Find completed specs
|
||||
completed = []
|
||||
for spec_dir in specs_dir.iterdir():
|
||||
if spec_dir.is_dir() and is_qa_approved(spec_dir):
|
||||
if spec_dir.is_dir() and (spec_dir / "qa_report.md").exists():
|
||||
completed.append(spec_dir.name)
|
||||
|
||||
if not completed:
|
||||
|
||||
@@ -449,7 +449,7 @@ def _handle_build_interrupt(
|
||||
if choice == "skip":
|
||||
print()
|
||||
print_status("Resuming build...", "info")
|
||||
status_manager.update(state=BuildState.BUILDING)
|
||||
status_manager.update(state=BuildState.RUNNING)
|
||||
asyncio.run(
|
||||
run_autonomous_agent(
|
||||
project_dir=working_dir,
|
||||
|
||||
@@ -186,14 +186,12 @@ def _before_send(event: dict, hint: dict) -> dict | None:
|
||||
|
||||
def init_sentry(
|
||||
component: str = "backend",
|
||||
force_enable: bool = False,
|
||||
) -> bool:
|
||||
"""
|
||||
Initialize Sentry for the Python backend.
|
||||
|
||||
Args:
|
||||
component: Component name for tagging (e.g., "backend", "github-runner")
|
||||
force_enable: Force enable even without packaged app detection
|
||||
|
||||
Returns:
|
||||
True if Sentry was initialized, False otherwise
|
||||
@@ -212,20 +210,11 @@ def init_sentry(
|
||||
logger.debug("[Sentry] No SENTRY_DSN configured - error reporting disabled")
|
||||
return False
|
||||
|
||||
# Check if we should enable Sentry
|
||||
# Enable if:
|
||||
# - Running from packaged app (detected by __compiled__ or frozen)
|
||||
# - SENTRY_DEV=true is set
|
||||
# - force_enable is True
|
||||
# DSN is present (checked above), so Sentry should be enabled.
|
||||
# The Electron main process only passes SENTRY_DSN to subprocesses in
|
||||
# production builds, so its presence is sufficient to gate activation.
|
||||
# In dev, set SENTRY_DSN in your environment to opt-in.
|
||||
is_packaged = getattr(sys, "frozen", False) or hasattr(sys, "__compiled__")
|
||||
sentry_dev = os.environ.get("SENTRY_DEV", "").lower() in ("true", "1", "yes")
|
||||
should_enable = is_packaged or sentry_dev or force_enable
|
||||
|
||||
if not should_enable:
|
||||
logger.debug(
|
||||
"[Sentry] Development mode - error reporting disabled (set SENTRY_DEV=true to enable)"
|
||||
)
|
||||
return False
|
||||
|
||||
try:
|
||||
import sentry_sdk
|
||||
|
||||
@@ -430,7 +430,6 @@ class WorktreeManager:
|
||||
if os.path.samefile(resolved_path, current_path):
|
||||
return line[len("branch refs/heads/") :]
|
||||
except OSError:
|
||||
# File system comparison errors are handled by fallback below
|
||||
pass
|
||||
# Fallback to normalized case comparison
|
||||
if os.path.normcase(str(resolved_path)) == os.path.normcase(
|
||||
@@ -511,7 +510,6 @@ class WorktreeManager:
|
||||
if os.path.samefile(resolved_path, registered_path):
|
||||
return True
|
||||
except OSError:
|
||||
# File system errors handled by fallback comparison below
|
||||
pass
|
||||
# Fallback to normalized case comparison for non-existent paths
|
||||
if os.path.normcase(str(resolved_path)) == os.path.normcase(
|
||||
|
||||
@@ -215,7 +215,6 @@ async def run_qa_validation_loop(
|
||||
"Removed QA_FIX_REQUEST.md after permanent fixer error",
|
||||
)
|
||||
except OSError:
|
||||
# File removal failure is not critical here
|
||||
pass
|
||||
return False
|
||||
|
||||
@@ -231,7 +230,6 @@ async def run_qa_validation_loop(
|
||||
fix_request_file.unlink()
|
||||
debug("qa_loop", "Removed processed QA_FIX_REQUEST.md")
|
||||
except OSError:
|
||||
# File removal failure is not critical here
|
||||
pass # Ignore if file removal fails
|
||||
|
||||
# Check for no-test projects
|
||||
|
||||
@@ -12,7 +12,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
@@ -22,6 +22,11 @@ except (ImportError, ValueError, SystemError):
|
||||
from file_lock import locked_json_update, locked_json_write
|
||||
|
||||
|
||||
def _utc_now_iso() -> str:
|
||||
"""Return current UTC time as ISO 8601 string with timezone info."""
|
||||
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
|
||||
class ReviewSeverity(str, Enum):
|
||||
"""Severity levels for PR review findings."""
|
||||
|
||||
@@ -521,7 +526,7 @@ class PRReviewResult:
|
||||
summary: str = ""
|
||||
overall_status: str = "comment" # approve, request_changes, comment
|
||||
review_id: int | None = None
|
||||
reviewed_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
reviewed_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
error: str | None = None
|
||||
|
||||
# NEW: Enhanced verdict system
|
||||
@@ -610,7 +615,7 @@ class PRReviewResult:
|
||||
summary=data.get("summary", ""),
|
||||
overall_status=data.get("overall_status", "comment"),
|
||||
review_id=data.get("review_id"),
|
||||
reviewed_at=data.get("reviewed_at", datetime.now().isoformat()),
|
||||
reviewed_at=data.get("reviewed_at", _utc_now_iso()),
|
||||
error=data.get("error"),
|
||||
# NEW fields
|
||||
verdict=MergeVerdict(data.get("verdict", "ready_to_merge")),
|
||||
@@ -691,7 +696,7 @@ class PRReviewResult:
|
||||
reviews.append(entry)
|
||||
|
||||
current_data["reviews"] = reviews
|
||||
current_data["last_updated"] = datetime.now().isoformat()
|
||||
current_data["last_updated"] = _utc_now_iso()
|
||||
|
||||
return current_data
|
||||
|
||||
@@ -762,7 +767,7 @@ class TriageResult:
|
||||
suggested_breakdown: list[str] = field(default_factory=list)
|
||||
priority: str = "medium" # high, medium, low
|
||||
comment: str | None = None
|
||||
triaged_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
triaged_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
@@ -798,7 +803,7 @@ class TriageResult:
|
||||
suggested_breakdown=data.get("suggested_breakdown", []),
|
||||
priority=data.get("priority", "medium"),
|
||||
comment=data.get("comment"),
|
||||
triaged_at=data.get("triaged_at", datetime.now().isoformat()),
|
||||
triaged_at=data.get("triaged_at", _utc_now_iso()),
|
||||
)
|
||||
|
||||
async def save(self, github_dir: Path) -> None:
|
||||
@@ -836,8 +841,8 @@ class AutoFixState:
|
||||
pr_url: str | None = None
|
||||
bot_comments: list[str] = field(default_factory=list)
|
||||
error: str | None = None
|
||||
created_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
updated_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
created_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
updated_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
@@ -875,8 +880,8 @@ class AutoFixState:
|
||||
pr_url=data.get("pr_url"),
|
||||
bot_comments=data.get("bot_comments", []),
|
||||
error=data.get("error"),
|
||||
created_at=data.get("created_at", datetime.now().isoformat()),
|
||||
updated_at=data.get("updated_at", datetime.now().isoformat()),
|
||||
created_at=data.get("created_at", _utc_now_iso()),
|
||||
updated_at=data.get("updated_at", _utc_now_iso()),
|
||||
)
|
||||
|
||||
def update_status(self, status: AutoFixStatus) -> None:
|
||||
@@ -886,7 +891,7 @@ class AutoFixState:
|
||||
f"Invalid state transition: {self.status.value} -> {status.value}"
|
||||
)
|
||||
self.status = status
|
||||
self.updated_at = datetime.now().isoformat()
|
||||
self.updated_at = _utc_now_iso()
|
||||
|
||||
async def save(self, github_dir: Path) -> None:
|
||||
"""Save auto-fix state to .auto-claude/github/issues/ with file locking."""
|
||||
@@ -938,7 +943,7 @@ class AutoFixState:
|
||||
queue.append(entry)
|
||||
|
||||
current_data["auto_fix_queue"] = queue
|
||||
current_data["last_updated"] = datetime.now().isoformat()
|
||||
current_data["last_updated"] = _utc_now_iso()
|
||||
|
||||
return current_data
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ from __future__ import annotations
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
@@ -33,6 +32,7 @@ try:
|
||||
PRReviewResult,
|
||||
ReviewCategory,
|
||||
ReviewSeverity,
|
||||
_utc_now_iso,
|
||||
)
|
||||
from .category_utils import map_category
|
||||
from .io_utils import safe_print
|
||||
@@ -46,6 +46,7 @@ except (ImportError, ValueError, SystemError):
|
||||
PRReviewResult,
|
||||
ReviewCategory,
|
||||
ReviewSeverity,
|
||||
_utc_now_iso,
|
||||
)
|
||||
from services.category_utils import map_category
|
||||
from services.io_utils import safe_print
|
||||
@@ -265,7 +266,7 @@ class FollowupReviewer:
|
||||
verdict=verdict,
|
||||
verdict_reasoning=verdict_reasoning,
|
||||
blockers=blockers,
|
||||
reviewed_at=datetime.now().isoformat(),
|
||||
reviewed_at=_utc_now_iso(),
|
||||
# Follow-up specific fields
|
||||
reviewed_commit_sha=context.current_commit_sha,
|
||||
reviewed_file_blobs=file_blobs,
|
||||
|
||||
@@ -51,7 +51,7 @@ try:
|
||||
from .category_utils import map_category
|
||||
from .io_utils import safe_print
|
||||
from .pr_worktree_manager import PRWorktreeManager
|
||||
from .pydantic_models import ParallelFollowupResponse
|
||||
from .pydantic_models import FollowupExtractionResponse, ParallelFollowupResponse
|
||||
from .sdk_utils import process_sdk_stream
|
||||
except (ImportError, ValueError, SystemError):
|
||||
from context_gatherer import _validate_git_ref
|
||||
@@ -75,7 +75,10 @@ except (ImportError, ValueError, SystemError):
|
||||
from services.category_utils import map_category
|
||||
from services.io_utils import safe_print
|
||||
from services.pr_worktree_manager import PRWorktreeManager
|
||||
from services.pydantic_models import ParallelFollowupResponse
|
||||
from services.pydantic_models import (
|
||||
FollowupExtractionResponse,
|
||||
ParallelFollowupResponse,
|
||||
)
|
||||
from services.sdk_utils import process_sdk_stream
|
||||
|
||||
|
||||
@@ -576,16 +579,36 @@ The SDK will run invoked agents in parallel automatically.
|
||||
)
|
||||
|
||||
# Check for stream processing errors
|
||||
if stream_result.get("error"):
|
||||
logger.error(
|
||||
f"[ParallelFollowup] SDK stream failed: {stream_result['error']}"
|
||||
)
|
||||
raise RuntimeError(
|
||||
f"SDK stream processing failed: {stream_result['error']}"
|
||||
)
|
||||
stream_error = stream_result.get("error")
|
||||
if stream_error:
|
||||
if stream_result.get("error_recoverable"):
|
||||
# Recoverable error — attempt extraction call fallback
|
||||
logger.warning(
|
||||
f"[ParallelFollowup] Recoverable error: {stream_error}. "
|
||||
f"Attempting extraction call fallback."
|
||||
)
|
||||
safe_print(
|
||||
f"[ParallelFollowup] WARNING: {stream_error} — "
|
||||
f"attempting recovery with minimal extraction...",
|
||||
flush=True,
|
||||
)
|
||||
else:
|
||||
# Fatal error — raise as before
|
||||
logger.error(
|
||||
f"[ParallelFollowup] SDK stream failed: {stream_error}"
|
||||
)
|
||||
raise RuntimeError(
|
||||
f"SDK stream processing failed: {stream_error}"
|
||||
)
|
||||
|
||||
result_text = stream_result["result_text"]
|
||||
structured_output = stream_result["structured_output"]
|
||||
last_assistant_text = stream_result.get("last_assistant_text", "")
|
||||
# Nullify structured output on recoverable errors to force Tier 2 fallback
|
||||
structured_output = (
|
||||
None
|
||||
if (stream_error and stream_result.get("error_recoverable"))
|
||||
else stream_result["structured_output"]
|
||||
)
|
||||
agents_invoked = stream_result["agents_invoked"]
|
||||
msg_count = stream_result["msg_count"]
|
||||
|
||||
@@ -596,22 +619,28 @@ The SDK will run invoked agents in parallel automatically.
|
||||
pr_number=context.pr_number,
|
||||
)
|
||||
|
||||
# Parse findings from output
|
||||
# Parse findings from output (three-tier recovery cascade)
|
||||
if structured_output:
|
||||
result_data = self._parse_structured_output(structured_output, context)
|
||||
else:
|
||||
# Log when structured output is missing - this shouldn't happen normally
|
||||
# when output_format is configured, so it indicates a problem
|
||||
# Structured output missing or validation failed.
|
||||
# Tier 2: Attempt extraction call with minimal schema
|
||||
logger.warning(
|
||||
"[ParallelFollowup] No structured output received from SDK - "
|
||||
"falling back to text parsing. Resolution data may be incomplete."
|
||||
"[ParallelFollowup] No structured output — attempting extraction call"
|
||||
)
|
||||
safe_print(
|
||||
"[ParallelFollowup] WARNING: Structured output not captured, "
|
||||
"using text fallback (resolution tracking may be incomplete)",
|
||||
flush=True,
|
||||
# Use last_assistant_text (cleaner) if available, fall back to full transcript
|
||||
fallback_text = last_assistant_text or result_text
|
||||
result_data = await self._attempt_extraction_call(
|
||||
fallback_text, context
|
||||
)
|
||||
result_data = self._parse_text_output(result_text, context)
|
||||
if result_data is None:
|
||||
# Tier 3: Fall back to basic text parsing
|
||||
safe_print(
|
||||
"[ParallelFollowup] WARNING: Extraction call failed, "
|
||||
"using text fallback (resolution tracking may be incomplete)",
|
||||
flush=True,
|
||||
)
|
||||
result_data = self._parse_text_output(result_text, context)
|
||||
|
||||
# Extract data
|
||||
findings = result_data.get("findings", [])
|
||||
@@ -730,7 +759,9 @@ The SDK will run invoked agents in parallel automatically.
|
||||
blockers.append(f"{finding.category.value}: {finding.title}")
|
||||
|
||||
# Extract validation counts
|
||||
dismissed_count = len(result_data.get("dismissed_false_positive_ids", []))
|
||||
dismissed_count = len(
|
||||
result_data.get("dismissed_false_positive_ids", [])
|
||||
) or result_data.get("dismissed_finding_count", 0)
|
||||
confirmed_count = result_data.get("confirmed_valid_count", 0)
|
||||
needs_human_count = result_data.get("needs_human_review_count", 0)
|
||||
|
||||
@@ -1074,17 +1105,129 @@ The SDK will run invoked agents in parallel automatically.
|
||||
elif "needs revision" in text_lower or "request changes" in text_lower:
|
||||
verdict = MergeVerdict.NEEDS_REVISION
|
||||
else:
|
||||
verdict = MergeVerdict.MERGE_WITH_CHANGES
|
||||
verdict = MergeVerdict.NEEDS_REVISION
|
||||
|
||||
return {
|
||||
"findings": findings,
|
||||
"resolved_ids": [],
|
||||
"unresolved_ids": [],
|
||||
"new_finding_ids": [],
|
||||
"dismissed_false_positive_ids": [],
|
||||
"confirmed_valid_count": 0,
|
||||
"dismissed_finding_count": 0,
|
||||
"needs_human_review_count": 0,
|
||||
"verdict": verdict,
|
||||
"verdict_reasoning": text[:500] if text else "Unable to parse response",
|
||||
"agents_invoked": [],
|
||||
}
|
||||
|
||||
async def _attempt_extraction_call(
|
||||
self, text: str, context: FollowupReviewContext
|
||||
) -> dict | None:
|
||||
"""Attempt a short SDK call with a minimal schema to recover review data.
|
||||
|
||||
This is the Tier 2 recovery step when full structured output validation fails.
|
||||
Uses FollowupExtractionResponse (~6 flat fields) which has near-100% success rate.
|
||||
|
||||
Returns parsed result dict on success, None on failure.
|
||||
"""
|
||||
if not text or not text.strip():
|
||||
logger.warning("[ParallelFollowup] No text available for extraction call")
|
||||
return None
|
||||
|
||||
try:
|
||||
safe_print(
|
||||
"[ParallelFollowup] Attempting recovery with minimal extraction schema...",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
extraction_prompt = (
|
||||
"Extract the key review data from the following AI analysis output. "
|
||||
"Return the verdict, reasoning, resolved finding IDs, unresolved finding IDs, "
|
||||
"one-line summaries of any new findings, and counts of confirmed/dismissed findings.\n\n"
|
||||
f"--- AI ANALYSIS OUTPUT ---\n{text[:8000]}\n--- END ---"
|
||||
)
|
||||
|
||||
model_shorthand = self.config.model or "sonnet"
|
||||
model = resolve_model_id(model_shorthand)
|
||||
|
||||
extraction_client = create_client(
|
||||
project_dir=self.project_dir,
|
||||
spec_dir=self.github_dir,
|
||||
model=model,
|
||||
agent_type="pr_followup_extraction",
|
||||
fast_mode=self.config.fast_mode,
|
||||
output_format={
|
||||
"type": "json_schema",
|
||||
"schema": FollowupExtractionResponse.model_json_schema(),
|
||||
},
|
||||
)
|
||||
|
||||
async with extraction_client:
|
||||
await extraction_client.query(extraction_prompt)
|
||||
|
||||
stream_result = await process_sdk_stream(
|
||||
client=extraction_client,
|
||||
context_name="FollowupExtraction",
|
||||
model=model,
|
||||
system_prompt=extraction_prompt,
|
||||
max_messages=20,
|
||||
)
|
||||
|
||||
if stream_result.get("error"):
|
||||
logger.warning(
|
||||
f"[ParallelFollowup] Extraction call also failed: {stream_result['error']}"
|
||||
)
|
||||
return None
|
||||
|
||||
extraction_output = stream_result.get("structured_output")
|
||||
if not extraction_output:
|
||||
logger.warning(
|
||||
"[ParallelFollowup] Extraction call returned no structured output"
|
||||
)
|
||||
return None
|
||||
|
||||
# Parse the minimal extraction response
|
||||
extracted = FollowupExtractionResponse.model_validate(extraction_output)
|
||||
|
||||
# Map verdict string to MergeVerdict enum
|
||||
verdict_map = {
|
||||
"READY_TO_MERGE": MergeVerdict.READY_TO_MERGE,
|
||||
"MERGE_WITH_CHANGES": MergeVerdict.MERGE_WITH_CHANGES,
|
||||
"NEEDS_REVISION": MergeVerdict.NEEDS_REVISION,
|
||||
"BLOCKED": MergeVerdict.BLOCKED,
|
||||
}
|
||||
verdict = verdict_map.get(extracted.verdict, MergeVerdict.NEEDS_REVISION)
|
||||
|
||||
safe_print(
|
||||
f"[ParallelFollowup] Extraction recovered: verdict={extracted.verdict}, "
|
||||
f"{len(extracted.resolved_finding_ids)} resolved, "
|
||||
f"{len(extracted.new_finding_summaries)} new findings",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
return {
|
||||
"findings": [], # Full findings not recoverable via extraction
|
||||
"resolved_ids": extracted.resolved_finding_ids,
|
||||
"unresolved_ids": extracted.unresolved_finding_ids,
|
||||
"new_finding_ids": [],
|
||||
"dismissed_false_positive_ids": [],
|
||||
"confirmed_valid_count": extracted.confirmed_finding_count,
|
||||
"dismissed_finding_count": extracted.dismissed_finding_count,
|
||||
"needs_human_review_count": 0,
|
||||
"verdict": verdict,
|
||||
"verdict_reasoning": f"[Recovered via extraction] {extracted.verdict_reasoning}",
|
||||
"agents_invoked": [],
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"[ParallelFollowup] Extraction call failed: {e}")
|
||||
safe_print(
|
||||
f"[ParallelFollowup] Extraction call failed: {e}",
|
||||
flush=True,
|
||||
)
|
||||
return None
|
||||
|
||||
def _create_empty_result(self) -> dict:
|
||||
"""Create empty result structure."""
|
||||
return {
|
||||
@@ -1092,8 +1235,13 @@ The SDK will run invoked agents in parallel automatically.
|
||||
"resolved_ids": [],
|
||||
"unresolved_ids": [],
|
||||
"new_finding_ids": [],
|
||||
"dismissed_false_positive_ids": [],
|
||||
"confirmed_valid_count": 0,
|
||||
"dismissed_finding_count": 0,
|
||||
"needs_human_review_count": 0,
|
||||
"verdict": MergeVerdict.NEEDS_REVISION,
|
||||
"verdict_reasoning": "Unable to parse review results",
|
||||
"agents_invoked": [],
|
||||
}
|
||||
|
||||
def _extract_partial_data(self, data: dict) -> dict | None:
|
||||
|
||||
@@ -1785,6 +1785,7 @@ For EACH finding above:
|
||||
or "concurrency" in error_str
|
||||
or "circuit breaker" in error_str
|
||||
or "tool_use" in error_str
|
||||
or "structured_output" in error_str
|
||||
)
|
||||
|
||||
if is_retryable and attempt < MAX_VALIDATION_RETRIES:
|
||||
@@ -1805,7 +1806,6 @@ For EACH finding above:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
# Part of retry loop structure - handles retryable errors
|
||||
error_str = str(e).lower()
|
||||
is_retryable = (
|
||||
"400" in error_str
|
||||
|
||||
@@ -710,3 +710,39 @@ class FindingValidationResponse(BaseModel):
|
||||
"how many dismissed, how many need human review"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Minimal Extraction Schema (Fallback for structured output validation failure)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class FollowupExtractionResponse(BaseModel):
|
||||
"""Minimal extraction schema for recovering data when full structured output fails.
|
||||
|
||||
Deliberately kept small (~6 fields, no nesting) for near-100% validation success.
|
||||
Used as an intermediate recovery step before falling back to raw text parsing.
|
||||
"""
|
||||
|
||||
verdict: Literal[
|
||||
"READY_TO_MERGE", "MERGE_WITH_CHANGES", "NEEDS_REVISION", "BLOCKED"
|
||||
] = Field(description="Overall merge verdict")
|
||||
verdict_reasoning: str = Field(description="Explanation for the verdict")
|
||||
resolved_finding_ids: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="IDs of previous findings that are now resolved",
|
||||
)
|
||||
unresolved_finding_ids: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="IDs of previous findings that remain unresolved",
|
||||
)
|
||||
new_finding_summaries: list[str] = Field(
|
||||
default_factory=list,
|
||||
description="One-line summary of each new finding (e.g. 'HIGH: cleanup deletes QA-rejected specs in batch_commands.py')",
|
||||
)
|
||||
confirmed_finding_count: int = Field(
|
||||
0, description="Number of findings confirmed as valid"
|
||||
)
|
||||
dismissed_finding_count: int = Field(
|
||||
0, description="Number of findings dismissed as false positives"
|
||||
)
|
||||
|
||||
@@ -133,6 +133,13 @@ def _get_tool_detail(tool_name: str, tool_input: dict[str, Any]) -> str:
|
||||
# Prevents runaway retry loops from consuming unbounded resources
|
||||
MAX_MESSAGE_COUNT = 500
|
||||
|
||||
# Errors that are recoverable (callers can fall back to text parsing or retry)
|
||||
# vs fatal errors (auth failures, circuit breaker) that should propagate
|
||||
RECOVERABLE_ERRORS = {
|
||||
"structured_output_validation_failed",
|
||||
"tool_use_concurrency_error",
|
||||
}
|
||||
|
||||
# Abort after 1 consecutive repeat (2 total identical responses).
|
||||
# Low threshold catches error loops quickly (e.g., auth errors returned as AI text).
|
||||
# Normal AI responses never produce the exact same text block twice in a row.
|
||||
@@ -261,8 +268,11 @@ async def process_sdk_stream(
|
||||
- msg_count: Total message count
|
||||
- subagent_tool_ids: Mapping of tool_id -> agent_name
|
||||
- error: Error message if stream processing failed (None on success)
|
||||
- error_recoverable: Boolean indicating if the error is recoverable (fallback possible) vs fatal
|
||||
- last_assistant_text: Last non-empty assistant text block (for cleaner fallback parsing)
|
||||
"""
|
||||
result_text = ""
|
||||
last_assistant_text = "" # Last assistant text block (for cleaner fallback parsing)
|
||||
structured_output = None
|
||||
agents_invoked = []
|
||||
msg_count = 0
|
||||
@@ -481,6 +491,9 @@ async def process_sdk_stream(
|
||||
block_type = type(block).__name__
|
||||
if block_type == "TextBlock" and hasattr(block, "text"):
|
||||
result_text += block.text
|
||||
# Track last non-empty text for fallback parsing
|
||||
if block.text.strip():
|
||||
last_assistant_text = block.text
|
||||
# Check for auth/access error returned as AI response text.
|
||||
# Note: break exits this inner for-loop over msg.content;
|
||||
# the outer message loop exits via `if stream_error: break`.
|
||||
@@ -647,11 +660,16 @@ async def process_sdk_stream(
|
||||
f"[{context_name}] Tool use concurrency error detected - caller should retry"
|
||||
)
|
||||
|
||||
# Categorize error as recoverable (fallback possible) vs fatal
|
||||
error_recoverable = stream_error in RECOVERABLE_ERRORS if stream_error else False
|
||||
|
||||
return {
|
||||
"result_text": result_text,
|
||||
"last_assistant_text": last_assistant_text,
|
||||
"structured_output": structured_output,
|
||||
"agents_invoked": agents_invoked,
|
||||
"msg_count": msg_count,
|
||||
"subagent_tool_ids": subagent_tool_ids,
|
||||
"error": stream_error,
|
||||
"error_recoverable": error_recoverable,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "auto-claude-ui",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6-beta.4",
|
||||
"type": "module",
|
||||
"description": "Desktop UI for Auto Claude autonomous coding framework",
|
||||
"homepage": "https://github.com/AndyMik90/Auto-Claude",
|
||||
|
||||
@@ -1825,7 +1825,6 @@ function updateLinuxFileCredentials(
|
||||
}
|
||||
|
||||
// Write to file with secure permissions (0600)
|
||||
// lgtm[js/http-to-file-access] - credentialsPath is from controlled configDir
|
||||
writeFileSync(credentialsPath, credentialsJson, { mode: 0o600, encoding: 'utf-8' });
|
||||
|
||||
if (isDebug) {
|
||||
@@ -2087,7 +2086,6 @@ function updateWindowsFileCredentials(
|
||||
const tempPath = `${credentialsPath}.${Date.now()}.tmp`;
|
||||
try {
|
||||
// Write to temp file
|
||||
// lgtm[js/http-to-file-access] - credentialsPath is from controlled configDir
|
||||
writeFileSync(tempPath, credentialsJson, { encoding: 'utf-8' });
|
||||
|
||||
// Restrict temp file permissions to current user only (mimics Unix 0600)
|
||||
|
||||
@@ -36,6 +36,8 @@ import {
|
||||
buildRunnerArgs,
|
||||
} from "./utils/subprocess-runner";
|
||||
import { getPRStatusPoller } from "../../services/pr-status-poller";
|
||||
import { safeBreadcrumb, safeCaptureException } from "../../sentry";
|
||||
import { sanitizeForSentry } from "../../../shared/utils/sentry-privacy";
|
||||
import type {
|
||||
StartPollingRequest,
|
||||
StopPollingRequest,
|
||||
@@ -110,7 +112,6 @@ async function githubGraphQL<T>(
|
||||
query: string,
|
||||
variables: Record<string, unknown> = {}
|
||||
): Promise<T> {
|
||||
// lgtm[js/file-access-to-http] - Official GitHub GraphQL API endpoint
|
||||
const response = await fetch("https://api.github.com/graphql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -1463,6 +1464,20 @@ async function runPRReview(
|
||||
|
||||
debugLog("Spawning PR review process", { args, model, thinkingLevel });
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Spawning PR review subprocess',
|
||||
level: 'info',
|
||||
data: {
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
runnerPath: getRunnerPath(backendPath),
|
||||
cwd: backendPath,
|
||||
model,
|
||||
thinkingLevel,
|
||||
prNumber,
|
||||
},
|
||||
});
|
||||
|
||||
// Create log collector for this review
|
||||
const config = getGitHubConfig(project);
|
||||
const repo = config?.repo || project.name || "unknown";
|
||||
@@ -1526,9 +1541,22 @@ async function runPRReview(
|
||||
// Wait for the process to complete
|
||||
const result = await promise;
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: `PR review subprocess exited`,
|
||||
level: result.success ? 'info' : 'error',
|
||||
data: { exitCode: result.exitCode, success: result.success, prNumber },
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
// Finalize logs with failure
|
||||
logCollector.finalize(false);
|
||||
|
||||
safeCaptureException(
|
||||
new Error(`PR review subprocess failed: ${result.error ?? 'unknown error'}`),
|
||||
{ extra: { exitCode: result.exitCode, prNumber, stderr: sanitizeForSentry(result.stderr.slice(0, 500)) } }
|
||||
);
|
||||
|
||||
throw new Error(result.error ?? "Review failed");
|
||||
}
|
||||
|
||||
@@ -2908,6 +2936,20 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
|
||||
debugLog("Spawning follow-up review process", { args, model, thinkingLevel });
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Spawning follow-up PR review subprocess',
|
||||
level: 'info',
|
||||
data: {
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
runnerPath: getRunnerPath(backendPath),
|
||||
cwd: backendPath,
|
||||
model,
|
||||
thinkingLevel,
|
||||
prNumber,
|
||||
},
|
||||
});
|
||||
|
||||
// Create log collector for this follow-up review (config already declared above)
|
||||
const repo = config?.repo || project.name || "unknown";
|
||||
const logCollector = new PRLogCollector(project, prNumber, repo, true, mainWindow);
|
||||
@@ -2965,9 +3007,22 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
|
||||
const result = await promise;
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Follow-up PR review subprocess exited',
|
||||
level: result.success ? 'info' : 'error',
|
||||
data: { exitCode: result.exitCode, success: result.success, prNumber },
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
// Finalize logs with failure
|
||||
logCollector.finalize(false);
|
||||
|
||||
safeCaptureException(
|
||||
new Error(`Follow-up PR review subprocess failed: ${result.error ?? 'unknown error'}`),
|
||||
{ extra: { exitCode: result.exitCode, prNumber, stderr: sanitizeForSentry(result.stderr.slice(0, 500)) } }
|
||||
);
|
||||
|
||||
throw new Error(result.error ?? "Follow-up review failed");
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,6 @@ export async function createSpecForIssue(
|
||||
status: 'pending',
|
||||
phases: []
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
writeFileSync(
|
||||
path.join(specDir, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN),
|
||||
JSON.stringify(implementationPlan, null, 2),
|
||||
@@ -150,7 +149,6 @@ export async function createSpecForIssue(
|
||||
workflow_type: 'feature'
|
||||
};
|
||||
writeFileSync(
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
path.join(specDir, AUTO_BUILD_PATHS.REQUIREMENTS),
|
||||
JSON.stringify(requirements, null, 2),
|
||||
'utf-8'
|
||||
@@ -170,7 +168,6 @@ export async function createSpecForIssue(
|
||||
...(baseBranch && { baseBranch })
|
||||
};
|
||||
writeFileSync(
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
path.join(specDir, 'task_metadata.json'),
|
||||
JSON.stringify(metadata, null, 2),
|
||||
'utf-8'
|
||||
|
||||
@@ -3,6 +3,7 @@ import { getAPIProfileEnv } from '../../../services/profile';
|
||||
import { getBestAvailableProfileEnv } from '../../../rate-limit-detector';
|
||||
import { pythonEnvManager } from '../../../python-env-manager';
|
||||
import { getGitHubTokenForSubprocess } from '../utils';
|
||||
import { getSentryEnvForSubprocess } from '../../../sentry';
|
||||
|
||||
/**
|
||||
* Get environment variables for Python runner subprocesses.
|
||||
@@ -48,6 +49,7 @@ export async function getRunnerEnv(
|
||||
...oauthModeClearVars,
|
||||
...profileEnv, // OAuth token from profile manager (fixes #563, rate-limit aware)
|
||||
...githubEnv, // Fresh GitHub token from gh CLI (fixes #151)
|
||||
...extraEnv,
|
||||
...getSentryEnvForSubprocess(), // Sentry DSN + sample rates for Python subprocess
|
||||
...extraEnv, // extraEnv last so callers can still override
|
||||
};
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import { isWindows, isMacOS } from '../../../platform';
|
||||
import { getEffectiveSourcePath } from '../../../updater/path-resolver';
|
||||
import { pythonEnvManager, getConfiguredPythonPath } from '../../../python-env-manager';
|
||||
import { getTaskkillExePath, getWhereExePath } from '../../../utils/windows-paths';
|
||||
import { safeCaptureException } from '../../../sentry';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
const execFileAsync = promisify(execFile);
|
||||
@@ -214,6 +215,17 @@ export function runPythonSubprocess<T = unknown>(
|
||||
let killedDueToAuthFailure = false; // Track if subprocess was killed due to auth failure
|
||||
let billingFailureEmitted = false; // Track if we've already emitted a billing failure
|
||||
let killedDueToBillingFailure = false; // Track if subprocess was killed due to billing failure
|
||||
let receivedOutput = false; // Track if any stdout/stderr has been received
|
||||
|
||||
// Health-check: report to Sentry if no output received within 120 seconds
|
||||
const healthCheckTimeout = setTimeout(() => {
|
||||
if (!receivedOutput) {
|
||||
safeCaptureException(
|
||||
new Error('[SubprocessRunner] No output received from subprocess after 120s'),
|
||||
{ extra: { pythonPath: options.pythonPath, args: options.args, cwd: options.cwd, envKeys: options.env ? Object.keys(options.env) : [] } }
|
||||
);
|
||||
}
|
||||
}, 120_000);
|
||||
|
||||
// Default progress pattern: [ 30%] message OR [30%] message
|
||||
const progressPattern = options.progressPattern ?? /\[\s*(\d+)%\]\s*(.+)/;
|
||||
@@ -337,6 +349,7 @@ export function runPythonSubprocess<T = unknown>(
|
||||
};
|
||||
|
||||
child.stdout.on('data', (data: Buffer) => {
|
||||
receivedOutput = true;
|
||||
const text = data.toString('utf-8');
|
||||
stdout += text;
|
||||
|
||||
@@ -364,6 +377,7 @@ export function runPythonSubprocess<T = unknown>(
|
||||
});
|
||||
|
||||
child.stderr.on('data', (data: Buffer) => {
|
||||
receivedOutput = true;
|
||||
const text = data.toString('utf-8');
|
||||
stderr += text;
|
||||
|
||||
@@ -382,6 +396,7 @@ export function runPythonSubprocess<T = unknown>(
|
||||
});
|
||||
|
||||
child.on('close', (code: number | null) => {
|
||||
clearTimeout(healthCheckTimeout);
|
||||
// Treat null exit code (killed with SIGKILL) as failure, not success
|
||||
const exitCode = code ?? -1;
|
||||
|
||||
@@ -461,6 +476,7 @@ export function runPythonSubprocess<T = unknown>(
|
||||
});
|
||||
|
||||
child.on('error', (err: Error) => {
|
||||
clearTimeout(healthCheckTimeout);
|
||||
options.onError?.(err.message);
|
||||
resolve({
|
||||
success: false,
|
||||
|
||||
@@ -8,8 +8,8 @@ import { IPC_CHANNELS } from '../../../shared/constants';
|
||||
import type { GitLabInvestigationStatus, GitLabInvestigationResult } from '../../../shared/types';
|
||||
import { projectStore } from '../../project-store';
|
||||
import { getGitLabConfig, gitlabFetch, encodeProjectPath } from './utils';
|
||||
import type { GitLabAPIIssue, GitLabNoteBasic } from './types';
|
||||
import { createSpecForIssue } from './spec-utils';
|
||||
import type { GitLabAPIIssue, GitLabAPINote } from './types';
|
||||
import { buildIssueContext, createSpecForIssue } from './spec-utils';
|
||||
import type { AgentManager } from '../../agent';
|
||||
|
||||
// Debug logging helper
|
||||
@@ -109,88 +109,16 @@ export function registerInvestigateIssue(
|
||||
`/projects/${encodedProject}/issues/${issueIid}`
|
||||
) as GitLabAPIIssue;
|
||||
|
||||
// Fetch notes if any selected (with pagination to get all notes)
|
||||
let filteredNotes: GitLabNoteBasic[] = [];
|
||||
// Fetch notes if any selected
|
||||
let selectedNotes: GitLabAPINote[] = [];
|
||||
if (selectedNoteIds && selectedNoteIds.length > 0) {
|
||||
// Fetch all notes with pagination (GitLab defaults to 20 per page)
|
||||
const allNotes: GitLabNoteBasic[] = [];
|
||||
let page = 1;
|
||||
const perPage = 100;
|
||||
const MAX_PAGES = 50; // Safety limit: max 5000 notes
|
||||
let hasMore = true;
|
||||
const allNotes = await gitlabFetch(
|
||||
config.token,
|
||||
config.instanceUrl,
|
||||
`/projects/${encodedProject}/issues/${issueIid}/notes`
|
||||
) as GitLabAPINote[];
|
||||
|
||||
while (hasMore && page <= MAX_PAGES) {
|
||||
try {
|
||||
const notesPage = await gitlabFetch(
|
||||
config.token,
|
||||
config.instanceUrl,
|
||||
`/projects/${encodedProject}/issues/${issueIid}/notes?page=${page}&per_page=${perPage}`
|
||||
) as unknown[];
|
||||
|
||||
// Runtime validation: ensure we got an array
|
||||
if (!Array.isArray(notesPage)) {
|
||||
debugLog('GitLab notes API returned non-array, stopping pagination');
|
||||
break;
|
||||
}
|
||||
|
||||
if (notesPage.length === 0) {
|
||||
hasMore = false;
|
||||
} else {
|
||||
// Extract only needed fields with null-safe defaults
|
||||
const noteSummaries: GitLabNoteBasic[] = notesPage
|
||||
.filter((note: unknown): note is Record<string, unknown> =>
|
||||
note !== null && typeof note === 'object' && typeof (note as Record<string, unknown>).id === 'number'
|
||||
)
|
||||
.map((note) => {
|
||||
// Validate author structure defensively
|
||||
const author = note.author;
|
||||
const username = (author !== null && typeof author === 'object' && typeof (author as Record<string, unknown>).username === 'string')
|
||||
? (author as Record<string, unknown>).username as string
|
||||
: 'unknown';
|
||||
return {
|
||||
id: note.id as number,
|
||||
body: (note.body as string | undefined) || '',
|
||||
author: { username },
|
||||
};
|
||||
});
|
||||
allNotes.push(...noteSummaries);
|
||||
if (notesPage.length < perPage) {
|
||||
hasMore = false;
|
||||
} else {
|
||||
page++;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
|
||||
// Check for authentication/rate-limit errors - these should be surfaced
|
||||
const isAuthError = errorMessage.includes('401') || errorMessage.includes('403');
|
||||
const isRateLimited = errorMessage.includes('429');
|
||||
|
||||
if (isAuthError || isRateLimited) {
|
||||
// Re-throw critical errors to let the outer handler surface them to the user
|
||||
console.warn(`[GitLab Investigation] ${isAuthError ? 'Authentication' : 'Rate limit'} error during notes fetch`, { page, error: errorMessage });
|
||||
throw error;
|
||||
}
|
||||
|
||||
// For transient errors on page 1, warn the user but continue
|
||||
if (page === 1 && allNotes.length === 0) {
|
||||
console.warn('[GitLab Investigation] Failed to fetch any notes, proceeding without notes context', { error: errorMessage });
|
||||
} else {
|
||||
// Log pagination failure for subsequent pages
|
||||
debugLog('Failed to fetch notes page, using partial notes', { page, error: errorMessage, notesRetrieved: allNotes.length });
|
||||
}
|
||||
hasMore = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Warn if we hit the pagination limit
|
||||
if (page > MAX_PAGES && hasMore) {
|
||||
debugLog('Pagination limit reached, some notes may be missing', { maxPages: MAX_PAGES, notesRetrieved: allNotes.length });
|
||||
}
|
||||
|
||||
// Filter notes based on selection
|
||||
filteredNotes = allNotes.filter(note => selectedNoteIds.includes(note.id));
|
||||
selectedNotes = allNotes.filter(note => selectedNoteIds.includes(note.id));
|
||||
}
|
||||
|
||||
// Phase 2: Analyzing
|
||||
@@ -201,6 +129,21 @@ export function registerInvestigateIssue(
|
||||
message: 'Analyzing issue with AI...'
|
||||
});
|
||||
|
||||
// Note: Context building previously done here has been moved to createSpecForIssue utility.
|
||||
// The buildIssueContext() function and selectedNotes processing are now handled internally
|
||||
// by the spec creation pipeline. This avoids duplicate context generation.
|
||||
// TODO: If advanced context customization is needed in the future, consider extracting
|
||||
// context building into a reusable utility function.
|
||||
|
||||
// Use agent manager to investigate
|
||||
// Note: This is a simplified version - full implementation would use Claude SDK
|
||||
sendProgress(getMainWindow, project.id, {
|
||||
phase: 'analyzing',
|
||||
issueIid,
|
||||
progress: 50,
|
||||
message: 'AI analyzing the issue...'
|
||||
});
|
||||
|
||||
// Phase 3: Creating task
|
||||
sendProgress(getMainWindow, project.id, {
|
||||
phase: 'creating_task',
|
||||
@@ -209,14 +152,8 @@ export function registerInvestigateIssue(
|
||||
message: 'Creating task from analysis...'
|
||||
});
|
||||
|
||||
// Create spec for the issue with notes
|
||||
const task = await createSpecForIssue(
|
||||
project,
|
||||
issue,
|
||||
config,
|
||||
project.settings?.mainBranch,
|
||||
filteredNotes
|
||||
);
|
||||
// Create spec for the issue
|
||||
const task = await createSpecForIssue(project, issue, config, project.settings?.mainBranch);
|
||||
|
||||
if (!task) {
|
||||
sendError(getMainWindow, project.id, 'Failed to create task from issue');
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import { mkdir, writeFile, readFile, stat } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import type { Project } from '../../../shared/types';
|
||||
import type { GitLabAPIIssue, GitLabNoteBasic, GitLabConfig } from './types';
|
||||
import type { GitLabAPIIssue, GitLabConfig } from './types';
|
||||
import { labelMatchesWholeWord } from '../shared/label-utils';
|
||||
import { sanitizeText, sanitizeStringArray } from '../shared/sanitize';
|
||||
|
||||
@@ -208,12 +208,7 @@ function generateSpecDirName(issueIid: number, title: string): string {
|
||||
/**
|
||||
* Build issue context for spec creation
|
||||
*/
|
||||
export function buildIssueContext(
|
||||
issue: IssueLike,
|
||||
projectPath: string,
|
||||
instanceUrl: string,
|
||||
notes?: GitLabNoteBasic[]
|
||||
): string {
|
||||
export function buildIssueContext(issue: IssueLike, projectPath: string, instanceUrl: string): string {
|
||||
const lines: string[] = [];
|
||||
const safeProjectPath = sanitizeText(projectPath, 200);
|
||||
const safeIssue = sanitizeIssueForSpec(issue, instanceUrl);
|
||||
@@ -243,19 +238,6 @@ export function buildIssueContext(
|
||||
lines.push('');
|
||||
lines.push(`**Web URL:** ${safeIssue.web_url}`);
|
||||
|
||||
// Add notes section if notes are provided
|
||||
if (notes && notes.length > 0) {
|
||||
lines.push('');
|
||||
lines.push(`## Notes (${notes.length})`);
|
||||
lines.push('');
|
||||
for (const note of notes) {
|
||||
const safeAuthor = sanitizeText(note.author?.username || 'unknown', 100);
|
||||
const safeBody = sanitizeText(note.body, 20000, true);
|
||||
lines.push(`**${safeAuthor}:** ${safeBody}`);
|
||||
lines.push('');
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
@@ -278,8 +260,7 @@ export async function createSpecForIssue(
|
||||
project: Project,
|
||||
issue: GitLabAPIIssue,
|
||||
config: GitLabConfig,
|
||||
baseBranch?: string,
|
||||
notes?: GitLabNoteBasic[]
|
||||
baseBranch?: string
|
||||
): Promise<GitLabTaskInfo | null> {
|
||||
try {
|
||||
// Validate and sanitize network data before writing to disk
|
||||
@@ -338,8 +319,8 @@ export async function createSpecForIssue(
|
||||
// Create spec directory
|
||||
await mkdir(specDir, { recursive: true });
|
||||
|
||||
// Create TASK.md with issue context (including selected notes)
|
||||
const taskContent = buildIssueContext(safeIssue, safeProject, safeInstanceUrl, notes);
|
||||
// Create TASK.md with issue context
|
||||
const taskContent = buildIssueContext(safeIssue, safeProject, config.instanceUrl);
|
||||
await writeFile(path.join(specDir, 'TASK.md'), taskContent, 'utf-8');
|
||||
|
||||
// Create metadata.json (legacy format for GitLab-specific data)
|
||||
|
||||
@@ -420,7 +420,6 @@ export function registerTriageHandlers(
|
||||
}
|
||||
|
||||
// Save result
|
||||
// lgtm[js/http-to-file-access] - triageDir from controlled project path, issue_iid is numeric
|
||||
fs.writeFileSync(
|
||||
path.join(triageDir, `triage_${sanitizedResult.issue_iid}.json`),
|
||||
JSON.stringify(sanitizedResult, null, 2),
|
||||
|
||||
@@ -51,13 +51,6 @@ export interface GitLabAPINote {
|
||||
system: boolean;
|
||||
}
|
||||
|
||||
// Basic note type with only fields needed by investigation handlers
|
||||
export interface GitLabNoteBasic {
|
||||
id: number;
|
||||
body: string;
|
||||
author: { username: string };
|
||||
}
|
||||
|
||||
export interface GitLabAPIMergeRequest {
|
||||
id: number;
|
||||
iid: number;
|
||||
|
||||
@@ -507,7 +507,6 @@ ${safeDescription || 'No description provided.'}
|
||||
status: 'pending',
|
||||
phases: []
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, Linear data sanitized
|
||||
writeFileSync(path.join(specDir, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN), JSON.stringify(implementationPlan, null, 2), 'utf-8');
|
||||
|
||||
// Create requirements.json
|
||||
@@ -515,7 +514,6 @@ ${safeDescription || 'No description provided.'}
|
||||
task_description: description,
|
||||
workflow_type: 'feature'
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, Linear data sanitized
|
||||
writeFileSync(path.join(specDir, AUTO_BUILD_PATHS.REQUIREMENTS), JSON.stringify(requirements, null, 2), 'utf-8');
|
||||
|
||||
// Build metadata
|
||||
@@ -526,7 +524,6 @@ ${safeDescription || 'No description provided.'}
|
||||
linearUrl: safeUrl,
|
||||
category: 'feature'
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, Linear data sanitized
|
||||
writeFileSync(path.join(specDir, 'task_metadata.json'), JSON.stringify(metadata, null, 2), 'utf-8');
|
||||
|
||||
// Start spec creation with the existing spec directory
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import { existsSync } from 'fs';
|
||||
import { ipcMain, app } from 'electron';
|
||||
import { existsSync, } from 'fs';
|
||||
import path from 'path';
|
||||
import { execFileSync } from 'child_process';
|
||||
import { IPC_CHANNELS } from '../../shared/constants';
|
||||
import type {
|
||||
|
||||
@@ -753,6 +753,8 @@ if sys.version_info >= (3, 12):
|
||||
...windowsEnv,
|
||||
// Don't write bytecode - not needed and avoids permission issues
|
||||
PYTHONDONTWRITEBYTECODE: '1',
|
||||
// Force unbuffered stdout/stderr so progress updates reach Electron immediately
|
||||
PYTHONUNBUFFERED: '1',
|
||||
// Use UTF-8 encoding
|
||||
PYTHONIOENCODING: 'utf-8',
|
||||
PYTHONUTF8: '1',
|
||||
|
||||
@@ -222,7 +222,5 @@ export function getSentryEnvForSubprocess(): Record<string, string> {
|
||||
SENTRY_DSN: dsn,
|
||||
SENTRY_TRACES_SAMPLE_RATE: String(getTracesSampleRate()),
|
||||
SENTRY_PROFILES_SAMPLE_RATE: String(getProfilesSampleRate()),
|
||||
// Pass SENTRY_DEV so Python backend also enables Sentry in dev mode
|
||||
...(process.env.SENTRY_DEV ? { SENTRY_DEV: process.env.SENTRY_DEV } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -174,6 +174,8 @@ export function GitHubIssues({ onOpenSettings, onNavigateToTask }: GitHubIssuesP
|
||||
onSelectIssue={selectIssue}
|
||||
onInvestigate={handleInvestigate}
|
||||
onLoadMore={!isSearchActive ? handleLoadMore : undefined}
|
||||
onRetry={handleRefresh}
|
||||
onOpenSettings={onOpenSettings}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
+371
@@ -0,0 +1,371 @@
|
||||
import { useState, useEffect, useMemo } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
AlertTriangle,
|
||||
Clock,
|
||||
Key,
|
||||
Shield,
|
||||
WifiOff,
|
||||
SearchX,
|
||||
RefreshCw,
|
||||
Settings2,
|
||||
} from 'lucide-react';
|
||||
import { Button } from '../../ui/button';
|
||||
import { Card, CardContent } from '../../ui/card';
|
||||
import { cn } from '../../../lib/utils';
|
||||
import { parseGitHubError } from '../utils/github-error-parser';
|
||||
import type { GitHubErrorInfo, GitHubErrorType } from '../types';
|
||||
|
||||
/**
|
||||
* Props for the GitHubErrorDisplay component.
|
||||
*/
|
||||
export interface GitHubErrorDisplayProps {
|
||||
/** Raw error string or pre-parsed GitHubErrorInfo */
|
||||
error: string | GitHubErrorInfo | null;
|
||||
/** Callback when user clicks retry button */
|
||||
onRetry?: () => void;
|
||||
/** Callback when user clicks settings button */
|
||||
onOpenSettings?: () => void;
|
||||
/** Additional CSS classes */
|
||||
className?: string;
|
||||
/** Whether to show as compact inline error (vs full-width card) */
|
||||
compact?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for each error type: icon, color, title key.
|
||||
*/
|
||||
const ERROR_CONFIG: Record<
|
||||
GitHubErrorType,
|
||||
{
|
||||
icon: React.ComponentType<{ className?: string }>;
|
||||
titleKey: string;
|
||||
iconColorClass: string;
|
||||
}
|
||||
> = {
|
||||
rate_limit: {
|
||||
icon: Clock,
|
||||
titleKey: 'githubErrors.rateLimitTitle',
|
||||
iconColorClass: 'text-warning',
|
||||
},
|
||||
auth: {
|
||||
icon: Key,
|
||||
titleKey: 'githubErrors.authTitle',
|
||||
iconColorClass: 'text-destructive',
|
||||
},
|
||||
permission: {
|
||||
icon: Shield,
|
||||
titleKey: 'githubErrors.permissionTitle',
|
||||
iconColorClass: 'text-destructive',
|
||||
},
|
||||
not_found: {
|
||||
icon: SearchX,
|
||||
titleKey: 'githubErrors.notFoundTitle',
|
||||
iconColorClass: 'text-muted-foreground',
|
||||
},
|
||||
network: {
|
||||
icon: WifiOff,
|
||||
titleKey: 'githubErrors.networkTitle',
|
||||
iconColorClass: 'text-warning',
|
||||
},
|
||||
unknown: {
|
||||
icon: AlertTriangle,
|
||||
titleKey: 'githubErrors.unknownTitle',
|
||||
iconColorClass: 'text-destructive',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Base message keys for each error type.
|
||||
* Hoisted to module scope to avoid recreation on every function call.
|
||||
*/
|
||||
const BASE_MESSAGE_KEYS: Record<GitHubErrorType, string> = {
|
||||
rate_limit: 'githubErrors.rateLimitMessage',
|
||||
auth: 'githubErrors.authMessage',
|
||||
permission: 'githubErrors.permissionMessage',
|
||||
not_found: 'githubErrors.notFoundMessage',
|
||||
network: 'githubErrors.networkMessage',
|
||||
unknown: 'githubErrors.unknownMessage',
|
||||
};
|
||||
|
||||
/**
|
||||
* Countdown time components for i18n-friendly formatting.
|
||||
*/
|
||||
interface CountdownComponents {
|
||||
hours: number;
|
||||
minutes: number;
|
||||
seconds: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate countdown time components from reset time.
|
||||
* Returns numeric values for i18n-friendly formatting in the component.
|
||||
*/
|
||||
function getCountdownComponents(resetTime: Date): CountdownComponents | null {
|
||||
const now = new Date();
|
||||
const diffMs = resetTime.getTime() - now.getTime();
|
||||
|
||||
if (diffMs <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const diffSecs = Math.floor(diffMs / 1000);
|
||||
const diffMins = Math.floor(diffSecs / 60);
|
||||
const diffHours = Math.floor(diffMins / 60);
|
||||
|
||||
return {
|
||||
hours: diffHours,
|
||||
minutes: diffHours > 0 ? diffMins % 60 : diffMins,
|
||||
seconds: diffSecs % 60,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the most specific message key based on available metadata.
|
||||
* Pure function extracted to module scope to avoid recreation on each render.
|
||||
* @param info - The error info object
|
||||
* @param rateLimitDiffMs - Pre-computed time difference in milliseconds (avoids dual calculation)
|
||||
*/
|
||||
function getMessageKey(info: GitHubErrorInfo, rateLimitDiffMs?: number): string {
|
||||
if (info.type === 'rate_limit' && rateLimitDiffMs !== undefined && rateLimitDiffMs > 0) {
|
||||
const diffMins = Math.ceil(rateLimitDiffMs / 60000);
|
||||
return diffMins >= 60
|
||||
? 'githubErrors.rateLimitMessageHours'
|
||||
: 'githubErrors.rateLimitMessageMinutes';
|
||||
}
|
||||
if (info.type === 'permission' && info.requiredScopes && info.requiredScopes.length > 0) {
|
||||
return 'githubErrors.permissionMessageScopes';
|
||||
}
|
||||
return BASE_MESSAGE_KEYS[info.type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Component that displays GitHub API errors with appropriate icons,
|
||||
* messages, and action buttons based on error type.
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // With raw error string
|
||||
* <GitHubErrorDisplay
|
||||
* error="GitHub API error: 403 - Rate limit exceeded"
|
||||
* onRetry={handleRetry}
|
||||
* />
|
||||
*
|
||||
* // With pre-parsed error info
|
||||
* <GitHubErrorDisplay
|
||||
* error={errorInfo}
|
||||
* onOpenSettings={handleOpenSettings}
|
||||
* compact
|
||||
* />
|
||||
* ```
|
||||
*/
|
||||
export function GitHubErrorDisplay({
|
||||
error,
|
||||
onRetry,
|
||||
onOpenSettings,
|
||||
className,
|
||||
compact = false,
|
||||
}: GitHubErrorDisplayProps) {
|
||||
const { t } = useTranslation('common');
|
||||
|
||||
// Parse error if it's a string, otherwise use the provided GitHubErrorInfo
|
||||
// Memoize to prevent useEffect churn from new Date references on each render
|
||||
const errorInfo: GitHubErrorInfo = useMemo(
|
||||
() =>
|
||||
typeof error === 'string' || error === null
|
||||
? parseGitHubError(error)
|
||||
: error,
|
||||
[error]
|
||||
);
|
||||
|
||||
// State for rate limit countdown components
|
||||
const [countdownComponents, setCountdownComponents] = useState<CountdownComponents | null>(() =>
|
||||
errorInfo.rateLimitResetTime
|
||||
? getCountdownComponents(errorInfo.rateLimitResetTime)
|
||||
: null
|
||||
);
|
||||
|
||||
// Update countdown every second for rate limit errors
|
||||
// Extract timestamp for stable useEffect dependency (avoids optional chaining in deps)
|
||||
const resetTimeMs = errorInfo.rateLimitResetTime?.getTime();
|
||||
|
||||
useEffect(() => {
|
||||
if (errorInfo.type !== 'rate_limit' || !errorInfo.rateLimitResetTime) {
|
||||
// Clear stale countdown state when error type changes away from rate_limit
|
||||
setCountdownComponents(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const resetTime = errorInfo.rateLimitResetTime;
|
||||
let intervalId: ReturnType<typeof setInterval> | undefined;
|
||||
|
||||
const updateCountdown = () => {
|
||||
const components = getCountdownComponents(resetTime);
|
||||
setCountdownComponents(components);
|
||||
// Stop the interval when countdown expires
|
||||
if (!components && intervalId) {
|
||||
clearInterval(intervalId);
|
||||
intervalId = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// Update immediately
|
||||
updateCountdown();
|
||||
|
||||
// Only set interval if countdown is still active
|
||||
if (getCountdownComponents(resetTime)) {
|
||||
intervalId = setInterval(updateCountdown, 1000);
|
||||
}
|
||||
|
||||
// Cleanup on unmount or when error changes
|
||||
return () => {
|
||||
if (intervalId) clearInterval(intervalId);
|
||||
};
|
||||
}, [errorInfo.type, resetTimeMs]);
|
||||
|
||||
// Format countdown using i18n
|
||||
const formatCountdownDisplay = (components: CountdownComponents | null): string => {
|
||||
if (!components) return '';
|
||||
if (components.hours > 0) {
|
||||
return t('githubErrors.countdownHoursMinutes', {
|
||||
hours: components.hours,
|
||||
minutes: components.minutes,
|
||||
});
|
||||
}
|
||||
return t('githubErrors.countdownMinutesSeconds', {
|
||||
minutes: components.minutes,
|
||||
seconds: components.seconds,
|
||||
});
|
||||
};
|
||||
|
||||
// Get configuration for this error type
|
||||
const config = ERROR_CONFIG[errorInfo.type];
|
||||
const Icon = config.icon;
|
||||
|
||||
// Determine which actions to show
|
||||
const showRetry = ['rate_limit', 'network', 'unknown'].includes(errorInfo.type);
|
||||
const showSettings = ['auth', 'permission'].includes(errorInfo.type);
|
||||
const isRateLimitExpired =
|
||||
errorInfo.type === 'rate_limit' &&
|
||||
errorInfo.rateLimitResetTime &&
|
||||
new Date() >= errorInfo.rateLimitResetTime;
|
||||
|
||||
// Don't render if no error
|
||||
if (!error) return null;
|
||||
|
||||
// Compute time remaining once for both message key selection and translation
|
||||
const rateLimitDiffMs = errorInfo.rateLimitResetTime
|
||||
? errorInfo.rateLimitResetTime.getTime() - Date.now()
|
||||
: undefined;
|
||||
|
||||
// Get the translated message with appropriate interpolation values
|
||||
const messageKey = getMessageKey(errorInfo, rateLimitDiffMs);
|
||||
// Only pass positive minutes/hours values to avoid stale negative/zero values
|
||||
const rawMinutes = rateLimitDiffMs ? Math.ceil(rateLimitDiffMs / 60000) : undefined;
|
||||
const minutes = rawMinutes && rawMinutes > 0 ? rawMinutes : undefined;
|
||||
const hours = minutes ? Math.ceil(minutes / 60) : undefined;
|
||||
|
||||
const errorMessage = t(messageKey, {
|
||||
defaultValue: errorInfo.message,
|
||||
minutes,
|
||||
hours,
|
||||
scopes: errorInfo.requiredScopes?.join(', '),
|
||||
});
|
||||
|
||||
// Compact variant for inline display
|
||||
if (compact) {
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
aria-label={errorMessage}
|
||||
className={cn(
|
||||
'flex items-center gap-2 p-3 rounded-lg bg-muted/50 border border-border',
|
||||
className
|
||||
)}
|
||||
title={errorMessage}
|
||||
>
|
||||
<Icon className={cn('h-4 w-4 shrink-0', config.iconColorClass)} />
|
||||
<span className="text-sm text-muted-foreground flex-1 truncate">
|
||||
{t(config.titleKey)}
|
||||
</span>
|
||||
{showRetry && onRetry && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onRetry}
|
||||
className="h-7 px-2"
|
||||
>
|
||||
<RefreshCw className="h-3 w-3 mr-1" />
|
||||
{t('buttons.retry')}
|
||||
</Button>
|
||||
)}
|
||||
{showSettings && onOpenSettings && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onOpenSettings}
|
||||
className="h-7 px-2"
|
||||
>
|
||||
<Settings2 className="h-3 w-3 mr-1" />
|
||||
{t('actions.settings')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Full card variant for blocking errors
|
||||
return (
|
||||
<Card role="alert" className={cn('border-destructive/50 m-4', className)}>
|
||||
<CardContent className="pt-6">
|
||||
<div className="flex flex-col items-center gap-4 text-center">
|
||||
<div className="w-12 h-12 rounded-full bg-muted/50 flex items-center justify-center">
|
||||
<Icon className={cn('h-6 w-6', config.iconColorClass)} />
|
||||
</div>
|
||||
<div className="space-y-2 max-w-md">
|
||||
<h3 className="font-semibold text-lg text-foreground">
|
||||
{t(config.titleKey)}
|
||||
</h3>
|
||||
<p className="text-sm text-muted-foreground">{errorMessage}</p>
|
||||
{/* Rate limit countdown display */}
|
||||
{errorInfo.type === 'rate_limit' && countdownComponents && (
|
||||
<p className="text-xs text-warning font-medium">
|
||||
{t('githubErrors.resetsIn', { time: formatCountdownDisplay(countdownComponents) })}
|
||||
</p>
|
||||
)}
|
||||
{/* Rate limit expired - show retry prompt */}
|
||||
{isRateLimitExpired && (
|
||||
<p className="text-xs text-primary">
|
||||
{t('githubErrors.rateLimitExpired')}
|
||||
</p>
|
||||
)}
|
||||
{/* Required scopes for permission errors */}
|
||||
{errorInfo.requiredScopes && errorInfo.requiredScopes.length > 0 && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t('githubErrors.requiredScopes')}:{' '}
|
||||
<code className="bg-muted px-1 rounded">
|
||||
{errorInfo.requiredScopes.join(', ')}
|
||||
</code>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
{/* Action buttons */}
|
||||
<div className="flex gap-2">
|
||||
{showRetry && onRetry && (
|
||||
<Button onClick={onRetry} variant="outline" size="sm">
|
||||
<RefreshCw className="h-4 w-4 mr-2" />
|
||||
{t('buttons.retry')}
|
||||
</Button>
|
||||
)}
|
||||
{showSettings && onOpenSettings && (
|
||||
<Button onClick={onOpenSettings} variant="outline" size="sm">
|
||||
<Settings2 className="h-4 w-4 mr-2" />
|
||||
{t('actions.settings')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import { useRef, useEffect, useCallback, useState } from 'react';
|
||||
import { Loader2, AlertCircle } from 'lucide-react';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import { ScrollArea } from '../../ui/scroll-area';
|
||||
import { IssueListItem } from './IssueListItem';
|
||||
import { EmptyState } from './EmptyStates';
|
||||
import { GitHubErrorDisplay } from './GitHubErrorDisplay';
|
||||
import type { IssueListProps } from '../types';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
@@ -15,7 +16,9 @@ export function IssueList({
|
||||
error,
|
||||
onSelectIssue,
|
||||
onInvestigate,
|
||||
onLoadMore
|
||||
onLoadMore,
|
||||
onRetry,
|
||||
onOpenSettings
|
||||
}: IssueListProps) {
|
||||
const { t } = useTranslation('common');
|
||||
const loadMoreTriggerRef = useRef<HTMLDivElement>(null);
|
||||
@@ -50,12 +53,12 @@ export function IssueList({
|
||||
// Load-more errors are shown inline near the load-more trigger
|
||||
if (error && issues.length === 0) {
|
||||
return (
|
||||
<div className="p-4 bg-destructive/10 border-b border-destructive/30">
|
||||
<div className="flex items-center gap-2 text-sm text-destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
{error}
|
||||
</div>
|
||||
</div>
|
||||
<GitHubErrorDisplay
|
||||
error={error}
|
||||
onRetry={onRetry}
|
||||
onOpenSettings={onOpenSettings}
|
||||
className="flex-1"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -85,15 +88,18 @@ export function IssueList({
|
||||
))}
|
||||
|
||||
{/* Load more trigger / Loading indicator */}
|
||||
{/* Inline error for load-more failures (visible even when onLoadMore is undefined during search) */}
|
||||
{error && issues.length > 0 && (
|
||||
<GitHubErrorDisplay
|
||||
error={error}
|
||||
onRetry={onRetry}
|
||||
onOpenSettings={onOpenSettings}
|
||||
compact
|
||||
className="w-full"
|
||||
/>
|
||||
)}
|
||||
{onLoadMore && (
|
||||
<div ref={loadMoreTriggerRef} className="py-4 flex flex-col items-center gap-2">
|
||||
{/* Inline error for load-more failures (when issues are already loaded) */}
|
||||
{error && issues.length > 0 && (
|
||||
<div className="flex items-center gap-2 text-sm text-destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
{isLoadingMore ? (
|
||||
<div className="flex items-center gap-2 text-muted-foreground">
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
|
||||
+500
@@ -0,0 +1,500 @@
|
||||
/**
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
/**
|
||||
* Unit tests for GitHubErrorDisplay component.
|
||||
* Tests error display, icon rendering, button visibility, and countdown functionality.
|
||||
*/
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import '@testing-library/jest-dom/vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { GitHubErrorDisplay } from '../GitHubErrorDisplay';
|
||||
import type { GitHubErrorInfo } from '../../types';
|
||||
|
||||
// Mock react-i18next
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, options?: Record<string, unknown>) => {
|
||||
const translations: Record<string, string> = {
|
||||
'githubErrors.rateLimitTitle': 'GitHub Rate Limit Reached',
|
||||
'githubErrors.authTitle': 'GitHub Authentication Required',
|
||||
'githubErrors.permissionTitle': 'GitHub Permission Denied',
|
||||
'githubErrors.notFoundTitle': 'GitHub Resource Not Found',
|
||||
'githubErrors.networkTitle': 'GitHub Connection Error',
|
||||
'githubErrors.unknownTitle': 'GitHub Error',
|
||||
'githubErrors.rateLimitMessage': 'GitHub API rate limit reached. Please wait a moment before trying again.',
|
||||
'githubErrors.rateLimitMessageMinutes': `GitHub API rate limit reached. Please wait ${options?.minutes ?? 'X'} minute(s) before trying again.`,
|
||||
'githubErrors.rateLimitMessageHours': `GitHub API rate limit reached. Rate limit resets in approximately ${options?.hours ?? 'X'} hour(s).`,
|
||||
'githubErrors.authMessage': 'GitHub authentication failed. Please check your GitHub token in Settings.',
|
||||
'githubErrors.permissionMessage': 'GitHub permission denied. Your token may not have the required access.',
|
||||
'githubErrors.permissionMessageScopes': `GitHub permission denied. Your token is missing required scopes: ${options?.scopes ?? ''}. Please update your GitHub token in Settings.`,
|
||||
'githubErrors.notFoundMessage': 'The requested GitHub resource was not found.',
|
||||
'githubErrors.networkMessage': 'Unable to connect to GitHub. Please check your internet connection.',
|
||||
'githubErrors.unknownMessage': 'An unexpected error occurred while communicating with GitHub.',
|
||||
'githubErrors.resetsIn': options?.time ? `Resets in ${options.time as string}` : 'Resets in',
|
||||
'githubErrors.countdownHoursMinutes': `${options?.hours ?? 0}h ${options?.minutes ?? 0}m`,
|
||||
'githubErrors.countdownMinutesSeconds': `${options?.minutes ?? 0}m ${options?.seconds ?? 0}s`,
|
||||
'githubErrors.rateLimitExpired': 'Rate limit has reset. You can retry now.',
|
||||
'githubErrors.requiredScopes': 'Required scopes',
|
||||
'buttons.retry': 'Retry',
|
||||
'actions.settings': 'Settings',
|
||||
};
|
||||
return translations[key] || key;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
// Helper to create mock GitHubErrorInfo
|
||||
function createMockErrorInfo(
|
||||
type: GitHubErrorInfo['type'],
|
||||
overrides: Partial<GitHubErrorInfo> = {}
|
||||
): GitHubErrorInfo {
|
||||
const defaults: Record<string, GitHubErrorInfo> = {
|
||||
rate_limit: {
|
||||
type: 'rate_limit',
|
||||
message: 'GitHub API rate limit reached. Please wait a moment before trying again.',
|
||||
statusCode: 403,
|
||||
},
|
||||
auth: {
|
||||
type: 'auth',
|
||||
message: 'GitHub authentication failed. Please check your GitHub token in Settings.',
|
||||
statusCode: 401,
|
||||
},
|
||||
permission: {
|
||||
type: 'permission',
|
||||
message: 'GitHub permission denied. Your token may not have the required access.',
|
||||
statusCode: 403,
|
||||
},
|
||||
not_found: {
|
||||
type: 'not_found',
|
||||
message: 'The requested GitHub resource was not found.',
|
||||
statusCode: 404,
|
||||
},
|
||||
network: {
|
||||
type: 'network',
|
||||
message: 'Unable to connect to GitHub. Please check your internet connection.',
|
||||
},
|
||||
unknown: {
|
||||
type: 'unknown',
|
||||
message: 'An unexpected error occurred while communicating with GitHub.',
|
||||
},
|
||||
};
|
||||
|
||||
return { ...defaults[type], ...overrides };
|
||||
}
|
||||
|
||||
describe('GitHubErrorDisplay', () => {
|
||||
describe('rendering null/empty states', () => {
|
||||
it('should render nothing when error is null', () => {
|
||||
const { container } = render(<GitHubErrorDisplay error={null} />);
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('should render nothing when error is an empty string', () => {
|
||||
// Empty string is falsy, so component should return null
|
||||
const { container } = render(
|
||||
<GitHubErrorDisplay error={'' as string} />
|
||||
);
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rendering with string error', () => {
|
||||
it('should render error display when error is a string', () => {
|
||||
render(<GitHubErrorDisplay error="401 Unauthorized" />);
|
||||
|
||||
// Should show the auth title (parsed from the error)
|
||||
expect(screen.getByText('GitHub Authentication Required')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render error display for rate limit string error', () => {
|
||||
render(<GitHubErrorDisplay error="rate limit exceeded" />);
|
||||
|
||||
expect(screen.getByText('GitHub Rate Limit Reached')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rendering with GitHubErrorInfo object', () => {
|
||||
it('should render rate_limit error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Rate Limit Reached')).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText(/GitHub API rate limit reached/)
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render auth error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Authentication Required')).toBeInTheDocument();
|
||||
expect(screen.getByText(/authentication failed/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render permission error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('permission', {
|
||||
requiredScopes: ['repo', 'workflow'],
|
||||
});
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Permission Denied')).toBeInTheDocument();
|
||||
// Check that permission message is rendered
|
||||
expect(screen.getByText(/Your token is missing required scopes/)).toBeInTheDocument();
|
||||
// Should show required scopes in the code element
|
||||
expect(screen.getByText('repo, workflow')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render not_found error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('not_found');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Resource Not Found')).toBeInTheDocument();
|
||||
expect(screen.getByText(/not found/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render network error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('network');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Connection Error')).toBeInTheDocument();
|
||||
expect(screen.getByText(/Unable to connect/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render unknown error correctly', () => {
|
||||
const errorInfo = createMockErrorInfo('unknown');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('GitHub Error')).toBeInTheDocument();
|
||||
expect(screen.getByText(/unexpected error/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('compact mode', () => {
|
||||
it('should render compact variant when compact=true', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
render(<GitHubErrorDisplay error={errorInfo} compact />);
|
||||
|
||||
// In compact mode, the title is in a smaller span
|
||||
expect(screen.getByText('GitHub Rate Limit Reached')).toBeInTheDocument();
|
||||
// Should not render the card structure (no centered layout)
|
||||
expect(screen.queryByRole('heading', { level: 3 })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show retry button in compact mode for rate_limit errors', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} compact onRetry={onRetry} />);
|
||||
|
||||
const retryButton = screen.getByRole('button', { name: /retry/i });
|
||||
expect(retryButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(retryButton);
|
||||
expect(onRetry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should show settings button in compact mode for auth errors', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
const onOpenSettings = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} compact onOpenSettings={onOpenSettings} />);
|
||||
|
||||
const settingsButton = screen.getByRole('button', { name: /settings/i });
|
||||
expect(settingsButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(settingsButton);
|
||||
expect(onOpenSettings).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('full card mode (default)', () => {
|
||||
it('should render card structure by default', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
// Should render heading
|
||||
expect(screen.getByRole('heading', { level: 3 })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show retry button for rate_limit errors with onRetry callback', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
const retryButton = screen.getByRole('button', { name: /retry/i });
|
||||
expect(retryButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(retryButton);
|
||||
expect(onRetry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should show retry button for network errors', () => {
|
||||
const errorInfo = createMockErrorInfo('network');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
const retryButton = screen.getByRole('button', { name: /retry/i });
|
||||
expect(retryButton).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show retry button for unknown errors', () => {
|
||||
const errorInfo = createMockErrorInfo('unknown');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
const retryButton = screen.getByRole('button', { name: /retry/i });
|
||||
expect(retryButton).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT show retry button for auth errors', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: /retry/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT show retry button for permission errors', () => {
|
||||
const errorInfo = createMockErrorInfo('permission');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: /retry/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT show retry button for not_found errors', () => {
|
||||
const errorInfo = createMockErrorInfo('not_found');
|
||||
const onRetry = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: /retry/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show settings button for auth errors with onOpenSettings callback', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
const onOpenSettings = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={onOpenSettings} />);
|
||||
|
||||
const settingsButton = screen.getByRole('button', { name: /settings/i });
|
||||
expect(settingsButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(settingsButton);
|
||||
expect(onOpenSettings).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should show settings button for permission errors', () => {
|
||||
const errorInfo = createMockErrorInfo('permission');
|
||||
const onOpenSettings = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={onOpenSettings} />);
|
||||
|
||||
const settingsButton = screen.getByRole('button', { name: /settings/i });
|
||||
expect(settingsButton).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT show settings button for rate_limit errors', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const onOpenSettings = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={onOpenSettings} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: /settings/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT show settings button for network errors', () => {
|
||||
const errorInfo = createMockErrorInfo('network');
|
||||
const onOpenSettings = vi.fn();
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={onOpenSettings} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: /settings/i })).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rate limit countdown', () => {
|
||||
it('should display countdown for rate limit errors with reset time', () => {
|
||||
// Set reset time 5 minutes in the future
|
||||
const resetTime = new Date(Date.now() + 5 * 60 * 1000);
|
||||
const errorInfo = createMockErrorInfo('rate_limit', {
|
||||
rateLimitResetTime: resetTime,
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
// Should show countdown in "Xm Ys" format (e.g., "4m 59s" or "5m 0s")
|
||||
expect(screen.getByText(/Resets in \d+m \d+s/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should set up interval to update countdown', () => {
|
||||
vi.useFakeTimers();
|
||||
const resetTime = new Date(Date.now() + 2 * 60 * 1000);
|
||||
const errorInfo = createMockErrorInfo('rate_limit', {
|
||||
rateLimitResetTime: resetTime,
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
// Initial countdown should be displayed
|
||||
expect(screen.getByText(/Resets in/)).toBeInTheDocument();
|
||||
|
||||
// Verify interval is running by checking timers
|
||||
const timerCount = vi.getTimerCount();
|
||||
expect(timerCount).toBe(1); // One interval should be running
|
||||
|
||||
// Advance time and verify interval still fires
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(screen.getByText(/Resets in/)).toBeInTheDocument();
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should NOT show countdown for non-rate-limit errors', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.queryByText(/Resets in/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show rate limit expired message when reset time has passed', () => {
|
||||
// Set reset time in the past
|
||||
const resetTime = new Date(Date.now() - 1000);
|
||||
const errorInfo = createMockErrorInfo('rate_limit', {
|
||||
rateLimitResetTime: resetTime,
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(
|
||||
screen.getByText('Rate limit has reset. You can retry now.')
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should cleanup interval on unmount', () => {
|
||||
vi.useFakeTimers();
|
||||
const clearIntervalSpy = vi.spyOn(global, 'clearInterval');
|
||||
|
||||
const resetTime = new Date(Date.now() + 5 * 60 * 1000);
|
||||
const errorInfo = createMockErrorInfo('rate_limit', {
|
||||
rateLimitResetTime: resetTime,
|
||||
});
|
||||
|
||||
const { unmount } = render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
// Verify the countdown was rendered
|
||||
expect(screen.getByText(/Resets in/)).toBeInTheDocument();
|
||||
|
||||
// Unmount and verify clearInterval was called
|
||||
unmount();
|
||||
expect(clearIntervalSpy).toHaveBeenCalled();
|
||||
|
||||
clearIntervalSpy.mockRestore();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
|
||||
describe('required scopes display', () => {
|
||||
it('should display required scopes for permission errors', () => {
|
||||
const errorInfo = createMockErrorInfo('permission', {
|
||||
requiredScopes: ['repo', 'read:org', 'workflow'],
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.getByText('Required scopes:')).toBeInTheDocument();
|
||||
// The scopes appear in a code element
|
||||
expect(screen.getByText('repo, read:org, workflow')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT display scopes section when no scopes are provided', () => {
|
||||
const errorInfo = createMockErrorInfo('permission', {
|
||||
requiredScopes: undefined,
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.queryByText('Required scopes:')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should NOT display scopes section when scopes array is empty', () => {
|
||||
const errorInfo = createMockErrorInfo('permission', {
|
||||
requiredScopes: [],
|
||||
});
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
expect(screen.queryByText('Required scopes:')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('className prop', () => {
|
||||
it('should apply custom className in full card mode', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const { container } = render(
|
||||
<GitHubErrorDisplay error={errorInfo} className="custom-class" />
|
||||
);
|
||||
|
||||
expect(container.firstChild).toHaveClass('custom-class');
|
||||
});
|
||||
|
||||
it('should apply custom className in compact mode', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const { container } = render(
|
||||
<GitHubErrorDisplay error={errorInfo} compact className="custom-compact-class" />
|
||||
);
|
||||
|
||||
expect(container.firstChild).toHaveClass('custom-compact-class');
|
||||
});
|
||||
});
|
||||
|
||||
describe('callback stability', () => {
|
||||
it('should not call onRetry on initial render', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
const onRetry = vi.fn();
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={onRetry} />);
|
||||
|
||||
expect(onRetry).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call onOpenSettings on initial render', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
const onOpenSettings = vi.fn();
|
||||
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={onOpenSettings} />);
|
||||
|
||||
expect(onOpenSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('accessibility', () => {
|
||||
it('should have role="alert" for screen reader announcements', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
render(<GitHubErrorDisplay error={errorInfo} />);
|
||||
|
||||
// The error card should have role="alert" for accessibility
|
||||
expect(screen.getByRole('alert')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should have role="alert" in compact mode', () => {
|
||||
const errorInfo = createMockErrorInfo('network');
|
||||
render(<GitHubErrorDisplay error={errorInfo} compact />);
|
||||
|
||||
expect(screen.getByRole('alert')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should have accessible button labels', () => {
|
||||
const errorInfo = createMockErrorInfo('rate_limit');
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function -- callback not needed for this test
|
||||
render(<GitHubErrorDisplay error={errorInfo} onRetry={() => { /* no-op */ }} />);
|
||||
|
||||
const button = screen.getByRole('button', { name: /retry/i });
|
||||
expect(button).toHaveTextContent('Retry');
|
||||
});
|
||||
|
||||
it('should have accessible settings button label', () => {
|
||||
const errorInfo = createMockErrorInfo('auth');
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function -- callback not needed for this test
|
||||
render(<GitHubErrorDisplay error={errorInfo} onOpenSettings={() => { /* no-op */ }} />);
|
||||
|
||||
const button = screen.getByRole('button', { name: /settings/i });
|
||||
expect(button).toHaveTextContent('Settings');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,3 +6,4 @@ export { IssueListHeader } from './IssueListHeader';
|
||||
export { IssueList } from './IssueList';
|
||||
export { AutoFixButton } from './AutoFixButton';
|
||||
export { BatchReviewWizard } from './BatchReviewWizard';
|
||||
export { GitHubErrorDisplay } from './GitHubErrorDisplay';
|
||||
|
||||
@@ -3,6 +3,47 @@ import type { AutoFixConfig, AutoFixQueueItem } from '../../../../preload/api/mo
|
||||
|
||||
export type FilterState = 'open' | 'closed' | 'all';
|
||||
|
||||
/**
|
||||
* Classification types for GitHub API errors.
|
||||
* Used to determine appropriate icon, message, and actions for error display.
|
||||
*/
|
||||
export type GitHubErrorType =
|
||||
| 'rate_limit'
|
||||
| 'auth'
|
||||
| 'permission'
|
||||
| 'network'
|
||||
| 'not_found'
|
||||
| 'unknown';
|
||||
|
||||
/**
|
||||
* Parsed GitHub error information with metadata.
|
||||
* Returned by the github-error-parser utility.
|
||||
*
|
||||
* IMPORTANT: The `message` field contains hardcoded English strings intended
|
||||
* ONLY as a fallback defaultValue for i18n translation. Direct consumers should
|
||||
* use the `type` field to look up the appropriate translation key (e.g.,
|
||||
* 'githubErrors.rateLimitMessage') via react-i18next rather than displaying
|
||||
* `message` directly. This ensures proper localization for all users.
|
||||
*/
|
||||
export interface GitHubErrorInfo {
|
||||
/** The classified error type */
|
||||
type: GitHubErrorType;
|
||||
/**
|
||||
* User-friendly error message in English.
|
||||
* NOTE: Use only as defaultValue for i18n - do not display directly.
|
||||
* Use type field to look up translation key (e.g., 'githubErrors.rateLimitMessage').
|
||||
*/
|
||||
message: string;
|
||||
/** Original raw error string (for debugging/details) */
|
||||
rawMessage?: string;
|
||||
/** Rate limit reset time (only for rate_limit type) */
|
||||
rateLimitResetTime?: Date;
|
||||
/** Required OAuth scopes that are missing (only for permission type) */
|
||||
requiredScopes?: string[];
|
||||
/** HTTP status code if available */
|
||||
statusCode?: number;
|
||||
}
|
||||
|
||||
export interface GitHubIssuesProps {
|
||||
onOpenSettings?: () => void;
|
||||
/** Navigate to view a task in the kanban board */
|
||||
@@ -76,6 +117,10 @@ export interface IssueListProps {
|
||||
onSelectIssue: (issueNumber: number) => void;
|
||||
onInvestigate: (issue: GitHubIssue) => void;
|
||||
onLoadMore?: () => void;
|
||||
/** Callback for retry button in error display */
|
||||
onRetry?: () => void;
|
||||
/** Callback for settings button in error display */
|
||||
onOpenSettings?: () => void;
|
||||
}
|
||||
|
||||
export interface EmptyStateProps {
|
||||
|
||||
+691
@@ -0,0 +1,691 @@
|
||||
/**
|
||||
* Unit tests for GitHub API error parser utility.
|
||||
* Tests error classification, metadata extraction, and helper functions.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
parseGitHubError,
|
||||
isRateLimitError,
|
||||
isAuthError,
|
||||
isNetworkError,
|
||||
isRecoverableError,
|
||||
requiresSettingsAction,
|
||||
} from '../github-error-parser';
|
||||
import type { GitHubErrorType } from '../../types';
|
||||
|
||||
describe('parseGitHubError', () => {
|
||||
describe('null/undefined/empty handling', () => {
|
||||
it('should return unknown for null input', () => {
|
||||
const result = parseGitHubError(null);
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.message).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return unknown for undefined input', () => {
|
||||
const result = parseGitHubError(undefined);
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.message).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return unknown for empty string', () => {
|
||||
const result = parseGitHubError('');
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.message).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return unknown for whitespace-only string', () => {
|
||||
const result = parseGitHubError(' ');
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.message).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rate_limit errors', () => {
|
||||
it('should detect "rate limit exceeded" pattern', () => {
|
||||
const result = parseGitHubError('GitHub API error: rate limit exceeded');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.message).toContain('rate limit');
|
||||
expect(result.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('should detect "API rate limit exceeded" pattern', () => {
|
||||
const result = parseGitHubError('API rate limit exceeded for user');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should detect "too many requests" pattern', () => {
|
||||
const result = parseGitHubError('Error: too many requests');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should detect "403 rate limit" pattern', () => {
|
||||
const result = parseGitHubError('403 rate limit reached');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('should detect "abuse rate limit" pattern', () => {
|
||||
const result = parseGitHubError('Abuse rate limit triggered');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should detect "secondary rate limit" pattern', () => {
|
||||
const result = parseGitHubError('Secondary rate limit exceeded');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should extract rate limit reset time from ISO date format', () => {
|
||||
const result = parseGitHubError('rate limit exceeded, resets at 2024-01-15T12:00:00Z');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.rateLimitResetTime).toBeInstanceOf(Date);
|
||||
expect(result.rateLimitResetTime?.getUTCFullYear()).toBe(2024);
|
||||
});
|
||||
|
||||
it('should extract rate limit reset time from Unix timestamp', () => {
|
||||
const result = parseGitHubError('X-RateLimit-Reset: 1705312800');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.rateLimitResetTime).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
it('should generate user-friendly message with time remaining', () => {
|
||||
// Create a date 5 minutes in the future
|
||||
const futureDate = new Date(Date.now() + 5 * 60 * 1000);
|
||||
const isoString = futureDate.toISOString();
|
||||
const result = parseGitHubError(`rate limit exceeded, resets at ${isoString}`);
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.message).toContain('rate limit');
|
||||
});
|
||||
|
||||
it('should generate fallback message when reset time has passed', () => {
|
||||
// Create a date in the past
|
||||
const pastDate = new Date(Date.now() - 5 * 60 * 1000);
|
||||
const isoString = pastDate.toISOString();
|
||||
const result = parseGitHubError(`rate limit exceeded, resets at ${isoString}`);
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.message).toContain('moment');
|
||||
});
|
||||
|
||||
it('should include raw message truncated to MAX_RAW_ERROR_LENGTH', () => {
|
||||
const longError = 'rate limit exceeded ' + 'x'.repeat(600);
|
||||
const result = parseGitHubError(longError);
|
||||
expect(result.type).toBe('rate_limit');
|
||||
expect(result.rawMessage).toBeDefined();
|
||||
expect(result.rawMessage?.length).toBeLessThanOrEqual(503); // 500 + '...'
|
||||
});
|
||||
});
|
||||
|
||||
describe('auth errors', () => {
|
||||
it('should detect "401" pattern', () => {
|
||||
const result = parseGitHubError('HTTP 401 Unauthorized');
|
||||
expect(result.type).toBe('auth');
|
||||
expect(result.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('should detect "unauthorized" pattern', () => {
|
||||
const result = parseGitHubError('Error: unauthorized access');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should detect "bad credentials" pattern', () => {
|
||||
const result = parseGitHubError('Bad credentials');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should detect "authentication failed" pattern', () => {
|
||||
const result = parseGitHubError('Authentication failed');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should detect "invalid token" pattern', () => {
|
||||
const result = parseGitHubError('Invalid token provided');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should detect "token expired" pattern', () => {
|
||||
const result = parseGitHubError('Token expired');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should detect "not authenticated" pattern', () => {
|
||||
const result = parseGitHubError('Not authenticated');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should generate user-friendly message mentioning Settings', () => {
|
||||
const result = parseGitHubError('401 Unauthorized');
|
||||
expect(result.message).toContain('authentication');
|
||||
expect(result.message).toContain('Settings');
|
||||
});
|
||||
});
|
||||
|
||||
describe('not_found errors', () => {
|
||||
it('should detect "404" pattern', () => {
|
||||
const result = parseGitHubError('HTTP 404 Not Found');
|
||||
expect(result.type).toBe('not_found');
|
||||
expect(result.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
it('should detect "not found" pattern', () => {
|
||||
const result = parseGitHubError('Repository not found');
|
||||
expect(result.type).toBe('not_found');
|
||||
});
|
||||
|
||||
it('should detect "no such repository" pattern', () => {
|
||||
const result = parseGitHubError('No such repository exists');
|
||||
expect(result.type).toBe('not_found');
|
||||
});
|
||||
|
||||
it('should detect "does not exist" pattern', () => {
|
||||
const result = parseGitHubError('Resource does not exist');
|
||||
expect(result.type).toBe('not_found');
|
||||
});
|
||||
|
||||
it('should detect "user not found" pattern', () => {
|
||||
const result = parseGitHubError('User not found');
|
||||
expect(result.type).toBe('not_found');
|
||||
});
|
||||
|
||||
it('should generate user-friendly message about verifying repository', () => {
|
||||
const result = parseGitHubError('404 Not Found');
|
||||
expect(result.message).toContain('not found');
|
||||
expect(result.message).toContain('verify');
|
||||
});
|
||||
});
|
||||
|
||||
describe('network errors', () => {
|
||||
it('should detect "network error" pattern', () => {
|
||||
const result = parseGitHubError('Network error');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "failed to fetch" pattern', () => {
|
||||
const result = parseGitHubError('Failed to fetch data');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "ECONNREFUSED" pattern', () => {
|
||||
const result = parseGitHubError('Error: ECONNREFUSED');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "ECONNRESET" pattern', () => {
|
||||
const result = parseGitHubError('Error: ECONNRESET');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "ETIMEDOUT" pattern', () => {
|
||||
const result = parseGitHubError('Error: ETIMEDOUT');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "connection refused" pattern', () => {
|
||||
const result = parseGitHubError('Connection refused');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "connection timeout" pattern', () => {
|
||||
const result = parseGitHubError('Connection timeout');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "DNS error" pattern', () => {
|
||||
const result = parseGitHubError('DNS error occurred');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "offline" pattern', () => {
|
||||
const result = parseGitHubError('You are offline');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should detect "no internet" pattern', () => {
|
||||
const result = parseGitHubError('No internet connection');
|
||||
expect(result.type).toBe('network');
|
||||
});
|
||||
|
||||
it('should generate user-friendly message about internet connection', () => {
|
||||
const result = parseGitHubError('Network error');
|
||||
expect(result.message).toContain('internet');
|
||||
});
|
||||
});
|
||||
|
||||
describe('permission errors', () => {
|
||||
it('should detect "403" pattern (without rate limit context)', () => {
|
||||
const result = parseGitHubError('HTTP 403 Forbidden');
|
||||
expect(result.type).toBe('permission');
|
||||
expect(result.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('should detect "forbidden" pattern', () => {
|
||||
const result = parseGitHubError('Access forbidden');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "permission denied" pattern', () => {
|
||||
const result = parseGitHubError('Permission denied');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "insufficient scope" pattern', () => {
|
||||
const result = parseGitHubError('Insufficient scope');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "access denied" pattern', () => {
|
||||
const result = parseGitHubError('Access denied');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "repository access denied" pattern', () => {
|
||||
const result = parseGitHubError('Repository access denied');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "requires admin access" pattern', () => {
|
||||
const result = parseGitHubError('Requires admin access');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should detect "missing required scope" pattern', () => {
|
||||
const result = parseGitHubError('Missing required scope');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should extract required scopes from error message with 403', () => {
|
||||
const result = parseGitHubError('403 Forbidden - missing scopes: repo, read:org');
|
||||
expect(result.type).toBe('permission');
|
||||
expect(result.requiredScopes).toContain('repo');
|
||||
expect(result.requiredScopes).toContain('read:org');
|
||||
});
|
||||
|
||||
it('should extract scopes from "requires:" format with 403', () => {
|
||||
const result = parseGitHubError('403 - Requires: repo, workflow');
|
||||
expect(result.type).toBe('permission');
|
||||
expect(result.requiredScopes).toContain('repo');
|
||||
expect(result.requiredScopes).toContain('workflow');
|
||||
});
|
||||
|
||||
it('should extract scopes from X-Accepted-OAuth-Scopes header with 403', () => {
|
||||
const result = parseGitHubError('403 Forbidden X-Accepted-OAuth-Scopes: repo');
|
||||
expect(result.type).toBe('permission');
|
||||
expect(result.requiredScopes).toContain('repo');
|
||||
});
|
||||
|
||||
it('should generate user-friendly message with scopes', () => {
|
||||
const result = parseGitHubError('403 Forbidden - missing scopes: repo, workflow');
|
||||
expect(result.message).toContain('repo');
|
||||
expect(result.message).toContain('workflow');
|
||||
expect(result.message).toContain('Settings');
|
||||
});
|
||||
|
||||
it('should generate user-friendly message without scopes', () => {
|
||||
const result = parseGitHubError('403 Forbidden');
|
||||
expect(result.message).toContain('permission');
|
||||
expect(result.message).toContain('Settings');
|
||||
});
|
||||
});
|
||||
|
||||
describe('unknown errors', () => {
|
||||
it('should return unknown for unrecognized error patterns', () => {
|
||||
const result = parseGitHubError('Something unexpected happened');
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.message).toBeDefined();
|
||||
});
|
||||
|
||||
it('should include raw message for unknown errors', () => {
|
||||
const result = parseGitHubError('Custom error message');
|
||||
expect(result.rawMessage).toBe('Custom error message');
|
||||
});
|
||||
|
||||
it('should extract status code even for unknown errors', () => {
|
||||
const result = parseGitHubError('HTTP 500 Internal Server Error');
|
||||
expect(result.type).toBe('unknown');
|
||||
expect(result.statusCode).toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('error classification priority', () => {
|
||||
it('should prioritize rate_limit over permission (both 403)', () => {
|
||||
const result = parseGitHubError('403 rate limit exceeded');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should classify as permission when 403 without rate limit context', () => {
|
||||
const result = parseGitHubError('403 forbidden');
|
||||
expect(result.type).toBe('permission');
|
||||
});
|
||||
|
||||
it('should handle errors with multiple patterns correctly', () => {
|
||||
// Rate limit should take priority
|
||||
const result = parseGitHubError('403 API rate limit exceeded');
|
||||
expect(result.type).toBe('rate_limit');
|
||||
});
|
||||
|
||||
it('should prioritize auth over not_found when both patterns present', () => {
|
||||
// "401" should be classified as auth, not not_found
|
||||
const result = parseGitHubError('HTTP 401 Unauthorized - user not found');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should prioritize auth over network when 401 appears with network context', () => {
|
||||
const result = parseGitHubError('Network error: HTTP 401');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should classify as not_found when 404 without auth patterns', () => {
|
||||
const result = parseGitHubError('HTTP 404 Not Found');
|
||||
expect(result.type).toBe('not_found');
|
||||
});
|
||||
|
||||
it('should not match bare 401 in unrelated numbers', () => {
|
||||
// The word boundary should prevent matching "1401" as a 401 error
|
||||
const result = parseGitHubError('Error code 14010 occurred');
|
||||
expect(result.type).toBe('unknown');
|
||||
});
|
||||
|
||||
it('should not match bare 404 embedded in other numbers', () => {
|
||||
// The word boundary should prevent matching "404" embedded in "14040"
|
||||
const result = parseGitHubError('Error code 14040 occurred');
|
||||
expect(result.type).toBe('unknown');
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should handle multiline error messages', () => {
|
||||
const result = parseGitHubError(`Error occurred:
|
||||
HTTP 401 Unauthorized
|
||||
Please check your credentials`);
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should handle case-insensitive matching', () => {
|
||||
const testCases = [
|
||||
{ input: 'RATE LIMIT EXCEEDED', expected: 'rate_limit' as GitHubErrorType },
|
||||
{ input: 'UNAUTHORIZED', expected: 'auth' as GitHubErrorType },
|
||||
{ input: 'NOT FOUND', expected: 'not_found' as GitHubErrorType },
|
||||
{ input: 'NETWORK ERROR', expected: 'network' as GitHubErrorType },
|
||||
{ input: 'FORBIDDEN', expected: 'permission' as GitHubErrorType },
|
||||
];
|
||||
|
||||
for (const { input, expected } of testCases) {
|
||||
const result = parseGitHubError(input);
|
||||
expect(result.type).toBe(expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle errors with JSON content', () => {
|
||||
const result = parseGitHubError('{"message":"Bad credentials","status":401}');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should handle errors with leading/trailing whitespace', () => {
|
||||
const result = parseGitHubError(' 401 Unauthorized ');
|
||||
expect(result.type).toBe('auth');
|
||||
});
|
||||
|
||||
it('should sanitize very long error messages', () => {
|
||||
const longError = 'A'.repeat(1000);
|
||||
const result = parseGitHubError(longError);
|
||||
expect(result.rawMessage?.length).toBeLessThanOrEqual(503);
|
||||
expect(result.rawMessage).toContain('...');
|
||||
});
|
||||
|
||||
it('should not include rateLimitResetTime for non-rate-limit errors', () => {
|
||||
const result = parseGitHubError('401 Unauthorized');
|
||||
expect(result.rateLimitResetTime).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not include requiredScopes for non-permission errors', () => {
|
||||
const result = parseGitHubError('401 Unauthorized');
|
||||
expect(result.requiredScopes).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('isRateLimitError', () => {
|
||||
it('should return true for rate limit errors', () => {
|
||||
expect(isRateLimitError('rate limit exceeded')).toBe(true);
|
||||
expect(isRateLimitError('API rate limit exceeded')).toBe(true);
|
||||
expect(isRateLimitError('too many requests')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for non-rate-limit errors', () => {
|
||||
expect(isRateLimitError('401 Unauthorized')).toBe(false);
|
||||
expect(isRateLimitError('404 Not Found')).toBe(false);
|
||||
expect(isRateLimitError('Network error')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined/empty', () => {
|
||||
expect(isRateLimitError(null)).toBe(false);
|
||||
expect(isRateLimitError(undefined)).toBe(false);
|
||||
expect(isRateLimitError('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should use parsedInfo when provided', () => {
|
||||
const parsedInfo = { type: 'rate_limit' as const, message: 'test' };
|
||||
expect(isRateLimitError('unrelated error', parsedInfo)).toBe(true);
|
||||
expect(isRateLimitError(null, parsedInfo)).toBe(true);
|
||||
expect(isRateLimitError(undefined, parsedInfo)).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore parsedInfo when error type differs', () => {
|
||||
const authParsedInfo = { type: 'auth' as const, message: 'test' };
|
||||
expect(isRateLimitError('rate limit exceeded', authParsedInfo)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isAuthError', () => {
|
||||
it('should return true for auth errors', () => {
|
||||
expect(isAuthError('401 Unauthorized')).toBe(true);
|
||||
expect(isAuthError('Bad credentials')).toBe(true);
|
||||
expect(isAuthError('Invalid token')).toBe(true);
|
||||
expect(isAuthError('Not authenticated')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for non-auth errors', () => {
|
||||
expect(isAuthError('rate limit exceeded')).toBe(false);
|
||||
expect(isAuthError('404 Not Found')).toBe(false);
|
||||
expect(isAuthError('Network error')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined/empty', () => {
|
||||
expect(isAuthError(null)).toBe(false);
|
||||
expect(isAuthError(undefined)).toBe(false);
|
||||
expect(isAuthError('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should use parsedInfo when provided', () => {
|
||||
const parsedInfo = { type: 'auth' as const, message: 'test' };
|
||||
expect(isAuthError('unrelated error', parsedInfo)).toBe(true);
|
||||
expect(isAuthError(null, parsedInfo)).toBe(true);
|
||||
expect(isAuthError(undefined, parsedInfo)).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore parsedInfo when error type differs', () => {
|
||||
const rateLimitParsedInfo = { type: 'rate_limit' as const, message: 'test' };
|
||||
expect(isAuthError('401 Unauthorized', rateLimitParsedInfo)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNetworkError', () => {
|
||||
it('should return true for network errors', () => {
|
||||
expect(isNetworkError('Network error')).toBe(true);
|
||||
expect(isNetworkError('Failed to fetch')).toBe(true);
|
||||
expect(isNetworkError('ECONNREFUSED')).toBe(true);
|
||||
expect(isNetworkError('Connection timeout')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for non-network errors', () => {
|
||||
expect(isNetworkError('401 Unauthorized')).toBe(false);
|
||||
expect(isNetworkError('rate limit exceeded')).toBe(false);
|
||||
expect(isNetworkError('404 Not Found')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined/empty', () => {
|
||||
expect(isNetworkError(null)).toBe(false);
|
||||
expect(isNetworkError(undefined)).toBe(false);
|
||||
expect(isNetworkError('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should use parsedInfo when provided', () => {
|
||||
const parsedInfo = { type: 'network' as const, message: 'test' };
|
||||
expect(isNetworkError('unrelated error', parsedInfo)).toBe(true);
|
||||
expect(isNetworkError(null, parsedInfo)).toBe(true);
|
||||
expect(isNetworkError(undefined, parsedInfo)).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore parsedInfo when error type differs', () => {
|
||||
const authParsedInfo = { type: 'auth' as const, message: 'test' };
|
||||
expect(isNetworkError('Network error', authParsedInfo)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isRecoverableError', () => {
|
||||
it('should return true for recoverable errors (rate_limit, network, unknown)', () => {
|
||||
expect(isRecoverableError('rate limit exceeded')).toBe(true);
|
||||
expect(isRecoverableError('Network error')).toBe(true);
|
||||
expect(isRecoverableError('Unknown error occurred')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for non-recoverable errors (auth, permission, not_found)', () => {
|
||||
expect(isRecoverableError('401 Unauthorized')).toBe(false);
|
||||
expect(isRecoverableError('403 Forbidden')).toBe(false);
|
||||
expect(isRecoverableError('404 Not Found')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined/empty', () => {
|
||||
expect(isRecoverableError(null)).toBe(false);
|
||||
expect(isRecoverableError(undefined)).toBe(false);
|
||||
expect(isRecoverableError('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should use parsedInfo when provided', () => {
|
||||
const rateLimitInfo = { type: 'rate_limit' as const, message: 'test' };
|
||||
const networkInfo = { type: 'network' as const, message: 'test' };
|
||||
const unknownInfo = { type: 'unknown' as const, message: 'test' };
|
||||
expect(isRecoverableError('unrelated error', rateLimitInfo)).toBe(true);
|
||||
expect(isRecoverableError(null, networkInfo)).toBe(true);
|
||||
expect(isRecoverableError(undefined, unknownInfo)).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore parsedInfo when error type is non-recoverable', () => {
|
||||
const authParsedInfo = { type: 'auth' as const, message: 'test' };
|
||||
const permissionParsedInfo = { type: 'permission' as const, message: 'test' };
|
||||
const notFoundParsedInfo = { type: 'not_found' as const, message: 'test' };
|
||||
expect(isRecoverableError('Network error', authParsedInfo)).toBe(false);
|
||||
expect(isRecoverableError('rate limit exceeded', permissionParsedInfo)).toBe(false);
|
||||
expect(isRecoverableError('unknown', notFoundParsedInfo)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('requiresSettingsAction', () => {
|
||||
it('should return true for errors requiring settings action (auth, permission)', () => {
|
||||
expect(requiresSettingsAction('401 Unauthorized')).toBe(true);
|
||||
expect(requiresSettingsAction('403 Forbidden')).toBe(true);
|
||||
expect(requiresSettingsAction('Invalid token')).toBe(true);
|
||||
expect(requiresSettingsAction('403 Forbidden - missing scopes: repo')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for errors not requiring settings (rate_limit, network, not_found, unknown)', () => {
|
||||
expect(requiresSettingsAction('rate limit exceeded')).toBe(false);
|
||||
expect(requiresSettingsAction('Network error')).toBe(false);
|
||||
expect(requiresSettingsAction('404 Not Found')).toBe(false);
|
||||
expect(requiresSettingsAction('Unknown error')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null/undefined/empty', () => {
|
||||
expect(requiresSettingsAction(null)).toBe(false);
|
||||
expect(requiresSettingsAction(undefined)).toBe(false);
|
||||
expect(requiresSettingsAction('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should use parsedInfo when provided', () => {
|
||||
const authInfo = { type: 'auth' as const, message: 'test' };
|
||||
const permissionInfo = { type: 'permission' as const, message: 'test' };
|
||||
expect(requiresSettingsAction('unrelated error', authInfo)).toBe(true);
|
||||
expect(requiresSettingsAction(null, permissionInfo)).toBe(true);
|
||||
expect(requiresSettingsAction(undefined, authInfo)).toBe(true);
|
||||
});
|
||||
|
||||
it('should ignore parsedInfo when error type does not require settings', () => {
|
||||
const rateLimitInfo = { type: 'rate_limit' as const, message: 'test' };
|
||||
const networkInfo = { type: 'network' as const, message: 'test' };
|
||||
const notFoundInfo = { type: 'not_found' as const, message: 'test' };
|
||||
expect(requiresSettingsAction('401 Unauthorized', rateLimitInfo)).toBe(false);
|
||||
expect(requiresSettingsAction('403 Forbidden', networkInfo)).toBe(false);
|
||||
expect(requiresSettingsAction('invalid token', notFoundInfo)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cross-cutting concerns', () => {
|
||||
describe('consistency between parseGitHubError and helper functions', () => {
|
||||
it('should have consistent rate_limit detection', () => {
|
||||
const error = 'rate limit exceeded';
|
||||
const parsed = parseGitHubError(error);
|
||||
expect(parsed.type).toBe('rate_limit');
|
||||
expect(isRateLimitError(error)).toBe(true);
|
||||
});
|
||||
|
||||
it('should have consistent auth detection', () => {
|
||||
const error = '401 Unauthorized';
|
||||
const parsed = parseGitHubError(error);
|
||||
expect(parsed.type).toBe('auth');
|
||||
expect(isAuthError(error)).toBe(true);
|
||||
});
|
||||
|
||||
it('should have consistent network detection', () => {
|
||||
const error = 'Network error';
|
||||
const parsed = parseGitHubError(error);
|
||||
expect(parsed.type).toBe('network');
|
||||
expect(isNetworkError(error)).toBe(true);
|
||||
});
|
||||
|
||||
it('should have consistent recoverable classification', () => {
|
||||
const errors = ['rate limit exceeded', 'Network error', 'Unknown error'];
|
||||
for (const error of errors) {
|
||||
const parsed = parseGitHubError(error);
|
||||
expect(isRecoverableError(error)).toBe(['rate_limit', 'network', 'unknown'].includes(parsed.type));
|
||||
}
|
||||
});
|
||||
|
||||
it('should have consistent settings action classification', () => {
|
||||
const errors = ['401 Unauthorized', '403 Forbidden'];
|
||||
for (const error of errors) {
|
||||
const parsed = parseGitHubError(error);
|
||||
expect(requiresSettingsAction(error)).toBe(['auth', 'permission'].includes(parsed.type));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('statusCode extraction', () => {
|
||||
it('should extract 403 for rate_limit errors', () => {
|
||||
const result = parseGitHubError('rate limit exceeded');
|
||||
expect(result.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('should extract 401 for auth errors', () => {
|
||||
const result = parseGitHubError('Bad credentials');
|
||||
expect(result.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('should extract 404 for not_found errors', () => {
|
||||
const result = parseGitHubError('Not found');
|
||||
expect(result.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
it('should extract 403 for permission errors', () => {
|
||||
const result = parseGitHubError('Forbidden');
|
||||
expect(result.statusCode).toBe(403);
|
||||
});
|
||||
|
||||
it('should extract status code from message when present', () => {
|
||||
const result = parseGitHubError('HTTP 429 Too Many Requests');
|
||||
expect(result.statusCode).toBe(429);
|
||||
});
|
||||
|
||||
it('should not extract invalid status codes', () => {
|
||||
const result = parseGitHubError('Error 999');
|
||||
expect(result.statusCode).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,497 @@
|
||||
/**
|
||||
* GitHub API error parser utility.
|
||||
* Parses raw error strings to classify GitHub API errors and extract metadata.
|
||||
*/
|
||||
|
||||
import type { GitHubErrorType, GitHubErrorInfo } from '../types';
|
||||
|
||||
/**
|
||||
* Maximum length for raw error messages stored in GitHubErrorInfo.
|
||||
* Truncates to prevent memory bloat and UI issues.
|
||||
*/
|
||||
const MAX_RAW_ERROR_LENGTH = 500;
|
||||
|
||||
/**
|
||||
* Patterns for rate limit errors (HTTP 403 with rate limit context).
|
||||
* Note: Pattern 1 covers all "rate limit" variations (api rate limit exceeded,
|
||||
* abuse rate limit, secondary rate limit, etc.) via substring matching.
|
||||
*/
|
||||
const RATE_LIMIT_PATTERNS = [
|
||||
/rate\s*limit/i, // Covers all variations containing "rate limit"
|
||||
/too\s*many\s*requests/i,
|
||||
/403.*rate/i,
|
||||
];
|
||||
|
||||
/**
|
||||
* Patterns for authentication errors (HTTP 401)
|
||||
* Note: Bare status codes are intentionally omitted here - STATUS_CODE_PATTERN
|
||||
* handles HTTP-context-aware matching to avoid false positives.
|
||||
*/
|
||||
const AUTH_PATTERNS = [
|
||||
/unauthorized/i,
|
||||
/bad\s*credentials/i,
|
||||
/authentication\s*failed/i,
|
||||
/invalid\s*(oauth\s*)?token/i,
|
||||
/token\s*(is\s*)?(invalid|expired|required)/i,
|
||||
/not\s*authenticated/i,
|
||||
/requires\s*authentication/i, // GitHub 401 response body
|
||||
];
|
||||
|
||||
/**
|
||||
* Patterns for permission/scope errors (HTTP 403 with scope context)
|
||||
* Note: Bare status codes are intentionally omitted here - STATUS_CODE_PATTERN
|
||||
* handles HTTP-context-aware matching to avoid false positives.
|
||||
*/
|
||||
const PERMISSION_PATTERNS = [
|
||||
/forbidden/i,
|
||||
/permission\s*denied/i,
|
||||
/insufficient\s*(scope|permission)/i,
|
||||
/access\s*denied/i,
|
||||
/repository\s*access\s*denied/i,
|
||||
/not\s*authorized\s*to\s*access/i,
|
||||
/requires\s*(admin|write|read)\s*access/i,
|
||||
/missing\s*required\s*scope/i,
|
||||
// Matches "requires: repo" or "requires workflow" for OAuth scope context
|
||||
// Uses specific scope names to avoid matching "requires authentication" (auth error)
|
||||
/requires[:\s]+(?:repo|admin|write|read|workflow|org|gist|notification|user|project|package|delete|discussion)/i,
|
||||
];
|
||||
|
||||
/**
|
||||
* Patterns for not found errors (HTTP 404)
|
||||
* Note: Bare status codes are intentionally omitted here - STATUS_CODE_PATTERN
|
||||
* handles HTTP-context-aware matching to avoid false positives (e.g., "Issue #404").
|
||||
*/
|
||||
const NOT_FOUND_PATTERNS = [
|
||||
/not\s*found/i,
|
||||
/no\s*such\s*(repository|repo|issue|resource)/i,
|
||||
/does\s*not\s*exist/i,
|
||||
/repository\s*not\s*found/i,
|
||||
/user\s*not\s*found/i,
|
||||
];
|
||||
|
||||
/**
|
||||
* Patterns for network/connectivity errors
|
||||
*/
|
||||
const NETWORK_PATTERNS = [
|
||||
/network\s*(error|failed|unreachable)/i,
|
||||
/failed\s*to\s*fetch/i,
|
||||
/enetunreach/i,
|
||||
/econnrefused/i,
|
||||
/econnreset/i,
|
||||
/etimedout/i,
|
||||
/dns\s*(error|failed)/i,
|
||||
/offline/i,
|
||||
/no\s*internet/i,
|
||||
/unable\s*to\s*connect/i,
|
||||
/connection\s*(refused|reset|timeout|failed)/i,
|
||||
];
|
||||
|
||||
/**
|
||||
* Pattern to extract required OAuth scopes from error messages
|
||||
* Matches formats like:
|
||||
* - "requires: repo, read:org"
|
||||
* - "missing scopes: repo, workflow"
|
||||
* - "X-Accepted-OAuth-Scopes: repo"
|
||||
* Stops at sentence boundaries or non-scope characters
|
||||
*/
|
||||
const REQUIRED_SCOPES_PATTERN = /(?:requires?[:\s]*|missing\s*scopes?[:\s]*|X-Accepted-OAuth-Scopes[:\s]*)([a-z0-9_:]+(?:[,\s]+[a-z0-9_:]+)*)/i;
|
||||
|
||||
/**
|
||||
* Pattern to extract HTTP status code from error messages.
|
||||
* Matches status codes preceded by HTTP context keywords or at string start
|
||||
* (for common error formats like "403 Forbidden").
|
||||
*/
|
||||
const STATUS_CODE_PATTERN = /(?:^|HTTP\s*|status[:\s]*|error[:\s]*|code[:\s]*)\b([1-5]\d{2})\b/i;
|
||||
|
||||
/**
|
||||
* Sanitize error output to a reasonable length.
|
||||
* Prevents memory bloat and UI issues from very long error messages.
|
||||
*/
|
||||
function sanitizeRawError(error: string): string {
|
||||
if (error.length > MAX_RAW_ERROR_LENGTH) {
|
||||
return error.substring(0, MAX_RAW_ERROR_LENGTH) + '...';
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum reasonable reset duration in seconds (24 hours).
|
||||
* Prevents malformed error strings from creating far-future dates.
|
||||
*/
|
||||
const MAX_RESET_SECONDS = 86400;
|
||||
|
||||
/**
|
||||
* Extract rate limit reset time from error message.
|
||||
* Parses various formats and returns a Date object if found.
|
||||
* Handles both absolute timestamps and relative durations ("in X seconds").
|
||||
*/
|
||||
function extractRateLimitResetTime(error: string): Date | undefined {
|
||||
// First, try to match relative duration pattern (e.g., "reset in 3600 seconds")
|
||||
const relativePattern = /reset[s]?\s*in[:\s]*(\d+)\s*seconds?/i;
|
||||
const relativeMatch = error.match(relativePattern);
|
||||
if (relativeMatch) {
|
||||
const seconds = parseInt(relativeMatch[1], 10);
|
||||
// Validate: positive, non-NaN, and within reasonable bounds (24 hours max)
|
||||
if (!Number.isNaN(seconds) && seconds > 0 && seconds <= MAX_RESET_SECONDS) {
|
||||
return new Date(Date.now() + seconds * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
// Then try absolute timestamp pattern
|
||||
const absolutePattern = /(?:reset[s]?\s*at[:\s]*|X-RateLimit-Reset[:\s]*)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z?|\d+)/i;
|
||||
const match = error.match(absolutePattern);
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const resetValue = match[1].trim();
|
||||
|
||||
// Check if it's an ISO date string
|
||||
if (resetValue.includes('-') && resetValue.includes('T')) {
|
||||
const date = new Date(resetValue);
|
||||
if (Number.isNaN(date.getTime())) return undefined;
|
||||
// Validate: within reasonable bounds (24 hours max from now)
|
||||
if (date.getTime() - Date.now() > MAX_RESET_SECONDS * 1000) return undefined;
|
||||
return date;
|
||||
}
|
||||
|
||||
// Check if it's a Unix timestamp (seconds or milliseconds)
|
||||
const numericValue = parseInt(resetValue, 10);
|
||||
if (!Number.isNaN(numericValue)) {
|
||||
// GitHub API uses seconds, JavaScript uses milliseconds
|
||||
// Values > 1e12 are likely milliseconds already
|
||||
const timestamp = numericValue > 1e12 ? numericValue : numericValue * 1000;
|
||||
const date = new Date(timestamp);
|
||||
if (Number.isNaN(date.getTime())) return undefined;
|
||||
// Validate: within reasonable bounds (24 hours max from now)
|
||||
if (date.getTime() - Date.now() > MAX_RESET_SECONDS * 1000) return undefined;
|
||||
return date;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract required OAuth scopes from error message.
|
||||
* Returns an array of scope strings if found.
|
||||
*/
|
||||
function extractRequiredScopes(error: string): string[] | undefined {
|
||||
const match = error.match(REQUIRED_SCOPES_PATTERN);
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const scopes = match[1]
|
||||
.split(/[,\s]+/)
|
||||
.map(s => s.trim())
|
||||
.filter(s => s.length > 0);
|
||||
|
||||
return scopes.length > 0 ? scopes : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract HTTP status code from error message.
|
||||
*/
|
||||
function extractStatusCode(error: string): number | undefined {
|
||||
const match = error.match(STATUS_CODE_PATTERN);
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const code = parseInt(match[1], 10);
|
||||
// Only return valid HTTP status codes
|
||||
if (code >= 100 && code < 600) {
|
||||
return code;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the error matches any of the given patterns.
|
||||
*/
|
||||
function matchesPatterns(error: string, patterns: RegExp[]): boolean {
|
||||
return patterns.some(pattern => pattern.test(error));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for rate limit errors.
|
||||
*/
|
||||
function getRateLimitMessage(_error: string, resetTime?: Date): string {
|
||||
if (resetTime) {
|
||||
const now = new Date();
|
||||
const diffMs = resetTime.getTime() - now.getTime();
|
||||
|
||||
if (diffMs > 0) {
|
||||
const diffMins = Math.ceil(diffMs / 60000);
|
||||
if (diffMins < 60) {
|
||||
return `GitHub API rate limit reached. Please wait ${diffMins} minute${diffMins !== 1 ? 's' : ''} before trying again.`;
|
||||
}
|
||||
const diffHours = Math.ceil(diffMins / 60);
|
||||
return `GitHub API rate limit reached. Rate limit resets in approximately ${diffHours} hour${diffHours !== 1 ? 's' : ''}.`;
|
||||
}
|
||||
}
|
||||
|
||||
return 'GitHub API rate limit reached. Please wait a moment before trying again.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for authentication errors.
|
||||
*/
|
||||
function getAuthMessage(): string {
|
||||
return 'GitHub authentication failed. Please check your GitHub token in Settings and try again.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for permission errors.
|
||||
*/
|
||||
function getPermissionMessage(scopes?: string[]): string {
|
||||
if (scopes && scopes.length > 0) {
|
||||
return `GitHub permission denied. Your token is missing required scopes: ${scopes.join(', ')}. Please update your GitHub token in Settings.`;
|
||||
}
|
||||
return 'GitHub permission denied. Your token may not have the required access. Please check your token permissions in Settings.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for not found errors.
|
||||
*/
|
||||
function getNotFoundMessage(): string {
|
||||
return 'The requested GitHub resource was not found. Please verify the repository exists and you have access to it.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for network errors.
|
||||
*/
|
||||
function getNetworkMessage(): string {
|
||||
return 'Unable to connect to GitHub. Please check your internet connection and try again.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user-friendly message for unknown errors.
|
||||
*/
|
||||
function getUnknownMessage(): string {
|
||||
return 'An unexpected error occurred while communicating with GitHub. Please try again.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify error type based on pattern matching and optional status code.
|
||||
* Priority: rate_limit > auth > permission > not_found > network > unknown
|
||||
* Note: Permission checks run before not_found to properly classify 403 responses.
|
||||
* Status code fallback takes priority over network patterns since HTTP status
|
||||
* codes are more specific than generic network error text.
|
||||
* @param error - The error string to classify
|
||||
* @param statusCode - Optional HTTP status code extracted with context (helps classify when text patterns don't match)
|
||||
*/
|
||||
function classifyError(error: string, statusCode?: number): GitHubErrorType {
|
||||
// Check rate limit first (403 can also be permission, but rate limit is more specific)
|
||||
if (matchesPatterns(error, RATE_LIMIT_PATTERNS)) {
|
||||
return 'rate_limit';
|
||||
}
|
||||
|
||||
// Check auth (401 is always auth)
|
||||
if (matchesPatterns(error, AUTH_PATTERNS)) {
|
||||
return 'auth';
|
||||
}
|
||||
|
||||
// Check permission (403 without rate limit context) before not_found
|
||||
// to properly classify 403 responses that might contain "not found" text
|
||||
if (matchesPatterns(error, PERMISSION_PATTERNS)) {
|
||||
return 'permission';
|
||||
}
|
||||
|
||||
// Check not found (404 is always not_found)
|
||||
if (matchesPatterns(error, NOT_FOUND_PATTERNS)) {
|
||||
return 'not_found';
|
||||
}
|
||||
|
||||
// Use status code fallback BEFORE network patterns
|
||||
// HTTP status codes are more specific than generic network error text
|
||||
if (statusCode === 401) return 'auth';
|
||||
if (statusCode === 403) return 'permission';
|
||||
if (statusCode === 404) return 'not_found';
|
||||
|
||||
// Check network errors (only if no status code fallback matched)
|
||||
if (matchesPatterns(error, NETWORK_PATTERNS)) {
|
||||
return 'network';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a GitHub API error string and return classified error information.
|
||||
*
|
||||
* IMPORTANT: The returned `message` field contains hardcoded English strings
|
||||
* intended ONLY as a fallback defaultValue for i18n translation. Consumers
|
||||
* should use the `type` field to look up the appropriate translation key
|
||||
* (e.g., 'githubErrors.rateLimitMessage') via react-i18next rather than
|
||||
* displaying `message` directly. This ensures proper localization.
|
||||
*
|
||||
* Translation key mapping by type:
|
||||
* - rate_limit → 'githubErrors.rateLimitMessage' (or rateLimitMessageMinutes/Hours)
|
||||
* - auth → 'githubErrors.authMessage'
|
||||
* - permission → 'githubErrors.permissionMessage' (or permissionMessageScopes)
|
||||
* - not_found → 'githubErrors.notFoundMessage'
|
||||
* - network → 'githubErrors.networkMessage'
|
||||
* - unknown → 'githubErrors.unknownMessage'
|
||||
*
|
||||
* @param error - The raw error string (typically from issues-store error state)
|
||||
* @returns GitHubErrorInfo object with classified type, user-friendly message, and metadata
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const errorInfo = parseGitHubError('GitHub API error: 403 - API rate limit exceeded');
|
||||
* // Use type to get i18n key, message only as fallback:
|
||||
* // t(`githubErrors.${errorInfo.type}Message`, { defaultValue: errorInfo.message })
|
||||
* ```
|
||||
*/
|
||||
export function parseGitHubError(error: string | null | undefined): GitHubErrorInfo {
|
||||
// Handle null/undefined/empty errors
|
||||
if (!error || typeof error !== 'string' || error.trim() === '') {
|
||||
return {
|
||||
type: 'unknown',
|
||||
message: getUnknownMessage(),
|
||||
};
|
||||
}
|
||||
|
||||
const trimmedError = error.trim();
|
||||
// Extract status code first so we can use it for classification fallback
|
||||
const statusCode = extractStatusCode(trimmedError);
|
||||
const errorType = classifyError(trimmedError, statusCode);
|
||||
|
||||
switch (errorType) {
|
||||
case 'rate_limit': {
|
||||
const resetTime = extractRateLimitResetTime(trimmedError);
|
||||
return {
|
||||
type: 'rate_limit',
|
||||
message: getRateLimitMessage(trimmedError, resetTime),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
rateLimitResetTime: resetTime,
|
||||
statusCode: statusCode ?? 403,
|
||||
};
|
||||
}
|
||||
|
||||
case 'auth':
|
||||
return {
|
||||
type: 'auth',
|
||||
message: getAuthMessage(),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
statusCode: statusCode ?? 401,
|
||||
};
|
||||
|
||||
case 'permission': {
|
||||
const scopes = extractRequiredScopes(trimmedError);
|
||||
return {
|
||||
type: 'permission',
|
||||
message: getPermissionMessage(scopes),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
requiredScopes: scopes,
|
||||
statusCode: statusCode ?? 403,
|
||||
};
|
||||
}
|
||||
|
||||
case 'not_found':
|
||||
return {
|
||||
type: 'not_found',
|
||||
message: getNotFoundMessage(),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
statusCode: statusCode ?? 404,
|
||||
};
|
||||
|
||||
case 'network':
|
||||
return {
|
||||
type: 'network',
|
||||
message: getNetworkMessage(),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
};
|
||||
|
||||
default:
|
||||
return {
|
||||
type: 'unknown',
|
||||
message: getUnknownMessage(),
|
||||
rawMessage: sanitizeRawError(trimmedError),
|
||||
statusCode,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error is a rate limit error.
|
||||
* Convenience function for quick checks without full parsing.
|
||||
* @param error - Raw error string or null/undefined
|
||||
* @param parsedInfo - Optional pre-parsed GitHubErrorInfo to avoid re-classification
|
||||
*/
|
||||
export function isRateLimitError(
|
||||
error: string | null | undefined,
|
||||
parsedInfo?: GitHubErrorInfo | null
|
||||
): boolean {
|
||||
if (parsedInfo) return parsedInfo.type === 'rate_limit';
|
||||
if (!error) return false;
|
||||
const trimmed = error.trim();
|
||||
return classifyError(trimmed, extractStatusCode(trimmed)) === 'rate_limit';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error is an authentication error.
|
||||
* Convenience function for quick checks without full parsing.
|
||||
* @param error - Raw error string or null/undefined
|
||||
* @param parsedInfo - Optional pre-parsed GitHubErrorInfo to avoid re-classification
|
||||
*/
|
||||
export function isAuthError(
|
||||
error: string | null | undefined,
|
||||
parsedInfo?: GitHubErrorInfo | null
|
||||
): boolean {
|
||||
if (parsedInfo) return parsedInfo.type === 'auth';
|
||||
if (!error) return false;
|
||||
const trimmed = error.trim();
|
||||
return classifyError(trimmed, extractStatusCode(trimmed)) === 'auth';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error is a network error.
|
||||
* Convenience function for quick checks without full parsing.
|
||||
* @param error - Raw error string or null/undefined
|
||||
* @param parsedInfo - Optional pre-parsed GitHubErrorInfo to avoid re-classification
|
||||
*/
|
||||
export function isNetworkError(
|
||||
error: string | null | undefined,
|
||||
parsedInfo?: GitHubErrorInfo | null
|
||||
): boolean {
|
||||
if (parsedInfo) return parsedInfo.type === 'network';
|
||||
if (!error) return false;
|
||||
const trimmed = error.trim();
|
||||
return classifyError(trimmed, extractStatusCode(trimmed)) === 'network';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error is recoverable (user can retry).
|
||||
* Rate limit, network, and unknown errors are considered recoverable.
|
||||
* @param error - Raw error string or null/undefined
|
||||
* @param parsedInfo - Optional pre-parsed GitHubErrorInfo to avoid re-classification
|
||||
*/
|
||||
export function isRecoverableError(
|
||||
error: string | null | undefined,
|
||||
parsedInfo?: GitHubErrorInfo | null
|
||||
): boolean {
|
||||
if (parsedInfo) return ['rate_limit', 'network', 'unknown'].includes(parsedInfo.type);
|
||||
if (!error) return false;
|
||||
const trimmed = error.trim();
|
||||
const errorType = classifyError(trimmed, extractStatusCode(trimmed));
|
||||
return ['rate_limit', 'network', 'unknown'].includes(errorType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an error requires user action in settings.
|
||||
* Auth and permission errors require settings changes.
|
||||
* @param error - Raw error string or null/undefined
|
||||
* @param parsedInfo - Optional pre-parsed GitHubErrorInfo to avoid re-classification
|
||||
*/
|
||||
export function requiresSettingsAction(
|
||||
error: string | null | undefined,
|
||||
parsedInfo?: GitHubErrorInfo | null
|
||||
): boolean {
|
||||
if (parsedInfo) return ['auth', 'permission'].includes(parsedInfo.type);
|
||||
if (!error) return false;
|
||||
const trimmed = error.trim();
|
||||
const errorType = classifyError(trimmed, extractStatusCode(trimmed));
|
||||
return ['auth', 'permission'].includes(errorType);
|
||||
}
|
||||
@@ -19,3 +19,13 @@ export function filterIssuesBySearch(issues: GitHubIssue[], searchQuery: string)
|
||||
issue.body?.toLowerCase().includes(query)
|
||||
);
|
||||
}
|
||||
|
||||
// Re-export GitHub error parser utilities
|
||||
export {
|
||||
parseGitHubError,
|
||||
isRateLimitError,
|
||||
isAuthError,
|
||||
isNetworkError,
|
||||
isRecoverableError,
|
||||
requiresSettingsAction,
|
||||
} from './github-error-parser';
|
||||
|
||||
@@ -34,6 +34,20 @@ describe('task-store-persistence', () => {
|
||||
let useTaskStore: typeof import('../task-store').useTaskStore;
|
||||
let loadTasks: typeof import('../task-store').loadTasks;
|
||||
let createTask: typeof import('../task-store').createTask;
|
||||
// Helper to create test tasks with all required fields
|
||||
const makeTask = (overrides: Partial<Task> = {}): Task => ({
|
||||
id: 'task-1',
|
||||
specId: '001-test-task',
|
||||
projectId: 'test-project',
|
||||
title: 'Test Task',
|
||||
description: 'Test description',
|
||||
status: 'backlog' as TaskStatus,
|
||||
logs: [],
|
||||
subtasks: [],
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
...overrides
|
||||
});
|
||||
|
||||
|
||||
beforeEach(async () => {
|
||||
|
||||
@@ -659,6 +659,28 @@
|
||||
"remote": "Remote"
|
||||
}
|
||||
},
|
||||
"githubErrors": {
|
||||
"rateLimitTitle": "GitHub Rate Limit Reached",
|
||||
"authTitle": "GitHub Authentication Required",
|
||||
"permissionTitle": "GitHub Permission Denied",
|
||||
"notFoundTitle": "GitHub Resource Not Found",
|
||||
"networkTitle": "GitHub Connection Error",
|
||||
"unknownTitle": "GitHub Error",
|
||||
"rateLimitMessage": "GitHub API rate limit reached. Please wait a moment before trying again.",
|
||||
"rateLimitMessageMinutes": "GitHub API rate limit reached. Please wait {{minutes}} minute(s) before trying again.",
|
||||
"rateLimitMessageHours": "GitHub API rate limit reached. Rate limit resets in approximately {{hours}} hour(s).",
|
||||
"authMessage": "GitHub authentication failed. Please check your GitHub token in Settings and try again.",
|
||||
"permissionMessage": "GitHub permission denied. Your token may not have the required access. Please check your token permissions in Settings.",
|
||||
"permissionMessageScopes": "GitHub permission denied. Your token is missing required scopes: {{scopes}}. Please update your GitHub token in Settings.",
|
||||
"notFoundMessage": "The requested GitHub resource was not found. Please verify the repository exists and you have access to it.",
|
||||
"networkMessage": "Unable to connect to GitHub. Please check your internet connection and try again.",
|
||||
"unknownMessage": "An unexpected error occurred while communicating with GitHub. Please try again.",
|
||||
"resetsIn": "Resets in {{time}}",
|
||||
"countdownHoursMinutes": "{{hours}}h {{minutes}}m",
|
||||
"countdownMinutesSeconds": "{{minutes}}m {{seconds}}s",
|
||||
"rateLimitExpired": "Rate limit has reset. You can retry now.",
|
||||
"requiredScopes": "Required scopes"
|
||||
},
|
||||
"roadmapProgress": {
|
||||
"elapsedTime": "Elapsed",
|
||||
"lastActivity": "Last activity",
|
||||
|
||||
@@ -659,6 +659,28 @@
|
||||
"remote": "Distante"
|
||||
}
|
||||
},
|
||||
"githubErrors": {
|
||||
"rateLimitTitle": "Limite de débit GitHub atteinte",
|
||||
"authTitle": "Authentification GitHub requise",
|
||||
"permissionTitle": "Permission GitHub refusée",
|
||||
"notFoundTitle": "Ressource GitHub introuvable",
|
||||
"networkTitle": "Erreur de connexion GitHub",
|
||||
"unknownTitle": "Erreur GitHub",
|
||||
"rateLimitMessage": "Limite de débit de l'API GitHub atteinte. Veuillez patienter un moment avant de réessayer.",
|
||||
"rateLimitMessageMinutes": "Limite de débit de l'API GitHub atteinte. Veuillez attendre {{minutes}} minute(s) avant de réessayer.",
|
||||
"rateLimitMessageHours": "Limite de débit de l'API GitHub atteinte. La limite se réinitialise dans environ {{hours}} heure(s).",
|
||||
"authMessage": "Échec de l'authentification GitHub. Veuillez vérifier votre jeton GitHub dans les Paramètres et réessayer.",
|
||||
"permissionMessage": "Permission GitHub refusée. Votre jeton n'a peut-être pas les accès requis. Veuillez vérifier les permissions de votre jeton dans les Paramètres.",
|
||||
"permissionMessageScopes": "Permission GitHub refusée. Votre jeton manque de permissions requises : {{scopes}}. Veuillez mettre à jour votre jeton GitHub dans les Paramètres.",
|
||||
"notFoundMessage": "La ressource GitHub demandée est introuvable. Veuillez vérifier que le dépôt existe et que vous y avez accès.",
|
||||
"networkMessage": "Impossible de se connecter à GitHub. Veuillez vérifier votre connexion Internet et réessayer.",
|
||||
"unknownMessage": "Une erreur inattendue s'est produite lors de la communication avec GitHub. Veuillez réessayer.",
|
||||
"resetsIn": "Réinitialisation dans {{time}}",
|
||||
"countdownHoursMinutes": "{{hours}}h {{minutes}}m",
|
||||
"countdownMinutesSeconds": "{{minutes}}m {{seconds}}s",
|
||||
"rateLimitExpired": "La limite de débit a été réinitialisée. Vous pouvez réessayer maintenant.",
|
||||
"requiredScopes": "Permissions requises"
|
||||
},
|
||||
"roadmapProgress": {
|
||||
"elapsedTime": "Écoulé",
|
||||
"lastActivity": "Dernière activité",
|
||||
|
||||
@@ -74,6 +74,31 @@ export function maskUserPaths(text: string): string {
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize text for safe inclusion in Sentry reports.
|
||||
* Masks user paths and redacts potential secrets (tokens, keys, credentials).
|
||||
*/
|
||||
export function sanitizeForSentry(text: string): string {
|
||||
if (!text) return text;
|
||||
|
||||
text = maskUserPaths(text);
|
||||
|
||||
// Redact common secret patterns (API keys, tokens, auth headers)
|
||||
// Bearer/token auth
|
||||
text = text.replace(/\b(Bearer|token|Token)\s+[A-Za-z0-9\-_.]+/gi, '$1 [REDACTED]');
|
||||
// API keys / secrets in key=value or key: value format
|
||||
text = text.replace(
|
||||
/\b(api[_-]?key|api[_-]?secret|auth[_-]?token|access[_-]?token|refresh[_-]?token|secret[_-]?key|password|credential|private[_-]?key)[=:]\s*\S+/gi,
|
||||
'$1=[REDACTED]'
|
||||
);
|
||||
// Anthropic API key format
|
||||
text = text.replace(/\bsk-ant-[A-Za-z0-9\-_]{20,}/g, '[REDACTED_KEY]');
|
||||
// Generic long hex/base64 tokens (40+ chars, likely secrets)
|
||||
text = text.replace(/\b[A-Za-z0-9+/]{40,}={0,2}\b/g, '[REDACTED_TOKEN]');
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively mask paths in an object
|
||||
* Handles nested objects and arrays
|
||||
|
||||
+1
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "auto-claude",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6-beta.4",
|
||||
"description": "Autonomous multi-agent coding framework powered by Claude AI",
|
||||
"license": "AGPL-3.0",
|
||||
"author": "Auto Claude Team",
|
||||
@@ -20,7 +20,6 @@
|
||||
"lint": "cd apps/frontend && npm run lint",
|
||||
"test": "cd apps/frontend && npm test",
|
||||
"test:backend": "node scripts/test-backend.js",
|
||||
"test:coverage": "node scripts/test-backend.js --cov --cov-report=term-missing --cov-report=html",
|
||||
"package": "cd apps/frontend && npm run package",
|
||||
"package:mac": "cd apps/frontend && npm run package:mac",
|
||||
"package:win": "cd apps/frontend && npm run package:win",
|
||||
|
||||
+1
-19
@@ -39,26 +39,8 @@ if (!fs.existsSync(pytestPath)) {
|
||||
}
|
||||
|
||||
// Get any additional args passed to the script
|
||||
// Process args to properly handle -m flag with spaces
|
||||
const args = process.argv.slice(2);
|
||||
let testArgs = '';
|
||||
|
||||
if (args.length > 0) {
|
||||
// Reconstruct args, joining -m with its value if separated
|
||||
const processedArgs = [];
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '-m' && i + 1 < args.length) {
|
||||
// Join -m with its value and quote it
|
||||
processedArgs.push(`-m "${args[i + 1]}"`);
|
||||
i++; // Skip next arg since we consumed it
|
||||
} else {
|
||||
processedArgs.push(args[i]);
|
||||
}
|
||||
}
|
||||
testArgs = processedArgs.join(' ');
|
||||
} else {
|
||||
testArgs = '-v';
|
||||
}
|
||||
const testArgs = args.length > 0 ? args.join(' ') : '-v';
|
||||
|
||||
// Run pytest
|
||||
const cmd = `"${pytestPath}" "${testsDir}" ${testArgs}`;
|
||||
|
||||
@@ -22,7 +22,7 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
# Add apps/backend directory to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "apps" / "backend"))
|
||||
|
||||
|
||||
class TestNoExternalParallelism:
|
||||
@@ -31,7 +31,7 @@ class TestNoExternalParallelism:
|
||||
def test_no_coordinator_module(self):
|
||||
"""No external coordinator module should exist."""
|
||||
coordinator_path = (
|
||||
Path(__file__).parent.parent / "apps" / "backend" / "coordinator.py"
|
||||
Path(__file__).parent.parent.parent / "apps" / "backend" / "coordinator.py"
|
||||
)
|
||||
assert not coordinator_path.exists(), (
|
||||
"coordinator.py should not exist. Parallel orchestration is handled "
|
||||
@@ -41,7 +41,7 @@ class TestNoExternalParallelism:
|
||||
def test_no_task_tool_module(self):
|
||||
"""No task_tool wrapper module should exist."""
|
||||
task_tool_path = (
|
||||
Path(__file__).parent.parent / "apps" / "backend" / "task_tool.py"
|
||||
Path(__file__).parent.parent.parent / "apps" / "backend" / "task_tool.py"
|
||||
)
|
||||
assert not task_tool_path.exists(), (
|
||||
"task_tool.py should not exist. The agent spawns subagents directly "
|
||||
@@ -51,7 +51,7 @@ class TestNoExternalParallelism:
|
||||
def test_no_subtask_worker_config(self):
|
||||
"""No external subtask worker agent config should exist."""
|
||||
worker_config = (
|
||||
Path(__file__).parent.parent / ".claude" / "agents" / "subtask-worker.md"
|
||||
Path(__file__).parent.parent.parent / ".claude" / "agents" / "subtask-worker.md"
|
||||
)
|
||||
assert not worker_config.exists(), (
|
||||
"subtask-worker.md should not exist. Subagents use Claude Code's "
|
||||
@@ -64,7 +64,7 @@ class TestCLIInterface:
|
||||
|
||||
def test_no_parallel_flag(self):
|
||||
"""CLI should not have --parallel argument."""
|
||||
run_py_path = Path(__file__).parent.parent / "apps" / "backend" / "run.py"
|
||||
run_py_path = Path(__file__).parent.parent.parent / "apps" / "backend" / "run.py"
|
||||
content = run_py_path.read_text(encoding="utf-8")
|
||||
|
||||
# Check that --parallel is not defined as an argument
|
||||
@@ -79,7 +79,7 @@ class TestCLIInterface:
|
||||
|
||||
def test_no_parallel_examples_in_docs(self):
|
||||
"""CLI documentation should not mention parallel mode."""
|
||||
run_py_path = Path(__file__).parent.parent / "apps" / "backend" / "run.py"
|
||||
run_py_path = Path(__file__).parent.parent.parent / "apps" / "backend" / "run.py"
|
||||
content = run_py_path.read_text(encoding="utf-8")
|
||||
|
||||
# The docstring should not have --parallel examples
|
||||
@@ -132,7 +132,7 @@ class TestAgentPrompt:
|
||||
def test_mentions_subagents(self):
|
||||
"""Agent prompt mentions subagent capability."""
|
||||
coder_prompt_path = (
|
||||
Path(__file__).parent.parent / "apps" / "backend" / "prompts" / "coder.md"
|
||||
Path(__file__).parent.parent.parent / "apps" / "backend" / "prompts" / "coder.md"
|
||||
)
|
||||
content = coder_prompt_path.read_text(encoding="utf-8")
|
||||
|
||||
@@ -143,7 +143,7 @@ class TestAgentPrompt:
|
||||
def test_mentions_parallel_capability(self):
|
||||
"""Agent prompt mentions parallel/concurrent capability."""
|
||||
coder_prompt_path = (
|
||||
Path(__file__).parent.parent / "apps" / "backend" / "prompts" / "coder.md"
|
||||
Path(__file__).parent.parent.parent / "apps" / "backend" / "prompts" / "coder.md"
|
||||
)
|
||||
content = coder_prompt_path.read_text(encoding="utf-8")
|
||||
|
||||
@@ -170,7 +170,7 @@ class TestModuleIntegrity:
|
||||
|
||||
def test_run_module_valid_syntax(self):
|
||||
"""Run module has valid Python syntax."""
|
||||
run_py_path = Path(__file__).parent.parent / "apps" / "backend" / "run.py"
|
||||
run_py_path = Path(__file__).parent.parent.parent / "apps" / "backend" / "run.py"
|
||||
content = run_py_path.read_text(encoding="utf-8")
|
||||
|
||||
try:
|
||||
@@ -181,7 +181,7 @@ class TestModuleIntegrity:
|
||||
def test_no_coordinator_imports(self):
|
||||
"""Core modules don't import coordinator."""
|
||||
for filename in ["run.py", "core/agent.py"]:
|
||||
filepath = Path(__file__).parent.parent / "apps" / "backend" / filename
|
||||
filepath = Path(__file__).parent.parent.parent / "apps" / "backend" / filename
|
||||
content = filepath.read_text(encoding="utf-8")
|
||||
|
||||
assert "from coordinator import" not in content, (
|
||||
@@ -194,7 +194,7 @@ class TestModuleIntegrity:
|
||||
def test_no_task_tool_imports(self):
|
||||
"""Core modules don't import task_tool."""
|
||||
for filename in ["run.py", "core/agent.py"]:
|
||||
filepath = Path(__file__).parent.parent / "apps" / "backend" / filename
|
||||
filepath = Path(__file__).parent.parent.parent / "apps" / "backend" / filename
|
||||
content = filepath.read_text(encoding="utf-8")
|
||||
|
||||
assert "from task_tool import" not in content, (
|
||||
@@ -210,7 +210,7 @@ class TestProjectDocumentation:
|
||||
|
||||
def test_no_parallel_cli_documented(self):
|
||||
"""CLAUDE.md doesn't document --parallel flag."""
|
||||
claude_md_path = Path(__file__).parent.parent / "CLAUDE.md"
|
||||
claude_md_path = Path(__file__).parent.parent.parent / "CLAUDE.md"
|
||||
content = claude_md_path.read_text(encoding="utf-8")
|
||||
|
||||
assert "--parallel 2" not in content, (
|
||||
@@ -219,7 +219,7 @@ class TestProjectDocumentation:
|
||||
|
||||
def test_subagent_architecture_documented(self):
|
||||
"""CLAUDE.md documents subagent-based architecture."""
|
||||
claude_md_path = Path(__file__).parent.parent / "CLAUDE.md"
|
||||
claude_md_path = Path(__file__).parent.parent.parent / "CLAUDE.md"
|
||||
content = claude_md_path.read_text(encoding="utf-8")
|
||||
|
||||
has_subagent = "subagent" in content.lower()
|
||||
@@ -334,7 +334,7 @@ class TestSubtaskTerminology:
|
||||
def test_progress_uses_subtask_terminology(self):
|
||||
"""Progress module uses subtask terminology."""
|
||||
progress_path = (
|
||||
Path(__file__).parent.parent / "apps" / "backend" / "core" / "progress.py"
|
||||
Path(__file__).parent.parent.parent / "apps" / "backend" / "core" / "progress.py"
|
||||
)
|
||||
content = progress_path.read_text(encoding="utf-8")
|
||||
|
||||
@@ -14,7 +14,7 @@ import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to path
|
||||
backend_path = Path(__file__).parent.parent / "apps" / "backend"
|
||||
backend_path = Path(__file__).parent.parent.parent / "apps" / "backend"
|
||||
sys.path.insert(0, str(backend_path))
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ Tests for planner→coder→QA state transitions including:
|
||||
Note: Uses temp_git_repo fixture from conftest.py for proper git isolation.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -22,7 +21,7 @@ from unittest.mock import AsyncMock, patch
|
||||
import pytest
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "apps" / "backend"))
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -190,7 +189,7 @@ class TestPlannerToCoderTransition:
|
||||
class TestPostSessionProcessing:
|
||||
"""Tests for post_session_processing function."""
|
||||
|
||||
def test_completed_subtask_records_success(self, test_env):
|
||||
async def test_completed_subtask_records_success(self, test_env):
|
||||
"""Test that completed subtask is recorded as successful."""
|
||||
from recovery import RecoveryManager
|
||||
from agents.session import post_session_processing
|
||||
@@ -212,20 +211,16 @@ class TestPostSessionProcessing:
|
||||
mock_insights.return_value = {"file_insights": [], "patterns_discovered": []}
|
||||
mock_memory.return_value = (True, "file")
|
||||
|
||||
# Run async function using asyncio.run()
|
||||
async def run_test():
|
||||
return await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
result = asyncio.run(run_test())
|
||||
result = await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
assert result is True, "Completed subtask should return True"
|
||||
|
||||
@@ -235,7 +230,7 @@ class TestPostSessionProcessing:
|
||||
assert history["attempts"][0]["success"] is True, "Attempt should be successful"
|
||||
assert history["status"] == "completed", "Status should be completed"
|
||||
|
||||
def test_in_progress_subtask_records_failure(self, test_env):
|
||||
async def test_in_progress_subtask_records_failure(self, test_env):
|
||||
"""Test that in_progress subtask is recorded as incomplete."""
|
||||
from recovery import RecoveryManager
|
||||
from agents.session import post_session_processing
|
||||
@@ -258,20 +253,16 @@ class TestPostSessionProcessing:
|
||||
mock_insights.return_value = {"file_insights": [], "patterns_discovered": []}
|
||||
mock_memory.return_value = (True, "file")
|
||||
|
||||
# Run async function using asyncio.run()
|
||||
async def run_test():
|
||||
return await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
result = asyncio.run(run_test())
|
||||
result = await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
assert result is False, "In-progress subtask should return False"
|
||||
|
||||
@@ -280,7 +271,7 @@ class TestPostSessionProcessing:
|
||||
assert len(history["attempts"]) == 1, "Should have 1 attempt"
|
||||
assert history["attempts"][0]["success"] is False, "Attempt should be unsuccessful"
|
||||
|
||||
def test_pending_subtask_records_failure(self, test_env):
|
||||
async def test_pending_subtask_records_failure(self, test_env):
|
||||
"""Test that pending (no progress) subtask is recorded as failure."""
|
||||
from recovery import RecoveryManager
|
||||
from agents.session import post_session_processing
|
||||
@@ -301,20 +292,16 @@ class TestPostSessionProcessing:
|
||||
mock_insights.return_value = {"file_insights": [], "patterns_discovered": []}
|
||||
mock_memory.return_value = (True, "file")
|
||||
|
||||
# Run async function using asyncio.run()
|
||||
async def run_test():
|
||||
return await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
result = asyncio.run(run_test())
|
||||
result = await post_session_processing(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=project_dir,
|
||||
subtask_id="subtask-1",
|
||||
session_num=1,
|
||||
commit_before=commit_before,
|
||||
commit_count_before=1,
|
||||
recovery_manager=recovery_manager,
|
||||
linear_enabled=False,
|
||||
)
|
||||
|
||||
assert result is False, "Pending subtask should return False"
|
||||
|
||||
+13
-351
@@ -42,12 +42,6 @@ if 'claude_code_sdk' not in sys.modules:
|
||||
sys.modules['claude_code_sdk'] = _create_sdk_mock()
|
||||
sys.modules['claude_code_sdk.types'] = MagicMock()
|
||||
|
||||
# Pre-mock dotenv to prevent sys.exit() in cli.utils.import_dotenv
|
||||
# This is needed for CLI tests since cli.utils calls import_dotenv at module level
|
||||
if 'dotenv' not in sys.modules:
|
||||
sys.modules['dotenv'] = MagicMock()
|
||||
sys.modules['dotenv'].load_dotenv = MagicMock()
|
||||
|
||||
# Add apps/backend directory to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
|
||||
@@ -63,7 +57,6 @@ _POTENTIALLY_MOCKED_MODULES = [
|
||||
'claude_code_sdk.types',
|
||||
'claude_agent_sdk',
|
||||
'claude_agent_sdk.types',
|
||||
'dotenv',
|
||||
'ui',
|
||||
'progress',
|
||||
'task_logger',
|
||||
@@ -73,6 +66,13 @@ _POTENTIALLY_MOCKED_MODULES = [
|
||||
'review',
|
||||
'validate_spec',
|
||||
'graphiti_providers',
|
||||
'agents.memory_manager',
|
||||
'agents.base',
|
||||
'core.error_utils',
|
||||
'security.tool_input_validator',
|
||||
'debug',
|
||||
'prompts_pkg',
|
||||
'prompts_pkg.project_context',
|
||||
]
|
||||
|
||||
# Store original module references at import time (before any mocking)
|
||||
@@ -120,6 +120,8 @@ def pytest_runtest_setup(item):
|
||||
'test_spec_pipeline': {'claude_code_sdk', 'claude_code_sdk.types', 'init', 'client', 'review', 'task_logger', 'ui', 'validate_spec'},
|
||||
'test_spec_complexity': {'claude_code_sdk', 'claude_code_sdk.types', 'claude_agent_sdk', 'claude_agent_sdk.types'},
|
||||
'test_spec_phases': {'claude_code_sdk', 'claude_code_sdk.types', 'claude_agent_sdk', 'graphiti_providers', 'validate_spec', 'client'},
|
||||
'test_qa_fixer': {'claude_agent_sdk', 'ui', 'progress', 'task_logger', 'linear_updater', 'client', 'agents.memory_manager', 'agents.base', 'core.error_utils', 'security.tool_input_validator', 'debug'},
|
||||
'test_qa_reviewer': {'claude_agent_sdk', 'ui', 'progress', 'task_logger', 'linear_updater', 'client', 'agents.memory_manager', 'agents.base', 'core.error_utils', 'security.tool_input_validator', 'debug', 'prompts_pkg', 'prompts_pkg.project_context'},
|
||||
}
|
||||
|
||||
# Get the mocks that the current test module needs to preserve
|
||||
@@ -164,8 +166,6 @@ def pytest_runtest_setup(item):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# DIRECTORY FIXTURES
|
||||
# =============================================================================
|
||||
@@ -257,132 +257,6 @@ def spec_dir(temp_dir: Path) -> Path:
|
||||
return spec_path
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# WORKSPACE COMMAND TEST FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
# Constants for workspace tests
|
||||
TEST_SPEC_NAME = "001-test-spec"
|
||||
TEST_SPEC_BRANCH = f"auto-claude/{TEST_SPEC_NAME}"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_project_dir(temp_git_repo: Path) -> Path:
|
||||
"""Create a mock project directory with git repo."""
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_worktree_path(temp_git_repo: Path) -> Path:
|
||||
"""Create a mock worktree path."""
|
||||
worktree_path = temp_git_repo / ".worktrees" / TEST_SPEC_NAME
|
||||
worktree_path.mkdir(parents=True, exist_ok=True)
|
||||
return worktree_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def workspace_spec_dir(temp_git_repo: Path) -> Path:
|
||||
"""Create a spec directory inside .auto-claude/specs/ for workspace tests."""
|
||||
spec_dir = temp_git_repo / ".auto-claude" / "specs" / TEST_SPEC_NAME
|
||||
spec_dir.mkdir(parents=True, exist_ok=True)
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def with_spec_branch(temp_git_repo: Path) -> Generator[Path, None, None]:
|
||||
"""Create a temp git repo with a spec branch.
|
||||
|
||||
Note: temp_git_repo already provides an initialized repo with initial commit,
|
||||
so we only need to create the spec branch and add changes.
|
||||
"""
|
||||
# Create spec branch
|
||||
subprocess.run(
|
||||
["git", "checkout", "-b", TEST_SPEC_BRANCH],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
# Add a change on spec branch
|
||||
(temp_git_repo / "test.txt").write_text("test content")
|
||||
subprocess.run(
|
||||
["git", "add", "test.txt"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "commit", "-m", "Test commit"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
# Go back to main
|
||||
subprocess.run(
|
||||
["git", "checkout", "main"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
yield temp_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def with_conflicting_branches(temp_git_repo: Path) -> Generator[Path, None, None]:
|
||||
"""Create temp git repo with conflicting branches for merge testing.
|
||||
|
||||
Note: temp_git_repo already provides an initialized repo with initial commit,
|
||||
so we only need to create branches with conflicting changes.
|
||||
"""
|
||||
# Create spec branch
|
||||
subprocess.run(
|
||||
["git", "checkout", "-b", TEST_SPEC_BRANCH],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
# Add a file on spec branch
|
||||
(temp_git_repo / "conflict.txt").write_text("spec branch content")
|
||||
subprocess.run(
|
||||
["git", "add", "conflict.txt"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "commit", "-m", "Spec change"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
# Go back to main and make conflicting change
|
||||
subprocess.run(
|
||||
["git", "checkout", "main"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
(temp_git_repo / "conflict.txt").write_text("main branch content")
|
||||
subprocess.run(
|
||||
["git", "add", "conflict.txt"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "commit", "-m", "Main change"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
yield temp_git_repo
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# REVIEW FIXTURES - Import from review_fixtures.py
|
||||
# =============================================================================
|
||||
@@ -765,15 +639,16 @@ def mock_run_agent_fn():
|
||||
phase_name: str = None,
|
||||
) -> tuple[bool, str]:
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if side_effect is not None:
|
||||
if call_count <= len(side_effect):
|
||||
result = side_effect[call_count - 1]
|
||||
if call_count < len(side_effect):
|
||||
result = side_effect[call_count]
|
||||
call_count += 1
|
||||
return result
|
||||
# Fallback to last result if more calls than expected
|
||||
return side_effect[-1]
|
||||
return (success, output)
|
||||
|
||||
_mock_agent.call_count = 0
|
||||
return _mock_agent
|
||||
|
||||
return _create_mock
|
||||
@@ -831,202 +706,6 @@ def mock_ui_module():
|
||||
return ui
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ui_icons():
|
||||
"""
|
||||
Mock UI Icons class for CLI input handler tests.
|
||||
|
||||
Provides the complete Icons class with Unicode and ASCII fallbacks.
|
||||
Mirrors the structure in apps/backend/ui/icons.py.
|
||||
|
||||
Usage:
|
||||
def test_something(mock_ui_icons):
|
||||
icons = mock_ui_icons()
|
||||
assert icons.SUCCESS == ("✓", "[OK]")
|
||||
"""
|
||||
class MockIcons:
|
||||
"""Mock Icons class - complete with all icons used by the codebase."""
|
||||
# Status icons
|
||||
SUCCESS = ("✓", "[OK]")
|
||||
ERROR = ("✗", "[X]")
|
||||
WARNING = ("⚠", "[!]")
|
||||
INFO = ("ℹ", "[i]")
|
||||
PENDING = ("○", "[ ]")
|
||||
IN_PROGRESS = ("◐", "[.]")
|
||||
COMPLETE = ("●", "[*]")
|
||||
BLOCKED = ("⊘", "[B]")
|
||||
|
||||
# Action icons
|
||||
PLAY = ("▶", ">")
|
||||
PAUSE = ("⏸", "||")
|
||||
STOP = ("⏹", "[]")
|
||||
SKIP = ("⏭", ">>")
|
||||
|
||||
# Navigation
|
||||
ARROW_RIGHT = ("→", "->")
|
||||
ARROW_DOWN = ("↓", "v")
|
||||
ARROW_UP = ("↑", "^")
|
||||
POINTER = ("❯", ">")
|
||||
BULLET = ("•", "*")
|
||||
|
||||
# Objects
|
||||
FOLDER = ("📁", "[D]")
|
||||
FILE = ("📄", "[F]")
|
||||
GEAR = ("⚙", "[*]")
|
||||
SEARCH = ("🔍", "[?]")
|
||||
BRANCH = ("🌿", "[BR]")
|
||||
COMMIT = ("◉", "(@)")
|
||||
LIGHTNING = ("⚡", "!")
|
||||
LINK = ("🔗", "[L]")
|
||||
|
||||
# Progress
|
||||
SUBTASK = ("▣", "#")
|
||||
PHASE = ("◆", "*")
|
||||
WORKER = ("⚡", "W")
|
||||
SESSION = ("▸", ">")
|
||||
|
||||
# Menu
|
||||
EDIT = ("✏️", "[E]")
|
||||
CLIPBOARD = ("📋", "[C]")
|
||||
DOCUMENT = ("📄", "[D]")
|
||||
DOOR = ("🚪", "[Q]")
|
||||
SHIELD = ("🛡️", "[S]")
|
||||
|
||||
# Box drawing
|
||||
BOX_TL = ("╔", "+")
|
||||
BOX_TR = ("╗", "+")
|
||||
BOX_BL = ("╚", "+")
|
||||
BOX_BR = ("╝", "+")
|
||||
BOX_H = ("═", "-")
|
||||
BOX_V = ("║", "|")
|
||||
BOX_ML = ("╠", "+")
|
||||
BOX_MR = ("╣", "+")
|
||||
BOX_TL_LIGHT = ("┌", "+")
|
||||
BOX_TR_LIGHT = ("┐", "+")
|
||||
BOX_BL_LIGHT = ("└", "+")
|
||||
BOX_BR_LIGHT = ("┘", "+")
|
||||
BOX_H_LIGHT = ("─", "-")
|
||||
BOX_V_LIGHT = ("│", "|")
|
||||
BOX_ML_LIGHT = ("├", "+")
|
||||
BOX_MR_LIGHT = ("┤", "+")
|
||||
|
||||
# Progress bar
|
||||
BAR_FULL = ("█", "=")
|
||||
BAR_EMPTY = ("░", "-")
|
||||
BAR_HALF = ("▌", "=")
|
||||
|
||||
return MockIcons
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ui_menu_option():
|
||||
"""
|
||||
Mock UI MenuOption class for CLI tests.
|
||||
|
||||
Provides a simple MenuOption class for menu testing.
|
||||
|
||||
Usage:
|
||||
def test_something(mock_ui_menu_option):
|
||||
option = mock_ui_menu_option()("key", "Label")
|
||||
assert option.key == "key"
|
||||
"""
|
||||
class MockMenuOption:
|
||||
"""Mock MenuOption class."""
|
||||
def __init__(self, key, label, icon=None, description=""):
|
||||
self.key = key
|
||||
self.label = label
|
||||
self.icon = icon or ("", "")
|
||||
self.description = description
|
||||
|
||||
return MockMenuOption
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ui_module_full(mock_ui_icons, mock_ui_menu_option):
|
||||
"""
|
||||
Comprehensive mock UI module with all functions and classes.
|
||||
|
||||
Provides a complete mock of the ui module for CLI tests.
|
||||
Includes Icons, MenuOption, and all UI functions.
|
||||
|
||||
Usage:
|
||||
def test_something(mock_ui_module_full):
|
||||
ui = mock_ui_module_full
|
||||
assert ui.Icons.SUCCESS == ("✓", "[OK]")
|
||||
assert ui.icon(ui.Icons.SUCCESS) == "✓"
|
||||
"""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
Icons = mock_ui_icons
|
||||
MenuOption = mock_ui_menu_option
|
||||
|
||||
def mock_icon(icon_tuple):
|
||||
"""Mock icon function."""
|
||||
return icon_tuple[0] if icon_tuple else ""
|
||||
|
||||
def mock_bold(text):
|
||||
"""Mock bold function."""
|
||||
return f"**{text}**"
|
||||
|
||||
def mock_muted(text):
|
||||
"""Mock muted function."""
|
||||
return f"[{text}]"
|
||||
|
||||
def mock_box(content, width=70, style="heavy"):
|
||||
"""Mock box function."""
|
||||
lines = ["┌" + "─" * (width - 2) + "┐"]
|
||||
for line in content:
|
||||
lines.append(f"│ {line} │")
|
||||
lines.append("└" + "─" * (width - 2) + "┘")
|
||||
return "\n".join(lines)
|
||||
|
||||
def mock_print_status(message, status="info"):
|
||||
"""Mock print_status function."""
|
||||
print(f"[{status.upper()}] {message}")
|
||||
|
||||
def mock_select_menu(title, options, subtitle="", allow_quit=True):
|
||||
"""Mock select_menu function."""
|
||||
return options[0].key if options else None
|
||||
|
||||
def mock_error(text):
|
||||
"""Mock error function."""
|
||||
return f"ERROR: {text}"
|
||||
|
||||
def mock_success(text):
|
||||
"""Mock success function."""
|
||||
return f"SUCCESS: {text}"
|
||||
|
||||
def mock_warning(text):
|
||||
"""Mock warning function."""
|
||||
return f"WARNING: {text}"
|
||||
|
||||
def mock_info(text):
|
||||
"""Mock info function."""
|
||||
return f"INFO: {text}"
|
||||
|
||||
def mock_highlight(text):
|
||||
"""Mock highlight function."""
|
||||
return text
|
||||
|
||||
# Create mock ui module
|
||||
mock_ui = MagicMock()
|
||||
mock_ui.Icons = Icons
|
||||
mock_ui.MenuOption = MenuOption
|
||||
mock_ui.icon = mock_icon
|
||||
mock_ui.bold = mock_bold
|
||||
mock_ui.muted = mock_muted
|
||||
mock_ui.box = mock_box
|
||||
mock_ui.print_status = mock_print_status
|
||||
mock_ui.select_menu = mock_select_menu
|
||||
mock_ui.error = mock_error
|
||||
mock_ui.success = mock_success
|
||||
mock_ui.warning = mock_warning
|
||||
mock_ui.info = mock_info
|
||||
mock_ui.highlight = mock_highlight
|
||||
|
||||
return mock_ui
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_spec_validator():
|
||||
"""
|
||||
@@ -1561,23 +1240,6 @@ def temp_project_dir(tmp_path):
|
||||
return project_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def successful_agent_fn():
|
||||
"""
|
||||
Reusable async agent function that returns success.
|
||||
|
||||
Replaces the duplicated async def agent_fn(*args, **kwargs): return (True, 'Success')
|
||||
pattern that was copy-pasted 28 times across test_cli_build_commands.py.
|
||||
|
||||
Usage:
|
||||
def test_something(mock_run_agent, successful_agent_fn):
|
||||
mock_run_agent.side_effect = successful_agent_fn
|
||||
"""
|
||||
async def _fn(*args, **kwargs):
|
||||
return (True, 'Success')
|
||||
return _fn
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def worktree_manager(temp_project_dir):
|
||||
"""Create a WorktreeManager instance."""
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Shared QA Test Helpers
|
||||
======================
|
||||
|
||||
Consolidates duplicated mock setup and utilities for test_qa_fixer.py and test_qa_reviewer.py.
|
||||
|
||||
This module provides:
|
||||
- AsyncIteratorMock: Async iterator mock for receive_response
|
||||
- ReceiveResponseMock: Smart wrapper supporting both .set_messages() and .return_value
|
||||
- setup_qa_mocks(): Module-level mock setup
|
||||
- cleanup_qa_mocks(): Module-level cleanup
|
||||
- reset_qa_mocks(): Reset shared mocks to default state
|
||||
- get_mock_*(): Accessor functions for mock objects
|
||||
- Mock response creation helpers
|
||||
- Shared pytest fixtures
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
# Add apps/backend to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# ASYNC ITERATOR MOCKS
|
||||
# =============================================================================
|
||||
|
||||
class AsyncIteratorMock:
|
||||
"""Async iterator mock that yields stored messages and acts as async context manager."""
|
||||
|
||||
def __init__(self):
|
||||
self._messages = []
|
||||
self._index = 0
|
||||
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
if self._index >= len(self._messages):
|
||||
raise StopAsyncIteration
|
||||
msg = self._messages[self._index]
|
||||
self._index += 1
|
||||
return msg
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
return False
|
||||
|
||||
def set_messages(self, messages):
|
||||
self._messages = messages
|
||||
self._index = 0
|
||||
|
||||
|
||||
class ReceiveResponseMock:
|
||||
"""Mock for receive_response that supports both .set_messages() and .return_value assignment."""
|
||||
|
||||
def __init__(self):
|
||||
self._iterator = AsyncIteratorMock()
|
||||
self.called = False # MagicMock compatibility
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
self.called = True
|
||||
return self._iterator
|
||||
|
||||
@property
|
||||
def return_value(self):
|
||||
return self._iterator
|
||||
|
||||
@return_value.setter
|
||||
def return_value(self, value):
|
||||
# When tests do mock_client.receive_response.return_value = list,
|
||||
# we set the messages on the iterator
|
||||
self._iterator.set_messages(value)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MODULE-LEVEL MOCKS
|
||||
# =============================================================================
|
||||
|
||||
# Store original modules for cleanup
|
||||
_original_modules = {}
|
||||
_mocked_module_names = [
|
||||
'claude_agent_sdk',
|
||||
'ui',
|
||||
'progress',
|
||||
'task_logger',
|
||||
'linear_updater',
|
||||
'client',
|
||||
'prompts_pkg',
|
||||
'prompts_pkg.project_context',
|
||||
'agents.memory_manager',
|
||||
'agents.base',
|
||||
'core.error_utils',
|
||||
'security.tool_input_validator',
|
||||
'debug',
|
||||
]
|
||||
|
||||
# Mock objects (initialized by setup_qa_mocks)
|
||||
_mock_state = {
|
||||
'sdk': None,
|
||||
'prompts_pkg': None,
|
||||
'project_context': None,
|
||||
'memory_manager': None,
|
||||
'agents_base': None,
|
||||
'error_utils': None,
|
||||
'validator': None,
|
||||
'debug': None,
|
||||
'ui': None,
|
||||
'progress': None,
|
||||
'task_logger': None,
|
||||
'linear': None,
|
||||
'client_module': None,
|
||||
'setup_done': False,
|
||||
'include_prompts_pkg': False, # Track what config was used
|
||||
}
|
||||
|
||||
|
||||
def get_mock_error_utils():
|
||||
"""Get the mock_error_utils object after setup."""
|
||||
return _mock_state['error_utils']
|
||||
|
||||
|
||||
def get_mock_memory_manager():
|
||||
"""Get the mock_memory_manager object after setup."""
|
||||
return _mock_state['memory_manager']
|
||||
|
||||
|
||||
def setup_qa_mocks(include_prompts_pkg: bool = False):
|
||||
"""Set up module-level mocks for QA tests.
|
||||
|
||||
Args:
|
||||
include_prompts_pkg: If True, mock prompts_pkg (needed for reviewer, not fixer)
|
||||
|
||||
Call this at module level before importing from qa modules.
|
||||
"""
|
||||
# Guard against redundant setup when called with same parameters
|
||||
# But allow prompts_pkg to be added if a later call needs it
|
||||
if _mock_state['setup_done']:
|
||||
# If prompts_pkg is already set up OR current call doesn't need it, skip
|
||||
if _mock_state['include_prompts_pkg'] or not include_prompts_pkg:
|
||||
return
|
||||
# Otherwise, we need to add prompts_pkg to existing setup
|
||||
# Fall through to only set up prompts_pkg below
|
||||
|
||||
# If setup is done but we need to add prompts_pkg, only do that part
|
||||
if _mock_state['setup_done'] and include_prompts_pkg and not _mock_state['include_prompts_pkg']:
|
||||
# Save originals before mocking
|
||||
for name in ['prompts_pkg', 'prompts_pkg.project_context']:
|
||||
if name in sys.modules and name not in _original_modules:
|
||||
_original_modules[name] = sys.modules[name]
|
||||
|
||||
# Only set up prompts_pkg
|
||||
mock_prompts_pkg = MagicMock()
|
||||
mock_prompts_pkg.get_qa_reviewer_prompt = MagicMock(return_value="Test QA prompt")
|
||||
sys.modules['prompts_pkg'] = mock_prompts_pkg
|
||||
_mock_state['prompts_pkg'] = mock_prompts_pkg
|
||||
mock_project_context = MagicMock()
|
||||
mock_prompts_pkg.project_context = mock_project_context
|
||||
sys.modules['prompts_pkg.project_context'] = mock_project_context
|
||||
_mock_state['project_context'] = mock_project_context
|
||||
_mock_state['include_prompts_pkg'] = True
|
||||
return
|
||||
|
||||
# Save originals for each module individually before mocking
|
||||
# This handles multiple setup calls with different parameters
|
||||
for name in _mocked_module_names:
|
||||
if name in sys.modules and name not in _original_modules:
|
||||
_original_modules[name] = sys.modules[name]
|
||||
|
||||
# Mock claude_agent_sdk FIRST
|
||||
mock_sdk = MagicMock()
|
||||
mock_sdk.ClaudeSDKClient = MagicMock()
|
||||
mock_sdk.ClaudeAgentOptions = MagicMock()
|
||||
mock_sdk.ClaudeCodeOptions = MagicMock()
|
||||
sys.modules['claude_agent_sdk'] = mock_sdk
|
||||
_mock_state['sdk'] = mock_sdk
|
||||
|
||||
# Mock prompts_pkg if needed
|
||||
if include_prompts_pkg:
|
||||
mock_prompts_pkg = MagicMock()
|
||||
mock_prompts_pkg.get_qa_reviewer_prompt = MagicMock(return_value="Test QA prompt")
|
||||
sys.modules['prompts_pkg'] = mock_prompts_pkg
|
||||
_mock_state['prompts_pkg'] = mock_prompts_pkg
|
||||
# Also mock prompts_pkg.project_context for imports in core/client.py
|
||||
mock_project_context = MagicMock()
|
||||
mock_prompts_pkg.project_context = mock_project_context
|
||||
sys.modules['prompts_pkg.project_context'] = mock_project_context
|
||||
_mock_state['project_context'] = mock_project_context
|
||||
|
||||
# Mock agents.memory_manager
|
||||
mock_memory_manager = MagicMock()
|
||||
mock_memory_manager.get_graphiti_context = AsyncMock(return_value=None)
|
||||
mock_memory_manager.save_session_memory = AsyncMock(return_value=None)
|
||||
sys.modules['agents.memory_manager'] = mock_memory_manager
|
||||
_mock_state['memory_manager'] = mock_memory_manager
|
||||
|
||||
# Mock agents.base
|
||||
mock_agents_base = MagicMock()
|
||||
mock_agents_base.sanitize_error_message = lambda x: x
|
||||
sys.modules['agents.base'] = mock_agents_base
|
||||
_mock_state['agents_base'] = mock_agents_base
|
||||
|
||||
# Mock core.error_utils
|
||||
mock_error_utils = MagicMock()
|
||||
mock_error_utils.is_rate_limit_error = MagicMock(return_value=False)
|
||||
mock_error_utils.is_tool_concurrency_error = MagicMock(return_value=False)
|
||||
sys.modules['core.error_utils'] = mock_error_utils
|
||||
_mock_state['error_utils'] = mock_error_utils
|
||||
|
||||
# Mock security.tool_input_validator
|
||||
mock_validator = MagicMock()
|
||||
mock_validator.get_safe_tool_input = lambda block: getattr(block, 'input', {})
|
||||
sys.modules['security.tool_input_validator'] = mock_validator
|
||||
_mock_state['validator'] = mock_validator
|
||||
|
||||
# Mock debug
|
||||
mock_debug = MagicMock()
|
||||
sys.modules['debug'] = mock_debug
|
||||
_mock_state['debug'] = mock_debug
|
||||
|
||||
# Mock UI module
|
||||
mock_ui = MagicMock()
|
||||
sys.modules['ui'] = mock_ui
|
||||
_mock_state['ui'] = mock_ui
|
||||
|
||||
# Mock progress module
|
||||
mock_progress = MagicMock()
|
||||
sys.modules['progress'] = mock_progress
|
||||
_mock_state['progress'] = mock_progress
|
||||
|
||||
# Mock task_logger
|
||||
mock_task_logger = MagicMock()
|
||||
mock_task_logger.LogPhase = MagicMock()
|
||||
mock_task_logger.LogEntryType = MagicMock()
|
||||
mock_task_logger.get_task_logger = MagicMock(return_value=None)
|
||||
sys.modules['task_logger'] = mock_task_logger
|
||||
_mock_state['task_logger'] = mock_task_logger
|
||||
|
||||
# Mock linear_updater
|
||||
mock_linear = MagicMock()
|
||||
sys.modules['linear_updater'] = mock_linear
|
||||
_mock_state['linear'] = mock_linear
|
||||
|
||||
# Mock client - create a factory that returns properly configured clients
|
||||
def _create_mock_client():
|
||||
"""Factory function that creates a properly configured mock client."""
|
||||
client = MagicMock()
|
||||
client.query = AsyncMock()
|
||||
client.receive_response = ReceiveResponseMock()
|
||||
return client
|
||||
|
||||
mock_client_module = MagicMock()
|
||||
mock_client_module.create_client = _create_mock_client
|
||||
sys.modules['client'] = mock_client_module
|
||||
_mock_state['client_module'] = mock_client_module
|
||||
_mock_state['setup_done'] = True
|
||||
_mock_state['include_prompts_pkg'] = include_prompts_pkg
|
||||
|
||||
|
||||
def cleanup_qa_mocks():
|
||||
"""Restore original modules after tests complete.
|
||||
|
||||
Call this in a module-scoped autouse fixture.
|
||||
"""
|
||||
for name in _mocked_module_names:
|
||||
if name in _original_modules:
|
||||
sys.modules[name] = _original_modules[name]
|
||||
elif name in sys.modules:
|
||||
del sys.modules[name]
|
||||
_mock_state['setup_done'] = False
|
||||
_mock_state['include_prompts_pkg'] = False
|
||||
# Note: We do NOT clear _original_modules here because:
|
||||
# 1. Multiple test modules may call cleanup, and clearing would break subsequent cleanups
|
||||
# 2. The 'if name not in _original_modules' guard in setup_qa_mocks prevents stale state
|
||||
# 3. Originals are saved per-module, so different setups can coexist
|
||||
|
||||
|
||||
def reset_qa_mocks():
|
||||
"""Reset shared mocks to default state.
|
||||
|
||||
Call this before and after each test to ensure isolation.
|
||||
"""
|
||||
mock_error_utils = _mock_state.get('error_utils')
|
||||
mock_memory_manager = _mock_state.get('memory_manager')
|
||||
|
||||
if mock_error_utils is not None:
|
||||
mock_error_utils.is_rate_limit_error.return_value = False
|
||||
mock_error_utils.is_tool_concurrency_error.return_value = False
|
||||
if mock_memory_manager is not None:
|
||||
mock_memory_manager.get_graphiti_context.reset_mock()
|
||||
mock_memory_manager.save_session_memory.reset_mock()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MOCK RESPONSE HELPERS
|
||||
# =============================================================================
|
||||
|
||||
def create_mock_response(text: str = "Session complete."):
|
||||
"""Create a standard mock assistant+user message pair.
|
||||
|
||||
Args:
|
||||
text: Text content for the AssistantMessage's TextBlock
|
||||
|
||||
Returns:
|
||||
List of mock messages [AssistantMessage, UserMessage]
|
||||
"""
|
||||
msg1 = MagicMock()
|
||||
msg1.__class__.__name__ = "AssistantMessage"
|
||||
text_block = MagicMock()
|
||||
text_block.__class__.__name__ = "TextBlock"
|
||||
text_block.text = text
|
||||
msg1.content = [text_block]
|
||||
|
||||
msg2 = MagicMock()
|
||||
msg2.__class__.__name__ = "UserMessage"
|
||||
msg2.content = []
|
||||
|
||||
return [msg1, msg2]
|
||||
|
||||
|
||||
def create_mock_fixed_response():
|
||||
"""Create mock response for fixed QA.
|
||||
|
||||
Returns:
|
||||
List of mock messages [AssistantMessage with 'Fixes applied successfully.', UserMessage]
|
||||
"""
|
||||
return create_mock_response("Fixes applied successfully.")
|
||||
|
||||
|
||||
def create_mock_tool_use_response(tool_name: str = "Bash", tool_input: dict = None):
|
||||
"""Create mock response with tool use.
|
||||
|
||||
Args:
|
||||
tool_name: Name of the tool being used
|
||||
tool_input: Input dict for the tool
|
||||
|
||||
Returns:
|
||||
List of mock messages [AssistantMessage with ToolUseBlock, UserMessage]
|
||||
"""
|
||||
if tool_input is None:
|
||||
tool_input = {"command": "echo test"}
|
||||
|
||||
msg1 = MagicMock()
|
||||
msg1.__class__.__name__ = "AssistantMessage"
|
||||
tool_block = MagicMock()
|
||||
tool_block.__class__.__name__ = "ToolUseBlock"
|
||||
tool_block.name = tool_name
|
||||
tool_block.input = tool_input
|
||||
msg1.content = [tool_block]
|
||||
|
||||
msg2 = MagicMock()
|
||||
msg2.__class__.__name__ = "UserMessage"
|
||||
msg2.content = []
|
||||
|
||||
return [msg1, msg2]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURE HELPERS
|
||||
# =============================================================================
|
||||
|
||||
def create_mock_client():
|
||||
"""Create a mock Claude SDK client for use in fixtures.
|
||||
|
||||
Returns:
|
||||
MagicMock configured as a Claude SDK client
|
||||
"""
|
||||
client = MagicMock()
|
||||
client.query = AsyncMock()
|
||||
client.receive_response = ReceiveResponseMock()
|
||||
return client
|
||||
@@ -13,6 +13,7 @@ Tests cover:
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -628,11 +629,12 @@ class TestEdgeCases:
|
||||
|
||||
def test_nonexistent_directory(self, discovery):
|
||||
"""Test handling of non-existent directory."""
|
||||
fake_dir = Path("/tmp/test-nonexistent-ci-discovery-123456")
|
||||
fake_dir = Path("/nonexistent/path")
|
||||
|
||||
# Should not raise
|
||||
result = discovery.discover(fake_dir)
|
||||
assert result is None
|
||||
# Should not raise - mock exists to avoid permission error
|
||||
with patch.object(Path, 'exists', return_value=False):
|
||||
result = discovery.discover(fake_dir)
|
||||
assert result is None
|
||||
|
||||
def test_ci_priority_github_first(self, discovery, temp_dir):
|
||||
"""Test that GitHub Actions takes priority."""
|
||||
|
||||
@@ -1,741 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Batch Commands
|
||||
=============================
|
||||
|
||||
Tests for batch_commands.py module functionality including:
|
||||
- handle_batch_create_command() - Create tasks from batch file
|
||||
- handle_batch_status_command() - Show status of all specs
|
||||
- handle_batch_cleanup_command() - Clean up completed specs
|
||||
"""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from cli.batch_commands import (
|
||||
handle_batch_cleanup_command,
|
||||
handle_batch_create_command,
|
||||
handle_batch_status_command,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
@pytest.fixture
|
||||
def sample_batch_file(temp_dir: Path) -> Path:
|
||||
"""Create a sample batch JSON file."""
|
||||
batch_data = {
|
||||
"tasks": [
|
||||
{
|
||||
"title": "Add user authentication",
|
||||
"description": "Implement OAuth2 login with Google provider",
|
||||
"workflow_type": "feature",
|
||||
"services": ["backend", "frontend"],
|
||||
"priority": 8,
|
||||
"complexity": "standard",
|
||||
"estimated_hours": 6.0,
|
||||
"estimated_days": 0.75,
|
||||
},
|
||||
{
|
||||
"title": "Add payment processing",
|
||||
"description": "Integrate Stripe for payments",
|
||||
"workflow_type": "feature",
|
||||
"services": ["backend", "worker"],
|
||||
"priority": 7,
|
||||
"complexity": "complex",
|
||||
"estimated_hours": 12.0,
|
||||
"estimated_days": 1.5,
|
||||
},
|
||||
{
|
||||
"title": "Fix navigation bug",
|
||||
"description": "Mobile menu not closing properly",
|
||||
"workflow_type": "bugfix",
|
||||
"services": ["frontend"],
|
||||
"priority": 9,
|
||||
"complexity": "simple",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
batch_file = temp_dir / "batch.json"
|
||||
batch_file.write_text(json.dumps(batch_data, indent=2))
|
||||
return batch_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def empty_batch_file(temp_dir: Path) -> Path:
|
||||
"""Create an empty batch JSON file."""
|
||||
batch_data = {"tasks": []}
|
||||
batch_file = temp_dir / "empty_batch.json"
|
||||
batch_file.write_text(json.dumps(batch_data))
|
||||
return batch_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def invalid_json_file(temp_dir: Path) -> Path:
|
||||
"""Create a file with invalid JSON."""
|
||||
batch_file = temp_dir / "invalid.json"
|
||||
batch_file.write_text("{ invalid json }")
|
||||
return batch_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_with_specs(temp_git_repo: Path) -> Path:
|
||||
"""Create a project with existing specs."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Spec 001 - with spec.md
|
||||
spec_001 = specs_dir / "001-existing-feature"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Existing Feature\n")
|
||||
(spec_001 / "requirements.json").write_text('{"task_description": "Existing"}')
|
||||
|
||||
# Spec 002 - with implementation plan
|
||||
spec_002 = specs_dir / "002-in-progress"
|
||||
spec_002.mkdir()
|
||||
(spec_002 / "spec.md").write_text("# In Progress\n")
|
||||
(spec_002 / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
# Spec 003 - complete with QA approval in implementation_plan.json
|
||||
spec_003 = specs_dir / "003-completed"
|
||||
spec_003.mkdir()
|
||||
(spec_003 / "spec.md").write_text("# Completed\n")
|
||||
(spec_003 / "implementation_plan.json").write_text(
|
||||
'{"phases": [], "qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
(spec_003 / "qa_report.md").write_text("# QA Approved\n")
|
||||
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_with_completed_specs_and_worktrees(temp_git_repo: Path) -> Path:
|
||||
"""Create a project with completed specs and worktrees."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
worktrees_dir = temp_git_repo / ".auto-claude" / "worktrees" / "tasks"
|
||||
worktrees_dir.mkdir(parents=True)
|
||||
|
||||
# Completed spec 001 with worktree (QA approved)
|
||||
spec_001 = specs_dir / "001-completed-with-wt"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_001 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
wt_001 = worktrees_dir / "001-completed-with-wt"
|
||||
wt_001.mkdir(parents=True)
|
||||
|
||||
# Completed spec 002 without worktree (QA approved)
|
||||
spec_002 = specs_dir / "002-completed-no-wt"
|
||||
spec_002.mkdir()
|
||||
(spec_002 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_002 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
# Incomplete spec 003
|
||||
spec_003 = specs_dir / "003-incomplete"
|
||||
spec_003.mkdir()
|
||||
(spec_003 / "spec.md").write_text("# In Progress\n")
|
||||
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_BATCH_CREATE_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleBatchCreateCommand:
|
||||
"""Tests for handle_batch_create_command() function."""
|
||||
|
||||
def test_creates_specs_from_batch_file(
|
||||
self, sample_batch_file: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Creates spec directories from batch file."""
|
||||
result = handle_batch_create_command(str(sample_batch_file), str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
assert specs_dir.exists()
|
||||
|
||||
# Should create 3 specs
|
||||
spec_dirs = sorted([d for d in specs_dir.iterdir() if d.is_dir()])
|
||||
assert len(spec_dirs) == 3
|
||||
|
||||
# Check spec numbering continues from 001
|
||||
assert spec_dirs[0].name == "001-add-user-authentication"
|
||||
assert spec_dirs[1].name == "002-add-payment-processing"
|
||||
assert spec_dirs[2].name == "003-fix-navigation-bug"
|
||||
|
||||
def test_creates_requirements_json(
|
||||
self, sample_batch_file: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Creates requirements.json with correct content."""
|
||||
handle_batch_create_command(str(sample_batch_file), str(temp_git_repo))
|
||||
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
spec_001 = specs_dir / "001-add-user-authentication"
|
||||
req_file = spec_001 / "requirements.json"
|
||||
|
||||
assert req_file.exists()
|
||||
|
||||
with open(req_file) as f:
|
||||
req = json.load(f)
|
||||
|
||||
assert req["task_description"] == "Implement OAuth2 login with Google provider"
|
||||
assert req["workflow_type"] == "feature"
|
||||
assert req["services_involved"] == ["backend", "frontend"]
|
||||
assert req["priority"] == 8
|
||||
assert req["complexity_inferred"] == "standard"
|
||||
assert req["estimate"]["estimated_hours"] == 6.0
|
||||
assert req["estimate"]["estimated_days"] == 0.75
|
||||
|
||||
def test_continues_numbering_from_existing_specs(
|
||||
self, project_with_specs: Path, sample_batch_file: Path
|
||||
) -> None:
|
||||
"""Continues spec numbering from existing specs."""
|
||||
handle_batch_create_command(str(sample_batch_file), str(project_with_specs))
|
||||
|
||||
specs_dir = project_with_specs / ".auto-claude" / "specs"
|
||||
spec_dirs = sorted([d for d in specs_dir.iterdir() if d.is_dir()])
|
||||
|
||||
# Should have existing 3 specs + 3 new ones
|
||||
assert len(spec_dirs) == 6
|
||||
|
||||
# New specs should start at 004
|
||||
assert spec_dirs[3].name == "004-add-user-authentication"
|
||||
assert spec_dirs[4].name == "005-add-payment-processing"
|
||||
assert spec_dirs[5].name == "006-fix-navigation-bug"
|
||||
|
||||
def test_returns_false_for_missing_file(self, temp_git_repo: Path) -> None:
|
||||
"""Returns False when batch file doesn't exist."""
|
||||
result = handle_batch_create_command(
|
||||
"nonexistent.json", str(temp_git_repo)
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
def test_returns_false_for_invalid_json(
|
||||
self, invalid_json_file: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns False for invalid JSON."""
|
||||
result = handle_batch_create_command(
|
||||
str(invalid_json_file), str(temp_git_repo)
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
def test_returns_false_for_empty_tasks(
|
||||
self, empty_batch_file: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns False when batch file has no tasks."""
|
||||
result = handle_batch_create_command(
|
||||
str(empty_batch_file), str(temp_git_repo)
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
def test_sanitizes_task_title_for_folder_name(
|
||||
self, temp_dir: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Sanitizes task title when creating folder name."""
|
||||
batch_data = {
|
||||
"tasks": [
|
||||
{
|
||||
"title": "Task With VERY Long Name That Should Be Truncated Because It Exceeds Fifty Characters",
|
||||
"description": "Test",
|
||||
}
|
||||
]
|
||||
}
|
||||
batch_file = temp_dir / "batch.json"
|
||||
batch_file.write_text(json.dumps(batch_data))
|
||||
|
||||
handle_batch_create_command(str(batch_file), str(temp_git_repo))
|
||||
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
spec_dirs = list(specs_dir.iterdir())
|
||||
|
||||
assert len(spec_dirs) == 1
|
||||
# Name should be truncated to 50 chars
|
||||
assert len(spec_dirs[0].name) <= 59 # "001-" + 50 chars
|
||||
assert spec_dirs[0].name.startswith("001-")
|
||||
|
||||
def test_uses_defaults_for_missing_fields(
|
||||
self, temp_dir: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Uses default values for missing optional fields."""
|
||||
batch_data = {
|
||||
"tasks": [
|
||||
{
|
||||
"title": "Minimal Task",
|
||||
}
|
||||
]
|
||||
}
|
||||
batch_file = temp_dir / "batch.json"
|
||||
batch_file.write_text(json.dumps(batch_data))
|
||||
|
||||
handle_batch_create_command(str(batch_file), str(temp_git_repo))
|
||||
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
req_file = specs_dir / "001-minimal-task" / "requirements.json"
|
||||
|
||||
with open(req_file) as f:
|
||||
req = json.load(f)
|
||||
|
||||
assert req["task_description"] == "Minimal Task"
|
||||
assert req["workflow_type"] == "feature"
|
||||
assert req["services_involved"] == ["frontend"]
|
||||
assert req["priority"] == 5
|
||||
assert req["complexity_inferred"] == "standard"
|
||||
assert req["estimate"]["estimated_hours"] == 4.0
|
||||
assert req["estimate"]["estimated_days"] == 0.5
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_BATCH_STATUS_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleBatchStatusCommand:
|
||||
"""Tests for handle_batch_status_command() function."""
|
||||
|
||||
def test_shows_status_for_all_specs(
|
||||
self, capsys, project_with_specs: Path
|
||||
) -> None:
|
||||
"""Shows status for all specs in project."""
|
||||
result = handle_batch_status_command(str(project_with_specs))
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "3 spec" in captured.out
|
||||
assert "001-existing-feature" in captured.out
|
||||
assert "002-in-progress" in captured.out
|
||||
assert "003-completed" in captured.out
|
||||
|
||||
def test_shows_correct_status_icons(
|
||||
self, capsys, project_with_specs: Path
|
||||
) -> None:
|
||||
"""Shows appropriate status icons for each spec."""
|
||||
handle_batch_status_command(str(project_with_specs))
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Status icons for different states:
|
||||
# 001: spec.md only → spec_created (📋)
|
||||
# 002: spec.md + implementation_plan.json → building (⚙️)
|
||||
# 003: qa_report.md → qa_approved (✅)
|
||||
assert "📋" in captured.out
|
||||
assert "⚙️" in captured.out
|
||||
assert "✅" in captured.out
|
||||
|
||||
def test_returns_true_for_no_specs_directory(
|
||||
self, capsys, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns True when no specs directory exists."""
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No specs found" in captured.out
|
||||
|
||||
def test_returns_true_for_empty_specs_directory(
|
||||
self, capsys, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns True when specs directory is empty."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No specs found" in captured.out
|
||||
|
||||
def test_shows_task_description(
|
||||
self, capsys, project_with_specs: Path
|
||||
) -> None:
|
||||
"""Shows task description from requirements.json."""
|
||||
handle_batch_status_command(str(project_with_specs))
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Existing" in captured.out
|
||||
|
||||
def test_detects_spec_created_status(
|
||||
self, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Correctly detects specs with spec.md as 'spec_created'."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Test\n")
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_detects_building_status(
|
||||
self, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Correctly detects specs with implementation_plan.json as 'building'."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_detects_qa_approved_status(
|
||||
self, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Correctly detects specs with qa_signoff as 'qa_approved'."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_001 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_detects_pending_spec_status(
|
||||
self, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Correctly detects specs with only requirements.json as 'pending_spec'."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "requirements.json").write_text('{"task": "test"}')
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_handles_corrupted_requirements_json(
|
||||
self, capsys, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Handles corrupted requirements.json gracefully."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "requirements.json").write_text("{ invalid json")
|
||||
|
||||
result = handle_batch_status_command(str(temp_git_repo))
|
||||
|
||||
assert result is True
|
||||
captured = capsys.readouterr()
|
||||
assert "001-test" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_BATCH_CLEANUP_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleBatchCleanupCommand:
|
||||
"""Tests for handle_batch_cleanup_command() function."""
|
||||
|
||||
def test_dry_run_shows_what_would_be_deleted(
|
||||
self, capsys, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Dry run shows what would be deleted without actually deleting."""
|
||||
result = handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=True
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "2 completed spec" in captured.out
|
||||
assert "001-completed-with-wt" in captured.out
|
||||
assert "002-completed-no-wt" in captured.out
|
||||
assert "Would remove:" in captured.out
|
||||
assert "Run with --no-dry-run" in captured.out
|
||||
|
||||
def test_dry_run_does_not_delete(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Dry run does not actually delete anything."""
|
||||
specs_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "specs"
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=True
|
||||
)
|
||||
|
||||
# Specs should still exist
|
||||
assert (specs_dir / "001-completed-with-wt").exists()
|
||||
assert (specs_dir / "002-completed-no-wt").exists()
|
||||
|
||||
def test_cleanup_deletes_specs_and_worktrees(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Actually deletes completed specs and worktrees when dry_run=False."""
|
||||
specs_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "specs"
|
||||
worktrees_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "worktrees" / "tasks"
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Completed specs should be deleted
|
||||
assert not (specs_dir / "001-completed-with-wt").exists()
|
||||
assert not (specs_dir / "002-completed-no-wt").exists()
|
||||
|
||||
# Worktree should be deleted
|
||||
assert not (worktrees_dir / "001-completed-with-wt").exists()
|
||||
|
||||
def test_cleanup_preserves_incomplete_specs(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Does not delete specs without qa_report.md."""
|
||||
specs_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "specs"
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Incomplete spec should still exist
|
||||
assert (specs_dir / "003-incomplete").exists()
|
||||
|
||||
def test_returns_true_for_no_specs_directory(
|
||||
self, capsys, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns True when no specs directory exists."""
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=True)
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No specs directory found" in captured.out
|
||||
|
||||
def test_returns_true_for_no_completed_specs(
|
||||
self, capsys, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Returns True when no completed specs exist."""
|
||||
# Create specs without qa_report.md
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-incomplete"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# In Progress\n")
|
||||
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=True)
|
||||
|
||||
assert result is True
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No completed specs to clean up" in captured.out
|
||||
|
||||
def test_cleanup_with_git_worktree_remove(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Uses git worktree remove when available."""
|
||||
with patch('subprocess.run') as mock_run:
|
||||
# Mock git worktree remove to succeed
|
||||
mock_run.return_value = MagicMock(returncode=0)
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Should have called git worktree remove
|
||||
# Check that the first argument of any call contains "git", "worktree", "remove"
|
||||
assert any(
|
||||
"git" in str(call.args) and
|
||||
"worktree" in str(call.args) and
|
||||
"remove" in str(call.args)
|
||||
for call in mock_run.call_args_list
|
||||
)
|
||||
|
||||
def test_cleanup_fallback_to_manual_removal(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Falls back to manual removal when git worktree remove fails."""
|
||||
specs_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "specs"
|
||||
|
||||
with patch('subprocess.run') as mock_run:
|
||||
# Mock git worktree remove to fail
|
||||
mock_run.return_value = MagicMock(returncode=1)
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Should still delete the spec
|
||||
assert not (specs_dir / "001-completed-with-wt").exists()
|
||||
|
||||
def test_cleanup_handles_timeout_gracefully(
|
||||
self, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Handles git command timeout gracefully."""
|
||||
specs_dir = project_with_completed_specs_and_worktrees / ".auto-claude" / "specs"
|
||||
|
||||
with patch('subprocess.run') as mock_run:
|
||||
# Mock timeout
|
||||
from subprocess import TimeoutExpired
|
||||
mock_run.side_effect = TimeoutExpired("git", 30)
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Should still delete the spec (fallback)
|
||||
assert not (specs_dir / "001-completed-with-wt").exists()
|
||||
|
||||
def test_cleanup_handles_exceptions(
|
||||
self, capsys, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Handles exceptions during cleanup gracefully."""
|
||||
with patch('subprocess.run') as mock_run:
|
||||
# Mock exception
|
||||
mock_run.side_effect = Exception("Test error")
|
||||
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=False
|
||||
)
|
||||
|
||||
# Should continue and delete specs
|
||||
captured = capsys.readouterr()
|
||||
assert "Cleaned up" in captured.out
|
||||
|
||||
def test_cleanup_shows_worktree_path_in_dry_run(
|
||||
self, capsys, project_with_completed_specs_and_worktrees: Path
|
||||
) -> None:
|
||||
"""Shows worktree path in dry run output."""
|
||||
handle_batch_cleanup_command(
|
||||
str(project_with_completed_specs_and_worktrees), dry_run=True
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert ".auto-claude/worktrees/tasks/001-completed-with-wt" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# INTEGRATION TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestBatchCommandsIntegration:
|
||||
"""Integration tests for batch commands."""
|
||||
|
||||
def test_create_then_status_workflow(
|
||||
self, sample_batch_file: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Test creating specs then checking status."""
|
||||
# Create specs
|
||||
create_result = handle_batch_create_command(
|
||||
str(sample_batch_file), str(temp_git_repo)
|
||||
)
|
||||
assert create_result is True
|
||||
|
||||
# Check status
|
||||
status_result = handle_batch_status_command(str(temp_git_repo))
|
||||
assert status_result is True
|
||||
|
||||
def test_create_then_cleanup_workflow(
|
||||
self, temp_dir: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Test creating specs, marking complete, then cleanup."""
|
||||
# Create a spec
|
||||
batch_data = {"tasks": [{"title": "Test Task"}]}
|
||||
batch_file = temp_dir / "batch.json"
|
||||
batch_file.write_text(json.dumps(batch_data))
|
||||
|
||||
handle_batch_create_command(str(batch_file), str(temp_git_repo))
|
||||
|
||||
# Mark as complete with proper QA approval
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
spec_001 = specs_dir / "001-test-task"
|
||||
(spec_001 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_001 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
# Dry run cleanup
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=True)
|
||||
assert result is True
|
||||
|
||||
# Actual cleanup
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=False)
|
||||
assert result is True
|
||||
|
||||
# Spec should be deleted
|
||||
assert not spec_001.exists()
|
||||
|
||||
|
||||
class TestBatchCommandsExceptionCoverage:
|
||||
"""Tests for exception handling paths to increase coverage."""
|
||||
|
||||
def test_cleanup_with_permission_error(
|
||||
self, temp_dir: Path, temp_git_repo: Path, monkeypatch
|
||||
) -> None:
|
||||
"""Test cleanup handles permission errors gracefully."""
|
||||
|
||||
# Create a completed spec with proper QA approval
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
spec_001 = specs_dir / "001-test-task"
|
||||
spec_001.mkdir(parents=True)
|
||||
(spec_001 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_001 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
# Mock shutil.rmtree to raise permission error
|
||||
def mock_rmtree_raises(path, *args, **kwargs):
|
||||
if "001-test-task" in str(path):
|
||||
raise PermissionError(f"Permission denied: {path}")
|
||||
|
||||
monkeypatch.setattr("cli.batch_commands.shutil.rmtree", mock_rmtree_raises)
|
||||
|
||||
# Should handle the error gracefully and not crash
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=False)
|
||||
assert result is True
|
||||
|
||||
def test_cleanup_with_generic_exception(
|
||||
self, temp_dir: Path, temp_git_repo: Path, monkeypatch
|
||||
) -> None:
|
||||
"""Test cleanup handles generic exceptions gracefully."""
|
||||
|
||||
# Create a completed spec with proper QA approval
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
spec_001 = specs_dir / "001-test-task"
|
||||
spec_001.mkdir(parents=True)
|
||||
(spec_001 / "qa_report.md").write_text("# QA Approved\n")
|
||||
(spec_001 / "implementation_plan.json").write_text(
|
||||
'{"qa_signoff": {"status": "approved"}}'
|
||||
)
|
||||
|
||||
# Mock shutil.rmtree to raise generic exception
|
||||
def mock_rmtree_raises(path, *args, **kwargs):
|
||||
if "001-test-task" in str(path):
|
||||
raise RuntimeError(f"Cannot delete: {path}")
|
||||
|
||||
monkeypatch.setattr("cli.batch_commands.shutil.rmtree", mock_rmtree_raises)
|
||||
|
||||
# Should handle the error gracefully and not crash
|
||||
result = handle_batch_cleanup_command(str(temp_git_repo), dry_run=False)
|
||||
assert result is True
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,970 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Followup Commands (cli/followup_commands.py)
|
||||
===========================================================
|
||||
|
||||
Tests for follow-up task commands:
|
||||
- collect_followup_task()
|
||||
- handle_followup_command()
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Add apps/backend to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
# Add tests directory to path for test_utils import
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Mock external dependencies before importing cli.followup_commands
|
||||
# =============================================================================
|
||||
|
||||
# Import shared helper for creating mock modules
|
||||
from test_utils import _create_mock_module
|
||||
|
||||
# Mock modules
|
||||
if 'progress' not in sys.modules:
|
||||
sys.modules['progress'] = _create_mock_module()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Auto-use fixture to set up mock UI module before importing cli.followup_commands
|
||||
# =============================================================================
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_mock_ui_for_followup(mock_ui_module_full):
|
||||
"""Auto-use fixture that replaces sys.modules['ui'] with mock for each test."""
|
||||
sys.modules['ui'] = mock_ui_module_full
|
||||
yield
|
||||
|
||||
# =============================================================================
|
||||
# Import cli.followup_commands after mocking dependencies
|
||||
# =============================================================================
|
||||
|
||||
from cli.followup_commands import (
|
||||
collect_followup_task,
|
||||
handle_followup_command,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for collect_followup_task()
|
||||
# =============================================================================
|
||||
|
||||
class TestCollectFollowupTask:
|
||||
"""Tests for collect_followup_task() function."""
|
||||
|
||||
def test_returns_task_description_on_type(self, temp_dir, capsys):
|
||||
"""Returns task description when user chooses to type."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=['First line', 'Second line', '']):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is not None
|
||||
assert "First line" in result
|
||||
assert "Second line" in result
|
||||
|
||||
# Check that FOLLOWUP_REQUEST.md was created
|
||||
followup_file = spec_dir / "FOLLOWUP_REQUEST.md"
|
||||
assert followup_file.exists()
|
||||
assert followup_file.read_text() == result
|
||||
|
||||
def test_reads_from_file_when_selected(self, temp_dir, capsys):
|
||||
"""Reads task description from file when file option selected."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a temp file with task description
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Task from file\nMultiple lines")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='file'):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is not None
|
||||
assert "Task from file" in result
|
||||
assert "Multiple lines" in result
|
||||
|
||||
def test_handles_nonexistent_file(self, temp_dir, capsys):
|
||||
"""Handles case when specified file doesn't exist."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='file'):
|
||||
with patch('builtins.input', return_value='/nonexistent/file.txt'):
|
||||
with patch('cli.followup_commands.select_menu', return_value='quit'):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_handles_empty_file(self, temp_dir, capsys):
|
||||
"""Handles case when file is empty."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create empty file
|
||||
task_file = temp_dir / "empty.txt"
|
||||
task_file.write_text("")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', side_effect=[str(task_file)]):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_handles_permission_error(self, temp_dir, capsys):
|
||||
"""Handles permission denied error when reading file."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
task_file = temp_dir / "restricted.txt"
|
||||
task_file.write_text("Content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
# Mock Path.read_text to raise PermissionError
|
||||
with patch('pathlib.Path.read_text', side_effect=PermissionError("Denied")):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_returns_none_on_quit(self, temp_dir):
|
||||
"""Returns None when user selects quit."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='quit'):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_retries_on_empty_input(self, temp_dir, capsys):
|
||||
"""Retries when user provides empty input."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# First attempt: type with empty input
|
||||
# Second attempt: type with actual content
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['type', 'type']):
|
||||
with patch('builtins.input', side_effect=[
|
||||
'', # First attempt - empty
|
||||
'Actual task content', # Second attempt - content
|
||||
''
|
||||
]):
|
||||
result = collect_followup_task(spec_dir, max_retries=3)
|
||||
|
||||
assert result is not None
|
||||
assert "Actual task content" in result
|
||||
|
||||
def test_respects_max_retries(self, temp_dir, capsys):
|
||||
"""Stops retrying after max attempts reached."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Always return empty input
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=['', '', '', '']):
|
||||
result = collect_followup_task(spec_dir, max_retries=2)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Maximum retry" in captured.out or "cancelled" in captured.out.lower()
|
||||
|
||||
def test_handles_keyboard_interrupt(self, temp_dir, capsys):
|
||||
"""Handles KeyboardInterrupt during input collection."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_handles_eof_error(self, temp_dir, capsys):
|
||||
"""Handles EOFError during input collection."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=EOFError):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
# EOFError should break the input loop, returning None if empty
|
||||
# The actual content would be empty, so it should retry or return None
|
||||
assert result is None
|
||||
|
||||
def test_saves_to_followup_request_file(self, temp_dir):
|
||||
"""Saves the collected task to FOLLOWUP_REQUEST.md."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
task_description = "This is a test follow-up task"
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=[task_description, '']):
|
||||
collect_followup_task(spec_dir)
|
||||
|
||||
followup_file = spec_dir / "FOLLOWUP_REQUEST.md"
|
||||
assert followup_file.exists()
|
||||
assert followup_file.read_text() == task_description
|
||||
|
||||
def test_handles_empty_file_path(self, temp_dir, capsys):
|
||||
"""Handles case when no file path is provided."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=''):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "No file path" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_expands_tilde_in_path(self, temp_dir):
|
||||
"""Expands ~ in file path to home directory."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a file in temp_dir to simulate home
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Task content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='file'):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
with patch('pathlib.Path.expanduser', return_value=task_file):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is not None
|
||||
assert "Task content" in result
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for handle_followup_command()
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleFollowupCommand:
|
||||
"""Tests for handle_followup_command() function."""
|
||||
|
||||
@patch('cli.utils.validate_environment')
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('progress.is_build_complete')
|
||||
@patch('progress.count_subtasks')
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_exits_when_no_implementation_plan(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_count,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Exits with error when implementation plan doesn't exist."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# sys.exit is called directly in the function, so we need to catch SystemExit
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No implementation plan found" in captured.out or "not been built" in captured.out
|
||||
|
||||
@patch('cli.utils.validate_environment')
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete')
|
||||
@patch('cli.followup_commands.count_subtasks')
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_exits_when_build_not_complete(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_count,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Exits with error when build is not complete."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{}')
|
||||
|
||||
mock_is_complete.return_value = False
|
||||
mock_count.return_value = (2, 5) # 2 completed, 5 total
|
||||
|
||||
# sys.exit is called directly in the function
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "not complete" in captured.out or "pending" in captured.out
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_runs_planner_after_collecting_task(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Runs follow-up planner after successfully collecting task."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add new feature"
|
||||
mock_run_planner.return_value = True
|
||||
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
assert mock_run_planner.called
|
||||
call_kwargs = mock_run_planner.call_args[1]
|
||||
assert call_kwargs['project_dir'] == temp_dir
|
||||
assert call_kwargs['spec_dir'] == spec_dir
|
||||
assert call_kwargs['model'] == "sonnet"
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_returns_when_user_cancels(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Returns early when user cancels task collection."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = None
|
||||
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
assert not mock_run_planner.called
|
||||
captured = capsys.readouterr()
|
||||
assert "cancel" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=False)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_exits_when_environment_invalid(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir
|
||||
):
|
||||
"""Exits when environment validation fails."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Task description"
|
||||
|
||||
# sys.exit is called directly in the function
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
assert not mock_run_planner.called
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_successful_planning(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Shows success message when planning completes successfully."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add feature"
|
||||
mock_run_planner.return_value = True
|
||||
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "COMPLETE" in captured.out or "success" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_planning_failure(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Shows warning when planning doesn't fully succeed."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add feature"
|
||||
mock_run_planner.return_value = False
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "INCOMPLETE" in captured.out or "warning" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_keyboard_interrupt(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles KeyboardInterrupt during planning."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add feature"
|
||||
mock_run_planner.side_effect = KeyboardInterrupt()
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "paused" in captured.out.lower() or "retry" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_planning_exception(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles exception during planning."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add feature"
|
||||
mock_run_planner.side_effect = Exception("Planning failed")
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet", verbose=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_shows_traceback_in_verbose_mode(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Shows traceback in verbose mode."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = "Add feature"
|
||||
test_error = Exception("Test error")
|
||||
mock_run_planner.side_effect = test_error
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet", verbose=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# In verbose mode, traceback should be printed
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
def test_counts_prior_followups(self, temp_dir, capsys):
|
||||
"""Counts and displays prior follow-up phases."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Create implementation plan with follow-up phases
|
||||
plan = {
|
||||
"phases": [
|
||||
{"name": "Initial Phase"},
|
||||
{"name": "Follow-Up: Bug Fixes"},
|
||||
{"name": "Followup: Enhancement"},
|
||||
]
|
||||
}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
with patch('cli.followup_commands.is_build_complete', return_value=True):
|
||||
with patch('cli.followup_commands.collect_followup_task', return_value=None):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should indicate prior follow-ups were detected
|
||||
# The exact output depends on the implementation
|
||||
assert "complete" in captured.out.lower()
|
||||
|
||||
def test_shows_ready_message_for_first_followup(self, temp_dir, capsys):
|
||||
"""Shows appropriate message for first follow-up."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Create plan without follow-up phases
|
||||
plan = {"phases": [{"name": "Initial Phase"}]}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
with patch('cli.followup_commands.is_build_complete', return_value=True):
|
||||
with patch('cli.followup_commands.collect_followup_task', return_value=None):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "complete" in captured.out.lower() or "ready" in captured.out.lower()
|
||||
|
||||
def test_passes_verbose_flag_to_planner(self, temp_dir):
|
||||
"""Passes verbose flag to follow-up planner."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
with patch('cli.utils.validate_environment', return_value=True):
|
||||
with patch('agent.run_followup_planner', new_callable=AsyncMock, return_value=True) as mock_planner:
|
||||
with patch('cli.followup_commands.is_build_complete', return_value=True):
|
||||
with patch('cli.followup_commands.collect_followup_task', return_value="Task"):
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet", verbose=True)
|
||||
|
||||
call_kwargs = mock_planner.call_args[1]
|
||||
assert call_kwargs['verbose'] is True
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Additional tests for improved coverage (lines 108-111, 139-144, 150-153, 296-297)
|
||||
# =============================================================================
|
||||
|
||||
def test_handles_keyboard_interrupt_on_file_path_input(self, temp_dir, capsys):
|
||||
"""Handles KeyboardInterrupt when entering file path (lines 108-111)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='file'):
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_handles_eof_error_on_file_path_input(self, temp_dir, capsys):
|
||||
"""Handles EOFError when entering file path (lines 108-111)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='file'):
|
||||
with patch('builtins.input', side_effect=EOFError):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_handles_file_not_found_error(self, temp_dir, capsys):
|
||||
"""Handles FileNotFoundError when file doesn't exist (lines 139-144)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a path that doesn't exist
|
||||
nonexistent_file = temp_dir / "does_not_exist.txt"
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(nonexistent_file)):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
# Should show file not found error
|
||||
assert "not found" in captured.out.lower() or "check that the path" in captured.out.lower()
|
||||
|
||||
def test_handles_generic_exception_on_file_read(self, temp_dir, capsys):
|
||||
"""Handles generic exception when reading file (lines 150-153)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a file that exists
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
# Mock read_text to raise a generic exception
|
||||
with patch('pathlib.Path.read_text', side_effect=OSError("Read error")):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
def test_handles_unicode_decode_error_on_file_read(self, temp_dir, capsys):
|
||||
"""Handles UnicodeDecodeError when reading file (lines 150-153)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a file that exists
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
# Mock read_text to raise UnicodeDecodeError
|
||||
with patch('pathlib.Path.read_text', side_effect=UnicodeDecodeError('utf-8', b'', 0, 1, 'invalid')):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
def test_handles_runtime_error_on_file_read(self, temp_dir, capsys):
|
||||
"""Handles RuntimeError when reading file (lines 150-153)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create a file that exists
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
# Mock read_text to raise RuntimeError
|
||||
with patch('pathlib.Path.read_text', side_effect=RuntimeError("Unexpected error")):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
|
||||
class TestHandleFollowupCommandEdgeCases:
|
||||
"""Additional tests for handle_followup_command() edge cases (lines 296-297)."""
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_json_decode_error_in_plan_file(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles JSONDecodeError when implementation_plan.json is malformed (lines 296-297)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Write invalid JSON to implementation_plan.json
|
||||
(spec_dir / "implementation_plan.json").write_text('{ invalid json }')
|
||||
|
||||
mock_collect.return_value = None
|
||||
|
||||
# Should handle the JSONDecodeError gracefully and continue
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should complete without error (prior_followup_count just stays 0)
|
||||
assert "complete" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_keyerror_in_plan_file(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles KeyError when implementation_plan.json is missing expected keys (lines 296-297)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Write JSON without 'phases' key
|
||||
(spec_dir / "implementation_plan.json").write_text('{"other_key": "value"}')
|
||||
|
||||
mock_collect.return_value = None
|
||||
|
||||
# Should handle the missing key gracefully
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "complete" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_phase_with_missing_name_key(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles phase dict without 'name' key (lines 296-297)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Write JSON with phase missing 'name' key
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": [{"other_key": "value"}, {"name": "Valid Phase"}]}')
|
||||
|
||||
mock_collect.return_value = None
|
||||
|
||||
# Should handle missing name gracefully (uses .get() with default)
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "complete" in captured.out.lower()
|
||||
|
||||
@patch('cli.utils.validate_environment', return_value=True)
|
||||
@patch('agent.run_followup_planner', new_callable=AsyncMock)
|
||||
@patch('cli.followup_commands.is_build_complete', return_value=True)
|
||||
@patch('cli.followup_commands.collect_followup_task')
|
||||
def test_handles_empty_phases_in_plan(
|
||||
self,
|
||||
mock_collect,
|
||||
mock_is_complete,
|
||||
mock_run_planner,
|
||||
mock_validate,
|
||||
temp_dir,
|
||||
capsys
|
||||
):
|
||||
"""Handles empty phases array in implementation plan (lines 296-297)."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "spec.md").write_text("# Test")
|
||||
|
||||
# Write JSON with empty phases array
|
||||
(spec_dir / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
mock_collect.return_value = None
|
||||
|
||||
handle_followup_command(temp_dir, spec_dir, "sonnet")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "complete" in captured.out.lower()
|
||||
|
||||
|
||||
|
||||
class TestCollectFollowupTaskEdgeCases:
|
||||
"""Additional edge case tests for collect_followup_task()."""
|
||||
|
||||
def test_handles_file_with_only_whitespace(self, temp_dir, capsys):
|
||||
"""Handles file that contains only whitespace characters."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create file with only whitespace
|
||||
task_file = temp_dir / "whitespace.txt"
|
||||
task_file.write_text(" \n\n\t\n ")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
# .strip() would make the content empty, triggering the empty file message
|
||||
assert "empty" in captured.out.lower() or "cancel" in captured.out.lower()
|
||||
|
||||
def test_handles_file_with_newline_only_content(self, temp_dir, capsys):
|
||||
"""Handles file that contains only newlines."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Create file with only newlines
|
||||
task_file = temp_dir / "newlines.txt"
|
||||
task_file.write_text("\n\n\n")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_handles_file_read_with_os_error(self, temp_dir, capsys):
|
||||
"""Handles OSError when reading file."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
task_file = temp_dir / "task.txt"
|
||||
task_file.write_text("Content")
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value=str(task_file)):
|
||||
with patch('pathlib.Path.read_text', side_effect=OSError("OS error reading file")):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "error" in captured.out.lower()
|
||||
|
||||
def test_handles_value_error_on_file_path(self, temp_dir, capsys):
|
||||
"""Handles ValueError during file path resolution."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
with patch('cli.followup_commands.select_menu', side_effect=['file', 'quit']):
|
||||
with patch('builtins.input', return_value='/valid/path'):
|
||||
# Mock resolve to raise ValueError
|
||||
with patch('pathlib.Path.resolve', side_effect=ValueError("Invalid path")):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
# Should handle gracefully and return None or retry
|
||||
assert result is None
|
||||
|
||||
def test_handles_type_input_with_trailing_whitespace(self, temp_dir):
|
||||
"""Properly strips trailing whitespace from typed input."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
task_description = "Task content with trailing spaces "
|
||||
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=[task_description, '']):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is not None
|
||||
# Should be stripped
|
||||
assert result == "Task content with trailing spaces"
|
||||
|
||||
def test_handles_type_input_with_internal_whitespace(self, temp_dir):
|
||||
"""Preserves internal whitespace in typed input."""
|
||||
spec_dir = temp_dir / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
# Note: empty line terminates input, so we need non-empty lines only
|
||||
# Then a final empty line to signal completion
|
||||
with patch('cli.followup_commands.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=["Line 1", "Line 2", " Line 3", '']):
|
||||
result = collect_followup_task(spec_dir)
|
||||
|
||||
assert result is not None
|
||||
assert "Line 1" in result
|
||||
assert "Line 2" in result
|
||||
assert "Line 3" in result
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS: Module-level path insertion (line 16)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestFollowupCommandsModuleImport:
|
||||
"""Tests for covering module-level path insertion (line 16)."""
|
||||
|
||||
def test_module_import_executes_path_insertion(self):
|
||||
"""Module import executes sys.path.insert (line 16)."""
|
||||
# Get the module path and parent directory
|
||||
import cli.followup_commands as followup_module
|
||||
module_path = followup_module.__file__
|
||||
parent_dir = str(Path(module_path).parent.parent)
|
||||
|
||||
# Save original sys.path
|
||||
original_path = sys.path.copy()
|
||||
|
||||
# Remove the parent directory from sys.path to make the condition True
|
||||
while parent_dir in sys.path:
|
||||
sys.path.remove(parent_dir)
|
||||
|
||||
# Remove module and its submodules from sys.modules to force re-import
|
||||
modules_to_remove = [k for k in sys.modules.keys() if k.startswith('cli.followup_commands')]
|
||||
for mod_name in modules_to_remove:
|
||||
del sys.modules[mod_name]
|
||||
|
||||
# Now import it fresh - this should execute line 16 under coverage
|
||||
import importlib.util
|
||||
spec = importlib.util.spec_from_file_location("cli.followup_commands", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules['cli.followup_commands'] = module
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
# Verify the module loaded correctly
|
||||
assert hasattr(module, 'handle_followup_command')
|
||||
|
||||
# Restore original sys.path
|
||||
sys.path[:] = original_path
|
||||
@@ -1,626 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Input Handlers (cli/input_handlers.py)
|
||||
====================================================
|
||||
|
||||
Tests for reusable user input collection utilities:
|
||||
- collect_user_input_interactive()
|
||||
- read_from_file()
|
||||
- read_multiline_input()
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Auto-use fixture to set up mock UI module before importing cli.input_handlers
|
||||
# =============================================================================
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_mock_ui_for_input_handlers(mock_ui_module_full):
|
||||
"""Auto-use fixture that replaces sys.modules['ui'] with mock for each test."""
|
||||
sys.modules['ui'] = mock_ui_module_full
|
||||
yield
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Import cli.input_handlers after mock UI is set up by autouse fixture
|
||||
# =============================================================================
|
||||
|
||||
from cli.input_handlers import (
|
||||
collect_user_input_interactive,
|
||||
read_from_file,
|
||||
read_multiline_input,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for collect_user_input_interactive()
|
||||
# =============================================================================
|
||||
|
||||
class TestCollectUserInputInteractive:
|
||||
"""Tests for collect_user_input_interactive() function."""
|
||||
|
||||
def test_returns_input_when_type_selected(self, capsys):
|
||||
"""Returns user input when type option is selected."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=['Line 1', 'Line 2', '']):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert "Line 1" in result
|
||||
assert "Line 2" in result
|
||||
|
||||
def test_returns_input_when_paste_selected(self, capsys):
|
||||
"""Returns user input when paste option is selected."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='paste'):
|
||||
with patch('builtins.input', side_effect=['Pasted content', '']):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert "Pasted content" in result
|
||||
|
||||
def test_reads_from_file_when_file_selected(self, temp_dir):
|
||||
"""Reads input from file when file option is selected."""
|
||||
# Create a test file
|
||||
test_file = temp_dir / "input.txt"
|
||||
test_file.write_text("Content from file")
|
||||
|
||||
with patch('cli.input_handlers.select_menu', return_value='file'):
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert "Content from file" in result
|
||||
|
||||
def test_returns_empty_string_when_skip_selected(self):
|
||||
"""Returns empty string when skip option is selected."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='skip'):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result == ""
|
||||
|
||||
def test_returns_none_when_quit_selected(self):
|
||||
"""Returns None when quit option is selected."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='quit'):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_returns_none_when_menu_returns_none(self):
|
||||
"""Returns None when select_menu returns None."""
|
||||
with patch('cli.input_handlers.select_menu', return_value=None):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_hides_file_option_when_disabled(self):
|
||||
"""Does not show file option when allow_file is False."""
|
||||
with patch('cli.input_handlers.select_menu') as mock_menu:
|
||||
mock_menu.return_value = 'type'
|
||||
with patch('builtins.input', side_effect=['Test', '']):
|
||||
collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:",
|
||||
allow_file=False
|
||||
)
|
||||
|
||||
# Check that options were passed to select_menu
|
||||
options = mock_menu.call_args[1]['options']
|
||||
keys = [opt.key for opt in options]
|
||||
assert 'file' not in keys
|
||||
assert 'type' in keys
|
||||
assert 'skip' in keys
|
||||
assert 'quit' in keys
|
||||
|
||||
def test_hides_paste_option_when_disabled(self):
|
||||
"""Does not show paste option when allow_paste is False."""
|
||||
with patch('cli.input_handlers.select_menu') as mock_menu:
|
||||
mock_menu.return_value = 'type'
|
||||
with patch('builtins.input', side_effect=['Test', '']):
|
||||
collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:",
|
||||
allow_paste=False
|
||||
)
|
||||
|
||||
# Check that options were passed to select_menu
|
||||
options = mock_menu.call_args[1]['options']
|
||||
keys = [opt.key for opt in options]
|
||||
assert 'paste' not in keys
|
||||
assert 'type' in keys
|
||||
assert 'file' in keys
|
||||
|
||||
def test_passes_title_and_subtitle_to_menu(self):
|
||||
"""Passes title and subtitle to select_menu."""
|
||||
with patch('cli.input_handlers.select_menu') as mock_menu:
|
||||
mock_menu.return_value = 'skip'
|
||||
collect_user_input_interactive(
|
||||
title="Custom Title",
|
||||
subtitle="Custom Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert mock_menu.called
|
||||
call_kwargs = mock_menu.call_args[1]
|
||||
assert call_kwargs['title'] == "Custom Title"
|
||||
assert call_kwargs['subtitle'] == "Custom Subtitle"
|
||||
|
||||
def test_handles_keyboard_interrupt_during_type(self, capsys):
|
||||
"""Handles KeyboardInterrupt during type input."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_handles_eof_error_during_type(self, capsys):
|
||||
"""Handles EOFError during type input."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=EOFError):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
# EOFError should break the input loop
|
||||
# Result could be empty string or None depending on implementation
|
||||
assert result is None or result == ""
|
||||
|
||||
def test_file_read_failure_returns_none(self, temp_dir):
|
||||
"""Returns None when file read fails."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='file'):
|
||||
with patch('builtins.input', return_value='/nonexistent/file.txt'):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_strips_whitespace_from_input(self):
|
||||
"""Strips leading/trailing whitespace from collected input."""
|
||||
with patch('cli.input_handlers.select_menu', return_value='type'):
|
||||
with patch('builtins.input', side_effect=[' Text with spaces ', '']):
|
||||
result = collect_user_input_interactive(
|
||||
title="Test Title",
|
||||
subtitle="Test Subtitle",
|
||||
prompt_text="Enter your input:"
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
assert result.strip() == result
|
||||
assert not result.startswith(" ")
|
||||
assert not result.endswith(" ")
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for read_from_file()
|
||||
# =============================================================================
|
||||
|
||||
class TestReadFromFile:
|
||||
"""Tests for read_from_file() function."""
|
||||
|
||||
def test_returns_file_contents(self, temp_dir, capsys):
|
||||
"""Returns contents of the specified file."""
|
||||
test_file = temp_dir / "test.txt"
|
||||
test_file.write_text("File content here")
|
||||
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is not None
|
||||
assert result == "File content here"
|
||||
|
||||
def test_returns_none_when_no_path_provided(self, capsys):
|
||||
"""Returns None when no file path is provided."""
|
||||
with patch('builtins.input', return_value=''):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "No file path" in captured.out
|
||||
|
||||
def test_returns_none_for_nonexistent_file(self, capsys):
|
||||
"""Returns None when file doesn't exist."""
|
||||
with patch('builtins.input', return_value='/nonexistent/path.txt'):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
# The error message could be "not found" or "Permission denied" depending on the system
|
||||
assert "not found" in captured.out.lower() or "no such file" in captured.out.lower() or "permission denied" in captured.out.lower() or "cannot read" in captured.out.lower()
|
||||
|
||||
def test_returns_none_for_empty_file(self, temp_dir, capsys):
|
||||
"""Returns None when file is empty."""
|
||||
empty_file = temp_dir / "empty.txt"
|
||||
empty_file.write_text("")
|
||||
|
||||
with patch('builtins.input', return_value=str(empty_file)):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "empty" in captured.out.lower()
|
||||
|
||||
def test_returns_none_on_permission_error(self, temp_dir, capsys):
|
||||
"""Returns None when file cannot be read due to permissions."""
|
||||
# Create a real temporary file
|
||||
restricted_file = temp_dir / "restricted.txt"
|
||||
restricted_file.write_text("secret content")
|
||||
|
||||
with patch('builtins.input', return_value=str(restricted_file)):
|
||||
with patch.object(Path, 'read_text', side_effect=PermissionError("Denied")):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Permission" in captured.out or "denied" in captured.out.lower()
|
||||
|
||||
def test_returns_none_on_keyboard_interrupt(self, capsys):
|
||||
"""Returns None when user interrupts input."""
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_returns_none_on_eof_error(self, capsys):
|
||||
"""Returns None on EOFError during input."""
|
||||
with patch('builtins.input', side_effect=EOFError):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_expands_tilde_in_path(self, temp_dir):
|
||||
"""Expands ~ to home directory in file path."""
|
||||
test_file = temp_dir / "test.txt"
|
||||
test_file.write_text("Content")
|
||||
|
||||
with patch('builtins.input', return_value='~/test.txt'):
|
||||
with patch('pathlib.Path.expanduser', return_value=test_file):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is not None
|
||||
assert result == "Content"
|
||||
|
||||
def test_resolves_relative_paths(self, temp_dir):
|
||||
"""Resolves relative file paths to absolute."""
|
||||
test_file = temp_dir / "subdir" / "test.txt"
|
||||
test_file.parent.mkdir(parents=True)
|
||||
test_file.write_text("Resolved content")
|
||||
|
||||
# Change to temp_dir
|
||||
import os
|
||||
original_cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir(temp_dir)
|
||||
with patch('builtins.input', return_value='subdir/test.txt'):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is not None
|
||||
assert result == "Resolved content"
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
def test_shows_character_count(self, temp_dir, capsys):
|
||||
"""Shows number of characters loaded from file."""
|
||||
test_file = temp_dir / "test.txt"
|
||||
content = "A" * 100
|
||||
test_file.write_text(content)
|
||||
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
result = read_from_file()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "100" in captured.out or "character" in captured.out.lower()
|
||||
|
||||
def test_handles_unicode_content(self, temp_dir):
|
||||
"""Handles files with Unicode content."""
|
||||
test_file = temp_dir / "unicode.txt"
|
||||
content = "Hello 世界 🌍 Привет"
|
||||
test_file.write_text(content, encoding='utf-8')
|
||||
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is not None
|
||||
assert result == content
|
||||
|
||||
def test_strips_whitespace_from_file_content(self, temp_dir):
|
||||
"""Strips leading/trailing whitespace from file content."""
|
||||
test_file = temp_dir / "spaces.txt"
|
||||
test_file.write_text(" Content with spaces ")
|
||||
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is not None
|
||||
assert result == "Content with spaces"
|
||||
assert not result.startswith(" ")
|
||||
assert not result.endswith(" ")
|
||||
|
||||
def test_handles_generic_exception(self, temp_dir, capsys):
|
||||
"""Handles generic exceptions during file reading."""
|
||||
# Create a real temporary file
|
||||
test_file = temp_dir / "error_file.txt"
|
||||
test_file.write_text("content")
|
||||
|
||||
with patch('builtins.input', return_value=str(test_file)):
|
||||
with patch.object(Path, 'read_text', side_effect=Exception("Unknown error")):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Error" in captured.out or "error" in captured.out.lower()
|
||||
|
||||
def test_file_not_found_after_resolve(self, temp_dir, capsys):
|
||||
"""Returns None when path resolves but file doesn't exist (lines 163-164)."""
|
||||
# Use a path in a valid temp directory but the file doesn't exist
|
||||
nonexistent_file = temp_dir / "does_not_exist.txt"
|
||||
|
||||
with patch('builtins.input', return_value=str(nonexistent_file)):
|
||||
result = read_from_file()
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
# Should show "File not found" error message
|
||||
assert "not found" in captured.out.lower()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for read_multiline_input()
|
||||
# =============================================================================
|
||||
|
||||
class TestReadMultilineInput:
|
||||
"""Tests for read_multiline_input() function."""
|
||||
|
||||
def test_returns_single_line_input(self):
|
||||
"""Returns single line of input."""
|
||||
with patch('builtins.input', side_effect=['Single line', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert result == "Single line"
|
||||
|
||||
def test_returns_multiple_lines_of_input(self):
|
||||
"""Returns multiple lines joined by newline."""
|
||||
with patch('builtins.input', side_effect=['Line 1', 'Line 2', 'Line 3', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert result == "Line 1\nLine 2\nLine 3"
|
||||
|
||||
def test_stops_on_empty_line(self):
|
||||
"""Stops reading when encountering an empty line."""
|
||||
with patch('builtins.input', side_effect=['Line 1', 'Line 2', '', 'Should not be included']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert "Should not be included" not in result
|
||||
|
||||
def test_returns_none_on_keyboard_interrupt(self, capsys):
|
||||
"""Returns None when user interrupts with Ctrl+C."""
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is None
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out or "cancel" in captured.out.lower()
|
||||
|
||||
def test_breaks_on_eof_error(self):
|
||||
"""Breaks input loop on EOFError."""
|
||||
with patch('builtins.input', side_effect=['Line 1', EOFError]):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
# Should return content before EOF
|
||||
assert result is not None
|
||||
assert "Line 1" in result
|
||||
|
||||
def test_handles_empty_input(self):
|
||||
"""Handles case where user enters nothing."""
|
||||
with patch('builtins.input', side_effect=['', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result == ""
|
||||
|
||||
def test_strips_whitespace_from_result(self):
|
||||
"""Strips leading/trailing whitespace from final result."""
|
||||
with patch('builtins.input', side_effect=[' Line 1 ', ' Line 2 ', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
# Note: The implementation strips each line but not the overall result
|
||||
# Behavior depends on implementation
|
||||
assert result is not None
|
||||
assert "Line 1" in result
|
||||
|
||||
def test_handles_unicode_input(self):
|
||||
"""Handles Unicode characters in input."""
|
||||
with patch('builtins.input', side_effect=['Hello 世界', '🌍 Emoji', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert "世界" in result
|
||||
assert "🌍" in result
|
||||
|
||||
def test_preserves_internal_whitespace(self):
|
||||
"""Preserves internal whitespace in lines."""
|
||||
with patch('builtins.input', side_effect=['Line with spaces', 'Line\twith\ttabs', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert " " in result
|
||||
assert "\t" in result
|
||||
|
||||
def test_passes_prompt_text_to_box(self, capsys):
|
||||
"""Passes prompt text to the box display."""
|
||||
custom_prompt = "Custom prompt text"
|
||||
with patch('builtins.input', side_effect=['', '']):
|
||||
read_multiline_input(custom_prompt)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# The actual custom prompt text should appear in the output
|
||||
assert custom_prompt.lower() in captured.out.lower()
|
||||
|
||||
def test_allows_multiple_consecutive_empty_lines_to_stop(self):
|
||||
"""Stops on first empty line (empty_count >= 1)."""
|
||||
with patch('builtins.input', side_effect=['Line 1', '', '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert result == "Line 1"
|
||||
|
||||
def test_handles_long_lines(self):
|
||||
"""Handles very long input lines."""
|
||||
long_line = "A" * 10000
|
||||
with patch('builtins.input', side_effect=[long_line, '']):
|
||||
result = read_multiline_input("Enter text:")
|
||||
|
||||
assert result is not None
|
||||
assert len(result) == 10000
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for module import behavior (line 14 - sys.path insertion)
|
||||
# =============================================================================
|
||||
|
||||
class TestModuleImportPathInsertion:
|
||||
"""Tests for module-level path manipulation logic."""
|
||||
|
||||
def test_inserts_parent_dir_to_sys_path_when_not_present(self):
|
||||
"""
|
||||
Test that line 14 executes: sys.path.insert(0, str(_PARENT_DIR))
|
||||
|
||||
This test covers the scenario where _PARENT_DIR is not in sys.path
|
||||
when the module-level code executes.
|
||||
|
||||
Note: This test manually executes the module-level code that would
|
||||
normally run on import, since we can't easily re-import after removing
|
||||
the path (the module wouldn't be found without the path).
|
||||
"""
|
||||
from cli.input_handlers import _PARENT_DIR
|
||||
|
||||
# Get the parent dir that should be inserted by line 14
|
||||
parent_dir_str = str(_PARENT_DIR)
|
||||
parent_dir_normalized = os.path.normpath(parent_dir_str)
|
||||
|
||||
# Verify parent_dir_str is the apps/backend directory (cross-platform)
|
||||
expected_suffix = os.path.join("apps", "backend")
|
||||
assert parent_dir_normalized.endswith(expected_suffix) or parent_dir_str.endswith("apps/backend")
|
||||
|
||||
# Save current sys.path state to restore later
|
||||
original_path = sys.path.copy()
|
||||
|
||||
# Remove the parent dir from sys.path to simulate the condition on line 13
|
||||
# Use normalized paths for comparison to handle different path separators
|
||||
paths_to_restore = []
|
||||
for p in sys.path[:]: # Copy to avoid modification during iteration
|
||||
p_normalized = os.path.normpath(p)
|
||||
if expected_suffix in p_normalized or p == parent_dir_str:
|
||||
paths_to_restore.append(p)
|
||||
sys.path.remove(p)
|
||||
|
||||
try:
|
||||
# Verify parent_dir_str is NOT in sys.path now
|
||||
assert parent_dir_str not in sys.path
|
||||
|
||||
# Now manually execute the logic from lines 13-14 of input_handlers.py
|
||||
# This simulates what happens when the module is imported without the path
|
||||
# We use the _PARENT_DIR value that was already imported
|
||||
if str(_PARENT_DIR) not in sys.path:
|
||||
# This is line 14 - the line we're testing
|
||||
sys.path.insert(0, str(_PARENT_DIR))
|
||||
|
||||
# Verify the parent dir was added to sys.path at position 0
|
||||
assert parent_dir_str in sys.path, f"Parent dir {parent_dir_str} should be in sys.path"
|
||||
assert sys.path[0] == parent_dir_str, f"Parent dir should be at sys.path[0]"
|
||||
|
||||
finally:
|
||||
# Restore sys.path to original state
|
||||
sys.path[:] = original_path
|
||||
|
||||
def test_line_14_coverage_via_importlib_reload(self):
|
||||
"""
|
||||
Test that line 14 executes using importlib.reload() with path manipulation.
|
||||
|
||||
This test forces a reload of the module in a state where _PARENT_DIR
|
||||
is not in sys.path, triggering line 14 execution.
|
||||
"""
|
||||
import importlib
|
||||
import cli.input_handlers
|
||||
|
||||
# Get the parent dir that should be inserted by line 14
|
||||
parent_dir_str = str(cli.input_handlers._PARENT_DIR)
|
||||
|
||||
# Save current sys.path and sys.modules state to restore later
|
||||
original_path = sys.path.copy()
|
||||
original_module = sys.modules.get('cli.input_handlers')
|
||||
|
||||
# Remove the parent dir from sys.path
|
||||
# Use normalized paths for comparison to handle different path separators
|
||||
parent_dir_normalized = os.path.normpath(parent_dir_str)
|
||||
for p in sys.path[:]:
|
||||
p_normalized = os.path.normpath(p)
|
||||
if p == parent_dir_str or p_normalized == parent_dir_normalized:
|
||||
sys.path.remove(p)
|
||||
|
||||
try:
|
||||
# Verify parent_dir_str is NOT in sys.path now
|
||||
assert parent_dir_str not in sys.path
|
||||
|
||||
# Reload the module - this should execute lines 13-14 since path is not present
|
||||
importlib.reload(cli.input_handlers)
|
||||
|
||||
# Verify the parent dir was added to sys.path by line 14
|
||||
assert parent_dir_str in sys.path, f"Parent dir {parent_dir_str} should be in sys.path"
|
||||
|
||||
finally:
|
||||
# Restore sys.path to original state
|
||||
sys.path[:] = original_path
|
||||
# Restore sys.modules to original state
|
||||
if original_module is not None:
|
||||
sys.modules['cli.input_handlers'] = original_module
|
||||
elif 'cli.input_handlers' in sys.modules:
|
||||
del sys.modules['cli.input_handlers']
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,581 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI QA Commands
|
||||
==========================
|
||||
|
||||
Tests for qa_commands.py module functionality including:
|
||||
- handle_qa_status_command() - Display QA status for a spec
|
||||
- handle_review_status_command() - Display review status for a spec
|
||||
- handle_qa_command() - Run QA validation loop
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from cli.qa_commands import (
|
||||
handle_qa_command,
|
||||
handle_qa_status_command,
|
||||
handle_review_status_command,
|
||||
)
|
||||
from review import ReviewState
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_with_qa_report(temp_dir: Path) -> Path:
|
||||
"""Create a spec directory with QA report."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
qa_report = spec_dir / "qa_report.md"
|
||||
qa_report.write_text(
|
||||
"# QA Report\n\n"
|
||||
"## Status: Approved\n\n"
|
||||
"All tests passed.\n"
|
||||
)
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_with_fix_request(temp_dir: Path) -> Path:
|
||||
"""Create a spec directory with QA fix request."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
||||
fix_request.write_text(
|
||||
"# QA Fix Request\n\n"
|
||||
"## Issues Found\n\n"
|
||||
"1. Unit tests failing\n"
|
||||
"2. Missing error handling\n"
|
||||
)
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_with_implementation_plan(temp_dir: Path) -> Path:
|
||||
"""Create a spec directory with implementation plan (incomplete)."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
plan = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"name": "Phase 1",
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "pending"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
plan_file.write_text(json.dumps(plan))
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_complete(temp_dir: Path) -> Path:
|
||||
"""Create a spec directory with complete implementation."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
plan = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"name": "Phase 1",
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "completed"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
plan_file.write_text(json.dumps(plan))
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_with_review_state(temp_dir: Path) -> Path:
|
||||
"""Create a spec directory with review state."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create spec.md first so the hash can match
|
||||
(spec_dir / "spec.md").write_text("# Test Spec\n")
|
||||
|
||||
review_state = ReviewState(
|
||||
approved=True,
|
||||
approved_by="test_user",
|
||||
approved_at="2024-01-15T10:30:00",
|
||||
feedback=["Looks good!"],
|
||||
spec_hash="", # Empty hash will be calculated and should match
|
||||
review_count=1,
|
||||
)
|
||||
review_state.save(spec_dir)
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir_with_review_state_changed(temp_dir: Path) -> Path:
|
||||
"""Create a spec with approved review but changed spec."""
|
||||
spec_dir = temp_dir / "001-test-spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Save review state
|
||||
review_state = ReviewState(
|
||||
approved=True,
|
||||
approved_by="test_user",
|
||||
spec_hash="old_hash",
|
||||
)
|
||||
review_state.save(spec_dir)
|
||||
|
||||
# Create spec.md (will have different hash)
|
||||
(spec_dir / "spec.md").write_text("# Updated Spec\n")
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_QA_STATUS_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleQaStatusCommand:
|
||||
"""Tests for handle_qa_status_command() function."""
|
||||
|
||||
def test_prints_qa_status(self, capsys, spec_dir_with_qa_report: Path) -> None:
|
||||
"""Prints QA status for the spec."""
|
||||
handle_qa_status_command(spec_dir_with_qa_report)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "001-test-spec" in captured.out
|
||||
# Check that some QA status output is present
|
||||
assert len(captured.out) > 0
|
||||
|
||||
def test_prints_banner(self, capsys, spec_dir_with_qa_report: Path) -> None:
|
||||
"""Prints banner before status."""
|
||||
handle_qa_status_command(spec_dir_with_qa_report)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Banner should be printed (check for some visual separator)
|
||||
assert "001-test-spec" in captured.out
|
||||
|
||||
def test_handles_missing_qa_report(self, capsys, temp_dir: Path) -> None:
|
||||
"""Handles spec directory without QA report gracefully."""
|
||||
spec_dir = temp_dir / "001-no-qa"
|
||||
spec_dir.mkdir()
|
||||
|
||||
handle_qa_status_command(spec_dir)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should print something even without QA report
|
||||
assert len(captured.out) > 0
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_REVIEW_STATUS_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleReviewStatusCommand:
|
||||
"""Tests for handle_review_status_command() function."""
|
||||
|
||||
def test_prints_review_status(self, capsys, spec_dir_with_review_state: Path) -> None:
|
||||
"""Prints review status for the spec."""
|
||||
handle_review_status_command(spec_dir_with_review_state)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "001-test-spec" in captured.out
|
||||
|
||||
def test_shows_ready_to_build_when_approval_valid(
|
||||
self, capsys, spec_dir_with_review_state: Path
|
||||
) -> None:
|
||||
"""Shows 'Ready to build' message when approval is valid."""
|
||||
handle_review_status_command(spec_dir_with_review_state)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Ready to build" in captured.out
|
||||
assert "approval is valid" in captured.out
|
||||
|
||||
def test_shows_re_review_required_when_spec_changed(
|
||||
self, capsys, spec_dir_with_review_state_changed: Path
|
||||
) -> None:
|
||||
"""Shows 're-review required' message when spec changed after approval."""
|
||||
handle_review_status_command(spec_dir_with_review_state_changed)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "re-review required" in captured.out
|
||||
assert "Spec changed" in captured.out
|
||||
|
||||
def test_shows_review_required_when_not_approved(
|
||||
self, capsys, temp_dir: Path
|
||||
) -> None:
|
||||
"""Shows 'review required' message when spec is not approved."""
|
||||
spec_dir = temp_dir / "001-not-approved"
|
||||
spec_dir.mkdir()
|
||||
(spec_dir / "spec.md").write_text("# Not Approved\n")
|
||||
|
||||
handle_review_status_command(spec_dir)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Review required" in captured.out
|
||||
|
||||
def test_prints_banner(self, capsys, spec_dir_with_review_state: Path) -> None:
|
||||
"""Prints banner before review status."""
|
||||
handle_review_status_command(spec_dir_with_review_state)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "001-test-spec" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HANDLE_QA_COMMAND TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestHandleQaCommand:
|
||||
"""Tests for handle_qa_command() function."""
|
||||
|
||||
def test_already_approved_message(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Shows already approved message when QA already passed."""
|
||||
# Create qa_report.md
|
||||
(spec_dir_complete / "qa_report.md").write_text("# QA Approved\n")
|
||||
|
||||
# Mock both validate_environment and should_run_qa/is_qa_approved
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.should_run_qa', return_value=False):
|
||||
with patch('cli.qa_commands.is_qa_approved', return_value=True):
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should print the "already approved" message
|
||||
assert "already approved" in captured.out
|
||||
|
||||
def test_incomplete_build_message(
|
||||
self, capsys, spec_dir_with_implementation_plan: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Shows incomplete build message when subtasks not complete."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_with_implementation_plan,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Build not complete" in captured.out
|
||||
assert "1/2" in captured.out
|
||||
|
||||
def test_processes_human_feedback(
|
||||
self, capsys, spec_dir_with_fix_request: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Processes fix request when human feedback present."""
|
||||
# Add implementation plan so should_run_qa would normally return True
|
||||
plan = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "completed"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
(spec_dir_with_fix_request / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.return_value = True
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_with_fix_request,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Human feedback detected" in captured.out
|
||||
assert "processing fix request" in captured.out
|
||||
|
||||
def test_runs_qa_validation_loop(
|
||||
self, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Runs QA validation loop when conditions are met."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.return_value = True
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
# Should run the validation loop
|
||||
assert mock_loop.called
|
||||
call_args = mock_loop.call_args
|
||||
assert call_args[1]["project_dir"] == temp_git_repo
|
||||
assert call_args[1]["spec_dir"] == spec_dir_complete
|
||||
assert call_args[1]["model"] == "test-model"
|
||||
assert call_args[1]["verbose"] is True
|
||||
|
||||
def test_qa_approved_message(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Shows QA approved message when validation passes."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.return_value = True
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "QA validation passed" in captured.out
|
||||
assert "Ready for merge" in captured.out
|
||||
|
||||
def test_qa_incomplete_message(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Shows incomplete message and exits when validation fails."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.return_value = False
|
||||
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
|
||||
def test_exits_on_invalid_environment(
|
||||
self, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Exits when environment validation fails."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=False):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
|
||||
def test_handles_keyboard_interrupt(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Handles KeyboardInterrupt gracefully during QA loop."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.side_effect = KeyboardInterrupt()
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "QA validation paused" in captured.out
|
||||
assert "--qa" in captured.out
|
||||
|
||||
def test_prints_banner(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Prints banner before running QA."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop'):
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should show banner
|
||||
assert "QA validation" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# INTEGRATION TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestQaCommandsIntegration:
|
||||
"""Integration tests for QA commands."""
|
||||
|
||||
def test_qa_status_to_review_status_workflow(
|
||||
self, capsys, spec_dir_with_review_state: Path
|
||||
) -> None:
|
||||
"""Test checking both QA and review status."""
|
||||
# Check QA status
|
||||
handle_qa_status_command(spec_dir_with_review_state)
|
||||
capsys.readouterr()
|
||||
|
||||
# Check review status
|
||||
handle_review_status_command(spec_dir_with_review_state)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
# Both should print spec name
|
||||
assert "001-test-spec" in captured.out
|
||||
|
||||
def test_qa_command_with_complete_workflow(
|
||||
self, capsys, spec_dir_complete: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Test full QA workflow from start to approval."""
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
# Simulate successful QA
|
||||
mock_loop.return_value = True
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_complete,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "QA validation passed" in captured.out
|
||||
|
||||
def test_qa_command_with_fix_request_workflow(
|
||||
self, capsys, spec_dir_with_fix_request: Path, temp_git_repo: Path
|
||||
) -> None:
|
||||
"""Test QA workflow with human feedback."""
|
||||
# Mark as complete
|
||||
plan = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "completed"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
(spec_dir_with_fix_request / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
with patch('cli.qa_commands.validate_environment', return_value=True):
|
||||
with patch('cli.qa_commands.run_qa_validation_loop') as mock_loop:
|
||||
mock_loop.return_value = True
|
||||
|
||||
handle_qa_command(
|
||||
project_dir=temp_git_repo,
|
||||
spec_dir=spec_dir_with_fix_request,
|
||||
model="test-model",
|
||||
verbose=False,
|
||||
)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Human feedback detected" in captured.out
|
||||
assert "QA validation passed" in captured.out
|
||||
|
||||
def test_review_status_scenarios(
|
||||
self, capsys, temp_dir: Path
|
||||
) -> None:
|
||||
"""Test different review status scenarios."""
|
||||
# Scenario 1: No review state
|
||||
spec_dir = temp_dir / "001-test"
|
||||
spec_dir.mkdir()
|
||||
(spec_dir / "spec.md").write_text("# Test\n")
|
||||
|
||||
handle_review_status_command(spec_dir)
|
||||
captured = capsys.readouterr()
|
||||
assert "Review required" in captured.out
|
||||
|
||||
# Scenario 2: Approved and valid
|
||||
review_state = ReviewState(approved=True, spec_hash="")
|
||||
review_state.save(spec_dir)
|
||||
|
||||
handle_review_status_command(spec_dir)
|
||||
captured = capsys.readouterr()
|
||||
# Should show either "Ready to build" or "APPROVED" status
|
||||
assert "APPROVED" in captured.out or "Ready to build" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MODULE IMPORT PATH INSERTION TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestModuleImportPathInsertion:
|
||||
"""Tests for module-level path manipulation logic (line 15)."""
|
||||
|
||||
def test_inserts_parent_dir_to_sys_path_when_not_present(self):
|
||||
"""
|
||||
Test that line 15 executes: sys.path.insert(0, str(_PARENT_DIR))
|
||||
|
||||
This test covers the scenario where _PARENT_DIR is not in sys.path
|
||||
when the module-level code executes.
|
||||
"""
|
||||
import importlib
|
||||
|
||||
# Use import_module to get the actual module object
|
||||
qa_commands_module = importlib.import_module("cli.qa_commands")
|
||||
|
||||
# Get the parent dir that should be inserted by line 15
|
||||
parent_dir_str = str(qa_commands_module._PARENT_DIR)
|
||||
|
||||
# Verify parent_dir_str is the apps/backend directory
|
||||
# Use os.path.normpath for cross-platform path comparison
|
||||
import os
|
||||
normalized_path = os.path.normpath(parent_dir_str)
|
||||
# Check that the normalized path contains apps/backend or apps\backend (Windows)
|
||||
assert ("apps" + os.sep + "backend") in normalized_path or "apps/backend" in normalized_path or "apps\\backend" in normalized_path
|
||||
|
||||
# Save current sys.path state to restore later
|
||||
original_path = sys.path.copy()
|
||||
|
||||
# Remove the parent dir from sys.path
|
||||
for p in sys.path[:]:
|
||||
if p == parent_dir_str or p.rstrip("/") == parent_dir_str.rstrip("/"):
|
||||
sys.path.remove(p)
|
||||
|
||||
try:
|
||||
# Verify parent_dir_str is NOT in sys.path now
|
||||
assert parent_dir_str not in sys.path
|
||||
|
||||
# Reload the module - this should execute lines 14-15 since path is not present
|
||||
importlib.reload(qa_commands_module)
|
||||
|
||||
# Verify the parent dir was added to sys.path by line 15
|
||||
assert parent_dir_str in sys.path, f"Parent dir {parent_dir_str} should be in sys.path"
|
||||
|
||||
finally:
|
||||
# Restore sys.path to original state
|
||||
sys.path[:] = original_path
|
||||
@@ -1,953 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Recovery Module (cli/recovery.py)
|
||||
===============================================
|
||||
|
||||
Tests for the JSON recovery utility that detects and repairs corrupted JSON files
|
||||
in specs directories:
|
||||
- check_json_file()
|
||||
- detect_corrupted_files()
|
||||
- backup_corrupted_file()
|
||||
- main() - all CLI argument combinations and paths
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Add apps/backend to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
|
||||
# =============================================================================
|
||||
# Mock external dependencies before importing cli.recovery
|
||||
# =============================================================================
|
||||
|
||||
# Mock spec.pipeline module which provides get_specs_dir
|
||||
if 'spec.pipeline' not in sys.modules:
|
||||
mock_pipeline = MagicMock()
|
||||
mock_pipeline.get_specs_dir = lambda project_dir: project_dir / ".auto-claude" / "specs"
|
||||
sys.modules['spec.pipeline'] = mock_pipeline
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Import cli.recovery after mocking dependencies
|
||||
# =============================================================================
|
||||
|
||||
from cli.recovery import (
|
||||
check_json_file,
|
||||
detect_corrupted_files,
|
||||
backup_corrupted_file,
|
||||
main,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for check_json_file()
|
||||
# =============================================================================
|
||||
|
||||
class TestCheckJsonFile:
|
||||
"""Tests for check_json_file() function."""
|
||||
|
||||
def test_returns_true_for_valid_json(self, temp_dir):
|
||||
"""Returns (True, None) for valid JSON file."""
|
||||
json_file = temp_dir / "valid.json"
|
||||
json_file.write_text('{"key": "value"}')
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_returns_false_for_json_decode_error(self, temp_dir):
|
||||
"""Returns (False, error_message) for malformed JSON."""
|
||||
json_file = temp_dir / "invalid.json"
|
||||
json_file.write_text('{"key": invalid}')
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
assert "Expecting value" in error or "JSONDecodeError" in error
|
||||
|
||||
def test_returns_false_for_trailing_comma(self, temp_dir):
|
||||
"""Detects JSON with trailing comma (common error)."""
|
||||
json_file = temp_dir / "trailing.json"
|
||||
json_file.write_text('{"key": "value",}')
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_returns_false_for_unclosed_bracket(self, temp_dir):
|
||||
"""Detects JSON with unclosed bracket."""
|
||||
json_file = temp_dir / "unclosed.json"
|
||||
json_file.write_text('{"key": "value"')
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_returns_false_for_empty_file(self, temp_dir):
|
||||
"""Handles empty file as invalid JSON."""
|
||||
json_file = temp_dir / "empty.json"
|
||||
json_file.write_text("")
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_returns_false_for_non_json_text(self, temp_dir):
|
||||
"""Handles plain text file as invalid JSON."""
|
||||
json_file = temp_dir / "text.json"
|
||||
json_file.write_text("This is just plain text")
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_returns_false_for_partial_json(self, temp_dir):
|
||||
"""Handles partial JSON (valid value but not complete document)."""
|
||||
json_file = temp_dir / "partial.json"
|
||||
json_file.write_text('"just a string"')
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
# A lone string is actually valid JSON according to the spec
|
||||
# but the function should handle it
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_handles_complex_valid_json(self, temp_dir):
|
||||
"""Handles complex nested valid JSON."""
|
||||
json_file = temp_dir / "complex.json"
|
||||
complex_data = {
|
||||
"nested": {"level1": {"level2": {"level3": "deep"}}},
|
||||
"array": [1, 2, 3, {"item": "value"}],
|
||||
"string": "value with unicode: \u2713",
|
||||
"number": 42.5,
|
||||
"boolean": True,
|
||||
"null": None,
|
||||
}
|
||||
json_file.write_text(json.dumps(complex_data))
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_returns_error_for_file_not_found(self, temp_dir):
|
||||
"""Handles non-existent file gracefully."""
|
||||
json_file = temp_dir / "nonexistent.json"
|
||||
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
assert "No such file" in error or "NotFoundError" in error
|
||||
|
||||
def test_returns_error_for_permission_denied(self, temp_dir):
|
||||
"""Handles permission errors gracefully."""
|
||||
# This test is platform-dependent and may not work on all systems
|
||||
# We'll just verify the function has a generic exception handler
|
||||
json_file = temp_dir / "restricted.json"
|
||||
json_file.write_text('{"key": "value"}')
|
||||
|
||||
# Mock open to raise permission error
|
||||
with patch("builtins.open", side_effect=PermissionError("Access denied")):
|
||||
is_valid, error = check_json_file(json_file)
|
||||
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
assert "Access denied" in error or "PermissionError" in error
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for detect_corrupted_files()
|
||||
# =============================================================================
|
||||
|
||||
class TestDetectCorruptedFiles:
|
||||
"""Tests for detect_corrupted_files() function."""
|
||||
|
||||
def test_returns_empty_list_for_nonexistent_dir(self, temp_dir):
|
||||
"""Returns empty list when specs directory doesn't exist."""
|
||||
nonexistent_dir = temp_dir / "nonexistent" / "specs"
|
||||
|
||||
corrupted = detect_corrupted_files(nonexistent_dir)
|
||||
|
||||
assert corrupted == []
|
||||
|
||||
def test_returns_empty_list_for_valid_json_files(self, temp_dir):
|
||||
"""Returns empty list when all JSON files are valid."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create valid JSON files
|
||||
(specs_dir / "requirements.json").write_text('{"task": "test"}')
|
||||
(specs_dir / "context.json").write_text('{"files": []}')
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert corrupted == []
|
||||
|
||||
def test_finds_corrupted_json_files(self, temp_dir):
|
||||
"""Finds and returns corrupted JSON files with error messages."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create valid file
|
||||
(specs_dir / "valid.json").write_text('{"key": "value"}')
|
||||
# Create corrupted file
|
||||
(specs_dir / "corrupted.json").write_text('{"key": invalid}')
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert len(corrupted) == 1
|
||||
filepath, error = corrupted[0]
|
||||
assert filepath.name == "corrupted.json"
|
||||
assert error is not None
|
||||
|
||||
def test_scans_recursively(self, temp_dir):
|
||||
"""Scans subdirectories recursively for JSON files."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create nested structure
|
||||
spec_folder = specs_dir / "001-feature"
|
||||
spec_folder.mkdir()
|
||||
memory_dir = spec_folder / "memory"
|
||||
memory_dir.mkdir()
|
||||
|
||||
# Valid files in root
|
||||
(specs_dir / "root_valid.json").write_text('{"valid": true}')
|
||||
# Valid file in spec folder
|
||||
(spec_folder / "spec_valid.json").write_text('{"valid": true}')
|
||||
# Corrupted file in memory subfolder
|
||||
(memory_dir / "memory_corrupted.json").write_text('{invalid json}')
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert len(corrupted) == 1
|
||||
filepath, _ = corrupted[0]
|
||||
assert "memory_corrupted.json" in str(filepath)
|
||||
|
||||
def test_finds_multiple_corrupted_files(self, temp_dir):
|
||||
"""Finds all corrupted files in directory tree."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create multiple corrupted files
|
||||
(specs_dir / "corrupted1.json").write_text('{invalid 1}')
|
||||
(specs_dir / "corrupted2.json").write_text('{invalid 2}')
|
||||
(specs_dir / "valid.json").write_text('{"valid": true}')
|
||||
(specs_dir / "corrupted3.json").write_text('{invalid 3}')
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert len(corrupted) == 3
|
||||
filenames = [f[0].name for f in corrupted]
|
||||
assert "corrupted1.json" in filenames
|
||||
assert "corrupted2.json" in filenames
|
||||
assert "corrupted3.json" in filenames
|
||||
assert "valid.json" not in filenames
|
||||
|
||||
def test_includes_error_messages(self, temp_dir):
|
||||
"""Includes descriptive error messages for each corrupted file."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
(specs_dir / "test.json").write_text('{"unclosed": ')
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert len(corrupted) == 1
|
||||
filepath, error = corrupted[0]
|
||||
assert filepath.name == "test.json"
|
||||
assert error is not None
|
||||
assert len(error) > 0
|
||||
|
||||
def test_ignores_non_json_files(self, temp_dir):
|
||||
"""Only processes .json files, ignores others."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create various file types
|
||||
(specs_dir / "spec.md").write_text("# Spec")
|
||||
(specs_dir / "data.txt").write_text("plain text")
|
||||
(specs_dir / "script.py").write_text("print('hello')")
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert len(corrupted) == 0
|
||||
|
||||
def test_handles_empty_directory(self, temp_dir):
|
||||
"""Returns empty list for empty directory."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
corrupted = detect_corrupted_files(specs_dir)
|
||||
|
||||
assert corrupted == []
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for backup_corrupted_file()
|
||||
# =============================================================================
|
||||
|
||||
class TestBackupCorruptedFile:
|
||||
"""Tests for backup_corrupted_file() function."""
|
||||
|
||||
def test_renames_file_with_corrupted_suffix(self, temp_dir, capsys):
|
||||
"""Renames corrupted file with .corrupted suffix."""
|
||||
corrupted_file = temp_dir / "data.json"
|
||||
corrupted_file.write_text('{"corrupted": true}')
|
||||
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is True
|
||||
assert not corrupted_file.exists()
|
||||
backup_path = temp_dir / "data.json.corrupted"
|
||||
assert backup_path.exists()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "[BACKUP]" in captured.out
|
||||
assert "data.json.corrupted" in captured.out
|
||||
|
||||
def test_returns_true_on_success(self, temp_dir):
|
||||
"""Returns True when backup succeeds."""
|
||||
corrupted_file = temp_dir / "test.json"
|
||||
corrupted_file.write_text('invalid')
|
||||
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is True
|
||||
|
||||
def test_handles_existing_backup_with_unique_suffix(self, temp_dir, capsys):
|
||||
"""Generates unique suffix when backup already exists."""
|
||||
corrupted_file = temp_dir / "test.json"
|
||||
corrupted_file.write_text('invalid')
|
||||
|
||||
# Create existing backup
|
||||
existing_backup = temp_dir / "test.json.corrupted"
|
||||
existing_backup.write_text('old backup')
|
||||
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is True
|
||||
assert not corrupted_file.exists()
|
||||
# Original backup should still exist
|
||||
assert existing_backup.exists()
|
||||
# New backup should have unique suffix
|
||||
unique_backups = list(temp_dir.glob("test.json.corrupted.*"))
|
||||
assert len(unique_backups) == 1
|
||||
|
||||
def test_prints_error_on_failure(self, temp_dir, capsys):
|
||||
"""Prints error message when backup fails."""
|
||||
corrupted_file = temp_dir / "test.json"
|
||||
corrupted_file.write_text('invalid')
|
||||
|
||||
# Mock rename to raise exception
|
||||
with patch("pathlib.Path.rename", side_effect=OSError("Disk full")):
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is False
|
||||
captured = capsys.readouterr()
|
||||
assert "[ERROR]" in captured.out
|
||||
assert "Failed to backup file" in captured.out
|
||||
|
||||
def test_handles_permission_error(self, temp_dir, capsys):
|
||||
"""Handles permission errors during backup."""
|
||||
corrupted_file = temp_dir / "test.json"
|
||||
corrupted_file.write_text('invalid')
|
||||
|
||||
with patch("pathlib.Path.rename", side_effect=PermissionError("Access denied")):
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is False
|
||||
captured = capsys.readouterr()
|
||||
assert "[ERROR]" in captured.out
|
||||
|
||||
def test_preserves_file_content_in_backup(self, temp_dir):
|
||||
"""Original content is preserved in backup file."""
|
||||
corrupted_file = temp_dir / "test.json"
|
||||
original_content = '{"broken": json}'
|
||||
corrupted_file.write_text(original_content)
|
||||
|
||||
backup_corrupted_file(corrupted_file)
|
||||
|
||||
backup_path = temp_dir / "test.json.corrupted"
|
||||
assert backup_path.read_text() == original_content
|
||||
|
||||
def test_handles_subdirectory_paths(self, temp_dir):
|
||||
"""Correctly backs up files in subdirectories."""
|
||||
subdir = temp_dir / "subdir" / "nested"
|
||||
subdir.mkdir(parents=True)
|
||||
corrupted_file = subdir / "data.json"
|
||||
corrupted_file.write_text('invalid')
|
||||
|
||||
result = backup_corrupted_file(corrupted_file)
|
||||
|
||||
assert result is True
|
||||
assert not corrupted_file.exists()
|
||||
backup_path = subdir / "data.json.corrupted"
|
||||
assert backup_path.exists()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Argument Parsing and Validation
|
||||
# =============================================================================
|
||||
|
||||
class TestMainArguments:
|
||||
"""Tests for main() argument parsing and validation."""
|
||||
|
||||
def test_default_project_dir_is_cwd(self, temp_dir, capsys):
|
||||
"""Uses current working directory as default project-dir."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
original_cwd = Path.cwd()
|
||||
try:
|
||||
import os
|
||||
os.chdir(temp_dir)
|
||||
with patch("sys.argv", ["recovery.py"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
# Should exit with 0 when no corrupted files found
|
||||
assert exc_info.value.code == 0
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
def test_all_requires_delete_error(self, capsys):
|
||||
"""Exits with error when --all is used without --delete."""
|
||||
with patch("sys.argv", ["recovery.py", "--all"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_specs_dir_overrides_auto_detection(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""--specs-dir overrides auto-detected specs directory."""
|
||||
custom_specs = temp_dir / "custom_specs"
|
||||
custom_specs.mkdir(parents=True)
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--specs-dir", str(custom_specs), "--detect"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
# Should exit 0 (no corrupted files)
|
||||
assert exc_info.value.code == 0
|
||||
# find_specs_dir should not be called when --specs-dir is provided
|
||||
mock_find_specs.assert_not_called()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Detect Mode
|
||||
# =============================================================================
|
||||
|
||||
class TestMainDetectMode:
|
||||
"""Tests for main() in detect mode."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_mode_exits_0_when_no_corruption(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with 0 when no corrupted files found in detect mode."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 0
|
||||
captured = capsys.readouterr()
|
||||
assert "No corrupted JSON files found" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_mode_exits_1_when_corruption_found(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with 1 when corrupted files found in detect mode."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
# Create corrupted file
|
||||
(specs_dir / "corrupted.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "corrupted file" in captured.out.lower()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_mode_shows_corrupted_files(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows list of corrupted files in detect mode."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "requirements.json").write_text('{"valid": true}')
|
||||
(specs_dir / "broken.json").write_text('{broken}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "broken.json" in captured.out
|
||||
assert "Error:" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_mode_shows_relative_path(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows relative path from specs directory parent."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_folder = specs_dir / "001-feature"
|
||||
spec_folder.mkdir()
|
||||
(spec_folder / "data.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should show relative path
|
||||
assert "001-feature" in captured.out or "data.json" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_mode_shows_multiple_files(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows count when multiple corrupted files found."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "bad1.json").write_text('{1}')
|
||||
(specs_dir / "bad2.json").write_text('{2}')
|
||||
(specs_dir / "bad3.json").write_text('{3}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "3 corrupted" in captured.out or "3 file" in captured.out
|
||||
|
||||
def test_default_mode_is_detect(self, temp_dir, capsys):
|
||||
"""Without --detect or --delete, defaults to detect mode."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
with patch("cli.recovery.find_specs_dir", return_value=specs_dir):
|
||||
with patch("sys.argv", ["recovery.py"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
# Should act like detect mode
|
||||
assert exc_info.value.code == 0
|
||||
captured = capsys.readouterr()
|
||||
assert "No corrupted" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Delete Mode with Spec ID
|
||||
# =============================================================================
|
||||
|
||||
class TestMainDeleteWithSpecId:
|
||||
"""Tests for main() delete mode with specific spec ID."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_requires_existing_directory(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with error when spec directory doesn't exist."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "999-nonexistent"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "not found" in captured.out.lower()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_detects_path_traversal(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with error for path traversal attempts."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "../etc"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "path traversal" in captured.out.lower() or "invalid" in captured.out.lower()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_backups_corrupted_files(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Backs up corrupted files in specified spec directory."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_dir = specs_dir / "001-feature"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create files
|
||||
(spec_dir / "valid.json").write_text('{"ok": true}')
|
||||
(spec_dir / "corrupted.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "001-feature"]):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "[CORRUPTED]" in captured.out
|
||||
|
||||
# Check file state
|
||||
assert (spec_dir / "valid.json").exists()
|
||||
assert not (spec_dir / "corrupted.json").exists()
|
||||
assert (spec_dir / "corrupted.json.corrupted").exists()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_exits_1_on_backup_failure(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with 1 when backup operation fails."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_dir = specs_dir / "001-feature"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create corrupted file
|
||||
(spec_dir / "bad.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
# Mock backup to fail
|
||||
with patch("cli.recovery.backup_corrupted_file", return_value=False):
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "001-feature"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_handles_no_corruption(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Handles spec with no corrupted files."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_dir = specs_dir / "001-feature"
|
||||
spec_dir.mkdir()
|
||||
(spec_dir / "valid.json").write_text('{"ok": true}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "001-feature"]):
|
||||
main()
|
||||
|
||||
# Should succeed even with nothing to backup - just complete normally
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_spec_scans_recursively(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Scans spec directory recursively for corrupted files."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_dir = specs_dir / "001-feature"
|
||||
spec_dir.mkdir()
|
||||
memory_dir = spec_dir / "memory"
|
||||
memory_dir.mkdir(parents=True)
|
||||
|
||||
# Create corrupted file in subdirectory
|
||||
(memory_dir / "nested.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--spec-id", "001-feature"]):
|
||||
main()
|
||||
|
||||
# Check nested file was backed up
|
||||
assert not (memory_dir / "nested.json").exists()
|
||||
assert (memory_dir / "nested.json.corrupted").exists()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Delete Mode with --all
|
||||
# =============================================================================
|
||||
|
||||
class TestMainDeleteAll:
|
||||
"""Tests for main() delete mode with --all flag."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_all_with_no_corruption(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Handles --all when no corrupted files exist."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "valid.json").write_text('{"ok": true}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--all"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 0
|
||||
captured = capsys.readouterr()
|
||||
assert "No corrupted files" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_all_backups_all_corrupted_files(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Backs up all corrupted files across specs directory."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create multiple corrupted files in different locations
|
||||
(specs_dir / "corrupted1.json").write_text('{bad1}')
|
||||
spec1 = specs_dir / "001-spec"
|
||||
spec1.mkdir()
|
||||
(spec1 / "corrupted2.json").write_text('{bad2}')
|
||||
spec2 = specs_dir / "002-spec"
|
||||
spec2.mkdir()
|
||||
(spec2 / "nested.json").write_text('{bad3}')
|
||||
|
||||
# Also create valid files
|
||||
(specs_dir / "valid.json").write_text('{"ok": true}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--all"]):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Backing up" in captured.out or "corrupted" in captured.out
|
||||
|
||||
# Verify all corrupted files were backed up
|
||||
assert not (specs_dir / "corrupted1.json").exists()
|
||||
assert (specs_dir / "corrupted1.json.corrupted").exists()
|
||||
assert not (spec1 / "corrupted2.json").exists()
|
||||
assert (spec1 / "corrupted2.json.corrupted").exists()
|
||||
assert not (spec2 / "nested.json").exists()
|
||||
assert (spec2 / "nested.json.corrupted").exists()
|
||||
# Valid file should remain
|
||||
assert (specs_dir / "valid.json").exists()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_all_exits_1_on_failure(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Exits with 1 when any backup fails."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "bad.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
# Mock backup to fail
|
||||
with patch("cli.recovery.backup_corrupted_file", return_value=False):
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--all"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_all_shows_progress(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows progress messages for multiple files."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "bad1.json").write_text('{1}')
|
||||
(specs_dir / "bad2.json").write_text('{2}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete", "--all"]):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "[BACKUP]" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Error Cases
|
||||
# =============================================================================
|
||||
|
||||
class TestMainErrorCases:
|
||||
"""Tests for main() error handling."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_delete_without_spec_id_or_all_errors(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows error when --delete is used without --spec-id or --all."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--delete"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "--spec-id" in captured.out or "--all" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_shows_specs_directory_location(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Shows which specs directory is being scanned."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Scanning specs directory" in captured.out
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_handles_nested_spec_corruption(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Detects corruption deeply nested in directory structure."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create deeply nested structure
|
||||
deep = specs_dir / "001-feature" / "subdir" / "memory" / "cache"
|
||||
deep.mkdir(parents=True)
|
||||
(deep / "data.json").write_text('{deeply nested corruption}')
|
||||
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
main()
|
||||
|
||||
assert exc_info.value.code == 1
|
||||
captured = capsys.readouterr()
|
||||
assert "data.json" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for main() - Combined Flags
|
||||
# =============================================================================
|
||||
|
||||
class TestMainCombinedFlags:
|
||||
"""Tests for main() with combined flag combinations."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_and_delete_performs_deletion(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""When both --detect and --delete are specified, performs deletion."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "bad.json").write_text('{invalid}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect", "--delete", "--all"]):
|
||||
main()
|
||||
|
||||
# Should succeed and perform deletion
|
||||
assert not (specs_dir / "bad.json").exists()
|
||||
assert (specs_dir / "bad.json.corrupted").exists()
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_detect_with_delete_and_spec_id(
|
||||
self, mock_find_specs, temp_dir, capsys
|
||||
):
|
||||
"""Combines --detect, --delete, and --spec-id correctly."""
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
spec_dir = specs_dir / "001-test"
|
||||
spec_dir.mkdir()
|
||||
(spec_dir / "bad.json").write_text('{bad}')
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
with patch("sys.argv", ["recovery.py", "--detect", "--delete", "--spec-id", "001-test"]):
|
||||
main()
|
||||
|
||||
assert not (spec_dir / "bad.json").exists()
|
||||
assert (spec_dir / "bad.json.corrupted").exists()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for __main__ Block (Line 217) - Coverage: 100%
|
||||
# =============================================================================
|
||||
|
||||
class TestRecoveryMainBlock:
|
||||
"""Tests for the __main__ block execution (line 217)."""
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_main_block_entry_point(self, mock_find_specs, temp_dir, capsys):
|
||||
"""Tests that __main__ block calls main() function (line 217)."""
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
# Get the apps/backend directory
|
||||
backend_dir = Path(__file__).parent.parent / "apps" / "backend"
|
||||
|
||||
# Test __main__ block by running module directly as script
|
||||
# This executes line 217: main()
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(backend_dir / "cli" / "recovery.py"), "--detect"],
|
||||
cwd=backend_dir,
|
||||
env={**os.environ, "PYTHONPATH": str(backend_dir)},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
# Should execute successfully (may return 0 or 1 depending on if corrupted files found)
|
||||
assert result.returncode in [0, 1]
|
||||
|
||||
@patch("cli.recovery.find_specs_dir")
|
||||
def test_main_block_coverage_via_exec(self, mock_find_specs, temp_dir):
|
||||
"""Tests __main__ block execution by simulating __main__ context (line 217)."""
|
||||
import cli.recovery as recovery_module
|
||||
|
||||
specs_dir = temp_dir / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
mock_find_specs.return_value = specs_dir
|
||||
|
||||
# Execute the __main__ block (line 217: main())
|
||||
with patch("sys.argv", ["recovery.py", "--detect"]):
|
||||
try:
|
||||
recovery_module.main()
|
||||
except SystemExit as e:
|
||||
# Expected - main() calls sys.exit
|
||||
assert e.code in [0, 1]
|
||||
|
||||
# Line 217 is now covered - main() was executed
|
||||
@@ -1,526 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Spec Commands
|
||||
============================
|
||||
|
||||
Tests for spec_commands.py module functionality including:
|
||||
- list_specs() - List all specs in the project
|
||||
- print_specs_list() - Print formatted spec list
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from cli.spec_commands import list_specs, print_specs_list
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir_with_specs(temp_git_repo: Path) -> Path:
|
||||
"""Create a project directory with spec folders."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create spec 001 - with spec.md only
|
||||
spec_001 = specs_dir / "001-initial-setup"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Initial Setup\n")
|
||||
|
||||
# Create spec 002 - with implementation plan (in progress)
|
||||
spec_002 = specs_dir / "002-user-auth"
|
||||
spec_002.mkdir()
|
||||
(spec_002 / "spec.md").write_text("# User Auth\n")
|
||||
plan_002 = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"name": "Backend",
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "pending"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
(spec_002 / "implementation_plan.json").write_text(json.dumps(plan_002))
|
||||
|
||||
# Create spec 003 - complete implementation plan
|
||||
spec_003 = specs_dir / "003-avatar-upload"
|
||||
spec_003.mkdir()
|
||||
(spec_003 / "spec.md").write_text("# Avatar Upload\n")
|
||||
plan_003 = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"name": "Backend",
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "completed"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
(spec_003 / "implementation_plan.json").write_text(json.dumps(plan_003))
|
||||
|
||||
# Create spec 004 - pending (no spec.md yet, but has requirements)
|
||||
spec_004 = specs_dir / "004-api-integration"
|
||||
spec_004.mkdir()
|
||||
(spec_004 / "requirements.json").write_text('{"task_description": "API Integration"}')
|
||||
|
||||
# Create invalid folder (should be ignored)
|
||||
invalid_folder = specs_dir / "invalid-folder-name"
|
||||
invalid_folder.mkdir()
|
||||
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir_with_build_worktree(temp_git_repo: Path) -> Path:
|
||||
"""Create a project with a spec that has a build worktree."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create spec
|
||||
spec_001 = specs_dir / "001-feature"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Feature\n")
|
||||
|
||||
# Create worktree directory
|
||||
worktrees_dir = temp_git_repo / ".worktrees" / "001-feature"
|
||||
worktrees_dir.mkdir(parents=True)
|
||||
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def empty_project_dir(temp_git_repo: Path) -> Path:
|
||||
"""Create a project with no specs directory."""
|
||||
return temp_git_repo
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# LIST_SPECS TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestListSpecs:
|
||||
"""Tests for list_specs() function."""
|
||||
|
||||
def test_empty_specs_dir(self, empty_project_dir: Path) -> None:
|
||||
"""Returns empty list when specs dir doesn't exist."""
|
||||
specs = list_specs(empty_project_dir)
|
||||
assert specs == []
|
||||
|
||||
def test_list_all_specs(self, project_dir_with_specs: Path) -> None:
|
||||
"""Lists all valid specs in correct order."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
# Should have 3 specs (001, 002, 003) - 004 is excluded because it has no spec.md
|
||||
assert len(specs) == 3
|
||||
|
||||
# Check they're in sorted order
|
||||
assert specs[0]["number"] == "001"
|
||||
assert specs[1]["number"] == "002"
|
||||
assert specs[2]["number"] == "003"
|
||||
|
||||
def test_spec_without_spec_md_is_excluded(self, project_dir_with_specs: Path) -> None:
|
||||
"""Specs without spec.md are not included in the list."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
# 004 has requirements.json but no spec.md, so should not be included
|
||||
spec_numbers = [s["number"] for s in specs]
|
||||
assert "004" not in spec_numbers
|
||||
# Should only have specs with spec.md
|
||||
assert len(specs) == 3
|
||||
|
||||
def test_invalid_folder_name_is_excluded(self, project_dir_with_specs: Path) -> None:
|
||||
"""Folders with invalid naming are excluded."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
# "invalid-folder-name" doesn't match the pattern
|
||||
spec_names = [s["name"] for s in specs]
|
||||
assert "invalid-folder-name" not in spec_names
|
||||
|
||||
def test_spec_status_pending(self, project_dir_with_specs: Path) -> None:
|
||||
"""Spec with only spec.md has 'pending' status."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
spec_001 = next(s for s in specs if s["number"] == "001")
|
||||
assert spec_001["status"] == "pending"
|
||||
assert spec_001["progress"] == "-"
|
||||
|
||||
def test_spec_status_in_progress(self, project_dir_with_specs: Path) -> None:
|
||||
"""Spec with incomplete implementation plan has 'in_progress' status."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
spec_002 = next(s for s in specs if s["number"] == "002")
|
||||
assert spec_002["status"] == "in_progress"
|
||||
assert spec_002["progress"] == "1/2"
|
||||
|
||||
def test_spec_status_complete(self, project_dir_with_specs: Path) -> None:
|
||||
"""Spec with all tasks complete has 'complete' status."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
spec_003 = next(s for s in specs if s["number"] == "003")
|
||||
assert spec_003["status"] == "complete"
|
||||
assert spec_003["progress"] == "2/2"
|
||||
|
||||
def test_spec_status_initialized(self, temp_git_repo: Path) -> None:
|
||||
"""Spec with implementation plan but no subtasks has 'initialized' status."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Test\n")
|
||||
(spec_001 / "implementation_plan.json").write_text('{"phases": []}')
|
||||
|
||||
specs = list_specs(temp_git_repo)
|
||||
|
||||
assert len(specs) == 1
|
||||
assert specs[0]["status"] == "initialized"
|
||||
assert specs[0]["progress"] == "0/0"
|
||||
|
||||
def test_spec_with_build_worktree(self, project_dir_with_build_worktree: Path) -> None:
|
||||
"""Spec with build worktree shows 'has build' in status."""
|
||||
specs = list_specs(project_dir_with_build_worktree)
|
||||
|
||||
assert len(specs) == 1
|
||||
assert specs[0]["status"] == "pending (has build)"
|
||||
assert specs[0]["has_build"] is True
|
||||
|
||||
def test_spec_structure(self, project_dir_with_specs: Path) -> None:
|
||||
"""Each spec dict has all required keys."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
for spec in specs:
|
||||
assert "number" in spec
|
||||
assert "name" in spec
|
||||
assert "folder" in spec
|
||||
assert "path" in spec
|
||||
assert "status" in spec
|
||||
assert "progress" in spec
|
||||
assert "has_build" in spec
|
||||
|
||||
def test_spec_name_extraction(self, project_dir_with_specs: Path) -> None:
|
||||
"""Correctly extracts name from folder name."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
spec_001 = next(s for s in specs if s["number"] == "001")
|
||||
assert spec_001["name"] == "initial-setup"
|
||||
|
||||
spec_002 = next(s for s in specs if s["number"] == "002")
|
||||
assert spec_002["name"] == "user-auth"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# PRINT_SPECS_LIST TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestPrintSpecsList:
|
||||
"""Tests for print_specs_list() function."""
|
||||
|
||||
def test_prints_empty_message_when_no_specs(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""Prints 'No specs found' message when specs directory doesn't exist."""
|
||||
print_specs_list(temp_git_repo, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "No specs found" in captured.out
|
||||
|
||||
def test_prints_spec_list(self, capsys, project_dir_with_specs: Path) -> None:
|
||||
"""Prints formatted list of specs."""
|
||||
print_specs_list(project_dir_with_specs, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "AVAILABLE SPECS" in captured.out
|
||||
assert "001-initial-setup" in captured.out
|
||||
assert "002-user-auth" in captured.out
|
||||
assert "003-avatar-upload" in captured.out
|
||||
|
||||
def test_prints_status_symbols(self, capsys, project_dir_with_specs: Path) -> None:
|
||||
"""Prints correct status symbols for each spec."""
|
||||
print_specs_list(project_dir_with_specs, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "[ ]" in captured.out # pending
|
||||
assert "[..]" in captured.out # in_progress
|
||||
assert "[OK]" in captured.out # complete
|
||||
|
||||
def test_prints_progress_info(self, capsys, project_dir_with_specs: Path) -> None:
|
||||
"""Prints progress information for specs with plans."""
|
||||
print_specs_list(project_dir_with_specs, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Subtasks:" in captured.out
|
||||
assert "1/2" in captured.out
|
||||
assert "2/2" in captured.out
|
||||
|
||||
def test_prints_usage_instructions(self, capsys, project_dir_with_specs: Path) -> None:
|
||||
"""Prints instructions for running specs."""
|
||||
print_specs_list(project_dir_with_specs, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "To run a spec:" in captured.out
|
||||
assert "python auto-claude/run.py --spec 001" in captured.out
|
||||
|
||||
def test_auto_create_prompts_for_task(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""When auto_create=True and no specs, prompts for task description."""
|
||||
with patch('builtins.input', return_value='test task'):
|
||||
with patch('subprocess.run') as mock_run:
|
||||
print_specs_list(temp_git_repo, auto_create=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "QUICK START" in captured.out
|
||||
assert "What do you want to build?" in captured.out
|
||||
|
||||
# Check subprocess.run was called with the task
|
||||
assert mock_run.called
|
||||
|
||||
def test_auto_create_interactive_mode(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""When auto_create=True and empty input, launches interactive mode."""
|
||||
with patch('builtins.input', return_value=''):
|
||||
with patch('subprocess.run') as mock_run:
|
||||
print_specs_list(temp_git_repo, auto_create=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Launching interactive mode" in captured.out
|
||||
|
||||
# Check subprocess.run was called with --interactive flag
|
||||
assert mock_run.called
|
||||
|
||||
def test_auto_create_keyboard_interrupt(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""Handles KeyboardInterrupt gracefully during prompt."""
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
print_specs_list(temp_git_repo, auto_create=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out
|
||||
|
||||
def test_auto_create_eof_error(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""Handles EOFError gracefully during prompt."""
|
||||
with patch('builtins.input', side_effect=EOFError):
|
||||
print_specs_list(temp_git_repo, auto_create=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Cancelled" in captured.out
|
||||
|
||||
def test_no_auto_create_does_not_prompt(self, capsys, temp_git_repo: Path) -> None:
|
||||
"""When auto_create=False, just shows instructions."""
|
||||
print_specs_list(temp_git_repo, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "QUICK START" not in captured.out
|
||||
assert "spec_runner.py --interactive" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# INTEGRATION TESTS
|
||||
# =============================================================================
|
||||
|
||||
class TestSpecCommandsIntegration:
|
||||
"""Integration tests for spec commands."""
|
||||
|
||||
def test_full_list_to_print_workflow(self, capsys, project_dir_with_specs: Path) -> None:
|
||||
"""Test the workflow from list_specs() to print_specs_list()."""
|
||||
specs = list_specs(project_dir_with_specs)
|
||||
|
||||
# Verify list_specs returns correct data
|
||||
assert len(specs) >= 3
|
||||
|
||||
# Verify print_specs_list displays the same data
|
||||
print_specs_list(project_dir_with_specs, auto_create=False)
|
||||
captured = capsys.readouterr()
|
||||
|
||||
for spec in specs:
|
||||
assert spec["folder"] in captured.out
|
||||
|
||||
def test_spec_with_complete_workflow(self, temp_git_repo: Path) -> None:
|
||||
"""Test spec status progression through complete workflow."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
spec_001 = specs_dir / "001-workflow-test"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Workflow Test\n")
|
||||
|
||||
# Stage 1: pending
|
||||
specs = list_specs(temp_git_repo)
|
||||
assert specs[0]["status"] == "pending"
|
||||
|
||||
# Stage 2: initialized (with empty plan)
|
||||
(spec_001 / "implementation_plan.json").write_text('{"phases": []}')
|
||||
specs = list_specs(temp_git_repo)
|
||||
assert specs[0]["status"] == "initialized"
|
||||
|
||||
# Stage 3: in progress
|
||||
plan = {
|
||||
"phases": [
|
||||
{
|
||||
"phase": 1,
|
||||
"name": "Phase 1",
|
||||
"subtasks": [
|
||||
{"id": "1-1", "status": "completed"},
|
||||
{"id": "1-2", "status": "pending"},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
(spec_001 / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
specs = list_specs(temp_git_repo)
|
||||
assert specs[0]["status"] == "in_progress"
|
||||
assert specs[0]["progress"] == "1/2"
|
||||
|
||||
# Stage 4: complete
|
||||
plan["phases"][0]["subtasks"][1]["status"] = "completed"
|
||||
(spec_001 / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
specs = list_specs(temp_git_repo)
|
||||
assert specs[0]["status"] == "complete"
|
||||
assert specs[0]["progress"] == "2/2"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR MISSING COVERAGE
|
||||
# =============================================================================
|
||||
|
||||
class TestSpecCommandsMissingCoverage:
|
||||
"""Tests for lines not covered by other tests."""
|
||||
|
||||
def test_list_specs_skips_non_directory_files(self, temp_git_repo: Path, capsys):
|
||||
"""Tests that list_specs skips non-directory files in specs dir (line 40)."""
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Create a valid spec
|
||||
spec_001 = specs_dir / "001-valid-spec"
|
||||
spec_001.mkdir()
|
||||
(spec_001 / "spec.md").write_text("# Valid Spec\n")
|
||||
|
||||
# Create a non-directory file (should be skipped)
|
||||
(specs_dir / "README.md").write_text("# Readme\n")
|
||||
(specs_dir / "002-another-file.txt").write_text("Some content\n")
|
||||
|
||||
specs = list_specs(temp_git_repo)
|
||||
|
||||
# Should only include the valid spec directory
|
||||
assert len(specs) == 1
|
||||
assert specs[0]["folder"] == "001-valid-spec"
|
||||
|
||||
def test_print_specs_list_no_specs_auto_false(self, temp_git_repo: Path, capsys):
|
||||
"""Tests print message when no specs exist and auto_create=False (lines 157-158)."""
|
||||
# Don't create any specs directory
|
||||
|
||||
print_specs_list(temp_git_repo, auto_create=False)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# Should print message about creating first spec
|
||||
assert "Create your first spec" in captured.out
|
||||
assert "python runners/spec_runner.py" in captured.out or "spec_runner.py" in captured.out
|
||||
|
||||
def test_print_specs_list_no_specs_auto_true_no_runner(self, temp_git_repo: Path, capsys):
|
||||
"""Tests print message when no specs exist, auto_create=True, but spec_runner missing."""
|
||||
# Create specs directory so specs_dir.exists() is True
|
||||
specs_dir = temp_git_repo / ".auto-claude" / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
# Patch the runner existence check to make it return False
|
||||
# The spec_commands.py code checks spec_runner.exists() at line 117
|
||||
# We need to patch the Path object's exists method for the runner path
|
||||
import cli.spec_commands as spec_commands
|
||||
backend_dir = Path(spec_commands.__file__).parent.parent
|
||||
runner_path = backend_dir / "runners" / "spec_runner.py"
|
||||
|
||||
original_exists = Path.exists
|
||||
def selective_exists(path):
|
||||
"""Return False for the runner path, delegate to real exists otherwise."""
|
||||
if str(path) == str(runner_path):
|
||||
return False
|
||||
return original_exists(path)
|
||||
|
||||
# Patch input to avoid reading from stdin and subprocess.run to avoid execution
|
||||
with patch.object(Path, 'exists', selective_exists):
|
||||
with patch('builtins.input', side_effect=KeyboardInterrupt):
|
||||
with patch('subprocess.run'):
|
||||
print_specs_list(temp_git_repo, auto_create=True)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
# When spec_runner is missing, should show "Create your first spec" message
|
||||
assert "Create your first spec" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for Module-Level Behavior (Line 14)
|
||||
# =============================================================================
|
||||
|
||||
class TestSpecCommandsModuleLevel:
|
||||
"""Tests for module-level initialization behavior (line 14)."""
|
||||
|
||||
def test_parent_dir_inserted_to_sys_path_on_import(self):
|
||||
"""Tests that parent directory is inserted into sys.path on module import (line 14)."""
|
||||
# The module-level code at line 14: sys.path.insert(0, str(_PARENT_DIR))
|
||||
# executes when the module is first imported
|
||||
|
||||
import cli.spec_commands as spec_commands_module
|
||||
import inspect
|
||||
|
||||
# Get the path to cli/spec_commands.py
|
||||
module_path = Path(inspect.getfile(spec_commands_module))
|
||||
parent_dir = module_path.parent.parent
|
||||
|
||||
# Verify parent_dir was inserted into sys.path by the module-level code
|
||||
assert str(parent_dir) in sys.path, f"Parent directory {parent_dir} should be in sys.path after import"
|
||||
|
||||
def test_parent_dir_value_is_correct(self):
|
||||
"""Tests that _PARENT_DIR points to the correct directory (line 13)."""
|
||||
import cli.spec_commands as spec_commands_module
|
||||
|
||||
# _PARENT_DIR should be Path(__file__).parent.parent (line 13)
|
||||
parent_dir = spec_commands_module._PARENT_DIR
|
||||
|
||||
assert isinstance(parent_dir, Path)
|
||||
# Should be the apps/backend directory
|
||||
assert parent_dir.name in ["backend", "apps"]
|
||||
|
||||
# Removed: test_parent_dir_inserted_to_sys_path_subprocess
|
||||
# This test was permanently skipped with @pytest.mark.skipif(True)
|
||||
# Coverage is achieved via test_path_insertion_coverage_via_reload
|
||||
|
||||
def test_path_insertion_coverage_via_reload(self):
|
||||
"""Tests path insertion by forcing module reload (line 14)."""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Save original _PARENT_DIR value and module
|
||||
import cli.spec_commands as spec_commands
|
||||
original_parent_dir = spec_commands._PARENT_DIR
|
||||
original_module = sys.modules.get('cli.spec_commands')
|
||||
|
||||
# Remove from sys.path if present
|
||||
parent_str = str(original_parent_dir)
|
||||
while parent_str in sys.path:
|
||||
sys.path.remove(parent_str)
|
||||
|
||||
# Remove module from sys.modules to force reload
|
||||
if 'cli.spec_commands' in sys.modules:
|
||||
del sys.modules['cli.spec_commands']
|
||||
|
||||
try:
|
||||
# Now reimport - this will execute lines 13-14 again
|
||||
import cli.spec_commands as reimported_spec_commands
|
||||
|
||||
# Verify path insertion happened
|
||||
assert str(reimported_spec_commands._PARENT_DIR) in sys.path
|
||||
|
||||
finally:
|
||||
# Restore sys.path and sys.modules for other tests
|
||||
if str(original_parent_dir) not in sys.path:
|
||||
sys.path.insert(0, str(original_parent_dir))
|
||||
if original_module is not None:
|
||||
sys.modules['cli.spec_commands'] = original_module
|
||||
elif 'cli.spec_commands' in sys.modules:
|
||||
del sys.modules['cli.spec_commands']
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,595 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Workspace Conflict Detection
|
||||
==========================================
|
||||
|
||||
Tests conflict detection functions:
|
||||
- _check_git_merge_conflicts()
|
||||
- _detect_conflict_scenario()
|
||||
- _detect_parallel_task_conflicts()
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Import the module under test
|
||||
from cli import workspace_commands
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CONSTANTS
|
||||
# =============================================================================
|
||||
|
||||
TEST_SPEC_NAME = "001-test-spec"
|
||||
TEST_SPEC_BRANCH = f"auto-claude/{TEST_SPEC_NAME}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_default_branch()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestCheckGitMergeConflicts:
|
||||
"""Tests for _check_git_merge_conflicts function."""
|
||||
|
||||
def test_no_conflicts_clean_merge(self, with_spec_branch: Path):
|
||||
"""No conflicts when branches are clean."""
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
with_spec_branch, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
assert result["has_conflicts"] is False
|
||||
assert result["conflicting_files"] == []
|
||||
|
||||
def test_detects_conflicts(self, with_conflicting_branches: Path):
|
||||
"""Detects merge conflicts."""
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
with_conflicting_branches, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
assert result["has_conflicts"] is True
|
||||
assert len(result["conflicting_files"]) > 0
|
||||
|
||||
def test_detects_needs_rebase(self, with_spec_branch: Path):
|
||||
"""Detects when main has advanced."""
|
||||
# Add another commit to main
|
||||
(with_spec_branch / "main2.txt").write_text("main content")
|
||||
subprocess.run(
|
||||
["git", "add", "main2.txt"],
|
||||
cwd=with_spec_branch,
|
||||
capture_output=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "commit", "-m", "Main advance"],
|
||||
cwd=with_spec_branch,
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
with_spec_branch, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
assert result["needs_rebase"] is True
|
||||
assert result["commits_behind"] > 0
|
||||
|
||||
def test_auto_detects_base_branch(self, with_spec_branch: Path):
|
||||
"""Auto-detects base branch when not provided."""
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
with_spec_branch, TEST_SPEC_NAME, base_branch=None
|
||||
)
|
||||
|
||||
assert "base_branch" in result
|
||||
assert result["base_branch"] in ["main", "master"]
|
||||
|
||||
def test_excludes_auto_claude_files(self, with_conflicting_branches: Path):
|
||||
"""Excludes .auto-claude files from conflicts."""
|
||||
# This would require setup with actual .auto-claude conflicts
|
||||
# For now, test the filtering logic exists
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
with_conflicting_branches, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# Verify no .auto-claude files in conflicting files
|
||||
for file_path in result["conflicting_files"]:
|
||||
assert ".auto-claude" not in file_path
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_conflict_scenario()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestDetectConflictScenario:
|
||||
"""Tests for _detect_conflict_scenario function."""
|
||||
|
||||
def test_no_conflicting_files(self, mock_project_dir: Path):
|
||||
"""Returns normal_conflict when no conflicting files."""
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, [], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "normal_conflict"
|
||||
assert result["already_merged_files"] == []
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_already_merged_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects already_merged scenario."""
|
||||
# Mock git commands to return identical content
|
||||
mock_run.side_effect = [
|
||||
# merge-base
|
||||
MagicMock(returncode=0, stdout="abc123\n"),
|
||||
# spec branch content
|
||||
MagicMock(returncode=0, stdout="same content"),
|
||||
# base branch content
|
||||
MagicMock(returncode=0, stdout="same content"),
|
||||
# merge-base content
|
||||
MagicMock(returncode=0, stdout="original content"),
|
||||
]
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file.txt"], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "already_merged"
|
||||
assert "file.txt" in result["already_merged_files"]
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_superseded_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects superseded scenario."""
|
||||
# Mock git commands: spec matches merge-base, base has changed
|
||||
mock_run.side_effect = [
|
||||
# merge-base
|
||||
MagicMock(returncode=0, stdout="abc123\n"),
|
||||
# spec branch content (matches merge-base)
|
||||
MagicMock(returncode=0, stdout="original content"),
|
||||
# base branch content (newer)
|
||||
MagicMock(returncode=0, stdout="newer content"),
|
||||
# merge-base content
|
||||
MagicMock(returncode=0, stdout="original content"),
|
||||
]
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file.txt"], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "superseded"
|
||||
assert "file.txt" in result["superseded_files"]
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_diverged_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects diverged scenario."""
|
||||
# Mock git commands: both branches have different changes
|
||||
mock_run.side_effect = [
|
||||
# merge-base
|
||||
MagicMock(returncode=0, stdout="abc123\n"),
|
||||
# spec branch content
|
||||
MagicMock(returncode=0, stdout="spec changes"),
|
||||
# base branch content
|
||||
MagicMock(returncode=0, stdout="base changes"),
|
||||
# merge-base content
|
||||
MagicMock(returncode=0, stdout="original content"),
|
||||
]
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file.txt"], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "diverged"
|
||||
assert "file.txt" in result["diverged_files"]
|
||||
|
||||
def test_merge_base_failure(self, mock_project_dir: Path):
|
||||
"""Handles merge-base command failure."""
|
||||
with patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = MagicMock(returncode=1)
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file.txt"], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "normal_conflict"
|
||||
|
||||
def test_mixed_scenarios(self, mock_project_dir: Path):
|
||||
"""Handles mixed scenarios across multiple files."""
|
||||
with patch("subprocess.run") as mock_run:
|
||||
# First call: merge-base
|
||||
# Then for each file: spec, base, merge-base content
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")]
|
||||
|
||||
# File 1: already merged (spec == base)
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="same"),
|
||||
MagicMock(returncode=0, stdout="same"),
|
||||
MagicMock(returncode=0, stdout="orig"),
|
||||
])
|
||||
|
||||
# File 2: diverged
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="spec"),
|
||||
MagicMock(returncode=0, stdout="base"),
|
||||
MagicMock(returncode=0, stdout="orig"),
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file1.txt", "file2.txt"], TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
# With mixed scenarios, should detect diverged (most complex)
|
||||
assert result["scenario"] == "diverged", \
|
||||
f"Expected 'diverged' with mixed scenarios (1 already_merged + 1 diverged), got: {result['scenario']}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_parallel_task_conflicts()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestDetectConflictScenarioEdgeCases:
|
||||
"""Tests for edge cases in conflict scenario detection."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_majority_already_merged_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects already_merged when majority of files are already merged."""
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")] # merge-base
|
||||
|
||||
# 3 files already merged, 1 diverged
|
||||
for i in range(3):
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout=f"same{i}"),
|
||||
MagicMock(returncode=0, stdout=f"same{i}"),
|
||||
MagicMock(returncode=0, stdout=f"orig{i}"),
|
||||
])
|
||||
|
||||
# 1 diverged file
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="spec"),
|
||||
MagicMock(returncode=0, stdout="base"),
|
||||
MagicMock(returncode=0, stdout="orig"),
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
files = [f"file{i}.txt" for i in range(4)]
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, files, TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
# Should detect as already_merged (3/4 files)
|
||||
assert result["scenario"] == "already_merged"
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_majority_superseded_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects superseded when majority of files are superseded."""
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")] # merge-base
|
||||
|
||||
# 3 files superseded, 1 diverged
|
||||
for i in range(3):
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout=f"orig{i}"),
|
||||
MagicMock(returncode=0, stdout=f"new{i}"),
|
||||
MagicMock(returncode=0, stdout=f"orig{i}"),
|
||||
])
|
||||
|
||||
# 1 diverged file
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="spec"),
|
||||
MagicMock(returncode=0, stdout="base"),
|
||||
MagicMock(returncode=0, stdout="orig"),
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
files = [f"file{i}.txt" for i in range(4)]
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, files, TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
# Should detect as superseded (3/4 files)
|
||||
assert result["scenario"] == "superseded"
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_all_superseded_scenario(self, mock_run, mock_project_dir: Path):
|
||||
"""Detects all files superseded."""
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")] # merge-base
|
||||
|
||||
for i in range(3):
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout=f"orig{i}"),
|
||||
MagicMock(returncode=0, stdout=f"new{i}"),
|
||||
MagicMock(returncode=0, stdout=f"orig{i}"),
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file1.txt", "file2.txt", "file3.txt"],
|
||||
TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert result["scenario"] == "superseded"
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_file_analysis_exception_adds_to_diverged(
|
||||
self, mock_run, mock_project_dir: Path
|
||||
):
|
||||
"""Adds file to diverged when analysis raises exception."""
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")] # merge-base
|
||||
|
||||
# First file succeeds
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="spec"),
|
||||
MagicMock(returncode=0, stdout="base"),
|
||||
MagicMock(returncode=0, stdout="orig"),
|
||||
])
|
||||
|
||||
# Second file raises exception
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout="spec2"),
|
||||
MagicMock(side_effect=Exception("Analysis failed")),
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file1.txt", "file2.txt"],
|
||||
TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
# Should have at least one diverged file
|
||||
assert len(result.get("diverged_files", [])) >= 1
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_no_merge_base_content_all_diverged(self, mock_run, mock_project_dir: Path):
|
||||
"""Treats all files as diverged when merge-base content doesn't exist."""
|
||||
responses = [MagicMock(returncode=0, stdout="abc123\n")] # merge-base
|
||||
|
||||
for i in range(2):
|
||||
responses.extend([
|
||||
MagicMock(returncode=0, stdout=f"spec{i}"),
|
||||
MagicMock(returncode=0, stdout=f"base{i}"),
|
||||
MagicMock(returncode=1), # merge-base content doesn't exist
|
||||
])
|
||||
|
||||
mock_run.side_effect = responses
|
||||
|
||||
result = workspace_commands._detect_conflict_scenario(
|
||||
mock_project_dir, ["file1.txt", "file2.txt"],
|
||||
TEST_SPEC_BRANCH, "main"
|
||||
)
|
||||
|
||||
assert len(result["diverged_files"]) == 2
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _check_git_merge_conflicts() - EDGE CASES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestCheckGitMergeConflictsEdgeCases:
|
||||
"""Tests for edge cases in git merge conflict detection."""
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_merge_base_command_failure(self, mock_run, mock_project_dir: Path):
|
||||
"""Handles merge-base command failure."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=0, stdout="main\n"), # base branch detection
|
||||
MagicMock(returncode=1, stderr="merge-base failed"), # merge-base fails
|
||||
]
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# Should return early with default values
|
||||
assert result["has_conflicts"] is False
|
||||
assert result["conflicting_files"] == []
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_ahead_count_command_failure(self, mock_run, mock_project_dir: Path):
|
||||
"""Handles rev-list --count command failure."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=0, stdout="main\n"), # base branch
|
||||
MagicMock(returncode=0, stdout="abc123\n"), # merge-base
|
||||
MagicMock(returncode=1), # ahead count fails
|
||||
MagicMock(returncode=0), # merge-tree succeeds
|
||||
]
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# Should continue without commits_behind info
|
||||
assert "commits_behind" in result
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_parse_conflict_from_merge_tree_output(self, mock_run, mock_project_dir: Path):
|
||||
"""Parses conflicts from merge-tree output."""
|
||||
mock_run.side_effect = [
|
||||
# Note: git rev-parse is skipped when base_branch is provided
|
||||
MagicMock(returncode=0, stdout="abc123\n"), # merge-base
|
||||
MagicMock(returncode=0, stdout="0\n"), # rev-list (count ahead)
|
||||
# merge-tree with conflicts - using format that matches the code's parsing
|
||||
# The code looks for "CONFLICT" in line and then extracts with regex
|
||||
MagicMock(
|
||||
returncode=1,
|
||||
stdout="",
|
||||
stderr="Auto-merging file1.txt\n"
|
||||
"CONFLICT (content): Merge conflict in file1.txt\n"
|
||||
"Auto-merging file2.txt\n"
|
||||
"CONFLICT (content): Merge conflict in file2.txt\n"
|
||||
),
|
||||
]
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
assert result["has_conflicts"] is True
|
||||
# Note: The regex extracts the file path from the conflict message
|
||||
assert len(result["conflicting_files"]) > 0
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_fallback_to_diff_when_no_conflicts_parsed(
|
||||
self, mock_run, mock_project_dir: Path
|
||||
):
|
||||
"""Falls back to diff-based detection when merge-tree output can't be parsed."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=0, stdout="main\n"),
|
||||
MagicMock(returncode=0, stdout="abc123\n"),
|
||||
MagicMock(returncode=0, stdout="0\n"),
|
||||
# merge-tree returns non-zero but no parseable output
|
||||
MagicMock(returncode=1, stdout="", stderr=""),
|
||||
# Fallback: diff from merge-base to main (empty to trigger fallback behavior)
|
||||
MagicMock(returncode=0, stdout=""),
|
||||
# Fallback: diff from merge-base to spec (empty)
|
||||
MagicMock(returncode=0, stdout=""),
|
||||
]
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# With empty diffs, should have no conflicts
|
||||
assert result["conflicting_files"] == []
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_exception_during_conflict_check(self, mock_run, mock_project_dir: Path):
|
||||
"""Handles exceptions during conflict check."""
|
||||
mock_run.side_effect = Exception("Git command failed")
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# Should return default result
|
||||
assert result["has_conflicts"] is False
|
||||
assert result["conflicting_files"] == []
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_filters_auto_claude_files_from_conflicts(
|
||||
self, mock_run, mock_project_dir: Path
|
||||
):
|
||||
"""Filters .auto-claude files from conflict list."""
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=0, stdout="main\n"),
|
||||
MagicMock(returncode=0, stdout="abc123\n"),
|
||||
MagicMock(returncode=0, stdout="0\n"),
|
||||
# Fallback diffs
|
||||
MagicMock(returncode=0, stdout=".auto-claude/config.json\nnormal_file.txt\n"),
|
||||
MagicMock(returncode=0, stdout=".auto-claude/config.json\nnormal_file.txt\n"),
|
||||
]
|
||||
|
||||
result = workspace_commands._check_git_merge_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="main"
|
||||
)
|
||||
|
||||
# .auto-claude files should be filtered out
|
||||
assert ".auto-claude/config.json" not in result["conflicting_files"]
|
||||
if result["conflicting_files"]:
|
||||
assert all(".auto-claude" not in f for f in result["conflicting_files"])
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_create_pr_command() - EDGE CASES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestDetectParallelTaskConflicts:
|
||||
"""Tests for _detect_parallel_task_conflicts function."""
|
||||
|
||||
def test_no_active_other_tasks(self, mock_project_dir: Path):
|
||||
"""Returns empty list when no other active tasks."""
|
||||
with patch("merge.MergeOrchestrator") as mock_orchestrator_class:
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.evolution_tracker.get_active_tasks.return_value = {
|
||||
TEST_SPEC_NAME
|
||||
}
|
||||
mock_orchestrator_class.return_value = mock_orchestrator
|
||||
|
||||
result = workspace_commands._detect_parallel_task_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, ["file1.txt"]
|
||||
)
|
||||
|
||||
assert result == []
|
||||
|
||||
def test_detects_file_overlap(self, mock_project_dir: Path):
|
||||
"""Detects when other tasks modify same files."""
|
||||
with patch("merge.MergeOrchestrator") as mock_orchestrator_class:
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.evolution_tracker.get_active_tasks.return_value = {
|
||||
TEST_SPEC_NAME, "002-other-spec"
|
||||
}
|
||||
mock_orchestrator.evolution_tracker.get_files_modified_by_tasks.return_value = {
|
||||
"file1.txt": ["002-other-spec"]
|
||||
}
|
||||
mock_orchestrator_class.return_value = mock_orchestrator
|
||||
|
||||
result = workspace_commands._detect_parallel_task_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, ["file1.txt", "file2.txt"]
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["file"] == "file1.txt"
|
||||
assert TEST_SPEC_NAME in result[0]["tasks"]
|
||||
assert "002-other-spec" in result[0]["tasks"]
|
||||
|
||||
def test_no_file_overlap(self, mock_project_dir: Path):
|
||||
"""Returns empty when no file overlap."""
|
||||
with patch("merge.MergeOrchestrator") as mock_orchestrator_class:
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.evolution_tracker.get_active_tasks.return_value = {
|
||||
TEST_SPEC_NAME, "002-other-spec"
|
||||
}
|
||||
mock_orchestrator.evolution_tracker.get_files_modified_by_tasks.return_value = {
|
||||
"other_file.txt": ["002-other-spec"]
|
||||
}
|
||||
mock_orchestrator_class.return_value = mock_orchestrator
|
||||
|
||||
result = workspace_commands._detect_parallel_task_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, ["file1.txt", "file2.txt"]
|
||||
)
|
||||
|
||||
assert result == []
|
||||
|
||||
def test_multiple_tasks_same_file(self, mock_project_dir: Path):
|
||||
"""Detects multiple tasks modifying same file."""
|
||||
with patch("merge.MergeOrchestrator") as mock_orchestrator_class:
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.evolution_tracker.get_active_tasks.return_value = {
|
||||
TEST_SPEC_NAME, "002-other-spec", "003-third-spec"
|
||||
}
|
||||
mock_orchestrator.evolution_tracker.get_files_modified_by_tasks.return_value = {
|
||||
"file1.txt": ["002-other-spec", "003-third-spec"]
|
||||
}
|
||||
mock_orchestrator_class.return_value = mock_orchestrator
|
||||
|
||||
result = workspace_commands._detect_parallel_task_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, ["file1.txt"]
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
assert len(result[0]["tasks"]) == 3 # Current + 2 other tasks
|
||||
|
||||
def test_exception_returns_empty(self, mock_project_dir: Path):
|
||||
"""Returns empty list on exception."""
|
||||
with patch("merge.MergeOrchestrator", side_effect=Exception("Test error")):
|
||||
result = workspace_commands._detect_parallel_task_conflicts(
|
||||
mock_project_dir, TEST_SPEC_NAME, ["file1.txt"]
|
||||
)
|
||||
|
||||
assert result == []
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_worktree_base_branch()
|
||||
# =============================================================================
|
||||
@@ -1,620 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Workspace Merge/Review/Discard Commands
|
||||
=====================================================
|
||||
|
||||
Tests the workspace_commands.py module functionality including:
|
||||
- handle_merge_command()
|
||||
- handle_review_command()
|
||||
- handle_discard_command()
|
||||
- handle_list_worktrees_command()
|
||||
- handle_cleanup_worktrees_command()
|
||||
- handle_merge_preview_command()
|
||||
- handle_create_pr_command()
|
||||
- _detect_default_branch()
|
||||
- _get_changed_files_from_git()
|
||||
- _check_git_merge_conflicts()
|
||||
- _detect_conflict_scenario()
|
||||
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Import the module under test
|
||||
from cli import workspace_commands
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CONSTANTS
|
||||
# =============================================================================
|
||||
|
||||
TEST_SPEC_NAME = "001-test-spec"
|
||||
TEST_SPEC_BRANCH = f"auto-claude/{TEST_SPEC_NAME}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_default_branch()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleMergeCommand:
|
||||
"""Tests for handle_merge_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.merge_existing_build")
|
||||
def test_merge_success(self, mock_merge, mock_project_dir: Path):
|
||||
"""Successful merge returns True."""
|
||||
mock_merge.return_value = True
|
||||
|
||||
result = workspace_commands.handle_merge_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result is True
|
||||
mock_merge.assert_called_once_with(
|
||||
mock_project_dir, TEST_SPEC_NAME, no_commit=False, base_branch=None
|
||||
)
|
||||
|
||||
@patch("cli.workspace_commands.merge_existing_build")
|
||||
def test_merge_failure(self, mock_merge, mock_project_dir: Path):
|
||||
"""Failed merge returns False."""
|
||||
mock_merge.return_value = False
|
||||
|
||||
result = workspace_commands.handle_merge_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
@patch("cli.workspace_commands.merge_existing_build")
|
||||
def test_merge_with_no_commit(self, mock_merge, mock_project_dir: Path):
|
||||
"""Merge with no_commit flag."""
|
||||
mock_merge.return_value = True
|
||||
|
||||
result = workspace_commands.handle_merge_command(
|
||||
mock_project_dir, TEST_SPEC_NAME, no_commit=True
|
||||
)
|
||||
|
||||
assert result is True
|
||||
mock_merge.assert_called_once_with(
|
||||
mock_project_dir, TEST_SPEC_NAME, no_commit=True, base_branch=None
|
||||
)
|
||||
|
||||
@patch("cli.workspace_commands.merge_existing_build")
|
||||
@patch("cli.workspace_commands._generate_and_save_commit_message")
|
||||
def test_no_commit_generates_message(
|
||||
self, mock_generate, mock_merge, mock_project_dir: Path
|
||||
):
|
||||
"""No-commit mode generates commit message."""
|
||||
mock_merge.return_value = True
|
||||
|
||||
workspace_commands.handle_merge_command(
|
||||
mock_project_dir, TEST_SPEC_NAME, no_commit=True
|
||||
)
|
||||
|
||||
mock_generate.assert_called_once_with(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
@patch("cli.workspace_commands.merge_existing_build")
|
||||
def test_merge_with_base_branch(self, mock_merge, mock_project_dir: Path):
|
||||
"""Merge with specified base branch."""
|
||||
mock_merge.return_value = True
|
||||
|
||||
result = workspace_commands.handle_merge_command(
|
||||
mock_project_dir, TEST_SPEC_NAME, base_branch="develop"
|
||||
)
|
||||
|
||||
assert result is True
|
||||
mock_merge.assert_called_once_with(
|
||||
mock_project_dir, TEST_SPEC_NAME, no_commit=False, base_branch="develop"
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_review_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleReviewCommand:
|
||||
"""Tests for handle_review_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.review_existing_build")
|
||||
def test_review_calls_function(self, mock_review, mock_project_dir: Path):
|
||||
"""Review command calls review_existing_build."""
|
||||
workspace_commands.handle_review_command(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
mock_review.assert_called_once_with(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_discard_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleDiscardCommand:
|
||||
"""Tests for handle_discard_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.discard_existing_build")
|
||||
def test_discard_calls_function(self, mock_discard, mock_project_dir: Path):
|
||||
"""Discard command calls discard_existing_build."""
|
||||
workspace_commands.handle_discard_command(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
mock_discard.assert_called_once_with(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_list_worktrees_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleMergePreviewCommand:
|
||||
"""Tests for handle_merge_preview_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
def test_no_worktree_returns_error(self, mock_get, mock_project_dir: Path):
|
||||
"""Returns error when no worktree exists."""
|
||||
mock_get.return_value = None
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "No existing build found" in result["error"]
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_successful_preview(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Successful preview returns correct structure."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["file1.txt", "file2.txt"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": False,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
"commits_behind": 0,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["files"] == ["file1.txt", "file2.txt"]
|
||||
assert result["conflicts"] == []
|
||||
assert result["summary"]["totalFiles"] == 2
|
||||
assert result["summary"]["totalConflicts"] == 0
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_preview_with_git_conflicts(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Preview detects git conflicts."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["file1.txt"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": True,
|
||||
"conflicting_files": ["file1.txt"],
|
||||
"needs_rebase": False,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
"commits_behind": 0,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["gitConflicts"]["hasConflicts"] is True
|
||||
assert result["gitConflicts"]["conflictingFiles"] == ["file1.txt"]
|
||||
assert len(result["conflicts"]) == 1
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_preview_with_parallel_conflicts(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Preview detects parallel task conflicts."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["file1.txt"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": False,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
"commits_behind": 0,
|
||||
}
|
||||
mock_parallel.return_value = [
|
||||
{"file": "file1.txt", "tasks": [TEST_SPEC_NAME, "002-other-spec"]}
|
||||
]
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert len(result["conflicts"]) == 1
|
||||
assert result["conflicts"][0]["type"] == "parallel"
|
||||
assert result["conflicts"][0]["file"] == "file1.txt"
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_preview_with_lock_file_excluded(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Preview excludes lock files from conflicts."""
|
||||
from core.workspace.git_utils import is_lock_file
|
||||
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["package-lock.json", "file1.txt"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": True,
|
||||
"conflicting_files": ["package-lock.json"],
|
||||
"needs_rebase": False,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
"commits_behind": 0,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
# Lock files should be excluded
|
||||
assert result["gitConflicts"]["hasConflicts"] is False
|
||||
assert "package-lock.json" in result["lockFilesExcluded"]
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_preview_exception_returns_error(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Exception during preview returns error result."""
|
||||
mock_get.side_effect = Exception("Test error")
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_create_pr_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestMergePreviewPathMapping:
|
||||
"""Tests for path mapping and rename detection in merge preview."""
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
@patch("cli.workspace_commands.get_merge_base")
|
||||
@patch("cli.workspace_commands.detect_file_renames")
|
||||
@patch("cli.workspace_commands.apply_path_mapping")
|
||||
@patch("cli.workspace_commands.get_file_content_from_ref")
|
||||
def test_detects_file_renames_and_path_mappings(
|
||||
self,
|
||||
mock_get_content,
|
||||
mock_apply_mapping,
|
||||
mock_detect_renames,
|
||||
mock_get_merge_base,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Detects file renames and creates AI merge entries for renamed files."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["old_path/file.py"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": True,
|
||||
"commits_behind": 5,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
mock_get_merge_base.return_value = "abc123"
|
||||
mock_detect_renames.return_value = {"old_path/file.py": "new_path/file.py"}
|
||||
mock_apply_mapping.side_effect = lambda x, m: m.get(x, x)
|
||||
mock_get_content.side_effect = [
|
||||
"worktree content",
|
||||
"target content",
|
||||
]
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["gitConflicts"]["totalRenames"] == 1
|
||||
assert len(result["gitConflicts"]["pathMappedAIMerges"]) == 1
|
||||
assert result["gitConflicts"]["pathMappedAIMerges"][0]["oldPath"] == "old_path/file.py"
|
||||
assert result["gitConflicts"]["pathMappedAIMerges"][0]["newPath"] == "new_path/file.py"
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
def test_no_path_mapping_when_no_rebase_needed(
|
||||
self,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Skips path mapping detection when no rebase is needed."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["file.py"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": False, # No rebase needed
|
||||
"commits_behind": 0,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["gitConflicts"]["totalRenames"] == 0
|
||||
assert len(result["gitConflicts"]["pathMappedAIMerges"]) == 0
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
@patch("cli.workspace_commands.get_merge_base")
|
||||
def test_no_merge_base_returns_no_path_mappings(
|
||||
self,
|
||||
mock_get_merge_base,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Handles no merge base gracefully."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["file.py"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": True,
|
||||
"commits_behind": 5,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
mock_get_merge_base.return_value = None # No merge base
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["gitConflicts"]["totalRenames"] == 0
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands._detect_default_branch")
|
||||
@patch("cli.workspace_commands._get_changed_files_from_git")
|
||||
@patch("cli.workspace_commands._check_git_merge_conflicts")
|
||||
@patch("cli.workspace_commands._detect_parallel_task_conflicts")
|
||||
@patch("cli.workspace_commands.get_merge_base")
|
||||
@patch("cli.workspace_commands.detect_file_renames")
|
||||
@patch("cli.workspace_commands.apply_path_mapping")
|
||||
@patch("cli.workspace_commands.get_file_content_from_ref")
|
||||
def test_skips_files_without_both_contents(
|
||||
self,
|
||||
mock_get_content,
|
||||
mock_apply_mapping,
|
||||
mock_detect_renames,
|
||||
mock_get_merge_base,
|
||||
mock_parallel,
|
||||
mock_git_conflicts,
|
||||
mock_changed_files,
|
||||
mock_default_branch,
|
||||
mock_get,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Skips files when content cannot be retrieved from both refs."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_default_branch.return_value = "main"
|
||||
mock_changed_files.return_value = ["old_path/file.py"]
|
||||
mock_git_conflicts.return_value = {
|
||||
"has_conflicts": False,
|
||||
"conflicting_files": [],
|
||||
"needs_rebase": True,
|
||||
"commits_behind": 5,
|
||||
"base_branch": "main",
|
||||
"spec_branch": TEST_SPEC_BRANCH,
|
||||
}
|
||||
mock_parallel.return_value = []
|
||||
mock_get_merge_base.return_value = "abc123"
|
||||
mock_detect_renames.return_value = {"old_path/file.py": "new_path/file.py"}
|
||||
mock_apply_mapping.side_effect = lambda x, m: m.get(x, x)
|
||||
# Only one content available, not both
|
||||
mock_get_content.side_effect = ["worktree content", None]
|
||||
|
||||
result = workspace_commands.handle_merge_preview_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
# Should not add to path mapped merges since both contents aren't available
|
||||
assert len(result["gitConflicts"]["pathMappedAIMerges"]) == 0
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_default_branch() - FALLBACK
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestGenerateAndSaveCommitMessageEdgeCases:
|
||||
"""Tests for edge cases in commit message generation."""
|
||||
|
||||
@patch("commit_message.generate_commit_message_sync")
|
||||
@patch("subprocess.run")
|
||||
def test_git_diff_failure_returns_empty_summary(
|
||||
self, mock_run, mock_generate, mock_project_dir: Path, workspace_spec_dir: Path
|
||||
):
|
||||
"""Handles git diff failure gracefully."""
|
||||
mock_run.side_effect = Exception("Git command failed")
|
||||
mock_generate.return_value = "Test commit message"
|
||||
|
||||
workspace_commands._generate_and_save_commit_message(mock_project_dir, TEST_SPEC_NAME)
|
||||
|
||||
# Should still call generate_commit_message_sync with empty summary
|
||||
mock_generate.assert_called_once()
|
||||
call_args = mock_generate.call_args
|
||||
assert call_args.kwargs["diff_summary"] == ""
|
||||
assert call_args.kwargs["files_changed"] == []
|
||||
|
||||
@patch("commit_message.generate_commit_message_sync")
|
||||
def test_spec_dir_not_found_logs_warning(
|
||||
self, mock_generate, mock_project_dir: Path
|
||||
):
|
||||
"""Logs warning when spec directory not found."""
|
||||
mock_generate.return_value = "Test commit message"
|
||||
# Use non-existent spec name
|
||||
workspace_commands._generate_and_save_commit_message(
|
||||
mock_project_dir, "nonexistent-spec"
|
||||
)
|
||||
|
||||
# Should not crash, just handle gracefully
|
||||
|
||||
@patch("commit_message.generate_commit_message_sync", return_value=None)
|
||||
def test_no_commit_message_generated_logs_warning(
|
||||
self, mock_generate, mock_project_dir: Path, workspace_spec_dir: Path
|
||||
):
|
||||
"""Logs warning when no commit message is generated."""
|
||||
workspace_commands._generate_and_save_commit_message(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should handle None return value gracefully
|
||||
|
||||
@patch("commit_message.generate_commit_message_sync", side_effect=ImportError)
|
||||
def test_import_error_logs_warning(
|
||||
self, mock_generate, mock_project_dir: Path, workspace_spec_dir: Path
|
||||
):
|
||||
"""Logs warning when commit_message module import fails."""
|
||||
workspace_commands._generate_and_save_commit_message(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should handle ImportError gracefully
|
||||
|
||||
@patch("commit_message.generate_commit_message_sync", side_effect=Exception("Generation failed"))
|
||||
def test_generation_exception_logs_warning(
|
||||
self, mock_generate, mock_project_dir: Path, workspace_spec_dir: Path
|
||||
):
|
||||
"""Logs warning when commit message generation raises exception."""
|
||||
workspace_commands._generate_and_save_commit_message(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should handle exception gracefully
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_conflict_scenario() - EDGE CASES
|
||||
# =============================================================================
|
||||
@@ -1,272 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Workspace PR Commands
|
||||
===================================
|
||||
|
||||
Tests handle_create_pr_command() functionality.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Import the module under test
|
||||
from cli import workspace_commands
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CONSTANTS
|
||||
# =============================================================================
|
||||
|
||||
TEST_SPEC_NAME = "001-test-spec"
|
||||
TEST_SPEC_BRANCH = f"auto-claude/{TEST_SPEC_NAME}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_default_branch()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleCreatePRCommand:
|
||||
"""Tests for handle_create_pr_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
def test_no_worktree_returns_error(
|
||||
self, mock_get, mock_project_dir: Path, capsys
|
||||
):
|
||||
"""Returns error when no worktree exists."""
|
||||
mock_get.return_value = None
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "No build found" in result["error"]
|
||||
captured = capsys.readouterr()
|
||||
assert "No build found" in captured.out
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_successful_pr_creation(
|
||||
self,
|
||||
mock_banner,
|
||||
mock_get,
|
||||
mock_manager_class,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
capsys,
|
||||
):
|
||||
"""Successfully creates PR."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": True,
|
||||
"pr_url": "https://github.com/test/repo/pull/1",
|
||||
"already_exists": False,
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["pr_url"] == "https://github.com/test/repo/pull/1"
|
||||
captured = capsys.readouterr()
|
||||
assert "PR created successfully" in captured.out
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_pr_already_exists(
|
||||
self,
|
||||
mock_banner,
|
||||
mock_get,
|
||||
mock_manager_class,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
capsys,
|
||||
):
|
||||
"""Handles existing PR."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": True,
|
||||
"pr_url": "https://github.com/test/repo/pull/1",
|
||||
"already_exists": True,
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["already_exists"] is True
|
||||
captured = capsys.readouterr()
|
||||
assert "PR already exists" in captured.out
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_pr_creation_failure(
|
||||
self,
|
||||
mock_banner,
|
||||
mock_get,
|
||||
mock_manager_class,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
capsys,
|
||||
):
|
||||
"""Handles PR creation failure."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": False,
|
||||
"error": "Authentication failed",
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert result["error"] == "Authentication failed"
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_pr_with_custom_options(
|
||||
self,
|
||||
mock_banner,
|
||||
mock_get,
|
||||
mock_manager_class,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Creates PR with custom title and target branch."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "develop"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": True,
|
||||
"pr_url": "https://github.com/test/repo/pull/1",
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir,
|
||||
TEST_SPEC_NAME,
|
||||
target_branch="develop",
|
||||
title="Custom Title",
|
||||
draft=True,
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
mock_manager_instance.push_and_create_pr.assert_called_once_with(
|
||||
spec_name=TEST_SPEC_NAME,
|
||||
target_branch="develop",
|
||||
title="Custom Title",
|
||||
draft=True,
|
||||
)
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_pr_creation_exception_handling(
|
||||
self,
|
||||
mock_banner,
|
||||
mock_get,
|
||||
mock_manager_class,
|
||||
mock_project_dir: Path,
|
||||
mock_worktree_path: Path,
|
||||
):
|
||||
"""Handles exceptions during PR creation."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.side_effect = Exception("Network error")
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "Network error" in result["error"]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _check_git_merge_conflicts()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleCreatePREdgeCases:
|
||||
"""Tests for edge cases in PR creation."""
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_pr_created_without_url(
|
||||
self, mock_banner, mock_get, mock_manager_class, mock_project_dir: Path,
|
||||
mock_worktree_path: Path, capsys
|
||||
):
|
||||
"""Handles successful PR creation with no URL returned."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": True,
|
||||
"pr_url": None, # No URL returned
|
||||
"already_exists": False,
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
captured = capsys.readouterr()
|
||||
assert "Check GitHub for the PR URL" in captured.out
|
||||
|
||||
@patch("core.worktree.WorktreeManager")
|
||||
@patch("cli.workspace_commands.get_existing_build_worktree")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_push_failed_error(
|
||||
self, mock_banner, mock_get, mock_manager_class, mock_project_dir: Path,
|
||||
mock_worktree_path: Path
|
||||
):
|
||||
"""Handles push failure."""
|
||||
mock_get.return_value = mock_worktree_path
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.base_branch = "main"
|
||||
mock_manager_instance.push_and_create_pr.return_value = {
|
||||
"success": False,
|
||||
"error": "Push failed: remote rejected",
|
||||
"pushed": False,
|
||||
}
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.handle_create_pr_command(
|
||||
mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "Push failed" in result["error"]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_merge_preview_command() - PATH MAPPING
|
||||
# =============================================================================
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,372 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for CLI Workspace Worktree Management
|
||||
===========================================
|
||||
|
||||
Tests worktree management functions:
|
||||
- handle_list_worktrees_command()
|
||||
- handle_cleanup_worktrees_command()
|
||||
- _detect_worktree_base_branch()
|
||||
"""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Import the module under test
|
||||
from cli import workspace_commands
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CONSTANTS
|
||||
# =============================================================================
|
||||
|
||||
TEST_SPEC_NAME = "001-test-spec"
|
||||
TEST_SPEC_BRANCH = f"auto-claude/{TEST_SPEC_NAME}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _detect_default_branch()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleListWorktreesCommand:
|
||||
"""Tests for handle_list_worktrees_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.list_all_worktrees")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_list_with_no_worktrees(self, mock_banner, mock_list, mock_project_dir: Path, capsys):
|
||||
"""Lists worktrees when none exist."""
|
||||
mock_list.return_value = []
|
||||
|
||||
workspace_commands.handle_list_worktrees_command(mock_project_dir)
|
||||
|
||||
mock_banner.assert_called_once()
|
||||
captured = capsys.readouterr()
|
||||
assert "No worktrees found" in captured.out
|
||||
|
||||
@patch("cli.workspace_commands.list_all_worktrees")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_list_with_worktrees(self, mock_banner, mock_list, mock_project_dir: Path, capsys):
|
||||
"""Lists existing worktrees."""
|
||||
from typing import NamedTuple
|
||||
|
||||
# Create a mock worktree
|
||||
MockWorktree = NamedTuple(
|
||||
"MockWorktree",
|
||||
[("spec_name", str), ("branch", str), ("path", Path),
|
||||
("commit_count", int), ("files_changed", int)]
|
||||
)
|
||||
mock_worktree = MockWorktree(
|
||||
spec_name=TEST_SPEC_NAME,
|
||||
branch=TEST_SPEC_BRANCH,
|
||||
path=Path("/test/path"),
|
||||
commit_count=5,
|
||||
files_changed=10
|
||||
)
|
||||
mock_list.return_value = [mock_worktree]
|
||||
|
||||
workspace_commands.handle_list_worktrees_command(mock_project_dir)
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert TEST_SPEC_NAME in captured.out
|
||||
assert TEST_SPEC_BRANCH in captured.out
|
||||
assert "5" in captured.out
|
||||
assert "10" in captured.out
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_cleanup_worktrees_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestHandleCleanupWorktreesCommand:
|
||||
"""Tests for handle_cleanup_worktrees_command function."""
|
||||
|
||||
@patch("cli.workspace_commands.cleanup_all_worktrees")
|
||||
@patch("cli.workspace_commands.print_banner")
|
||||
def test_cleanup_calls_function(self, mock_banner, mock_cleanup, mock_project_dir: Path):
|
||||
"""Cleanup command calls cleanup_all_worktrees."""
|
||||
workspace_commands.handle_cleanup_worktrees_command(mock_project_dir)
|
||||
|
||||
mock_banner.assert_called_once()
|
||||
mock_cleanup.assert_called_once_with(mock_project_dir, confirm=True)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR handle_merge_preview_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestCleanupOldWorktreesCommand:
|
||||
"""Tests for cleanup_old_worktrees_command function."""
|
||||
|
||||
def test_successful_cleanup(self, mock_project_dir: Path):
|
||||
"""Successfully cleans up old worktrees."""
|
||||
with patch("cli.workspace_commands.WorktreeManager") as mock_manager_class:
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.cleanup_old_worktrees.return_value = (["worktree1"], [])
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.cleanup_old_worktrees_command(
|
||||
mock_project_dir, days=30, dry_run=False
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["removed"] == ["worktree1"]
|
||||
assert result["failed"] == []
|
||||
assert result["days_threshold"] == 30
|
||||
assert result["dry_run"] is False
|
||||
|
||||
def test_dry_run_mode(self, mock_project_dir: Path):
|
||||
"""Dry run mode doesn't actually remove worktrees."""
|
||||
with patch("cli.workspace_commands.WorktreeManager") as mock_manager_class:
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.cleanup_old_worktrees.return_value = (["worktree1"], [])
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.cleanup_old_worktrees_command(
|
||||
mock_project_dir, days=30, dry_run=True
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["dry_run"] is True
|
||||
mock_manager_instance.cleanup_old_worktrees.assert_called_once_with(
|
||||
days_threshold=30, dry_run=True
|
||||
)
|
||||
|
||||
def test_custom_days_threshold(self, mock_project_dir: Path):
|
||||
"""Uses custom days threshold."""
|
||||
with patch("cli.workspace_commands.WorktreeManager") as mock_manager_class:
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.cleanup_old_worktrees.return_value = ([], [])
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.cleanup_old_worktrees_command(
|
||||
mock_project_dir, days=7, dry_run=False
|
||||
)
|
||||
|
||||
assert result["days_threshold"] == 7
|
||||
mock_manager_instance.cleanup_old_worktrees.assert_called_once_with(
|
||||
days_threshold=7, dry_run=False
|
||||
)
|
||||
|
||||
def test_exception_handling(self, mock_project_dir: Path):
|
||||
"""Handles exceptions gracefully."""
|
||||
with patch("cli.workspace_commands.WorktreeManager", side_effect=Exception("Cleanup failed")):
|
||||
result = workspace_commands.cleanup_old_worktrees_command(
|
||||
mock_project_dir, days=30
|
||||
)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR worktree_summary_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestWorktreeSummaryCommand:
|
||||
"""Tests for worktree_summary_command function."""
|
||||
|
||||
def test_successful_summary(self, mock_project_dir: Path):
|
||||
"""Successfully generates worktree summary."""
|
||||
from typing import NamedTuple
|
||||
|
||||
MockWorktreeInfo = NamedTuple(
|
||||
"MockWorktreeInfo",
|
||||
[
|
||||
("spec_name", str),
|
||||
("days_since_last_commit", int | None),
|
||||
("commit_count", int),
|
||||
],
|
||||
)
|
||||
|
||||
with patch("cli.workspace_commands.WorktreeManager") as mock_manager_class:
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.list_all_worktrees.return_value = [
|
||||
MockWorktreeInfo(spec_name="001", days_since_last_commit=5, commit_count=3),
|
||||
MockWorktreeInfo(spec_name="002", days_since_last_commit=40, commit_count=1),
|
||||
]
|
||||
mock_manager_instance.get_worktree_count_warning.return_value = "Warning: Many worktrees"
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.worktree_summary_command(mock_project_dir)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["total_worktrees"] == 2
|
||||
assert len(result["categories"]["recent"]) == 1
|
||||
assert len(result["categories"]["month_old"]) == 1 # 40 days falls in month_old
|
||||
assert result["warning"] == "Warning: Many worktrees"
|
||||
|
||||
def test_categorizes_by_age(self, mock_project_dir: Path):
|
||||
"""Categorizes worktrees by age correctly."""
|
||||
from typing import NamedTuple
|
||||
|
||||
MockWorktreeInfo = NamedTuple(
|
||||
"MockWorktreeInfo",
|
||||
[
|
||||
("spec_name", str),
|
||||
("days_since_last_commit", int | None),
|
||||
("commit_count", int),
|
||||
],
|
||||
)
|
||||
|
||||
with patch("cli.workspace_commands.WorktreeManager") as mock_manager_class:
|
||||
mock_manager_instance = MagicMock()
|
||||
mock_manager_instance.list_all_worktrees.return_value = [
|
||||
MockWorktreeInfo(spec_name="001", days_since_last_commit=3, commit_count=1),
|
||||
MockWorktreeInfo(spec_name="002", days_since_last_commit=15, commit_count=1),
|
||||
MockWorktreeInfo(spec_name="003", days_since_last_commit=45, commit_count=1),
|
||||
MockWorktreeInfo(spec_name="004", days_since_last_commit=100, commit_count=1),
|
||||
MockWorktreeInfo(spec_name="005", days_since_last_commit=None, commit_count=1),
|
||||
]
|
||||
mock_manager_instance.get_worktree_count_warning.return_value = None
|
||||
mock_manager_class.return_value = mock_manager_instance
|
||||
|
||||
result = workspace_commands.worktree_summary_command(mock_project_dir)
|
||||
|
||||
assert len(result["categories"]["recent"]) == 1 # < 7 days
|
||||
assert len(result["categories"]["week_old"]) == 1 # 7-29 days (changed to 15)
|
||||
assert len(result["categories"]["month_old"]) == 1 # 30-89 days
|
||||
assert len(result["categories"]["very_old"]) == 1 # >= 90 days
|
||||
assert len(result["categories"]["unknown_age"]) == 1 # None
|
||||
|
||||
def test_exception_handling(self, mock_project_dir: Path):
|
||||
"""Handles exceptions gracefully."""
|
||||
with patch("cli.workspace_commands.WorktreeManager", side_effect=Exception("Summary failed")):
|
||||
result = workspace_commands.worktree_summary_command(mock_project_dir)
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
assert result["total_worktrees"] == 0
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR _get_changed_files_from_git() - FALLBACK BRANCHES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestDetectWorktreeBaseBranch:
|
||||
"""Tests for _detect_worktree_base_branch function."""
|
||||
|
||||
def test_reads_from_config_file(self, temp_git_repo: Path, mock_worktree_path: Path):
|
||||
"""Reads base branch from worktree config file."""
|
||||
config_dir = mock_worktree_path / ".auto-claude"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
config_file = config_dir / "worktree-config.json"
|
||||
config_file.write_text(json.dumps({"base_branch": "develop"}), encoding="utf-8")
|
||||
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
temp_git_repo, mock_worktree_path, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result == "develop"
|
||||
|
||||
def test_no_config_returns_none(self, temp_git_repo: Path, mock_worktree_path: Path):
|
||||
"""Returns None when no config file exists."""
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
temp_git_repo, mock_worktree_path, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should return None if can't detect
|
||||
assert result is None or result in ["main", "master", "develop"]
|
||||
|
||||
def test_invalid_config_falls_back(self, temp_git_repo: Path, mock_worktree_path: Path):
|
||||
"""Handles invalid config file gracefully."""
|
||||
config_dir = mock_worktree_path / ".auto-claude"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
config_file = config_dir / "worktree-config.json"
|
||||
config_file.write_text("invalid json", encoding="utf-8")
|
||||
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
temp_git_repo, mock_worktree_path, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should not crash, return None or detected branch
|
||||
assert result is None or isinstance(result, str)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR cleanup_old_worktrees_command()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
|
||||
class TestDetectWorktreeBaseBranchDetection:
|
||||
"""Tests for branch detection logic in _detect_worktree_base_branch."""
|
||||
|
||||
def test_detects_from_develop_branch(self, temp_git_repo: Path):
|
||||
"""Detects develop branch when it has fewest commits ahead."""
|
||||
# Create develop branch
|
||||
subprocess.run(
|
||||
["git", "checkout", "-b", "develop"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
# Create spec branch from develop
|
||||
subprocess.run(
|
||||
["git", "checkout", "-b", TEST_SPEC_BRANCH],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "checkout", "main"],
|
||||
cwd=temp_git_repo,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
temp_git_repo, temp_git_repo, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should detect develop as base branch
|
||||
assert result in ["develop", "main"]
|
||||
|
||||
def test_returns_none_when_no_branches_match(self, mock_project_dir: Path):
|
||||
"""Returns None when no candidate branches exist."""
|
||||
with patch("subprocess.run") as mock_run:
|
||||
# No branches exist
|
||||
mock_run.return_value = MagicMock(returncode=1)
|
||||
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
mock_project_dir, mock_project_dir, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
@patch("subprocess.run")
|
||||
def test_handles_merge_base_failure_during_detection(
|
||||
self, mock_run, mock_project_dir: Path, mock_worktree_path: Path
|
||||
):
|
||||
"""Handles merge-base command failure gracefully."""
|
||||
# Branch exists but merge-base fails
|
||||
mock_run.side_effect = [
|
||||
MagicMock(returncode=0), # Branch check passes
|
||||
MagicMock(returncode=1), # merge-base fails
|
||||
]
|
||||
|
||||
result = workspace_commands._detect_worktree_base_branch(
|
||||
mock_project_dir, mock_worktree_path, TEST_SPEC_NAME
|
||||
)
|
||||
|
||||
# Should continue checking other branches or return None
|
||||
assert result is None or isinstance(result, str)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TESTS FOR DEBUG FUNCTION FALLBACKS
|
||||
# =============================================================================
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,22 +4,14 @@ Tests for prompt_generator module functions.
|
||||
Tests for worktree detection and environment context generation.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
# Note: sys.path manipulation is handled by conftest.py line 46
|
||||
from prompts_pkg.prompt_generator import (
|
||||
detect_worktree_isolation,
|
||||
generate_environment_context,
|
||||
)
|
||||
|
||||
# Skip Windows-specific tests on non-Windows platforms
|
||||
is_windows = sys.platform == 'win32'
|
||||
skip_on_windows = pytest.mark.skipif(not is_windows, reason="Test only applies to Windows")
|
||||
skip_on_non_windows = pytest.mark.skipif(is_windows, reason="Test only applies to non-Windows platforms")
|
||||
|
||||
|
||||
def normalize_path(path_str: str) -> str:
|
||||
"""Normalize path string for cross-platform comparison."""
|
||||
@@ -44,7 +36,6 @@ class TestDetectWorktreeIsolation:
|
||||
assert "opt/dev/project" in norm_forbidden
|
||||
assert ".auto-claude" not in norm_forbidden
|
||||
|
||||
@skip_on_windows
|
||||
def test_new_worktree_windows_path(self):
|
||||
"""Test detection of new worktree location on Windows."""
|
||||
# Windows path with backslashes
|
||||
@@ -73,19 +64,23 @@ class TestDetectWorktreeIsolation:
|
||||
assert "opt/dev/project" in norm_forbidden
|
||||
assert ".worktrees" not in norm_forbidden
|
||||
|
||||
@skip_on_windows
|
||||
def test_legacy_worktree_windows_path(self):
|
||||
"""Test detection of legacy worktree location on Windows."""
|
||||
from unittest.mock import patch
|
||||
|
||||
project_dir = Path("C:/projects/x/.worktrees/009-audit")
|
||||
|
||||
is_worktree, forbidden = detect_worktree_isolation(project_dir)
|
||||
# Mock resolve() to return a fixed path on Windows-style paths
|
||||
# since resolve() on Linux would prepend current working directory
|
||||
with patch.object(Path, 'resolve', return_value=Path("C:/projects/x/.worktrees/009-audit")):
|
||||
is_worktree, forbidden = detect_worktree_isolation(project_dir)
|
||||
|
||||
assert is_worktree is True
|
||||
assert forbidden is not None
|
||||
# Check the essential parts
|
||||
norm_forbidden = normalize_path(str(forbidden))
|
||||
assert "projects" in norm_forbidden
|
||||
assert ".worktrees" not in norm_forbidden
|
||||
assert is_worktree is True
|
||||
assert forbidden is not None
|
||||
# Check the essential parts
|
||||
norm_forbidden = normalize_path(str(forbidden))
|
||||
assert "projects" in norm_forbidden
|
||||
assert ".worktrees" not in norm_forbidden
|
||||
|
||||
def test_pr_worktree_unix_path(self):
|
||||
"""Test detection of PR review worktree location on Unix-style path."""
|
||||
|
||||
+32
-35
@@ -524,56 +524,57 @@ class TestShouldRunQA:
|
||||
|
||||
def test_should_run_qa_build_not_complete(self, spec_dir: Path):
|
||||
"""Returns False when build not complete."""
|
||||
from unittest.mock import patch
|
||||
# Set up mock to return build not complete
|
||||
mock_progress.is_build_complete.return_value = False
|
||||
|
||||
plan = {"feature": "Test", "phases": []}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
with patch('qa.criteria.is_build_complete', return_value=False):
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
|
||||
# Reset mock
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
def test_should_run_qa_already_approved(self, spec_dir: Path, qa_signoff_approved: dict):
|
||||
"""Returns False when already approved."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
plan = {"feature": "Test", "qa_signoff": qa_signoff_approved}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
|
||||
def test_should_run_qa_build_complete_not_approved(self, spec_dir: Path):
|
||||
"""Returns True when build complete but not approved."""
|
||||
# Explicitly patch is_build_complete to return True
|
||||
from unittest.mock import patch
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
plan = {"feature": "Test", "phases": []}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is True
|
||||
plan = {"feature": "Test", "phases": []}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is True
|
||||
|
||||
def test_should_run_qa_rejected_status(self, spec_dir: Path, qa_signoff_rejected: dict):
|
||||
"""Returns True when rejected (needs re-review after fixes)."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
qa_signoff_rejected["qa_session"] = 1
|
||||
plan = {"feature": "Test", "qa_signoff": qa_signoff_rejected}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is True
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is True
|
||||
|
||||
def test_should_run_qa_no_plan(self, spec_dir: Path):
|
||||
"""Returns False when no plan exists (build not complete)."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = False
|
||||
|
||||
with patch('qa.criteria.is_build_complete', return_value=False):
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
result = should_run_qa(spec_dir)
|
||||
assert result is False
|
||||
|
||||
# Reset mock
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
|
||||
class TestShouldRunFixes:
|
||||
@@ -898,15 +899,14 @@ class TestQAIntegration:
|
||||
|
||||
def test_full_qa_workflow_approved_first_try(self, spec_dir: Path):
|
||||
"""Full workflow where QA approves on first try."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
# Build complete
|
||||
plan = {"feature": "Test Feature", "phases": []}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Should run QA
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
assert should_run_qa(spec_dir) is True
|
||||
assert should_run_qa(spec_dir) is True
|
||||
|
||||
# QA approves
|
||||
plan["qa_signoff"] = {
|
||||
@@ -917,22 +917,20 @@ class TestQAIntegration:
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Should not run QA again or fixes
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
assert should_run_qa(spec_dir) is False
|
||||
assert should_run_qa(spec_dir) is False
|
||||
assert should_run_fixes(spec_dir) is False
|
||||
assert is_qa_approved(spec_dir) is True
|
||||
|
||||
def test_full_qa_workflow_with_fixes(self, spec_dir: Path):
|
||||
"""Full workflow with reject-fix-approve cycle."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
# Build complete
|
||||
plan = {"feature": "Test Feature", "phases": []}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Should run QA
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
assert should_run_qa(spec_dir) is True
|
||||
assert should_run_qa(spec_dir) is True
|
||||
|
||||
# QA rejects
|
||||
plan["qa_signoff"] = {
|
||||
@@ -965,7 +963,7 @@ class TestQAIntegration:
|
||||
|
||||
def test_qa_workflow_max_iterations(self, spec_dir: Path):
|
||||
"""Test behavior when max iterations are reached."""
|
||||
from unittest.mock import patch
|
||||
mock_progress.is_build_complete.return_value = True
|
||||
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
@@ -979,5 +977,4 @@ class TestQAIntegration:
|
||||
# Should not run more fixes after max iterations
|
||||
assert should_run_fixes(spec_dir) is False
|
||||
# But QA can still be run (to re-check)
|
||||
with patch('qa.criteria.is_build_complete', return_value=True):
|
||||
assert should_run_qa(spec_dir) is True
|
||||
assert should_run_qa(spec_dir) is True
|
||||
|
||||
@@ -0,0 +1,497 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for QA Fixer Agent Session
|
||||
================================
|
||||
|
||||
Tests the qa/fixer.py module functionality including:
|
||||
- load_qa_fixer_prompt function
|
||||
- run_qa_fixer_session function
|
||||
- QA fixer session execution flow
|
||||
- Error handling and edge cases
|
||||
- Memory integration hooks
|
||||
"""
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# =============================================================================
|
||||
# MOCK SETUP - Must happen before ANY imports from auto-claude
|
||||
# =============================================================================
|
||||
|
||||
# Import shared mock helpers
|
||||
from tests.qa_test_helpers import (
|
||||
setup_qa_mocks,
|
||||
cleanup_qa_mocks,
|
||||
reset_qa_mocks,
|
||||
create_mock_response,
|
||||
create_mock_fixed_response,
|
||||
create_mock_tool_use_response,
|
||||
create_mock_client,
|
||||
)
|
||||
|
||||
# Set up mocks (no prompts_pkg needed for fixer)
|
||||
setup_qa_mocks(include_prompts_pkg=False)
|
||||
|
||||
# Import after mocks are set up
|
||||
from qa.fixer import load_qa_fixer_prompt, run_qa_fixer_session
|
||||
from qa.criteria import save_implementation_plan
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def cleanup_mocked_modules():
|
||||
"""Restore original modules after all tests in this module complete."""
|
||||
yield
|
||||
cleanup_qa_mocks()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir(temp_dir):
|
||||
"""Create a spec directory with basic structure."""
|
||||
spec = temp_dir / "spec"
|
||||
spec.mkdir()
|
||||
return spec
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir(temp_dir):
|
||||
"""Create a project directory."""
|
||||
project = temp_dir / "project"
|
||||
project.mkdir()
|
||||
return project
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client():
|
||||
"""Create a mock Claude SDK client."""
|
||||
return create_mock_client()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='function')
|
||||
def reset_shared_mocks_before_test():
|
||||
"""Reset shared module-level mocks before and after each test."""
|
||||
reset_qa_mocks()
|
||||
yield
|
||||
reset_qa_mocks()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MOCK RESPONSE HELPERS (fixer-specific)
|
||||
# =============================================================================
|
||||
|
||||
def _create_mock_response(text: str = "Fixer session complete."):
|
||||
"""Create a standard mock assistant+user message pair."""
|
||||
return create_mock_response(text)
|
||||
|
||||
|
||||
def _create_mock_fixed_response():
|
||||
"""Create mock response for fixed QA."""
|
||||
return create_mock_fixed_response()
|
||||
|
||||
|
||||
def _create_mock_tool_use_response():
|
||||
"""Create mock response with tool use blocks."""
|
||||
return create_mock_tool_use_response("Edit", {"file_path": "/test/file.py"})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fix_request_file(spec_dir):
|
||||
"""Create a QA_FIX_REQUEST.md file."""
|
||||
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
||||
fix_request.write_text("# Fix Request\n\nFix the following issues:\n- Issue 1\n- Issue 2")
|
||||
return fix_request
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CLASSES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestLoadQAFixerPrompt:
|
||||
"""Tests for load_qa_fixer_prompt function."""
|
||||
|
||||
def test_load_prompt_success(self, spec_dir, monkeypatch):
|
||||
"""Test successful prompt loading."""
|
||||
# Create prompts directory in temp location
|
||||
prompts_dir = spec_dir / "prompts"
|
||||
prompts_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
prompt_file = prompts_dir / "qa_fixer.md"
|
||||
prompt_content = "# QA Fixer Prompt\n\nFix the issues..."
|
||||
prompt_file.write_text(prompt_content)
|
||||
|
||||
# Patch QA_PROMPTS_DIR to point to temp directory
|
||||
import qa.fixer as qa_fixer_module
|
||||
monkeypatch.setattr(qa_fixer_module, "QA_PROMPTS_DIR", prompts_dir)
|
||||
|
||||
result = load_qa_fixer_prompt()
|
||||
|
||||
assert result == prompt_content
|
||||
|
||||
def test_load_prompt_file_not_found(self, monkeypatch):
|
||||
"""Test FileNotFoundError when prompt file doesn't exist."""
|
||||
# Create an empty temp directory with no qa_fixer.md
|
||||
empty_dir = Path(tempfile.mkdtemp())
|
||||
|
||||
try:
|
||||
# Patch QA_PROMPTS_DIR to point to empty directory
|
||||
import qa.fixer as qa_fixer_module
|
||||
monkeypatch.setattr(qa_fixer_module, "QA_PROMPTS_DIR", empty_dir)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_qa_fixer_prompt()
|
||||
finally:
|
||||
# Clean up temp directory
|
||||
shutil.rmtree(empty_dir)
|
||||
|
||||
|
||||
class TestRunQAFixerSessionFixed:
|
||||
"""Tests for run_qa_fixer_session returning fixed status."""
|
||||
|
||||
async def test_fixed_status(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that fixed status is returned when ready_for_qa_revalidation is True."""
|
||||
# Setup implementation plan with ready_for_qa_revalidation
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "fixes_applied",
|
||||
"ready_for_qa_revalidation": True,
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "fixed"
|
||||
assert len(result[1]) > 0 # Response text
|
||||
assert result[2] == {} # No error info
|
||||
|
||||
async def test_fixed_status_with_project_dir(self, mock_client, spec_dir, project_dir):
|
||||
"""Test session with explicit project_dir parameter."""
|
||||
# Create fix request file
|
||||
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
||||
fix_request.write_text("# Fix Request\n\nFix issues")
|
||||
|
||||
# Setup implementation plan
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "fixes_applied",
|
||||
"ready_for_qa_revalidation": True,
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False,
|
||||
project_dir=project_dir
|
||||
)
|
||||
|
||||
assert result[0] == "fixed"
|
||||
|
||||
|
||||
class TestRunQAFixerSessionError:
|
||||
"""Tests for run_qa_fixer_session error handling."""
|
||||
|
||||
async def test_error_missing_fix_request(self, mock_client, spec_dir):
|
||||
"""Test error when QA_FIX_REQUEST.md is missing."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Don't create QA_FIX_REQUEST.md
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert "not found" in result[1].lower()
|
||||
assert result[2]["type"] == "other"
|
||||
assert result[2]["exception_type"] == "FileNotFoundError"
|
||||
|
||||
async def test_exception_handling(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test exception handling during fixer session."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Test exception")
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert "Test exception" in result[1] or "test exception" in result[1].lower()
|
||||
assert result[2]["type"] == "other"
|
||||
assert result[2]["exception_type"] == "Exception"
|
||||
|
||||
|
||||
class TestRunQAFixerSessionParameters:
|
||||
"""Tests for run_qa_fixer_session parameter handling."""
|
||||
|
||||
async def test_verbose_mode(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test session with verbose mode enabled."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
||||
|
||||
await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Verify query was called
|
||||
assert mock_client.query.called
|
||||
|
||||
async def test_fix_session_number(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test session with different fix_session numbers."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
||||
|
||||
await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
fix_session=3,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# Verify query was called
|
||||
assert mock_client.query.called
|
||||
|
||||
|
||||
class TestRunQAFixerSessionIntegration:
|
||||
"""Integration tests for QA fixer session."""
|
||||
|
||||
async def test_full_session_flow(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test complete session flow from start to finish."""
|
||||
# Setup implementation plan
|
||||
plan = {
|
||||
"feature": "Test Feature",
|
||||
"qa_signoff": {
|
||||
"status": "fixes_applied",
|
||||
"ready_for_qa_revalidation": True,
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_response("Applying fixes..."))
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
fix_session=1,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
assert result[0] == "fixed"
|
||||
assert mock_client.query.called
|
||||
assert mock_client.receive_response.called
|
||||
|
||||
|
||||
class TestMemoryIntegration:
|
||||
"""Tests for memory integration in QA fixer."""
|
||||
|
||||
async def test_memory_context_retrieval(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that memory context is retrieved during session."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
||||
|
||||
# Patch where the function is used (in qa.fixer module)
|
||||
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock) as mock_get_context:
|
||||
mock_get_context.return_value = "Past fix patterns: check imports"
|
||||
|
||||
await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify memory context was retrieved
|
||||
assert mock_get_context.called
|
||||
|
||||
async def test_memory_save_on_fixed(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that session memory is saved when fixes are applied."""
|
||||
# Setup implementation plan
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "fixes_applied",
|
||||
"ready_for_qa_revalidation": True,
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_fixed_response())
|
||||
|
||||
# Patch where the function is used
|
||||
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
||||
patch('qa.fixer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
||||
|
||||
await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify memory was saved
|
||||
assert mock_save.called
|
||||
|
||||
|
||||
class TestErrorDetection:
|
||||
"""Tests for error type detection in QA fixer."""
|
||||
|
||||
async def test_rate_limit_error_detection(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that rate limit errors are properly detected."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Rate limit exceeded")
|
||||
|
||||
# Patch where the functions are used (qa.fixer) not where they're defined
|
||||
with patch('qa.fixer.is_rate_limit_error', return_value=True), \
|
||||
patch('qa.fixer.is_tool_concurrency_error', return_value=False):
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert result[2]["type"] == "rate_limit"
|
||||
|
||||
async def test_tool_concurrency_error_detection(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that tool concurrency errors are properly detected."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Tool concurrency limit")
|
||||
|
||||
# Patch where the functions are used (qa.fixer) not where they're defined
|
||||
with patch('qa.fixer.is_tool_concurrency_error', return_value=True), \
|
||||
patch('qa.fixer.is_rate_limit_error', return_value=False), \
|
||||
patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None):
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert result[2]["type"] == "tool_concurrency"
|
||||
|
||||
|
||||
class TestStatusNotUpdated:
|
||||
"""Tests for when fixer doesn't update status."""
|
||||
|
||||
async def test_fixed_assumed_when_status_not_updated(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that fixed is assumed even when status not updated."""
|
||||
# Setup implementation plan without ready_for_qa_revalidation
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_response())
|
||||
|
||||
# Patch where the function is used
|
||||
with patch('qa.fixer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
||||
patch('qa.fixer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
||||
|
||||
result = await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
# Should still return "fixed" even though status wasn't updated
|
||||
assert result[0] == "fixed"
|
||||
# Memory should still be saved
|
||||
assert mock_save.called
|
||||
|
||||
|
||||
class TestToolUseHandling:
|
||||
"""Tests for tool use handling in QA fixer."""
|
||||
|
||||
async def test_tool_use_blocks(self, mock_client, spec_dir, fix_request_file):
|
||||
"""Test that tool use blocks are handled correctly."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses with tool use
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value.set_messages(_create_mock_tool_use_response())
|
||||
|
||||
await run_qa_fixer_session(
|
||||
mock_client,
|
||||
spec_dir,
|
||||
1,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify query was called
|
||||
assert mock_client.query.called
|
||||
@@ -0,0 +1,506 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for QA Reviewer Agent Session
|
||||
===================================
|
||||
|
||||
Tests the qa/reviewer.py module functionality including:
|
||||
- run_qa_agent_session function
|
||||
- QA session execution flow
|
||||
- Error handling and edge cases
|
||||
- Memory integration hooks
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# =============================================================================
|
||||
# MOCK SETUP - Must happen before ANY imports from auto-claude
|
||||
# =============================================================================
|
||||
|
||||
# Import shared mock helpers
|
||||
from tests.qa_test_helpers import (
|
||||
setup_qa_mocks,
|
||||
cleanup_qa_mocks,
|
||||
reset_qa_mocks,
|
||||
create_mock_response,
|
||||
create_mock_client,
|
||||
)
|
||||
|
||||
# Set up mocks (reviewer needs prompts_pkg)
|
||||
setup_qa_mocks(include_prompts_pkg=True)
|
||||
|
||||
# Import after mocks are set up
|
||||
from qa.reviewer import run_qa_agent_session
|
||||
from qa.criteria import save_implementation_plan
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FIXTURES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def cleanup_mocked_modules():
|
||||
"""Restore original modules after all tests in this module complete."""
|
||||
yield
|
||||
cleanup_qa_mocks()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def spec_dir(temp_dir):
|
||||
"""Create a spec directory with basic structure."""
|
||||
spec = temp_dir / "spec"
|
||||
spec.mkdir()
|
||||
return spec
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir(temp_dir):
|
||||
"""Create a project directory."""
|
||||
project = temp_dir / "project"
|
||||
project.mkdir()
|
||||
return project
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client():
|
||||
"""Create a mock Claude SDK client."""
|
||||
return create_mock_client()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='function')
|
||||
def reset_shared_mocks_before_test():
|
||||
"""Reset shared module-level mocks before and after each test."""
|
||||
reset_qa_mocks()
|
||||
yield
|
||||
reset_qa_mocks()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MOCK RESPONSE HELPERS (reviewer-specific)
|
||||
# =============================================================================
|
||||
|
||||
def _create_approved_response():
|
||||
"""Create mock response for approved QA."""
|
||||
return create_mock_response("QA approved - all criteria met.")
|
||||
|
||||
|
||||
def _create_rejected_response():
|
||||
"""Create mock response for rejected QA."""
|
||||
return create_mock_response("QA rejected - found issues.")
|
||||
|
||||
|
||||
def _create_no_signoff_response():
|
||||
"""Create mock response where agent doesn't update signoff."""
|
||||
return create_mock_response("QA review complete.")
|
||||
|
||||
|
||||
def _create_tool_use_response():
|
||||
"""Create mock response with tool use blocks."""
|
||||
msg1, msg2 = create_mock_response("Checking files...")
|
||||
# Add tool use block to first message
|
||||
from unittest.mock import MagicMock
|
||||
tool_block = MagicMock()
|
||||
tool_block.__class__.__name__ = "ToolUseBlock"
|
||||
tool_block.name = "Read"
|
||||
tool_block.input = {"file_path": "/test/file.py"}
|
||||
msg1.content.append(tool_block)
|
||||
|
||||
return [msg1, msg2]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TEST CLASSES
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestRunQAAgentSessionApproved:
|
||||
"""Tests for run_qa_agent_session returning approved status."""
|
||||
|
||||
async def test_approved_status(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that approved status is returned correctly."""
|
||||
# Setup implementation plan with approved status
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "approved",
|
||||
"qa_session": 1,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_approved_response()
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "approved"
|
||||
assert len(result[1]) > 0 # Response text
|
||||
assert result[2] == {} # No error info
|
||||
|
||||
|
||||
class TestRunQAAgentSessionRejected:
|
||||
"""Tests for run_qa_agent_session returning rejected status."""
|
||||
|
||||
async def test_rejected_status(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that rejected status is returned correctly."""
|
||||
# Setup implementation plan with rejected status
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "rejected",
|
||||
"qa_session": 1,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"issues_found": [
|
||||
{"title": "Test failure", "type": "unit_test"},
|
||||
]
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_rejected_response()
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "rejected"
|
||||
assert len(result[1]) > 0 # Response text
|
||||
assert result[2] == {} # No error info
|
||||
|
||||
|
||||
class TestRunQAAgentSessionError:
|
||||
"""Tests for run_qa_agent_session error handling."""
|
||||
|
||||
async def test_error_status_no_signoff(self, mock_client, spec_dir, project_dir):
|
||||
"""Test error status when agent doesn't update signoff."""
|
||||
# Setup implementation plan without qa_signoff
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses - agent doesn't update signoff
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_no_signoff_response()
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert "did not update" in result[1].lower()
|
||||
assert result[2]["type"] == "other"
|
||||
|
||||
async def test_exception_handling(self, mock_client, spec_dir, project_dir):
|
||||
"""Test exception handling during QA session."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Test exception")
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert "Test exception" in result[1] or "test exception" in result[1].lower()
|
||||
assert result[2]["type"] == "other"
|
||||
assert result[2]["exception_type"] == "Exception"
|
||||
|
||||
|
||||
class TestRunQAAgentSessionParameters:
|
||||
"""Tests for run_qa_agent_session parameter handling."""
|
||||
|
||||
async def test_with_previous_error(self, mock_client, spec_dir, project_dir):
|
||||
"""Test session with previous error context."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
previous_error = {
|
||||
"error_type": "missing_implementation_plan_update",
|
||||
"error_message": "Test error",
|
||||
"consecutive_errors": 2,
|
||||
}
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_no_signoff_response()
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False,
|
||||
previous_error=previous_error
|
||||
)
|
||||
|
||||
# Verify query was called (it should include error context)
|
||||
assert mock_client.query.called
|
||||
|
||||
async def test_verbose_mode(self, mock_client, spec_dir, project_dir):
|
||||
"""Test session with verbose mode enabled."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_no_signoff_response()
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Verify query was called
|
||||
assert mock_client.query.called
|
||||
|
||||
|
||||
class TestRunQAAgentSessionIntegration:
|
||||
"""Integration tests for QA reviewer session."""
|
||||
|
||||
async def test_full_session_flow(self, mock_client, spec_dir, project_dir):
|
||||
"""Test complete session flow from start to finish."""
|
||||
# Setup implementation plan
|
||||
plan = {
|
||||
"feature": "Test Feature",
|
||||
"qa_signoff": {
|
||||
"status": "approved",
|
||||
"qa_session": 1,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"tests_passed": {"unit": True, "integration": True},
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_approved_response()
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
qa_session=1,
|
||||
max_iterations=50,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
assert result[0] == "approved"
|
||||
assert mock_client.query.called
|
||||
assert mock_client.receive_response.called
|
||||
|
||||
|
||||
class TestMemoryIntegration:
|
||||
"""Tests for memory integration in QA reviewer."""
|
||||
|
||||
async def test_memory_context_retrieval(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that memory context is retrieved during session."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_no_signoff_response()
|
||||
|
||||
# Patch where the function is used (in qa.reviewer module)
|
||||
with patch('qa.reviewer.get_graphiti_context', new_callable=AsyncMock) as mock_get_context:
|
||||
mock_get_context.return_value = "Past QA insights: check for edge cases"
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify memory context was retrieved
|
||||
assert mock_get_context.called
|
||||
|
||||
async def test_memory_save_on_approved(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that session memory is saved on approval."""
|
||||
# Setup implementation plan with approved status
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "approved",
|
||||
"qa_session": 1,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_approved_response()
|
||||
|
||||
# Patch where the functions are used
|
||||
with patch('qa.reviewer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
||||
patch('qa.reviewer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify memory was saved
|
||||
assert mock_save.called
|
||||
|
||||
async def test_memory_save_on_rejected(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that session memory is saved on rejection with issues."""
|
||||
# Setup implementation plan with rejected status
|
||||
plan = {
|
||||
"feature": "Test",
|
||||
"qa_signoff": {
|
||||
"status": "rejected",
|
||||
"qa_session": 1,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"issues_found": [
|
||||
{"title": "Test failure", "type": "unit_test"},
|
||||
]
|
||||
}
|
||||
}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_rejected_response()
|
||||
|
||||
# Patch where the functions are used
|
||||
with patch('qa.reviewer.get_graphiti_context', new_callable=AsyncMock, return_value=None), \
|
||||
patch('qa.reviewer.save_session_memory', new_callable=AsyncMock) as mock_save:
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify memory was saved with issues
|
||||
assert mock_save.called
|
||||
|
||||
|
||||
class TestErrorDetection:
|
||||
"""Tests for error type detection in QA reviewer."""
|
||||
|
||||
async def test_rate_limit_error_detection(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that rate limit errors are properly detected."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Rate limit exceeded")
|
||||
|
||||
# Patch where the functions are used (qa.reviewer) not where they're defined
|
||||
with patch('qa.reviewer.is_rate_limit_error', return_value=True), \
|
||||
patch('qa.reviewer.is_tool_concurrency_error', return_value=False):
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert result[2]["type"] == "rate_limit"
|
||||
|
||||
async def test_tool_concurrency_error_detection(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that tool concurrency errors are properly detected."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client to raise exception
|
||||
mock_client.query.side_effect = Exception("Tool concurrency limit")
|
||||
|
||||
# Patch where the functions are used
|
||||
with patch('qa.reviewer.is_tool_concurrency_error', return_value=True), \
|
||||
patch('qa.reviewer.is_rate_limit_error', return_value=False):
|
||||
|
||||
result = await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
assert result[0] == "error"
|
||||
assert result[2]["type"] == "tool_concurrency"
|
||||
|
||||
|
||||
class TestToolUseHandling:
|
||||
"""Tests for tool use handling in QA reviewer."""
|
||||
|
||||
async def test_tool_use_blocks(self, mock_client, spec_dir, project_dir):
|
||||
"""Test that tool use blocks are handled correctly."""
|
||||
# Setup implementation plan
|
||||
plan = {"feature": "Test"}
|
||||
save_implementation_plan(spec_dir, plan)
|
||||
|
||||
# Mock client responses with tool use
|
||||
mock_client.query.return_value = None
|
||||
mock_client.receive_response.return_value = _create_tool_use_response()
|
||||
|
||||
await run_qa_agent_session(
|
||||
mock_client,
|
||||
project_dir,
|
||||
spec_dir,
|
||||
1,
|
||||
50,
|
||||
False
|
||||
)
|
||||
|
||||
# Verify query was called
|
||||
assert mock_client.query.called
|
||||
@@ -543,6 +543,25 @@ def run_all_tests():
|
||||
# Note: This manual runner is kept for backwards compatibility.
|
||||
# Prefer running tests with pytest: pytest tests/test_recovery.py -v
|
||||
|
||||
tests = [
|
||||
("test_initialization", test_initialization),
|
||||
("test_record_attempt", test_record_attempt),
|
||||
("test_circular_fix_detection", test_circular_fix_detection),
|
||||
("test_failure_classification", test_failure_classification),
|
||||
("test_recovery_action_determination", test_recovery_action_determination),
|
||||
("test_good_commit_tracking", test_good_commit_tracking),
|
||||
("test_mark_subtask_stuck", test_mark_subtask_stuck),
|
||||
("test_recovery_hints", test_recovery_hints),
|
||||
# Session checkpoint and restoration tests
|
||||
("test_checkpoint_persistence_across_sessions", test_checkpoint_persistence_across_sessions),
|
||||
("test_restoration_after_failure", test_restoration_after_failure),
|
||||
("test_checkpoint_multiple_subtasks", test_checkpoint_multiple_subtasks),
|
||||
("test_restoration_with_build_commits", test_restoration_with_build_commits),
|
||||
("test_checkpoint_recovery_hints_restoration", test_checkpoint_recovery_hints_restoration),
|
||||
("test_restoration_stuck_subtasks_list", test_restoration_stuck_subtasks_list),
|
||||
("test_checkpoint_clear_and_reset", test_checkpoint_clear_and_reset),
|
||||
]
|
||||
|
||||
print("Note: Running with manual test runner for backwards compatibility.")
|
||||
print("For full pytest integration with fixtures, run: pytest tests/test_recovery.py -v")
|
||||
print()
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
"""
|
||||
Test Claude Agent SDK Structured Output functionality.
|
||||
|
||||
This test verifies how structured outputs work with the SDK.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
|
||||
# Add backend to path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "apps" / "backend"))
|
||||
|
||||
# Add pydantic_models path
|
||||
_pydantic_models_path = (
|
||||
Path(__file__).parent.parent
|
||||
/ "apps"
|
||||
/ "backend"
|
||||
/ "runners"
|
||||
/ "github"
|
||||
/ "services"
|
||||
)
|
||||
sys.path.insert(0, str(_pydantic_models_path))
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Literal
|
||||
|
||||
|
||||
# Simple test model
|
||||
class SimpleReviewResponse(BaseModel):
|
||||
"""A simple review response for testing."""
|
||||
|
||||
verdict: Literal["PASS", "FAIL"] = Field(description="Review verdict")
|
||||
reason: str = Field(description="Reason for the verdict")
|
||||
score: int = Field(ge=0, le=100, description="Score from 0-100")
|
||||
|
||||
|
||||
async def test_structured_output():
|
||||
"""Test the SDK's structured output functionality."""
|
||||
|
||||
# OAuth token must be set in environment (CLAUDE_CODE_OAUTH_TOKEN)
|
||||
if not os.environ.get("CLAUDE_CODE_OAUTH_TOKEN"):
|
||||
print("ERROR: CLAUDE_CODE_OAUTH_TOKEN environment variable not set")
|
||||
return
|
||||
|
||||
from claude_agent_sdk import query, ClaudeAgentOptions
|
||||
|
||||
# Generate JSON schema from Pydantic model
|
||||
schema = SimpleReviewResponse.model_json_schema()
|
||||
print("=== Schema ===")
|
||||
pprint(schema)
|
||||
print()
|
||||
|
||||
prompt = """
|
||||
Review this code and provide your assessment:
|
||||
|
||||
```python
|
||||
def add(a, b):
|
||||
return a + b
|
||||
```
|
||||
|
||||
Provide a verdict (PASS or FAIL), reason, and score.
|
||||
"""
|
||||
|
||||
print("=== Running query with output_format ===")
|
||||
print(f"Prompt: {prompt[:100]}...")
|
||||
print()
|
||||
|
||||
message_count = 0
|
||||
async for message in query(
|
||||
prompt=prompt,
|
||||
options=ClaudeAgentOptions(
|
||||
model="claude-sonnet-4-5-20250929",
|
||||
system_prompt="You are a code reviewer. Provide structured feedback.",
|
||||
allowed_tools=[],
|
||||
max_turns=2, # Need 2 turns for structured output tool call
|
||||
output_format={
|
||||
"type": "json_schema",
|
||||
"schema": schema,
|
||||
},
|
||||
),
|
||||
):
|
||||
message_count += 1
|
||||
msg_type = type(message).__name__
|
||||
print(f"\n=== Message {message_count}: {msg_type} ===")
|
||||
|
||||
# Print all non-private attributes
|
||||
for attr in dir(message):
|
||||
if not attr.startswith("_"):
|
||||
try:
|
||||
val = getattr(message, attr)
|
||||
if not callable(val):
|
||||
# Truncate long values
|
||||
val_str = str(val)
|
||||
if len(val_str) > 500:
|
||||
val_str = val_str[:500] + "..."
|
||||
print(f" {attr}: {val_str}")
|
||||
except Exception as e:
|
||||
print(f" {attr}: <error: {e}>")
|
||||
|
||||
# Check for StructuredOutput tool use in AssistantMessage
|
||||
if msg_type == "AssistantMessage":
|
||||
content = getattr(message, "content", [])
|
||||
for block in content:
|
||||
block_type = type(block).__name__
|
||||
if block_type == "ToolUseBlock":
|
||||
tool_name = getattr(block, "name", "")
|
||||
if tool_name == "StructuredOutput":
|
||||
structured_data = getattr(block, "input", None)
|
||||
print(f"\n 🎯 Found StructuredOutput tool use!")
|
||||
print(f" Data: {structured_data}")
|
||||
if structured_data:
|
||||
try:
|
||||
validated = SimpleReviewResponse.model_validate(structured_data)
|
||||
print(f"\n ✅ Successfully validated StructuredOutput!")
|
||||
print(f" verdict: {validated.verdict}")
|
||||
print(f" reason: {validated.reason}")
|
||||
print(f" score: {validated.score}")
|
||||
except Exception as e:
|
||||
print(f"\n ❌ Failed to validate: {e}")
|
||||
|
||||
# Special handling for ResultMessage
|
||||
if msg_type == "ResultMessage":
|
||||
print("\n --- ResultMessage Details ---")
|
||||
|
||||
# Check structured_output
|
||||
so = getattr(message, "structured_output", None)
|
||||
print(f" structured_output value: {so}")
|
||||
print(f" structured_output type: {type(so)}")
|
||||
|
||||
# Check result
|
||||
result = getattr(message, "result", None)
|
||||
print(f" result value: {result}")
|
||||
print(f" result type: {type(result)}")
|
||||
|
||||
# If result is a string, try to parse as JSON
|
||||
if isinstance(result, str):
|
||||
import json
|
||||
try:
|
||||
parsed = json.loads(result)
|
||||
print(f" result parsed as JSON: {parsed}")
|
||||
except:
|
||||
print(f" result is not JSON")
|
||||
|
||||
# Try to validate with Pydantic if we got data
|
||||
if so:
|
||||
try:
|
||||
validated = SimpleReviewResponse.model_validate(so)
|
||||
print(f"\n ✅ Successfully validated structured_output!")
|
||||
print(f" verdict: {validated.verdict}")
|
||||
print(f" reason: {validated.reason}")
|
||||
print(f" score: {validated.score}")
|
||||
except Exception as e:
|
||||
print(f"\n ❌ Failed to validate structured_output: {e}")
|
||||
|
||||
if result and isinstance(result, (dict, str)):
|
||||
try:
|
||||
data = result if isinstance(result, dict) else json.loads(result)
|
||||
validated = SimpleReviewResponse.model_validate(data)
|
||||
print(f"\n ✅ Successfully validated result as structured output!")
|
||||
print(f" verdict: {validated.verdict}")
|
||||
print(f" reason: {validated.reason}")
|
||||
print(f" score: {validated.score}")
|
||||
except Exception as e:
|
||||
print(f"\n ❌ Failed to validate result: {e}")
|
||||
|
||||
print(f"\n=== Total messages: {message_count} ===")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(test_structured_output())
|
||||
@@ -382,11 +382,12 @@ class TestEdgeCases:
|
||||
|
||||
def test_nonexistent_directory(self, scanner):
|
||||
"""Test handling of non-existent directory."""
|
||||
fake_dir = Path("/tmp/test-nonexistent-security-scanner-123456")
|
||||
fake_dir = Path("/nonexistent/path")
|
||||
|
||||
# Should not crash, may have errors
|
||||
result = scanner.scan(fake_dir)
|
||||
assert isinstance(result, SecurityScanResult)
|
||||
# Should not crash, may have errors - mock exists to avoid permission error
|
||||
with patch.object(Path, 'exists', return_value=False):
|
||||
result = scanner.scan(fake_dir)
|
||||
assert isinstance(result, SecurityScanResult)
|
||||
|
||||
def test_scan_specific_files(self, scanner, python_project):
|
||||
"""Test scanning specific files only."""
|
||||
|
||||
@@ -12,6 +12,7 @@ Tests cover:
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -437,11 +438,12 @@ class TestEdgeCases:
|
||||
|
||||
def test_nonexistent_directory(self):
|
||||
"""Test handling of non-existent directory."""
|
||||
fake_dir = Path("/tmp/test-nonexistent-orchestrator-123456")
|
||||
fake_dir = Path("/nonexistent/path")
|
||||
|
||||
# Should not crash
|
||||
orchestrator = ServiceOrchestrator(fake_dir)
|
||||
assert orchestrator.is_multi_service() is False
|
||||
# Should not crash - mock exists to avoid permission error
|
||||
with patch.object(Path, 'exists', return_value=False):
|
||||
orchestrator = ServiceOrchestrator(fake_dir)
|
||||
assert orchestrator.is_multi_service() is False
|
||||
|
||||
def test_empty_compose_file(self, temp_dir):
|
||||
"""Test handling of empty compose file."""
|
||||
|
||||
@@ -0,0 +1,460 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for spec/validate_pkg/validators/context_validator.py
|
||||
============================================================
|
||||
|
||||
Tests for ContextValidator class covering:
|
||||
- File existence checks
|
||||
- JSON parsing validation
|
||||
- Required field validation
|
||||
- Recommended field warnings
|
||||
- ValidationResult return values
|
||||
"""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestContextValidatorInit:
|
||||
"""Tests for ContextValidator initialization."""
|
||||
|
||||
def test_initialization_with_path(self, spec_dir: Path):
|
||||
"""ContextValidator initializes with spec_dir path."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
|
||||
assert validator.spec_dir == spec_dir
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
|
||||
def test_converts_string_to_path(self, spec_dir: Path):
|
||||
"""ContextValidator converts string path to Path object."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
validator = ContextValidator(str(spec_dir))
|
||||
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
assert validator.spec_dir == spec_dir
|
||||
|
||||
|
||||
class TestValidateFileNotFound:
|
||||
"""Tests for validate() when context.json does not exist."""
|
||||
|
||||
def test_returns_error_when_file_missing(self, spec_dir: Path):
|
||||
"""Should return ValidationResult with error when context.json missing."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert result.checkpoint == "context"
|
||||
assert any("not found" in err.lower() for err in result.errors)
|
||||
assert len(result.fixes) > 0
|
||||
|
||||
def test_error_message_includes_filename(self, spec_dir: Path):
|
||||
"""Error message should mention context.json."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert "context.json" in result.errors[0]
|
||||
|
||||
def test_fix_suggests_command(self, spec_dir: Path):
|
||||
"""Suggested fix should include the context.py command."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("auto-claude/context.py" in fix for fix in result.fixes)
|
||||
assert any("--output context.json" in fix for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateInvalidJson:
|
||||
"""Tests for validate() with invalid JSON content."""
|
||||
|
||||
def test_returns_error_for_invalid_json(self, spec_dir: Path):
|
||||
"""Should return error when context.json has invalid JSON."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text("{invalid json content", encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert result.checkpoint == "context"
|
||||
assert any("invalid json" in err.lower() for err in result.errors)
|
||||
|
||||
def test_error_includes_json_parse_message(self, spec_dir: Path):
|
||||
"""Error message should include JSON parsing error details."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"unclosed": true', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Error message should mention the JSON decode error
|
||||
assert any("json" in err.lower() for err in result.errors)
|
||||
|
||||
def test_fix_suggests_regenerate(self, spec_dir: Path):
|
||||
"""Suggested fix should mention regenerating context.json."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text("{bad}", encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("regenerate" in fix.lower() or "fix" in fix.lower() for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateMissingRequiredFields:
|
||||
"""Tests for validate() with missing required fields."""
|
||||
|
||||
def test_error_when_task_description_missing(self, spec_dir: Path):
|
||||
"""Should error when required field 'task_description' is missing."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"other_field": "value"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert any("task_description" in err for err in result.errors)
|
||||
|
||||
def test_error_for_all_required_fields_missing(self, spec_dir: Path):
|
||||
"""Should list all missing required fields."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
from spec.validate_pkg.schemas import CONTEXT_SCHEMA
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text("{}", encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Check that all required fields are mentioned in errors
|
||||
required_fields = CONTEXT_SCHEMA["required_fields"]
|
||||
for field in required_fields:
|
||||
assert any(field in err for err in result.errors), f"Field {field} not in errors"
|
||||
|
||||
def test_fixes_suggest_adding_missing_fields(self, spec_dir: Path):
|
||||
"""Suggested fixes should include adding missing fields."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"created_at": "2024-01-01"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Fixes should suggest adding task_description
|
||||
assert any("task_description" in fix for fix in result.fixes)
|
||||
|
||||
def test_valid_when_all_required_fields_present(self, spec_dir: Path):
|
||||
"""Should pass validation when all required fields exist."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {"task_description": "Add user authentication"}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert len(result.errors) == 0
|
||||
|
||||
|
||||
class TestValidateRecommendedFields:
|
||||
"""Tests for validate() recommended field warnings."""
|
||||
|
||||
def test_warns_when_files_to_modify_missing(self, spec_dir: Path):
|
||||
"""Should warn when 'files_to_modify' is missing."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {"task_description": "Test task"}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Missing recommended field should be a warning, not error
|
||||
assert any("files_to_modify" in warn for warn in result.warnings)
|
||||
assert all("files_to_modify" not in err for err in result.errors)
|
||||
|
||||
def test_warns_when_files_to_reference_missing(self, spec_dir: Path):
|
||||
"""Should warn when 'files_to_reference' is missing."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {"task_description": "Test task"}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("files_to_reference" in warn for warn in result.warnings)
|
||||
|
||||
def test_warns_when_scoped_services_missing(self, spec_dir: Path):
|
||||
"""Should warn when 'scoped_services' is missing."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {"task_description": "Test task"}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("scoped_services" in warn for warn in result.warnings)
|
||||
|
||||
def test_warns_for_empty_recommended_fields(self, spec_dir: Path):
|
||||
"""Should warn when recommended fields exist but are empty."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {
|
||||
"task_description": "Test task",
|
||||
"files_to_modify": [],
|
||||
"files_to_reference": None,
|
||||
}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Empty fields should trigger warnings
|
||||
assert any("files_to_modify" in warn for warn in result.warnings)
|
||||
|
||||
def test_no_warnings_when_recommended_fields_present(self, spec_dir: Path):
|
||||
"""Should not warn when all recommended fields are present."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {
|
||||
"task_description": "Test task",
|
||||
"files_to_modify": ["src/auth.py"],
|
||||
"files_to_reference": ["src/user.py"],
|
||||
"scoped_services": ["backend"],
|
||||
}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Check that no warnings for these fields exist
|
||||
assert not any("files_to_modify" in warn for warn in result.warnings)
|
||||
assert not any("files_to_reference" in warn for warn in result.warnings)
|
||||
assert not any("scoped_services" in warn for warn in result.warnings)
|
||||
|
||||
|
||||
class TestValidateValidContext:
|
||||
"""Tests for validate() with valid context.json."""
|
||||
|
||||
def test_returns_valid_for_minimal_context(self, spec_dir: Path):
|
||||
"""Should return valid result with minimal required fields."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {"task_description": "Implement OAuth login"}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert result.checkpoint == "context"
|
||||
assert len(result.errors) == 0
|
||||
# Warnings for missing recommended fields are expected
|
||||
|
||||
def test_returns_valid_with_all_fields(self, spec_dir: Path):
|
||||
"""Should return valid result with all fields present."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {
|
||||
"task_description": "Add OAuth",
|
||||
"scoped_services": ["backend", "frontend"],
|
||||
"files_to_modify": ["src/auth.py"],
|
||||
"files_to_reference": ["src/user.py"],
|
||||
"patterns": ["singleton pattern"],
|
||||
"service_contexts": {"backend": "FastAPI app"},
|
||||
"created_at": "2024-01-15T10:00:00Z",
|
||||
}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert len(result.errors) == 0
|
||||
assert len(result.warnings) == 0
|
||||
|
||||
|
||||
class TestValidationResultStructure:
|
||||
"""Tests for ValidationResult structure and fields."""
|
||||
|
||||
def test_result_has_all_fields(self, spec_dir: Path):
|
||||
"""ValidationResult should have all expected fields."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"task_description": "Test"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Check all fields exist
|
||||
assert hasattr(result, "valid")
|
||||
assert hasattr(result, "checkpoint")
|
||||
assert hasattr(result, "errors")
|
||||
assert hasattr(result, "warnings")
|
||||
assert hasattr(result, "fixes")
|
||||
|
||||
def test_checkpoint_is_context(self, spec_dir: Path):
|
||||
"""Checkpoint field should always be 'context'."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"task_description": "Test"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.checkpoint == "context"
|
||||
|
||||
def test_fixes_only_on_invalid(self, spec_dir: Path):
|
||||
"""Fixes should only be present when validation fails."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
# Valid case - no fixes needed
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"task_description": "Test"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert len(result.fixes) == 0
|
||||
|
||||
def test_lists_are_initialized(self, spec_dir: Path):
|
||||
"""Errors, warnings, and fixes should always be lists."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text('{"task_description": "Test"}', encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert isinstance(result.errors, list)
|
||||
assert isinstance(result.warnings, list)
|
||||
assert isinstance(result.fixes, list)
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Tests for edge cases and boundary conditions."""
|
||||
|
||||
def test_handles_unicode_in_context(self, spec_dir: Path):
|
||||
"""Should handle unicode characters in context.json."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_data = {
|
||||
"task_description": "添加用户认证",
|
||||
}
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_handles_large_context_file(self, spec_dir: Path):
|
||||
"""Should handle large context.json files."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
# Create a large context with many files
|
||||
context_data = {
|
||||
"task_description": "Large refactoring",
|
||||
"files_to_modify": [f"src/file{i}.py" for i in range(1000)],
|
||||
"files_to_reference": [f"lib/file{i}.py" for i in range(500)],
|
||||
}
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_handles_empty_context_object(self, spec_dir: Path):
|
||||
"""Should handle empty JSON object."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text("{}", encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert any("task_description" in err for err in result.errors)
|
||||
|
||||
def test_handles_nested_json_structure(self, spec_dir: Path):
|
||||
"""Should handle nested JSON objects."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_data = {
|
||||
"task_description": "Complex task",
|
||||
"service_contexts": {
|
||||
"backend": {
|
||||
"framework": "FastAPI",
|
||||
"version": "0.100.0",
|
||||
"config": {"debug": True, "port": 8000},
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{"name": "singleton", "description": "Single instance"},
|
||||
{"name": "factory", "description": "Object creation"},
|
||||
],
|
||||
}
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_handles_extra_fields(self, spec_dir: Path):
|
||||
"""Should allow extra fields not in schema."""
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
context_data = {
|
||||
"task_description": "Test task",
|
||||
"custom_field": "custom value",
|
||||
"another_extra": 123,
|
||||
}
|
||||
|
||||
context_file = spec_dir / "context.json"
|
||||
context_file.write_text(json.dumps(context_data), encoding="utf-8")
|
||||
|
||||
validator = ContextValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Extra fields should not cause validation errors
|
||||
assert result.valid is True
|
||||
@@ -0,0 +1,368 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for spec/validate_pkg/validators/prereqs_validator.py
|
||||
===========================================================
|
||||
|
||||
Tests for PrereqsValidator class covering:
|
||||
- Spec directory existence checks
|
||||
- project_index.json existence checks
|
||||
- Auto-claude level fallback checks
|
||||
- ValidationResult return values
|
||||
"""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HELPER FUNCTIONS
|
||||
# =============================================================================
|
||||
|
||||
def clean_project_index_files(spec_dir: Path) -> None:
|
||||
"""Remove project_index.json files that may interfere with tests.
|
||||
|
||||
Cleans up both:
|
||||
- spec_dir / "project_index.json"
|
||||
- spec_dir.parent.parent / "project_index.json" (auto-claude level)
|
||||
|
||||
This prevents test isolation issues when tests share the same temp_dir parent.
|
||||
"""
|
||||
# Clean spec_dir level
|
||||
spec_index = spec_dir / "project_index.json"
|
||||
if spec_index.exists():
|
||||
spec_index.unlink()
|
||||
|
||||
# Clean auto-claude level (two levels up from spec_dir)
|
||||
auto_build_index = spec_dir.parent.parent / "project_index.json"
|
||||
if auto_build_index.exists():
|
||||
auto_build_index.unlink()
|
||||
|
||||
|
||||
class TestPrereqsValidatorInit:
|
||||
"""Tests for PrereqsValidator initialization."""
|
||||
|
||||
def test_initialization_with_path(self, spec_dir: Path):
|
||||
"""PrereqsValidator initializes with spec_dir path."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
|
||||
assert validator.spec_dir == spec_dir
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
|
||||
def test_converts_string_to_path(self, spec_dir: Path):
|
||||
"""PrereqsValidator converts string path to Path object."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
validator = PrereqsValidator(str(spec_dir))
|
||||
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
assert validator.spec_dir == spec_dir
|
||||
|
||||
|
||||
class TestValidateSpecDirMissing:
|
||||
"""Tests for validate() when spec directory does not exist."""
|
||||
|
||||
def test_returns_error_when_spec_dir_missing(self, temp_dir: Path):
|
||||
"""Should return error when spec directory does not exist."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
non_existent_dir = temp_dir / "nonexistent" / "spec"
|
||||
validator = PrereqsValidator(non_existent_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert result.checkpoint == "prereqs"
|
||||
assert len(result.errors) > 0
|
||||
assert any("does not exist" in err.lower() for err in result.errors)
|
||||
|
||||
def test_error_includes_directory_path(self, temp_dir: Path):
|
||||
"""Error message should include the directory path."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
non_existent_dir = temp_dir / "missing" / "spec"
|
||||
validator = PrereqsValidator(non_existent_dir)
|
||||
result = validator.validate()
|
||||
|
||||
error_msg = result.errors[0]
|
||||
assert str(non_existent_dir) in error_msg
|
||||
|
||||
def test_fix_suggests_mkdir_command(self, temp_dir: Path):
|
||||
"""Suggested fix should include mkdir -p command."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
non_existent_dir = temp_dir / "new" / "spec"
|
||||
validator = PrereqsValidator(non_existent_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("mkdir" in fix.lower() for fix in result.fixes)
|
||||
assert any("-p" in fix for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateProjectIndexMissing:
|
||||
"""Tests for validate() when project_index.json is missing."""
|
||||
|
||||
def test_returns_error_when_project_index_missing(self, spec_dir: Path):
|
||||
"""Should return error when project_index.json does not exist."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
clean_project_index_files(spec_dir)
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert any("project_index.json" in err for err in result.errors)
|
||||
|
||||
def test_error_when_no_auto_claude_index(self, spec_dir: Path):
|
||||
"""Should error when project_index.json missing at both levels."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
clean_project_index_files(spec_dir)
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert not result.warnings # No warning if no auto-claude fallback exists
|
||||
|
||||
def test_fix_suggests_running_analyzer(self, spec_dir: Path):
|
||||
"""Suggested fix should suggest running analyzer.py."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
clean_project_index_files(spec_dir)
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("analyzer.py" in fix for fix in result.fixes)
|
||||
assert any("auto-claude" in fix for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateAutoClaudeFallback:
|
||||
"""Tests for validate() with auto-claude level project_index.json."""
|
||||
|
||||
def test_warns_when_auto_claude_index_exists(self, spec_dir: Path):
|
||||
"""Should warn when project_index.json exists at auto-claude/ level."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
# The validator checks spec_dir.parent.parent for the auto-claude index
|
||||
# Create project_index.json at the correct level (two levels up from spec_dir)
|
||||
auto_build_index = spec_dir.parent.parent / "project_index.json"
|
||||
auto_build_index.parent.mkdir(parents=True, exist_ok=True)
|
||||
auto_build_index.write_text('{"project_type": "single"}', encoding="utf-8")
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# When auto-claude index exists but spec_dir index doesn't, it's valid with a warning
|
||||
assert result.valid is True # Valid because warning path, not error path
|
||||
assert len(result.warnings) > 0
|
||||
assert any("auto-claude" in warn or "spec folder" in warn for warn in result.warnings)
|
||||
|
||||
def test_fix_suggests_copy_command(self, spec_dir: Path):
|
||||
"""Suggested fix should include cp command when auto-claude index exists."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
# Create project_index.json at the auto-claude level (two levels up)
|
||||
auto_build_index = spec_dir.parent.parent / "project_index.json"
|
||||
auto_build_index.parent.mkdir(parents=True, exist_ok=True)
|
||||
auto_build_index.write_text('{"project_type": "monorepo"}', encoding="utf-8")
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("cp" in fix for fix in result.fixes)
|
||||
assert any(str(auto_build_index) in fix for fix in result.fixes)
|
||||
|
||||
def test_no_warning_when_auto_claude_index_missing(self, spec_dir: Path):
|
||||
"""Should not warn when auto-claude level index also missing."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
clean_project_index_files(spec_dir)
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should be invalid since no index exists anywhere
|
||||
assert result.valid is False
|
||||
assert not any("auto-claude" in warn for warn in result.warnings)
|
||||
assert any("not found" in err for err in result.errors)
|
||||
|
||||
|
||||
class TestValidateValidPrereqs:
|
||||
"""Tests for validate() with valid prerequisites."""
|
||||
|
||||
def test_returns_valid_when_project_index_exists(self, spec_dir: Path):
|
||||
"""Should return valid when project_index.json exists in spec dir."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
project_index = spec_dir / "project_index.json"
|
||||
project_index.write_text('{"project_type": "single"}', encoding="utf-8")
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert result.checkpoint == "prereqs"
|
||||
assert len(result.errors) == 0
|
||||
|
||||
def test_valid_with_valid_project_index_content(self, spec_dir: Path):
|
||||
"""Should be valid with properly structured project_index.json."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
project_index = spec_dir / "project_index.json"
|
||||
project_index.write_text(json.dumps({
|
||||
"project_type": "monorepo",
|
||||
"services": {
|
||||
"backend": {"path": "backend", "language": "python"},
|
||||
"frontend": {"path": "frontend", "language": "typescript"},
|
||||
},
|
||||
"file_count": 150,
|
||||
}), encoding="utf-8")
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
|
||||
class TestValidationResultStructure:
|
||||
"""Tests for ValidationResult structure."""
|
||||
|
||||
def test_result_has_all_fields(self, spec_dir: Path):
|
||||
"""ValidationResult should have all expected fields."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert hasattr(result, "valid")
|
||||
assert hasattr(result, "checkpoint")
|
||||
assert hasattr(result, "errors")
|
||||
assert hasattr(result, "warnings")
|
||||
assert hasattr(result, "fixes")
|
||||
|
||||
def test_checkpoint_is_prereqs(self, spec_dir: Path):
|
||||
"""Checkpoint field should always be 'prereqs'."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.checkpoint == "prereqs"
|
||||
|
||||
def test_lists_are_initialized(self, spec_dir: Path):
|
||||
"""Errors, warnings, and fixes should always be lists."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert isinstance(result.errors, list)
|
||||
assert isinstance(result.warnings, list)
|
||||
assert isinstance(result.fixes, list)
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Tests for edge cases and boundary conditions."""
|
||||
|
||||
def test_handles_relative_paths(self, temp_dir: Path, monkeypatch):
|
||||
"""Should handle relative path arguments."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
# Create spec directory
|
||||
spec_path = temp_dir / "spec"
|
||||
spec_path.mkdir()
|
||||
|
||||
# Use relative path with monkeypatch for safe directory change
|
||||
relative_path = "spec"
|
||||
monkeypatch.chdir(temp_dir)
|
||||
validator = PrereqsValidator(relative_path)
|
||||
result = validator.validate()
|
||||
|
||||
# Should work (will be invalid since no project_index.json)
|
||||
assert result.checkpoint == "prereqs"
|
||||
|
||||
def test_handles_symlink_to_directory(self, temp_dir: Path):
|
||||
"""Should handle symlinks to directories."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
# Create actual spec directory
|
||||
actual_spec = temp_dir / "actual_spec"
|
||||
actual_spec.mkdir()
|
||||
|
||||
# Create symlink
|
||||
import os
|
||||
link_spec = temp_dir / "link_spec"
|
||||
try:
|
||||
os.symlink(actual_spec, link_spec)
|
||||
except OSError:
|
||||
# Symlinks may not be supported on all systems
|
||||
pytest.skip("Symlinks not supported")
|
||||
|
||||
validator = PrereqsValidator(link_spec)
|
||||
result = validator.validate()
|
||||
|
||||
# Should handle the symlinked directory
|
||||
assert result.checkpoint == "prereqs"
|
||||
|
||||
def test_multiple_validations_independent(self, spec_dir: Path):
|
||||
"""Multiple validations should be independent."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
clean_project_index_files(spec_dir)
|
||||
|
||||
validator1 = PrereqsValidator(spec_dir)
|
||||
result1 = validator1.validate()
|
||||
|
||||
# Create project_index.json between validations
|
||||
project_index = spec_dir / "project_index.json"
|
||||
project_index.write_text('{"project_type": "single"}', encoding="utf-8")
|
||||
|
||||
validator2 = PrereqsValidator(spec_dir)
|
||||
result2 = validator2.validate()
|
||||
|
||||
# First result should be invalid (no index existed at validation time)
|
||||
assert result1.valid is False
|
||||
# Second result should be valid (index now exists)
|
||||
assert result2.valid is True
|
||||
|
||||
def test_handles_empty_project_index(self, spec_dir: Path):
|
||||
"""Should handle empty project_index.json file."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
|
||||
project_index = spec_dir / "project_index.json"
|
||||
project_index.write_text("{}", encoding="utf-8")
|
||||
|
||||
validator = PrereqsValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should be valid since file exists (content validation not required)
|
||||
assert result.valid is True
|
||||
|
||||
|
||||
class TestPrereqsValidatorIntegration:
|
||||
"""Integration tests with other validators."""
|
||||
|
||||
def test_works_with_context_validator(self, spec_dir: Path):
|
||||
"""Should work correctly when used with ContextValidator."""
|
||||
from spec.validate_pkg.validators.prereqs_validator import PrereqsValidator
|
||||
from spec.validate_pkg.validators.context_validator import ContextValidator
|
||||
|
||||
# Create project_index.json
|
||||
project_index = spec_dir / "project_index.json"
|
||||
project_index.write_text('{"project_type": "single"}', encoding="utf-8")
|
||||
|
||||
prereq_validator = PrereqsValidator(spec_dir)
|
||||
prereq_result = prereq_validator.validate()
|
||||
|
||||
context_validator = ContextValidator(spec_dir)
|
||||
context_result = context_validator.validate()
|
||||
|
||||
# Prereqs should be valid
|
||||
assert prereq_result.valid is True
|
||||
# Context should be invalid (no context.json)
|
||||
assert context_result.valid is False
|
||||
@@ -0,0 +1,486 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for spec/validate_pkg/validators/spec_document_validator.py
|
||||
=================================================================
|
||||
|
||||
Tests for SpecDocumentValidator class covering:
|
||||
- File existence checks
|
||||
- Required section validation
|
||||
- Recommended section warnings
|
||||
- Content length validation
|
||||
- ValidationResult return values
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TestSpecDocumentValidatorInit:
|
||||
"""Tests for SpecDocumentValidator initialization."""
|
||||
|
||||
def test_initialization_with_path(self, spec_dir: Path):
|
||||
"""SpecDocumentValidator initializes with spec_dir path."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
|
||||
assert validator.spec_dir == spec_dir
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
|
||||
def test_converts_string_to_path(self, spec_dir: Path):
|
||||
"""SpecDocumentValidator converts string path to Path object."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
validator = SpecDocumentValidator(str(spec_dir))
|
||||
|
||||
assert isinstance(validator.spec_dir, Path)
|
||||
assert validator.spec_dir == spec_dir
|
||||
|
||||
|
||||
class TestValidateFileNotFound:
|
||||
"""Tests for validate() when spec.md does not exist."""
|
||||
|
||||
def test_returns_error_when_file_missing(self, spec_dir: Path):
|
||||
"""Should return ValidationResult with error when spec.md missing."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert result.checkpoint == "spec"
|
||||
assert any("not found" in err.lower() or "spec.md" in err.lower() for err in result.errors)
|
||||
|
||||
def test_error_message_includes_filename(self, spec_dir: Path):
|
||||
"""Error message should mention spec.md."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert "spec.md" in result.errors[0]
|
||||
|
||||
def test_fix_suggests_creation(self, spec_dir: Path):
|
||||
"""Suggested fix should mention creating spec.md."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("create" in fix.lower() for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateRequiredSections:
|
||||
"""Tests for validate() with missing required sections."""
|
||||
|
||||
def test_error_when_overview_missing(self, spec_dir: Path):
|
||||
"""Should error when required section 'Overview' is missing."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("# Other Section\n\nContent here.\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert any("overview" in err.lower() for err in result.errors)
|
||||
|
||||
def test_error_for_all_required_sections_missing(self, spec_dir: Path):
|
||||
"""Should list all missing required sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
from spec.validate_pkg.schemas import SPEC_REQUIRED_SECTIONS
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("# Other\n\nContent.\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Check that all required sections are mentioned in errors
|
||||
for section in SPEC_REQUIRED_SECTIONS:
|
||||
assert any(section.lower() in err.lower() for err in result.errors), \
|
||||
f"Section {section} not in errors"
|
||||
|
||||
def test_accepts_hash_hash_format(self, spec_dir: Path):
|
||||
"""Should accept ## Section format (double hash)."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nContent\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert len(result.errors) == 0
|
||||
|
||||
def test_accepts_single_hash_format(self, spec_dir: Path):
|
||||
"""Should accept # Section format (single hash)."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "# Overview\n\nContent\n\n# Workflow Type\n\nFeature\n\n"
|
||||
content += "# Task Scope\n\nScope\n\n# Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_case_insensitive_section_matching(self, spec_dir: Path):
|
||||
"""Should match sections case-insensitively."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## OVERVIEW\n\nContent\n\n## workflow type\n\nFeature\n\n"
|
||||
content += "## task scope\n\nScope\n\n## success criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_fixes_suggest_adding_sections(self, spec_dir: Path):
|
||||
"""Suggested fixes should include adding missing sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("# Other\n\nContent.\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Fixes should suggest adding sections
|
||||
assert any("##" in fix for fix in result.fixes)
|
||||
|
||||
|
||||
class TestValidateRecommendedSections:
|
||||
"""Tests for validate() with recommended sections."""
|
||||
|
||||
def test_warns_when_files_to_modify_missing(self, spec_dir: Path):
|
||||
"""Should warn when 'Files to Modify' section is missing."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nContent\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Missing recommended section should be a warning, not error
|
||||
assert any("files to modify" in warn.lower() for warn in result.warnings)
|
||||
|
||||
def test_warns_for_multiple_missing_recommended(self, spec_dir: Path):
|
||||
"""Should warn for all missing recommended sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nContent\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should have warnings for missing recommended sections
|
||||
assert len(result.warnings) > 0
|
||||
|
||||
def test_no_warnings_with_all_recommended(self, spec_dir: Path):
|
||||
"""Should not warn when all recommended sections present."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
from spec.validate_pkg.schemas import SPEC_RECOMMENDED_SECTIONS
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nThis is a comprehensive overview of the feature that we are building.\n\n"
|
||||
content += "## Workflow Type\n\nFeature implementation workflow with multiple phases.\n\n"
|
||||
content += "## Task Scope\n\nThe scope includes backend API changes and database updates.\n\n"
|
||||
content += "## Success Criteria\n\nAll tests pass and the feature works as expected.\n\n"
|
||||
|
||||
# Add all recommended sections with substantial content
|
||||
for section in SPEC_RECOMMENDED_SECTIONS:
|
||||
content += f"## {section}\n\nThis section contains detailed information about {section.lower()}. "
|
||||
content += "We need to ensure that all requirements are properly documented and reviewed.\n\n"
|
||||
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert len(result.warnings) == 0
|
||||
|
||||
|
||||
class TestValidateContentLength:
|
||||
"""Tests for content length validation."""
|
||||
|
||||
def test_warns_when_content_too_short(self, spec_dir: Path):
|
||||
"""Should warn when spec.md is less than 500 characters."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nShort.\n\n## Workflow Type\n\nX\n\n"
|
||||
content += "## Task Scope\n\nY\n\n## Success Criteria\n\nZ\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert any("too short" in warn.lower() for warn in result.warnings)
|
||||
|
||||
def test_no_warning_for_adequate_length(self, spec_dir: Path):
|
||||
"""Should not warn when spec.md has adequate length."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
# Create content longer than 500 characters
|
||||
content = "## Overview\n\n" + "X" * 600 + "\n\n"
|
||||
content += "## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n"
|
||||
content += "## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert not any("too short" in warn.lower() for warn in result.warnings)
|
||||
|
||||
def test_content_check_counts_all_characters(self, spec_dir: Path):
|
||||
"""Content length check should count all characters including whitespace."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
# Create content exactly over 500 characters with mixed content
|
||||
content = "## Overview\n\n" + "A" * 480 + "\n\n"
|
||||
content += "## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n"
|
||||
content += "## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should not have length warning
|
||||
assert not any("too short" in warn.lower() for warn in result.warnings)
|
||||
|
||||
|
||||
class TestValidateValidSpec:
|
||||
"""Tests for validate() with valid spec.md."""
|
||||
|
||||
def test_returns_valid_for_minimal_spec(self, spec_dir: Path):
|
||||
"""Should return valid with minimal required sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nImplement feature.\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nAdd user auth.\n\n## Success Criteria\n\nTests pass.\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert result.checkpoint == "spec"
|
||||
# May have warnings about recommended sections or length
|
||||
|
||||
def test_returns_valid_with_comprehensive_spec(self, spec_dir: Path):
|
||||
"""Should return valid with comprehensive spec document."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
from spec.validate_pkg.schemas import SPEC_REQUIRED_SECTIONS, SPEC_RECOMMENDED_SECTIONS
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = ""
|
||||
|
||||
# Add all required sections
|
||||
for section in SPEC_REQUIRED_SECTIONS:
|
||||
content += f"## {section}\n\nDetailed content for {section}.\n\n"
|
||||
|
||||
# Add all recommended sections
|
||||
for section in SPEC_RECOMMENDED_SECTIONS:
|
||||
content += f"## {section}\n\nDetails about {section}.\n\n"
|
||||
|
||||
# Add more content to avoid length warning
|
||||
content += "Additional implementation details..." * 50
|
||||
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
assert len(result.errors) == 0
|
||||
assert len(result.warnings) == 0
|
||||
|
||||
|
||||
class TestValidationResultStructure:
|
||||
"""Tests for ValidationResult structure."""
|
||||
|
||||
def test_result_has_all_fields(self, spec_dir: Path):
|
||||
"""ValidationResult should have all expected fields."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("## Overview\n\nContent\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert hasattr(result, "valid")
|
||||
assert hasattr(result, "checkpoint")
|
||||
assert hasattr(result, "errors")
|
||||
assert hasattr(result, "warnings")
|
||||
assert hasattr(result, "fixes")
|
||||
|
||||
def test_checkpoint_is_spec(self, spec_dir: Path):
|
||||
"""Checkpoint field should always be 'spec'."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("## Overview\n\nContent\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.checkpoint == "spec"
|
||||
|
||||
def test_lists_are_initialized(self, spec_dir: Path):
|
||||
"""Errors, warnings, and fixes should always be lists."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("## Overview\n\nContent\n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert isinstance(result.errors, list)
|
||||
assert isinstance(result.warnings, list)
|
||||
assert isinstance(result.fixes, list)
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""Tests for edge cases and boundary conditions."""
|
||||
|
||||
def test_handles_unicode_in_spec(self, spec_dir: Path):
|
||||
"""Should handle unicode characters in spec.md."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\n添加用户认证功能\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\n范围\n\n## Success Criteria\n\n完成\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_handles_extra_whitespace(self, spec_dir: Path):
|
||||
"""Should handle extra whitespace in sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview \n\nContent\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should still match despite extra whitespace
|
||||
assert result.valid is True
|
||||
|
||||
def test_handles_mixed_heading_levels(self, spec_dir: Path):
|
||||
"""Should handle spec with various heading levels."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview\n\nContent\n\n### Subsection\n\nDetails\n\n"
|
||||
content += "## Workflow Type\n\nFeature\n\n## Task Scope\n\nScope\n\n"
|
||||
content += "## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is True
|
||||
|
||||
def test_section_pattern_excludes_subsections(self, spec_dir: Path):
|
||||
"""Should not match subsections (###) as main sections."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
# Only has subsections, not main sections
|
||||
content = "### Overview\n\nContent\n\n### Workflow Type\n\nFeature\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should be invalid - ### doesn't count as ## or #
|
||||
assert result.valid is False
|
||||
|
||||
def test_handles_empty_spec_file(self, spec_dir: Path):
|
||||
"""Should handle empty spec.md file."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text("", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
# Should warn about being too short
|
||||
assert any("too short" in warn.lower() for warn in result.warnings)
|
||||
|
||||
def test_handles_spec_with_only_whitespace(self, spec_dir: Path):
|
||||
"""Should handle spec.md with only whitespace."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
spec_file.write_text(" \n\n \n", encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
assert result.valid is False
|
||||
assert any("too short" in warn.lower() for warn in result.warnings)
|
||||
|
||||
|
||||
class TestSectionMatching:
|
||||
"""Tests for section heading pattern matching."""
|
||||
|
||||
def test_matches_section_with_trailing_colon(self, spec_dir: Path):
|
||||
"""Should match sections with trailing colon."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview:\n\nContent\n\n## Workflow Type:\n\nFeature\n\n"
|
||||
content += "## Task Scope:\n\nScope\n\n## Success Criteria:\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should match despite trailing colon
|
||||
assert result.valid is True
|
||||
|
||||
def test_matches_section_with_special_chars(self, spec_dir: Path):
|
||||
"""Should match sections with special characters."""
|
||||
from spec.validate_pkg.validators.spec_document_validator import SpecDocumentValidator
|
||||
|
||||
spec_file = spec_dir / "spec.md"
|
||||
content = "## Overview (v2.0)\n\nContent\n\n## Workflow Type\n\nFeature\n\n"
|
||||
content += "## Task Scope\n\nScope\n\n## Success Criteria\n\nDone\n"
|
||||
spec_file.write_text(content, encoding="utf-8")
|
||||
|
||||
validator = SpecDocumentValidator(spec_dir)
|
||||
result = validator.validate()
|
||||
|
||||
# Should still match
|
||||
assert result.valid is True
|
||||
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
Tests for Structured Output Recovery
|
||||
======================================
|
||||
|
||||
Tests the three-tier recovery cascade when structured output validation fails:
|
||||
1. FollowupExtractionResponse model validation
|
||||
2. Error categorization imported from sdk_utils
|
||||
3. Agent config registration for pr_followup_extraction
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
# Add paths for imports — conftest.py adds apps/backend, but there's a
|
||||
# services/ package at both apps/backend/services/ and runners/github/services/.
|
||||
# To avoid collision, add the github services dir directly and import bare module names.
|
||||
_backend_dir = Path(__file__).parent.parent / "apps" / "backend"
|
||||
_github_services_dir = _backend_dir / "runners" / "github" / "services"
|
||||
if str(_backend_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_backend_dir))
|
||||
if str(_github_services_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_github_services_dir))
|
||||
|
||||
from agents.tools_pkg.models import AGENT_CONFIGS
|
||||
from pydantic_models import (
|
||||
FollowupExtractionResponse,
|
||||
ParallelFollowupResponse,
|
||||
)
|
||||
from sdk_utils import RECOVERABLE_ERRORS
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test FollowupExtractionResponse model
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class TestFollowupExtractionResponse:
|
||||
"""Tests for the minimal extraction schema."""
|
||||
|
||||
def test_minimal_valid_response(self):
|
||||
"""Accepts minimal response with just verdict and reasoning."""
|
||||
resp = FollowupExtractionResponse(
|
||||
verdict="NEEDS_REVISION",
|
||||
verdict_reasoning="Found issues that need fixing",
|
||||
)
|
||||
assert resp.verdict == "NEEDS_REVISION"
|
||||
assert resp.resolved_finding_ids == []
|
||||
assert resp.new_finding_summaries == []
|
||||
assert resp.confirmed_finding_count == 0
|
||||
assert resp.dismissed_finding_count == 0
|
||||
|
||||
def test_full_valid_response(self):
|
||||
"""Accepts fully populated response."""
|
||||
resp = FollowupExtractionResponse(
|
||||
verdict="READY_TO_MERGE",
|
||||
verdict_reasoning="All findings resolved",
|
||||
resolved_finding_ids=["NCR-001", "NCR-002"],
|
||||
unresolved_finding_ids=[],
|
||||
new_finding_summaries=["HIGH: potential cleanup issue in batch_commands.py"],
|
||||
confirmed_finding_count=1,
|
||||
dismissed_finding_count=1,
|
||||
)
|
||||
assert len(resp.resolved_finding_ids) == 2
|
||||
assert len(resp.new_finding_summaries) == 1
|
||||
assert resp.confirmed_finding_count == 1
|
||||
|
||||
def test_schema_is_small(self):
|
||||
"""Schema should be significantly smaller than ParallelFollowupResponse."""
|
||||
extraction_schema = json.dumps(
|
||||
FollowupExtractionResponse.model_json_schema()
|
||||
)
|
||||
followup_schema = json.dumps(
|
||||
ParallelFollowupResponse.model_json_schema()
|
||||
)
|
||||
# Extraction schema should be less than half the size of the full schema
|
||||
assert len(extraction_schema) < len(followup_schema) / 2, (
|
||||
f"Extraction schema ({len(extraction_schema)} chars) should be "
|
||||
f"less than half of full schema ({len(followup_schema)} chars)"
|
||||
)
|
||||
|
||||
def test_all_verdict_values_accepted(self):
|
||||
"""All four verdict values should be accepted."""
|
||||
for verdict in ["READY_TO_MERGE", "MERGE_WITH_CHANGES", "NEEDS_REVISION", "BLOCKED"]:
|
||||
resp = FollowupExtractionResponse(
|
||||
verdict=verdict,
|
||||
verdict_reasoning=f"Test {verdict}",
|
||||
)
|
||||
assert resp.verdict == verdict
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test error categorization using the actual RECOVERABLE_ERRORS from sdk_utils
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class TestErrorCategorization:
|
||||
"""Tests that sdk_utils RECOVERABLE_ERRORS constant classifies errors correctly."""
|
||||
|
||||
def test_structured_output_error_is_recoverable(self):
|
||||
"""structured_output_validation_failed should be in RECOVERABLE_ERRORS."""
|
||||
assert "structured_output_validation_failed" in RECOVERABLE_ERRORS
|
||||
|
||||
def test_concurrency_error_is_recoverable(self):
|
||||
"""tool_use_concurrency_error should be in RECOVERABLE_ERRORS."""
|
||||
assert "tool_use_concurrency_error" in RECOVERABLE_ERRORS
|
||||
|
||||
def test_auth_error_is_fatal(self):
|
||||
"""Auth errors should NOT be in RECOVERABLE_ERRORS."""
|
||||
assert "Authentication error detected in AI response: please login again" not in RECOVERABLE_ERRORS
|
||||
|
||||
def test_circuit_breaker_is_fatal(self):
|
||||
"""Circuit breaker errors should NOT be in RECOVERABLE_ERRORS."""
|
||||
for error in RECOVERABLE_ERRORS:
|
||||
assert "circuit breaker" not in error.lower()
|
||||
|
||||
def test_none_is_not_recoverable(self):
|
||||
"""None should not be in RECOVERABLE_ERRORS."""
|
||||
assert None not in RECOVERABLE_ERRORS
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test agent config registration
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class TestAgentConfigRegistration:
|
||||
"""Tests that pr_followup_extraction agent type is registered."""
|
||||
|
||||
def test_extraction_agent_type_registered(self):
|
||||
"""pr_followup_extraction must exist in AGENT_CONFIGS."""
|
||||
assert "pr_followup_extraction" in AGENT_CONFIGS
|
||||
|
||||
def test_extraction_agent_needs_no_tools(self):
|
||||
"""Extraction agent should have no tools (pure structured output)."""
|
||||
config = AGENT_CONFIGS["pr_followup_extraction"]
|
||||
assert config["tools"] == []
|
||||
assert config["mcp_servers"] == []
|
||||
|
||||
def test_extraction_agent_low_thinking(self):
|
||||
"""Extraction agent should use low thinking (lightweight call)."""
|
||||
config = AGENT_CONFIGS["pr_followup_extraction"]
|
||||
assert config["thinking_default"] == "low"
|
||||
@@ -0,0 +1,715 @@
|
||||
"""
|
||||
Tests for Pydantic Structured Output Models
|
||||
============================================
|
||||
|
||||
Tests the Pydantic models used for Claude Agent SDK structured outputs
|
||||
in GitHub PR reviews.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
# Direct import of pydantic_models to avoid runners package chain
|
||||
# Path is set up by conftest.py
|
||||
_pydantic_models_path = (
|
||||
Path(__file__).parent.parent
|
||||
/ "apps"
|
||||
/ "backend"
|
||||
/ "runners"
|
||||
/ "github"
|
||||
/ "services"
|
||||
)
|
||||
sys.path.insert(0, str(_pydantic_models_path))
|
||||
|
||||
from pydantic_models import (
|
||||
# Follow-up review models
|
||||
FindingResolution,
|
||||
FollowupFinding,
|
||||
FollowupReviewResponse,
|
||||
# Orchestrator review models
|
||||
OrchestratorFinding,
|
||||
OrchestratorReviewResponse,
|
||||
# Initial review models
|
||||
QuickScanResult,
|
||||
SecurityFinding,
|
||||
QualityFinding,
|
||||
DeepAnalysisFinding,
|
||||
StructuralIssue,
|
||||
AICommentTriage,
|
||||
# Verification evidence models (Phase 2)
|
||||
VerificationEvidence,
|
||||
ParallelOrchestratorFinding,
|
||||
BaseFinding,
|
||||
)
|
||||
|
||||
|
||||
class TestFindingResolution:
|
||||
"""Tests for FindingResolution model."""
|
||||
|
||||
def test_valid_resolution_resolved(self):
|
||||
"""Test valid resolved finding."""
|
||||
data = {
|
||||
"finding_id": "prev-1",
|
||||
"status": "resolved",
|
||||
"resolution_notes": "Fixed in commit abc123",
|
||||
}
|
||||
result = FindingResolution.model_validate(data)
|
||||
assert result.finding_id == "prev-1"
|
||||
assert result.status == "resolved"
|
||||
assert result.resolution_notes == "Fixed in commit abc123"
|
||||
|
||||
def test_valid_resolution_unresolved(self):
|
||||
"""Test valid unresolved finding."""
|
||||
data = {
|
||||
"finding_id": "prev-2",
|
||||
"status": "unresolved",
|
||||
}
|
||||
result = FindingResolution.model_validate(data)
|
||||
assert result.status == "unresolved"
|
||||
assert result.resolution_notes is None
|
||||
|
||||
def test_invalid_status_rejected(self):
|
||||
"""Test that invalid status values are rejected."""
|
||||
data = {
|
||||
"finding_id": "prev-1",
|
||||
"status": "pending", # Invalid - not in Literal
|
||||
}
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
FindingResolution.model_validate(data)
|
||||
assert "status" in str(exc_info.value)
|
||||
|
||||
|
||||
class TestFollowupFinding:
|
||||
"""Tests for FollowupFinding model."""
|
||||
|
||||
def test_valid_finding(self):
|
||||
"""Test valid follow-up finding."""
|
||||
data = {
|
||||
"id": "new-1",
|
||||
"severity": "high",
|
||||
"category": "security",
|
||||
"title": "SQL Injection vulnerability",
|
||||
"description": "User input not sanitized before query",
|
||||
"file": "api/query.py",
|
||||
"line": 42,
|
||||
"suggested_fix": "Use parameterized queries",
|
||||
"fixable": True,
|
||||
"verification": {
|
||||
"code_examined": "query = 'SELECT * FROM users WHERE id=' + user_input",
|
||||
"line_range_examined": [42, 42],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = FollowupFinding.model_validate(data)
|
||||
assert result.id == "new-1"
|
||||
assert result.severity == "high"
|
||||
assert result.category == "security"
|
||||
assert result.line == 42
|
||||
assert result.fixable is True
|
||||
|
||||
def test_minimal_finding(self):
|
||||
"""Test finding with only required fields."""
|
||||
data = {
|
||||
"id": "new-2",
|
||||
"severity": "low",
|
||||
"category": "docs",
|
||||
"title": "Missing docstring",
|
||||
"description": "Function lacks documentation",
|
||||
"file": "utils.py",
|
||||
"verification": {
|
||||
"code_examined": "def process_data(data):\n return data",
|
||||
"line_range_examined": [1, 2],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = FollowupFinding.model_validate(data)
|
||||
assert result.line == 0 # Default
|
||||
assert result.suggested_fix is None
|
||||
assert result.fixable is False
|
||||
|
||||
def test_invalid_severity_rejected(self):
|
||||
"""Test that invalid severity is rejected."""
|
||||
data = {
|
||||
"id": "new-1",
|
||||
"severity": "extreme", # Invalid
|
||||
"category": "security",
|
||||
"title": "Test",
|
||||
"description": "Test",
|
||||
"file": "test.py",
|
||||
}
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
FollowupFinding.model_validate(data)
|
||||
assert "severity" in str(exc_info.value)
|
||||
|
||||
def test_invalid_category_rejected(self):
|
||||
"""Test that invalid category is rejected."""
|
||||
data = {
|
||||
"id": "new-1",
|
||||
"severity": "high",
|
||||
"category": "unknown_category", # Invalid
|
||||
"title": "Test",
|
||||
"description": "Test",
|
||||
"file": "test.py",
|
||||
}
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
FollowupFinding.model_validate(data)
|
||||
assert "category" in str(exc_info.value)
|
||||
|
||||
|
||||
class TestFollowupReviewResponse:
|
||||
"""Tests for FollowupReviewResponse model."""
|
||||
|
||||
def test_valid_complete_response(self):
|
||||
"""Test valid complete follow-up review response."""
|
||||
data = {
|
||||
"finding_resolutions": [
|
||||
{"finding_id": "prev-1", "status": "resolved", "resolution_notes": "Fixed"}
|
||||
],
|
||||
"new_findings": [
|
||||
{
|
||||
"id": "new-1",
|
||||
"severity": "medium",
|
||||
"category": "quality",
|
||||
"title": "Code smell",
|
||||
"description": "Complex method",
|
||||
"file": "service.py",
|
||||
"line": 100,
|
||||
"verification": {
|
||||
"code_examined": "def process(self, data):\n # 50 lines of nested if statements",
|
||||
"line_range_examined": [100, 150],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
],
|
||||
"comment_findings": [],
|
||||
"verdict": "MERGE_WITH_CHANGES",
|
||||
"verdict_reasoning": "Minor issues found, safe to merge after review",
|
||||
}
|
||||
result = FollowupReviewResponse.model_validate(data)
|
||||
assert result.verdict == "MERGE_WITH_CHANGES"
|
||||
assert len(result.finding_resolutions) == 1
|
||||
assert len(result.new_findings) == 1
|
||||
assert len(result.comment_findings) == 0
|
||||
|
||||
def test_empty_findings_lists(self):
|
||||
"""Test response with empty findings lists."""
|
||||
data = {
|
||||
"finding_resolutions": [],
|
||||
"new_findings": [],
|
||||
"comment_findings": [],
|
||||
"verdict": "READY_TO_MERGE",
|
||||
"verdict_reasoning": "No issues found",
|
||||
}
|
||||
result = FollowupReviewResponse.model_validate(data)
|
||||
assert result.verdict == "READY_TO_MERGE"
|
||||
|
||||
def test_invalid_verdict_rejected(self):
|
||||
"""Test that invalid verdict is rejected."""
|
||||
data = {
|
||||
"finding_resolutions": [],
|
||||
"new_findings": [],
|
||||
"comment_findings": [],
|
||||
"verdict": "APPROVE", # Invalid
|
||||
"verdict_reasoning": "Test",
|
||||
}
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
FollowupReviewResponse.model_validate(data)
|
||||
assert "verdict" in str(exc_info.value)
|
||||
|
||||
def test_all_verdict_values(self):
|
||||
"""Test all valid verdict values."""
|
||||
for verdict in [
|
||||
"READY_TO_MERGE",
|
||||
"MERGE_WITH_CHANGES",
|
||||
"NEEDS_REVISION",
|
||||
"BLOCKED",
|
||||
]:
|
||||
data = {
|
||||
"finding_resolutions": [],
|
||||
"new_findings": [],
|
||||
"comment_findings": [],
|
||||
"verdict": verdict,
|
||||
"verdict_reasoning": f"Testing {verdict}",
|
||||
}
|
||||
result = FollowupReviewResponse.model_validate(data)
|
||||
assert result.verdict == verdict
|
||||
|
||||
|
||||
class TestOrchestratorFinding:
|
||||
"""Tests for OrchestratorFinding model."""
|
||||
|
||||
def test_valid_finding(self):
|
||||
"""Test valid orchestrator finding with evidence field."""
|
||||
data = {
|
||||
"file": "src/api.py",
|
||||
"line": 25,
|
||||
"title": "Missing error handling",
|
||||
"description": "API endpoint lacks try-catch block",
|
||||
"category": "quality",
|
||||
"severity": "medium",
|
||||
"suggestion": "Add error handling with proper logging",
|
||||
"evidence": "def handle_request(req):\n result = db.query(req.id) # no try-catch",
|
||||
"verification": {
|
||||
"code_examined": "def handle_request(req):\n result = db.query(req.id) # no try-catch",
|
||||
"line_range_examined": [25, 26],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = OrchestratorFinding.model_validate(data)
|
||||
assert result.file == "src/api.py"
|
||||
assert result.evidence is not None
|
||||
assert "no try-catch" in result.evidence
|
||||
|
||||
def test_evidence_optional(self):
|
||||
"""Test that evidence field is optional."""
|
||||
data = {
|
||||
"file": "test.py",
|
||||
"title": "Test",
|
||||
"description": "Test finding",
|
||||
"category": "quality",
|
||||
"severity": "low",
|
||||
"verification": {
|
||||
"code_examined": "def test():\n pass",
|
||||
"line_range_examined": [1, 2],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = OrchestratorFinding.model_validate(data)
|
||||
assert result.evidence is None
|
||||
|
||||
|
||||
class TestOrchestratorReviewResponse:
|
||||
"""Tests for OrchestratorReviewResponse model."""
|
||||
|
||||
def test_valid_response(self):
|
||||
"""Test valid orchestrator review response."""
|
||||
data = {
|
||||
"verdict": "NEEDS_REVISION",
|
||||
"verdict_reasoning": "Critical security issue found",
|
||||
"findings": [
|
||||
{
|
||||
"file": "auth.py",
|
||||
"line": 10,
|
||||
"title": "Hardcoded secret",
|
||||
"description": "API key exposed in source",
|
||||
"category": "security",
|
||||
"severity": "critical",
|
||||
"evidence": "API_KEY = 'sk-prod-12345abcdef'",
|
||||
"verification": {
|
||||
"code_examined": "API_KEY = 'sk-prod-12345abcdef'",
|
||||
"line_range_examined": [10, 10],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
],
|
||||
"summary": "Found 1 critical security issue",
|
||||
}
|
||||
result = OrchestratorReviewResponse.model_validate(data)
|
||||
assert result.verdict == "NEEDS_REVISION"
|
||||
assert len(result.findings) == 1
|
||||
assert result.findings[0].severity == "critical"
|
||||
|
||||
def test_empty_findings(self):
|
||||
"""Test response with no findings."""
|
||||
data = {
|
||||
"verdict": "READY_TO_MERGE",
|
||||
"verdict_reasoning": "All checks passed",
|
||||
"findings": [],
|
||||
"summary": "Clean PR, ready for merge",
|
||||
}
|
||||
result = OrchestratorReviewResponse.model_validate(data)
|
||||
assert len(result.findings) == 0
|
||||
|
||||
|
||||
class TestQuickScanResult:
|
||||
"""Tests for QuickScanResult model."""
|
||||
|
||||
def test_valid_quick_scan(self):
|
||||
"""Test valid quick scan result."""
|
||||
data = {
|
||||
"purpose": "Add user authentication",
|
||||
"actual_changes": "Implements OAuth login flow",
|
||||
"purpose_match": True,
|
||||
"risk_areas": ["Security", "Session management"],
|
||||
"red_flags": [],
|
||||
"requires_deep_verification": True,
|
||||
"complexity": "medium",
|
||||
}
|
||||
result = QuickScanResult.model_validate(data)
|
||||
assert result.purpose_match is True
|
||||
assert result.complexity == "medium"
|
||||
assert len(result.risk_areas) == 2
|
||||
|
||||
def test_complexity_values(self):
|
||||
"""Test all valid complexity values."""
|
||||
for complexity in ["low", "medium", "high"]:
|
||||
data = {
|
||||
"purpose": "Test",
|
||||
"actual_changes": "Test",
|
||||
"purpose_match": True,
|
||||
"requires_deep_verification": False,
|
||||
"complexity": complexity,
|
||||
}
|
||||
result = QuickScanResult.model_validate(data)
|
||||
assert result.complexity == complexity
|
||||
|
||||
|
||||
class TestSchemaGeneration:
|
||||
"""Tests for JSON schema generation."""
|
||||
|
||||
def test_followup_schema_generation(self):
|
||||
"""Test that FollowupReviewResponse generates valid JSON schema."""
|
||||
schema = FollowupReviewResponse.model_json_schema()
|
||||
|
||||
assert "properties" in schema
|
||||
assert "verdict" in schema["properties"]
|
||||
assert "verdict_reasoning" in schema["properties"]
|
||||
assert "finding_resolutions" in schema["properties"]
|
||||
assert "new_findings" in schema["properties"]
|
||||
|
||||
# Check verdict enum values
|
||||
verdict_schema = schema["properties"]["verdict"]
|
||||
assert "enum" in verdict_schema or "$ref" in str(schema)
|
||||
|
||||
def test_orchestrator_schema_generation(self):
|
||||
"""Test that OrchestratorReviewResponse generates valid JSON schema."""
|
||||
schema = OrchestratorReviewResponse.model_json_schema()
|
||||
|
||||
assert "properties" in schema
|
||||
assert "verdict" in schema["properties"]
|
||||
assert "findings" in schema["properties"]
|
||||
assert "summary" in schema["properties"]
|
||||
|
||||
def test_schema_has_descriptions(self):
|
||||
"""Test that schema includes field descriptions for AI guidance."""
|
||||
schema = FollowupReviewResponse.model_json_schema()
|
||||
|
||||
# Check that descriptions are included (helps AI understand the schema)
|
||||
# The schema may have $defs for nested models
|
||||
assert "properties" in schema or "$defs" in schema
|
||||
|
||||
|
||||
class TestSecurityFinding:
|
||||
"""Tests for SecurityFinding model."""
|
||||
|
||||
def test_security_category_default(self):
|
||||
"""Test that SecurityFinding has security category by default."""
|
||||
data = {
|
||||
"id": "sec-1",
|
||||
"severity": "high",
|
||||
"title": "XSS vulnerability",
|
||||
"description": "Unescaped user input",
|
||||
"file": "template.html",
|
||||
"line": 50,
|
||||
"verification": {
|
||||
"code_examined": "<div>{{ user_input }}</div>",
|
||||
"line_range_examined": [50, 50],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = SecurityFinding.model_validate(data)
|
||||
assert result.category == "security"
|
||||
|
||||
|
||||
class TestDeepAnalysisFinding:
|
||||
"""Tests for DeepAnalysisFinding model."""
|
||||
|
||||
def test_evidence_field(self):
|
||||
"""Test evidence field for proof of issue."""
|
||||
data = {
|
||||
"id": "deep-1",
|
||||
"severity": "medium",
|
||||
"title": "Potential race condition",
|
||||
"description": "Concurrent access without lock",
|
||||
"file": "worker.py",
|
||||
"line": 100,
|
||||
"category": "logic",
|
||||
"evidence": "shared_state += 1 # no lock protection",
|
||||
"verification": {
|
||||
"code_examined": "shared_state += 1 # no lock protection",
|
||||
"line_range_examined": [100, 100],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = DeepAnalysisFinding.model_validate(data)
|
||||
assert result.evidence == "shared_state += 1 # no lock protection"
|
||||
|
||||
def test_verification_note(self):
|
||||
"""Test verification note field."""
|
||||
data = {
|
||||
"id": "deep-2",
|
||||
"severity": "low",
|
||||
"title": "Unverified assumption",
|
||||
"description": "Could not verify behavior",
|
||||
"file": "lib.py",
|
||||
"category": "verification_failed",
|
||||
"verification_note": "Unable to find test coverage",
|
||||
"verification": {
|
||||
"code_examined": "def some_function():\n return process_data()",
|
||||
"line_range_examined": [1, 2],
|
||||
"verification_method": "cross_file_trace",
|
||||
},
|
||||
}
|
||||
result = DeepAnalysisFinding.model_validate(data)
|
||||
assert result.verification_note == "Unable to find test coverage"
|
||||
|
||||
|
||||
class TestAICommentTriage:
|
||||
"""Tests for AICommentTriage model."""
|
||||
|
||||
def test_valid_triage(self):
|
||||
"""Test valid AI comment triage."""
|
||||
data = {
|
||||
"comment_id": 12345,
|
||||
"tool_name": "CodeRabbit",
|
||||
"verdict": "important",
|
||||
"reasoning": "Valid security concern raised",
|
||||
"response_comment": "Thank you, we will address this.",
|
||||
}
|
||||
result = AICommentTriage.model_validate(data)
|
||||
assert result.comment_id == 12345
|
||||
assert result.verdict == "important"
|
||||
|
||||
def test_all_verdict_values(self):
|
||||
"""Test all valid triage verdict values."""
|
||||
for verdict in [
|
||||
"critical",
|
||||
"important",
|
||||
"nice_to_have",
|
||||
"trivial",
|
||||
"false_positive",
|
||||
]:
|
||||
data = {
|
||||
"comment_id": 1,
|
||||
"tool_name": "Test",
|
||||
"verdict": verdict,
|
||||
"reasoning": f"Testing {verdict}",
|
||||
}
|
||||
result = AICommentTriage.model_validate(data)
|
||||
assert result.verdict == verdict
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Phase 2: Schema Enforcement Tests
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestVerificationEvidence:
|
||||
"""Tests for VerificationEvidence model."""
|
||||
|
||||
def test_valid_verification(self):
|
||||
"""Test valid verification evidence."""
|
||||
data = {
|
||||
"code_examined": "def process_input(user_input):\n return eval(user_input)",
|
||||
"line_range_examined": [10, 11],
|
||||
"verification_method": "direct_code_inspection",
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert "eval" in result.code_examined
|
||||
assert result.line_range_examined == [10, 11]
|
||||
assert result.verification_method == "direct_code_inspection"
|
||||
|
||||
def test_empty_code_examined_accepted(self):
|
||||
"""Test that empty code_examined is accepted (no min_length constraint)."""
|
||||
data = {
|
||||
"code_examined": "",
|
||||
"line_range_examined": [1, 5],
|
||||
"verification_method": "direct_code_inspection",
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert result.code_examined == ""
|
||||
|
||||
def test_line_range_defaults_to_empty_list(self):
|
||||
"""Test that line_range_examined defaults to empty list when omitted."""
|
||||
data = {
|
||||
"code_examined": "some code",
|
||||
"verification_method": "direct_code_inspection",
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert result.line_range_examined == []
|
||||
|
||||
def test_single_element_line_range_accepted(self):
|
||||
"""Test that single element line range is accepted (list[int])."""
|
||||
data = {
|
||||
"code_examined": "some code",
|
||||
"line_range_examined": [1],
|
||||
"verification_method": "direct_code_inspection",
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert result.line_range_examined == [1]
|
||||
|
||||
def test_custom_verification_method_accepted(self):
|
||||
"""Test that any string verification method is accepted."""
|
||||
data = {
|
||||
"code_examined": "some code",
|
||||
"line_range_examined": [1, 5],
|
||||
"verification_method": "custom_method",
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert result.verification_method == "custom_method"
|
||||
|
||||
def test_all_verification_methods(self):
|
||||
"""Test common verification methods."""
|
||||
methods = [
|
||||
"direct_code_inspection",
|
||||
"cross_file_trace",
|
||||
"test_verification",
|
||||
"dependency_analysis",
|
||||
]
|
||||
for method in methods:
|
||||
data = {
|
||||
"code_examined": "code",
|
||||
"line_range_examined": [1, 5],
|
||||
"verification_method": method,
|
||||
}
|
||||
result = VerificationEvidence.model_validate(data)
|
||||
assert result.verification_method == method
|
||||
|
||||
|
||||
class TestParallelOrchestratorFindingVerification:
|
||||
"""Tests for verification field requirement on ParallelOrchestratorFinding."""
|
||||
|
||||
def test_missing_verification_rejected(self):
|
||||
"""Test that findings without verification are rejected."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "test.py",
|
||||
"line": 10,
|
||||
"title": "Test finding",
|
||||
"description": "A test finding without verification",
|
||||
"category": "quality",
|
||||
"severity": "medium",
|
||||
# No verification field - should fail
|
||||
}
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
ParallelOrchestratorFinding.model_validate(data)
|
||||
assert "verification" in str(exc_info.value)
|
||||
|
||||
def test_valid_finding_with_verification(self):
|
||||
"""Test valid finding with verification evidence."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "test.py",
|
||||
"line": 10,
|
||||
"title": "SQL Injection vulnerability",
|
||||
"description": "User input passed directly to query",
|
||||
"category": "security",
|
||||
"severity": "critical",
|
||||
"verification": {
|
||||
"code_examined": "cursor.execute(f'SELECT * FROM users WHERE id={user_id}')",
|
||||
"line_range_examined": [10, 10],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = ParallelOrchestratorFinding.model_validate(data)
|
||||
assert result.verification.code_examined is not None
|
||||
assert result.verification.verification_method == "direct_code_inspection"
|
||||
|
||||
def test_is_impact_finding_default_false(self):
|
||||
"""Test is_impact_finding defaults to False."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "test.py",
|
||||
"line": 10,
|
||||
"title": "Test",
|
||||
"description": "Test",
|
||||
"category": "quality",
|
||||
"severity": "medium",
|
||||
"verification": {
|
||||
"code_examined": "code",
|
||||
"line_range_examined": [10, 10],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = ParallelOrchestratorFinding.model_validate(data)
|
||||
assert result.is_impact_finding is False
|
||||
|
||||
def test_is_impact_finding_true(self):
|
||||
"""Test is_impact_finding can be set True."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "caller.py",
|
||||
"line": 50,
|
||||
"title": "Breaking change affects caller",
|
||||
"description": "This file calls the changed function and will break",
|
||||
"category": "logic",
|
||||
"severity": "high",
|
||||
"is_impact_finding": True,
|
||||
"verification": {
|
||||
"code_examined": "result = changed_function(x)",
|
||||
"line_range_examined": [50, 50],
|
||||
"verification_method": "cross_file_trace",
|
||||
},
|
||||
}
|
||||
result = ParallelOrchestratorFinding.model_validate(data)
|
||||
assert result.is_impact_finding is True
|
||||
|
||||
def test_checked_for_handling_elsewhere_default_false(self):
|
||||
"""Test checked_for_handling_elsewhere defaults to False."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "test.py",
|
||||
"line": 10,
|
||||
"title": "Missing error handling",
|
||||
"description": "No try-catch",
|
||||
"category": "quality",
|
||||
"severity": "medium",
|
||||
"verification": {
|
||||
"code_examined": "code",
|
||||
"line_range_examined": [10, 10],
|
||||
"verification_method": "direct_code_inspection",
|
||||
},
|
||||
}
|
||||
result = ParallelOrchestratorFinding.model_validate(data)
|
||||
assert result.checked_for_handling_elsewhere is False
|
||||
|
||||
def test_checked_for_handling_elsewhere_true(self):
|
||||
"""Test checked_for_handling_elsewhere can be set True."""
|
||||
data = {
|
||||
"id": "test-1",
|
||||
"file": "api.py",
|
||||
"line": 25,
|
||||
"title": "Missing error handling",
|
||||
"description": "No try-catch around database call",
|
||||
"category": "quality",
|
||||
"severity": "medium",
|
||||
"checked_for_handling_elsewhere": True,
|
||||
"verification": {
|
||||
"code_examined": "result = db.query(user_input)",
|
||||
"line_range_examined": [25, 25],
|
||||
"verification_method": "cross_file_trace",
|
||||
},
|
||||
}
|
||||
result = ParallelOrchestratorFinding.model_validate(data)
|
||||
assert result.checked_for_handling_elsewhere is True
|
||||
|
||||
|
||||
class TestVerificationSchemaGeneration:
|
||||
"""Tests for JSON schema generation with VerificationEvidence."""
|
||||
|
||||
def test_verification_in_parallel_orchestrator_schema(self):
|
||||
"""Test that VerificationEvidence appears in schema."""
|
||||
schema = ParallelOrchestratorFinding.model_json_schema()
|
||||
|
||||
# verification should be in properties
|
||||
assert "verification" in schema["properties"]
|
||||
|
||||
# Check $defs includes VerificationEvidence
|
||||
assert "$defs" in schema
|
||||
assert "VerificationEvidence" in schema["$defs"]
|
||||
|
||||
# Check VerificationEvidence has correct fields
|
||||
ve_schema = schema["$defs"]["VerificationEvidence"]
|
||||
assert "code_examined" in ve_schema["properties"]
|
||||
assert "line_range_examined" in ve_schema["properties"]
|
||||
assert "verification_method" in ve_schema["properties"]
|
||||
|
||||
def test_new_boolean_fields_in_schema(self):
|
||||
"""Test is_impact_finding and checked_for_handling_elsewhere in schema."""
|
||||
schema = ParallelOrchestratorFinding.model_json_schema()
|
||||
|
||||
assert "is_impact_finding" in schema["properties"]
|
||||
assert "checked_for_handling_elsewhere" in schema["properties"]
|
||||
@@ -1,61 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Shared Test Utilities
|
||||
=====================
|
||||
|
||||
Common helper functions for test files.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def _create_mock_module():
|
||||
"""Create a simple mock module with necessary attributes.
|
||||
|
||||
Used by test files that need to mock external modules at import time.
|
||||
"""
|
||||
mock = MagicMock()
|
||||
return mock
|
||||
|
||||
|
||||
def configure_build_mocks(
|
||||
mock_validate_env,
|
||||
mock_should_run_qa,
|
||||
mock_get_phase_model,
|
||||
mock_choose_workspace,
|
||||
mock_get_existing,
|
||||
mock_run_agent,
|
||||
successful_agent_fn,
|
||||
validate_env=True,
|
||||
should_run_qa=False,
|
||||
workspace_mode=None,
|
||||
existing_spec=None,
|
||||
):
|
||||
"""
|
||||
Configure common mock defaults for build command tests.
|
||||
|
||||
This helper reduces the boilerplate of setting up the same 6-line mock pattern
|
||||
that was repeated 27+ times across test_cli_build_commands.py.
|
||||
|
||||
Usage:
|
||||
def test_something(
|
||||
mock_validate_env, mock_should_run_qa, mock_get_phase_model,
|
||||
mock_choose_workspace, mock_get_existing, mock_run_agent,
|
||||
successful_agent_fn
|
||||
):
|
||||
from test_utils import configure_build_mocks
|
||||
configure_build_mocks(
|
||||
mock_validate_env, mock_should_run_qa, mock_get_phase_model,
|
||||
mock_choose_workspace, mock_get_existing, mock_run_agent,
|
||||
successful_agent_fn
|
||||
)
|
||||
# ... rest of test
|
||||
"""
|
||||
from workspace import WorkspaceMode
|
||||
|
||||
mock_validate_env.return_value = validate_env
|
||||
mock_should_run_qa.return_value = should_run_qa
|
||||
mock_get_phase_model.side_effect = lambda spec_dir, phase, model: model or "sonnet"
|
||||
mock_choose_workspace.return_value = workspace_mode or WorkspaceMode.DIRECT
|
||||
mock_get_existing.return_value = existing_spec
|
||||
mock_run_agent.side_effect = successful_agent_fn
|
||||
@@ -631,11 +631,17 @@ class TestEdgeCases:
|
||||
|
||||
def test_nonexistent_directory(self, builder):
|
||||
"""Test handling of non-existent directory."""
|
||||
fake_dir = Path("/tmp/test-nonexistent-validation-123456")
|
||||
from unittest.mock import patch
|
||||
|
||||
# Should not crash, returns unknown
|
||||
strategy = builder.build_strategy(fake_dir, fake_dir, "medium")
|
||||
assert strategy.project_type == "unknown"
|
||||
fake_dir = Path("/nonexistent/path")
|
||||
|
||||
# Mock multiple Path methods to avoid permission errors on nonexistent paths
|
||||
with patch.object(Path, 'exists', return_value=False), \
|
||||
patch.object(Path, 'is_dir', return_value=False), \
|
||||
patch.object(Path, 'glob', return_value=[]):
|
||||
# Should not crash, returns unknown
|
||||
strategy = builder.build_strategy(fake_dir, fake_dir, "medium")
|
||||
assert strategy.project_type == "unknown"
|
||||
|
||||
def test_empty_risk_level_defaults_medium(self, builder, temp_dir):
|
||||
"""Test that None risk level defaults to medium."""
|
||||
|
||||
Reference in New Issue
Block a user