---
estimated_steps: 21
estimated_files: 1
skills_used: []
---

# T03: Unit tests for compose-mode prompt building and validation

Write unit tests for the compose harness plumbing — no LLM calls. Tests cover prompt construction, citation re-indexing math, category filtering, word count comparison, and edge cases.

Steps:
1. Read the updated `backend/pipeline/test_harness.py` (from T02) to understand `build_compose_prompt()` signature and `run_compose()` interface.
2. Read `backend/pipeline/test_harness_v2_format.py` for test patterns (helper functions, BodySection construction, etc.).
3. Create `backend/pipeline/test_harness_compose.py` with these test classes:
   - `TestBuildComposePrompt`: 
     - Existing page + 3 old moments + 2 new moments → prompt contains all XML tags, old moments indexed [0]-[2], new moments indexed [3]-[4]
     - Creator name appears in `<creator>` tag
     - Existing page JSON appears in `<existing_page>` tag and is valid JSON
     - Moment text uses same format as `build_moments_text()`
   - `TestCitationReindexing`:
     - 5 old + 3 new moments → valid citation range is [0]-[7]
     - validate_citations() with moment_count=8 accepts citations in [0]-[7]
     - validate_citations() with moment_count=8 rejects citation [8]
   - `TestCategoryFiltering`:
     - Build fixture with moments in 2 categories → compose with existing page of category A → only category A new moments used
   - `TestEdgeCases`:
     - Empty new moments list → prompt still valid, just no new_moments content
     - Single new moment → prompt has one entry at index [N]
     - Existing page with no subsections → handled correctly
4. Run tests: `cd backend && python -m pytest pipeline/test_harness_compose.py -v`

## Inputs

- ``backend/pipeline/test_harness.py` — build_compose_prompt() and run_compose() from T02`
- ``backend/pipeline/schemas.py` — SynthesizedPage, BodySection for test fixtures`
- ``backend/pipeline/citation_utils.py` — validate_citations() for citation math tests`
- ``backend/pipeline/test_harness_v2_format.py` — test patterns and helpers to follow`

## Expected Output

- ``backend/pipeline/test_harness_compose.py` — unit tests for compose prompt construction, citation re-indexing, category filtering, edge cases`

## Verification

cd backend && python -m pytest pipeline/test_harness_compose.py -v
