# Story 11.6: Split Large Monolithic Test Files

**Status:** Complete
**Priority:** Medium
**Depends on:** None

---

## Context / Background

Four test files exceed 1,000 lines, making them difficult to maintain and limiting parallel test execution:

| File | Lines | Tests |
|------|-------|-------|
| `tests/unit/test_error_handling.py` | 1,790 | 68 |
| `tests/unit/test_high_performance_lru_cache.py` | 1,176 | 59 |
| `tests/unit/test_logger_pipeline.py` | 1,114 | 33 |
| `tests/unit/test_rotating_file_sink.py` | 1,069 | 37 |

**Problems with large files:**
1. **Slow parsing** - pytest takes longer to collect tests from large files
2. **Limited parallelization** - pytest-xdist distributes by file, so one large file becomes a bottleneck
3. **Cognitive overhead** - Hard to navigate and understand test organization
4. **Merge conflicts** - More likely when multiple developers touch the same file

**Goal:** Split these 4 files into smaller, focused modules.

---

## Scope (In / Out)

### In Scope

- Split each large file into focused modules
- Maintain all existing tests (no removals)
- Organize by logical grouping (feature, scenario, error type)
- Update any imports that reference split files

### Out of Scope

- Refactoring test logic
- Adding new tests
- Removing or combining tests
- Changing test behavior

---

## Acceptance Criteria

### AC1: Large Files Split

**Description:** The 4 files over 1,000 lines are split into smaller modules.

**Validation:**
```bash
wc -l tests/unit/test_error_handling.py tests/unit/test_high_performance_lru_cache.py \
      tests/unit/test_logger_pipeline.py tests/unit/test_rotating_file_sink.py 2>/dev/null
# Expected: Files no longer exist (replaced by smaller modules)
```

### AC2: All Tests Still Exist

**Description:** Test count before and after split is identical.

**Validation:**
```bash
# Before split
pytest tests/ --collect-only -q | tail -1 > /tmp/before.txt

# After split
pytest tests/ --collect-only -q | tail -1 > /tmp/after.txt

diff /tmp/before.txt /tmp/after.txt
# Expected: no difference
```

### AC3: Tests Still Pass

**Description:** Full test suite passes after split.

**Validation:**
```bash
pytest tests/unit/ -v
```

### AC4: Logical Organization

**Description:** Split files have clear, focused names indicating their purpose.

**Validation:**
- Files named by feature: `test_lru_cache_eviction.py`, `test_lru_cache_concurrent.py`
- Related tests grouped together
- Each file has a clear docstring explaining scope

---

## Implementation Notes

### Proposed Splits

#### 1. `test_error_handling.py` (1,790 lines) → 4 files

```
test_error_handling.py (1,790 lines)
├── test_error_handling_sink.py (~400 lines)
│   └── Sink error scenarios
├── test_error_handling_pipeline.py (~400 lines)
│   └── Pipeline error propagation
├── test_error_handling_recovery.py (~400 lines)
│   └── Recovery and fallback behavior
└── test_error_handling_logging.py (~400 lines)
    └── Error logging and reporting
```

#### 2. `test_high_performance_lru_cache.py` (1,176 lines) → 3 files

```
test_high_performance_lru_cache.py (1,176 lines)
├── test_lru_cache_basic.py (~400 lines)
│   └── Basic get/set/delete operations
├── test_lru_cache_eviction.py (~400 lines)
│   └── Eviction policies, TTL, size limits
└── test_lru_cache_concurrent.py (~376 lines)
    └── Thread safety, async access
```

#### 3. `test_logger_pipeline.py` (1,114 lines) → 3 files

```
test_logger_pipeline.py (1,114 lines)
├── test_pipeline_basic.py (~400 lines)
│   └── Basic pipeline flow
├── test_pipeline_stages.py (~400 lines)
│   └── Individual stage behavior
└── test_pipeline_async.py (~314 lines)
    └── Async pipeline operations
```

#### 4. `test_rotating_file_sink.py` (1,069 lines) → 3 files

```
test_rotating_file_sink.py (1,069 lines)
├── test_rotating_file_basic.py (~350 lines)
│   └── Basic write operations
├── test_rotating_file_rotation.py (~350 lines)
│   └── Rotation triggers, naming
└── test_rotating_file_edge_cases.py (~369 lines)
    └── Edge cases, error handling
```

### Split Process

For each file:

1. **Analyze structure** - Read file, identify logical groupings
2. **Create new files** - Write header with docstring explaining scope
3. **Move tests** - Cut/paste test classes/functions to appropriate file
4. **Update imports** - Ensure fixtures and helpers are accessible
5. **Verify** - Run tests to ensure nothing broke

### Shared Fixtures

If tests share fixtures, either:
1. Move fixtures to `conftest.py` in the same directory
2. Create a `_fixtures.py` module and import

```python
# tests/unit/conftest.py
@pytest.fixture
def mock_sink():
    return MockSink()

# tests/unit/test_error_handling_sink.py
def test_sink_error(mock_sink):  # Fixture auto-discovered
    ...
```

### File Naming Convention

Use descriptive suffixes:
- `_basic.py` - Core functionality
- `_concurrent.py` - Threading/async tests
- `_edge_cases.py` - Edge cases and error conditions
- `_integration.py` - Integration with other components
- `_performance.py` - Performance-related tests

---

## Tasks

### Phase 1: test_error_handling.py

- [ ] Analyze test groupings in file
- [ ] Create 4 new files with appropriate names
- [ ] Move tests to new files
- [ ] Move shared fixtures to conftest.py
- [ ] Verify all tests pass
- [ ] Delete original file

### Phase 2: test_high_performance_lru_cache.py

- [ ] Analyze test groupings
- [ ] Create 3 new files
- [ ] Move tests
- [ ] Verify tests pass
- [ ] Delete original file

### Phase 3: test_logger_pipeline.py

- [ ] Analyze test groupings
- [ ] Create 3 new files
- [ ] Move tests
- [ ] Verify tests pass
- [ ] Delete original file

### Phase 4: test_rotating_file_sink.py

- [ ] Analyze test groupings
- [ ] Create 3 new files
- [ ] Move tests
- [ ] Verify tests pass
- [ ] Delete original file

### Phase 5: Validation

- [ ] Verify no file > 500 lines
- [ ] Verify test count unchanged
- [ ] Run full test suite
- [ ] Update any documentation referencing old files

---

## Tests

### Verification

```bash
# Count tests before and after
pytest tests/unit/ --collect-only -q 2>/dev/null | tail -1

# Verify line counts
find tests/unit/ -name "*.py" -exec wc -l {} \; | sort -n | tail -10
```

### Regression

```bash
pytest tests/unit/ -v --tb=short
```

---

## Definition of Done

### Code Complete

- [x] All 4 large files split into smaller modules
- [x] Test count unchanged
- [x] Logical organization with clear naming

### Quality Assurance

- [x] All tests pass
- [x] `ruff check` passes
- [x] No import errors
- [x] Coverage unchanged

### Documentation

- [x] Each new file has docstring explaining scope
- [x] CHANGELOG updated

---

## Risks / Rollback

### Risks

1. **Risk:** Import errors after split
   - **Mitigation:** Run tests after each file split

2. **Risk:** Fixtures not accessible in new files
   - **Mitigation:** Move to conftest.py proactively

3. **Risk:** Merge conflicts with in-progress work
   - **Mitigation:** Coordinate with team, do splits in single PR

### Rollback Plan

If issues occur:
1. Git revert to before split
2. Keep original files temporarily
3. Investigate import/fixture issues

---

## Related Stories

- **Enables:** Story 11.9 - pytest-xdist benefits from smaller files
- **Related:** Story 7.5 - consolidates duplicative tests (may reduce file sizes)

---

## Change Log

| Date | Change | Author |
|------|--------|--------|
| 2026-01-14 | Initial draft | Claude |
| 2026-01-14 | Fix test counts; simplify goal to splitting 4 largest files | Claude |
