Zum Inhalt

QA Learning: H01-422 — Contract Test Pattern

Date: 2026-06-10 Trigger: H01-422 retrospective (Work alpha Closed, PR #54 merged) Type: Practice learning — Contract Test layer implementation

Summary

Implemented the contract-test layer for narrative unit creation. Key discovery: FakeNarrativeUnitService bypasses Fragment.create() entirely — router tests using a fake service can never verify domain invariants. Solution: _make_real_service_client().

The Pattern

def _make_real_service_client() -> AsyncClient:
    """Returns a client wired to the real NarrativeUnitService with a FakeNarrativeUnitRepository."""
    real_service = NarrativeUnitService(repository=FakeNarrativeUnitRepository())
    app.dependency_overrides[get_narrative_unit_service] = lambda: real_service
    return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")

Why this works: - Real NarrativeUnitService → Fragment.create() fires → domain invariants checked - FakeNarrativeUnitRepository → no database connection needed - The 422 fires before any repository call, so the fake never needs to simulate failure

When to apply: When the test must verify behavior rooted in a domain factory method (X.create()), not in the router's own HTTPException raises.

Both cases required: Failure path (422) AND happy path (201) — a contract test without the success case is an incomplete contract.

qa-review Found Real Bugs (Evidence)

The qa-review sub-agent (TDD Step 3) found 5 additional tests that covered real gaps:

Test Gap it closed
test_valid_content_returns_201 Happy path with real service was missing entirely
TestCreateWorkContract.test_empty_title_returns_422 Work type excluded from contract class
TestCreateWorkContract.test_whitespace_only_title_returns_422 Same — whitespace case
test_create_fragment_without_parent_id_returns_422 parent_id requirement for fragment
test_create_scene_without_parent_id_returns_422 parent_id requirement for scene

This run confirms: qa-review is not a coverage check — it exercises QA judgment about what cases matter and which combinations were overlooked.

Parked Finding: DELETE-404 Unreachable

SupabaseNarrativeUnitRepository.remove() is idempotent — no result check, no raise, even for unknown IDs. update() is strict (checks result, raises NarrativeUnitNotFoundError).

The global handler in main.py correctly maps NarrativeUnitNotFoundError → 404, but remove() never raises it. Therefore test_remove_unknown_unit_returns_404 is unreachable in production.

Status: PARKED in PENDING.md. Two options for the retro: - Option A (REST semantics): Keep idempotent — DELETE on unknown ID returns 204 (not an error). Fix or remove the test. - Option B (strict): Make remove() check the result like update() does. Fix implementation.

Needs Narrative Expert + SA decision.

False Persistence — Lesson Learned

This learning file was described as "written" in a previous session's context. It was never committed. git status would have shown the truth. Lesson: session memory is not an artifact. Only git status confirms what is real.

Applied going forward: every capture ends with a mandatory git status verification.