Skip to content

[Test Improver] test: add unit tests for AgentsCompiler (65%→82% coverage)#221

Merged
danielmeppiel merged 1 commit intomainfrom
test-assist/agents-compiler-coverage-1773105017-70450d4bafccea6d
Mar 22, 2026
Merged

[Test Improver] test: add unit tests for AgentsCompiler (65%→82% coverage)#221
danielmeppiel merged 1 commit intomainfrom
test-assist/agents-compiler-coverage-1773105017-70450d4bafccea6d

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

🤖 Test Improver – automated AI assistant

Goal & Rationale

src/apm_cli/compilation/agents_compiler.py is the main orchestrator for all AGENTS.md/CLAUDE.md compilation. At 65% coverage, many important error-handling and edge-case branches were untested, including:

  • Config loading from apm.yml (legacy single_file flag, placement settings, source attribution)
  • Exception paths that produce silent fallbacks instead of crashes
  • File-write error handling for both single-file and distributed modes
  • The _write_distributed_file() method (0% covered)

Approach

Added 34 focused unit tests in tests/unit/compilation/test_agents_compiler_coverage.py, complementing the existing test_compilation.py:

Test class Tests What it covers
TestCompilationConfigFromApmYmlAdditional 10 target, strategy, legacy single_file, min_instructions_per_file, source_attribution, exception fallback, no-file defaults
TestCompilationConfigPostInit 2 single_agents → strategy, exclude=None init
TestAgentsCompilerCompileException 2 Exception → failure result; local_only uses basic discover
TestValidatePrimitivesErrors 4 Primitive errors → warnings; outside-base-dir path; link errors
TestWriteOutputFile 1 OSError adds compiler error
TestWriteDistributedFile 4 Dir creation, no-constitution, injection failure fallback, OSError re-raise
TestGenerateSummaries 4 Relative and absolute-path cases for both summary methods
TestMergeResults 5 Empty list, single pass-through, two-result merge, failure propagation
TestCompileAgentsMdFunction 2 Error path raises RuntimeError; success returns content

Coverage Impact

Module Before After
agents_compiler.py 65% 82%

Remaining uncovered lines (264-275, 278-280, 294-295, 312-333, 436-437, 467-468, 472-473, 501, 503, 517-518, 759-774, 783-801) are inside _compile_distributed() and _compile_claude_md() display/formatting paths that require a full distributed compiler stack — not targeted in this PR.

Test Status

34 passed in 0.27s   (new tests alone)
964 passed, 1 failed (full suite, pre-existing ANSI failure in test_install_command.py)

The pre-existing failure (test_install_no_apm_yml_no_packages_shows_helpful_error) is unrelated to this change — it's caused by Rich-formatted output containing ANSI escape codes that the test asserts against plain text.

Reproducibility

uv sync --extra dev
uv run pytest tests/unit/compilation/test_agents_compiler_coverage.py -v
uv run pytest tests/unit/compilation/ --cov=apm_cli.compilation.agents_compiler --cov-report=term-missing

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

@danielmeppiel danielmeppiel added automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0. labels Mar 10, 2026
@danielmeppiel danielmeppiel marked this pull request as ready for review March 22, 2026 15:48
Copilot AI review requested due to automatic review settings March 22, 2026 15:48
Cover previously untested branches in agents_compiler.py:
- CompilationConfig.from_apm_yml(): target, strategy, single_file legacy,
  min_instructions_per_file, source_attribution fields, exception fallback,
  no-file fallback, single_agents override, None override ignored
- CompilationConfig.__post_init__: single_agents and exclude=None paths
- AgentsCompiler.compile(): exception handling and local_only discovery path
- AgentsCompiler.validate_primitives(): primitive errors → warnings,
  absolute-path fallback, link validation errors
- AgentsCompiler._write_output_file(): OSError adds compiler error
- AgentsCompiler._write_distributed_file(): full method coverage (dir creation,
  no-constitution, constitution injection failure fallback, OSError re-raise)
- AgentsCompiler._generate_placement_summary(): relative and absolute path cases
- AgentsCompiler._generate_distributed_summary(): format and path fallback
- AgentsCompiler._merge_results(): empty list, single pass-through, two-result
  merge (content, warnings, stats), failure propagation, empty-path exclusion
- compile_agents_md(): error path raises RuntimeError, success returns content

34 new tests, all pass. Overall agents_compiler coverage: 65% → 82%.

Co-authored-by: Copilot <[email protected]>
@danielmeppiel danielmeppiel force-pushed the test-assist/agents-compiler-coverage-1773105017-70450d4bafccea6d branch from ced329c to 99bd0cd Compare March 22, 2026 15:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a dedicated unit test module to increase branch/edge-case coverage for apm_cli.compilation.agents_compiler—the main orchestration layer for AGENTS.md/CLAUDE.md compilation—focusing on error handling, config loading, and distributed write helpers.

Changes:

  • Add new unit tests covering CompilationConfig.from_apm_yml() overrides/legacy fields and failure fallbacks.
  • Add tests for AgentsCompiler exception handling, primitive validation warnings (including link validation), and output write error paths.
  • Add tests for distributed write helpers and summary/merge helpers, plus compile_agents_md() failure/success behavior.

import tempfile
import unittest
from pathlib import Path
from unittest.mock import MagicMock, PropertyMock, patch
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import: PropertyMock is imported but never used in this test module. Removing it will reduce noise and avoid unused-import warnings in editors/linting.

Suggested change
from unittest.mock import MagicMock, PropertyMock, patch
from unittest.mock import MagicMock, patch

Copilot uses AI. Check for mistakes.
Comment on lines +371 to +378
"apm_cli.compilation.agents_compiler.AgentsCompiler._write_distributed_file",
wraps=compiler._write_distributed_file,
):
with patch(
"apm_cli.compilation.injector.ConstitutionInjector.inject",
side_effect=RuntimeError("injection error"),
):
compiler._write_distributed_file(target, "original content", config)
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nested patch of AgentsCompiler._write_distributed_file wraps the method under test but doesn't assert on the mock or change behavior. It can be removed to simplify the test and avoid accidental recursion/patching complexity.

Suggested change
"apm_cli.compilation.agents_compiler.AgentsCompiler._write_distributed_file",
wraps=compiler._write_distributed_file,
):
with patch(
"apm_cli.compilation.injector.ConstitutionInjector.inject",
side_effect=RuntimeError("injection error"),
):
compiler._write_distributed_file(target, "original content", config)
"apm_cli.compilation.injector.ConstitutionInjector.inject",
side_effect=RuntimeError("injection error"),
):
compiler._write_distributed_file(target, "original content", config)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants