UAT: Production code application/container.py imports mock from features/mocks/ test directory — violates test isolation rules #6794

Open
opened 2026-04-10 02:06:35 +00:00 by HAL9000 · 0 comments
Owner

Background

CONTRIBUTING.md mandates strict separation between production source code and test infrastructure, including mocks. Production code must never import from test directories.

What Was Tested

Code analysis of src/cleveragents/application/container.py — specifically the _build_ai_provider() function.

Expected Behavior (from CONTRIBUTING.md)

The CONTRIBUTING.md Test Isolation and Mock Placement section states:

All mocks, test doubles, and mock implementations must exist only within test directories. Production source code and utility scripts must never contain mock implementations, test data, or conditional testing behavior.

No Conditional Test Behavior: Production code must not contain if testing: guards, test-only code paths, or any logic that changes behavior based on whether the code is under test.

Dependency Injection: Use dependency injection to swap implementations during tests.

Actual Behavior

src/cleveragents/application/container.py contains the following production code (lines ~191-204):

if use_mock_ai:
    try:
        import sys
        from pathlib import Path

        features_path = Path(__file__).parent.parent.parent.parent / "features"
        if features_path.exists() and str(features_path) not in sys.path:
            sys.path.insert(0, str(features_path))

        from mocks.mock_ai_provider import MockAIProvider  # type: ignore

        return MockAIProvider()  # type: ignore
    except ImportError:
        return None

This is a triple violation:

  1. Imports from test directory: The path features/ is the BDD test directory. Production code is reaching into the test directory to import MockAIProvider.
  2. Conditional test behavior in production: The if use_mock_ai: guard is exactly the pattern CONTRIBUTING.md forbids — production code changing behavior based on testing state.
  3. sys.path mutation at runtime: Modifying sys.path at runtime to inject the test directory into the import path is a dangerous anti-pattern that can cause import conflicts.

The condition use_mock_ai is driven by either settings.mock_providers or the environment variable CLEVERAGENTS_TESTING_USE_MOCK_AI, making this a test-only code path embedded directly in the DI container.

Impact

  • Any deployed production container that has CLEVERAGENTS_TESTING_USE_MOCK_AI=true set will attempt to import from features/mocks/ which may not exist in production deployments
  • The features/ directory is in sys.path at runtime which can cause import ambiguity
  • Violates the DI/dependency injection principle for swapping implementations under test

Steps to Reproduce

Review src/cleveragents/application/container.py, function _build_ai_provider() (approximately lines 165-209).

Acceptance Criteria

  • src/cleveragents/application/container.py contains no imports from features/ or any test directory
  • The mock AI provider is injected via dependency injection only (e.g., container.ai_provider.override(...) in test setup)
  • No if testing: guards or CLEVERAGENTS_TESTING_USE_MOCK_AI checks remain in production source code
  • The mock provider is wired exclusively through test fixtures/conftest

Subtasks

  • Remove the use_mock_ai conditional block from _build_ai_provider() in container.py
  • Move mock provider wiring to Behave step definitions / test fixtures using DI override
  • Verify features/mocks/mock_ai_provider.py is never imported from production paths
  • Run nox -e typecheck to confirm no type: ignore changes
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when production code contains no references to test directories or conditional testing behavior, and tests still pass.


Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: uat-tester

## Background CONTRIBUTING.md mandates strict separation between production source code and test infrastructure, including mocks. Production code must never import from test directories. ## What Was Tested Code analysis of `src/cleveragents/application/container.py` — specifically the `_build_ai_provider()` function. ## Expected Behavior (from CONTRIBUTING.md) The CONTRIBUTING.md **Test Isolation and Mock Placement** section states: > All mocks, test doubles, and mock implementations must exist only within test directories. Production source code and utility scripts **must never contain mock implementations, test data, or conditional testing behavior.** > **No Conditional Test Behavior:** Production code must not contain `if testing:` guards, test-only code paths, or any logic that changes behavior based on whether the code is under test. > **Dependency Injection:** Use dependency injection to swap implementations during tests. ## Actual Behavior `src/cleveragents/application/container.py` contains the following production code (lines ~191-204): ```python if use_mock_ai: try: import sys from pathlib import Path features_path = Path(__file__).parent.parent.parent.parent / "features" if features_path.exists() and str(features_path) not in sys.path: sys.path.insert(0, str(features_path)) from mocks.mock_ai_provider import MockAIProvider # type: ignore return MockAIProvider() # type: ignore except ImportError: return None ``` This is a **triple violation**: 1. **Imports from test directory**: The path `features/` is the BDD test directory. Production code is reaching into the test directory to import `MockAIProvider`. 2. **Conditional test behavior in production**: The `if use_mock_ai:` guard is exactly the pattern CONTRIBUTING.md forbids — production code changing behavior based on testing state. 3. **sys.path mutation at runtime**: Modifying `sys.path` at runtime to inject the test directory into the import path is a dangerous anti-pattern that can cause import conflicts. The condition `use_mock_ai` is driven by either `settings.mock_providers` or the environment variable `CLEVERAGENTS_TESTING_USE_MOCK_AI`, making this a test-only code path embedded directly in the DI container. ## Impact - Any deployed production container that has `CLEVERAGENTS_TESTING_USE_MOCK_AI=true` set will attempt to import from `features/mocks/` which may not exist in production deployments - The `features/` directory is in `sys.path` at runtime which can cause import ambiguity - Violates the DI/dependency injection principle for swapping implementations under test ## Steps to Reproduce Review `src/cleveragents/application/container.py`, function `_build_ai_provider()` (approximately lines 165-209). ## Acceptance Criteria - [ ] `src/cleveragents/application/container.py` contains no imports from `features/` or any test directory - [ ] The mock AI provider is injected via dependency injection only (e.g., `container.ai_provider.override(...)` in test setup) - [ ] No `if testing:` guards or `CLEVERAGENTS_TESTING_USE_MOCK_AI` checks remain in production source code - [ ] The mock provider is wired exclusively through test fixtures/conftest ## Subtasks - [ ] Remove the `use_mock_ai` conditional block from `_build_ai_provider()` in `container.py` - [ ] Move mock provider wiring to Behave step definitions / test fixtures using DI override - [ ] Verify `features/mocks/mock_ai_provider.py` is never imported from production paths - [ ] Run `nox -e typecheck` to confirm no type: ignore changes - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when production code contains no references to test directories or conditional testing behavior, and tests still pass. --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: uat-tester
HAL9000 added this to the v3.4.0 milestone 2026-04-10 02:06:42 +00:00
HAL9000 self-assigned this 2026-04-10 06:06:51 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#6794
No description provided.