|
| 1 | +"""Tests for analyzer module.""" |
| 2 | + |
| 3 | +from roast.analyzer import AI_SLOP, CODE_QUALITY, STYLE, analyze |
| 4 | +from roast.scanner import FileResult |
| 5 | + |
| 6 | + |
| 7 | +def test_todo_comments_generate_ai_slop_issues() -> None: |
| 8 | + file = FileResult( |
| 9 | + path="app/main.py", |
| 10 | + content=( |
| 11 | + "# TODO: clean this\n" |
| 12 | + "x = 1\n" |
| 13 | + "# FIXME: replace later\n" |
| 14 | + "y = 2\n" |
| 15 | + "# HACK: temporary workaround\n" |
| 16 | + ), |
| 17 | + language="python", |
| 18 | + line_count=5, |
| 19 | + ) |
| 20 | + report = analyze([file]) |
| 21 | + todo_issues = [ |
| 22 | + issue |
| 23 | + for issue in report.issues |
| 24 | + if issue.category == AI_SLOP and "TODO/FIXME/HACK marker" in issue.description |
| 25 | + ] |
| 26 | + assert len(todo_issues) == 3 |
| 27 | + |
| 28 | + |
| 29 | +def test_function_over_50_lines_generates_code_quality_issue() -> None: |
| 30 | + long_body = "\n".join(" x += 1" for _ in range(51)) |
| 31 | + content = f"def massive_function():\n x = 0\n{long_body}\n return x\n" |
| 32 | + file = FileResult( |
| 33 | + path="app/logic.py", |
| 34 | + content=content, |
| 35 | + language="python", |
| 36 | + line_count=content.count("\n"), |
| 37 | + ) |
| 38 | + report = analyze([file]) |
| 39 | + long_fn_issues = [ |
| 40 | + issue |
| 41 | + for issue in report.issues |
| 42 | + if issue.category == CODE_QUALITY and "exceeds 50 lines" in issue.description |
| 43 | + ] |
| 44 | + assert len(long_fn_issues) == 1 |
| 45 | + |
| 46 | + |
| 47 | +def test_scores_are_clamped_to_zero_minimum() -> None: |
| 48 | + slop_lines = "\n".join("# TODO: fix me" for _ in range(20)) |
| 49 | + file = FileResult( |
| 50 | + path="bad/file.py", |
| 51 | + content=slop_lines, |
| 52 | + language="python", |
| 53 | + line_count=20, |
| 54 | + ) |
| 55 | + report = analyze([file]) |
| 56 | + assert report.scores[AI_SLOP] == 0 |
| 57 | + assert report.scores[CODE_QUALITY] >= 0 |
| 58 | + assert report.scores[STYLE] >= 0 |
| 59 | + assert report.scores["Overall"] >= 0 |
| 60 | + |
| 61 | + |
| 62 | +def test_empty_files_are_handled_without_error() -> None: |
| 63 | + file = FileResult( |
| 64 | + path="empty.py", |
| 65 | + content="", |
| 66 | + language="python", |
| 67 | + line_count=0, |
| 68 | + ) |
| 69 | + report = analyze([file]) |
| 70 | + assert report.total_files == 1 |
| 71 | + assert report.total_lines == 0 |
| 72 | + assert report.issues == [] |
0 commit comments