testing · diff

git:20260719.3848a77 to git:20260728.312fef9

38 added, 234 removed. Audit A to A.

---
name: testing
- description: "Testing principles including TDD, test quality, coverage standards, and test design. Use when: writing tests, designing test strategies, reviewing test quality, or following Red-Green-Refactor cycle."
+ description: "Repository-aware test execution rules for TDD, observable proof, and boundary selection. Use when writing, reviewing, or fixing tests."
---
- # Testing Principles
-
- ## Language-Specific References
-
- For language-specific testing patterns, also read:
- - **TypeScript (Vitest/RTL/Playwright)**: [references/typescript.md](references/typescript.md)
-
- ## Core Testing Philosophy
-
- 1. **Tests are First-Class Code**: Maintain test quality equal to production code
- 2. **Fast Feedback**: Tests should run quickly and provide immediate feedback
- 3. **Reliability**: Tests should be deterministic and reproducible
- 4. **Independence**: Each test should run in isolation
-
- ## TDD Process [MANDATORY for all code changes]
-
- **Execute this process for every code change:**
-
- ### RED Phase
- **STEP 1**: Write test that defines expected behavior
- **STEP 2**: Run test
- **STEP 3**: Confirm the test fails for the targeted missing behavior or regression. If it passes before implementation, verify that it can fail for the targeted defect; then determine whether the behavior is already implemented or the test does not exercise the intended failure mode.
-
- ### GREEN Phase
- **STEP 1**: Write MINIMAL code to make test pass
- **STEP 2**: Run test
- **STEP 3**: Confirm test PASSES
-
- ### REFACTOR Phase
- **STEP 1**: Improve code quality (eliminate duplication, improve naming)
- **STEP 2**: Run test
- **STEP 3**: Confirm test STILL PASSES
-
- ### VERIFY Phase [MANDATORY - 0 ERRORS REQUIRED]
- **STEP 1**: Execute ALL quality check commands for your language/project
- **STEP 2**: Fix any errors until ALL commands pass with 0 errors
- **STEP 3**: Confirm no regressions
-
- **ENFORCEMENT**: Cannot proceed to next phase with ANY quality check failures
-
- ### TDD Exceptions (no TDD required)
- - Pure configuration files
- - Documentation only
- - Emergency fixes (but add tests immediately after)
- - Exploratory spikes (discard or rewrite with tests before merging)
- - Build/deployment scripts (unless they contain business logic)
-
- ## Quality Requirements [MANDATORY]
-
- ### Coverage
-
- - Treat coverage as a diagnostic signal for finding untested areas, not a target. Targets get gamed into trivial tests.
- - Concentrate tests on critical paths, business logic, and behavior whose regression would matter.
- - Prioritize meaningful assertions over the coverage number. Any required threshold comes from the project's CI, task file, work plan, or Design Doc.
-
- ### Test Characteristics
-
- All tests MUST be:
-
- - **Independent**: No dependencies between tests
- - **Reproducible**: Same input always produces same output
- - **Fast**: Complete the full test suite within the project's accepted feedback window and flag suites that materially slow local iteration or CI
- - **Self-checking**: Clear pass/fail without manual verification
- - **Timely**: Written close to the code they test
-
- **ENFORCEMENT**: Tests failing ANY characteristic MUST be fixed immediately
-
- ## Test Level Selection
-
- - **Unit/local**: Exercise one unit or in-process behavior and isolate external I/O
- - **Integration**: Exercise the real component, persistence, process, or contract boundary named by the proof obligation
- - **End-to-end**: Exercise the complete user, browser, process, or service journey named by the proof obligation
- - Select the level that proves the required boundary; a broader test does not replace a required focused check, and a focused test does not prove an integration boundary
-
- ## Test Design Principles
-
- ### AAA Pattern (Arrange-Act-Assert)
-
- Structure every test in clear Arrange, Act, and Assert phases.
-
- ### One Assertion Per Concept
-
- - Test one behavior per test case
- - Multiple assertions OK if testing single concept
- - Split unrelated assertions into separate tests — one test MUST verify one behavior
-
- ### Descriptive Test Names
-
- Test names should clearly describe:
- - What is being tested
- - Under what conditions
- - What the expected outcome is
-
- **Recommended format**: `"should [expected behavior] when [condition]"`
-
- ## Test Independence
-
- ### Isolation Requirements
-
- - Each test creates its own test data
- - No dependencies on execution order
- - Clean up own state
- - Pass when run in isolation
-
- ### Setup and Teardown
-
- - Use setup hooks to prepare test environment
- - Use teardown hooks to clean up resources
- - Keep setup scoped to the data, dependencies, and fixtures required for the behavior under test
- - Ensure teardown runs even if test fails
-
- ## Mocking and Test Doubles
-
- ### Boundary Selection
-
- - In unit/local tests, isolate external I/O such as APIs, databases, file systems, network calls, time, and randomness
- - In integration and data-layer tests, keep the dependency or boundary named by the proof obligation real or production-like
- - Isolate unavailable third-party services unless their contract payload or failure behavior is the boundary under test
- - Follow the Design Doc `Test Boundaries` decision when one exists
-
- ### Mocking Principles [MANDATORY]
-
- - Mock at boundaries, not internally — use real implementations for internal utilities
- - Keep each mock limited to the behavior the test needs to control or observe
- - Verify mock expectations when relevant
- - Use adapters for external libraries/frameworks you do not control
-
- ## Data Layer Testing
-
- ### Mock Limitations for Data Access
-
- Mocks validate call patterns but do not validate schema correctness, query correctness, or storage constraints.
- Examples of issues that mocks can miss:
- - schema drift
- - column or field mismatches
- - incorrect joins, filters, or aggregations
- - migration incompatibility
-
- ### When Real Data Layer Verification Adds Value
-
- Use real or production-like data access verification when testing:
- - repository or DAO implementations
- - ORM mappings
- - query builders or raw SQL
- - persistence behavior that depends on constraints or schema shape
-
- ### Environment Options
-
- Choose the most practical option for the project environment:
- - containerized database
- - dedicated test database
- - in-memory database with documented limitations
- - adapter-backed local test harness
-
- ### Design Alignment
-
- When a Design Doc includes `Test Boundaries`, follow it as the baseline for deciding which dependencies stay real and which boundaries are isolated.
-
- ## Test Quality Practices [MANDATORY]
-
- ### Keep Tests Active
-
- - **Fix or delete failing tests**: Resolve failures immediately
- - **Remove commented-out tests**: Fix them or delete entirely
- - **Keep tests running**: Broken tests lose value quickly
- - **Maintain test suite**: Refactor tests as needed
-
- ### Test Code Quality
-
- - Apply same standards as production code
- - Use descriptive variable names
- - Extract test helpers to reduce duplication
- - Keep tests readable and maintainable
-
- ### Test Helpers and Utilities
-
- - Create reusable test data builders
- - Extract common setup into helper functions
- - Build test utilities for complex scenarios
- - Share helpers across test files appropriately
-
- ## What to Test
-
- - Test accepted behavior through the public or externally observable boundary
- - Cover the happy path and the boundary, error, and regression cases required by the proof obligation
- - Do not assert private state or private call order unless that internal contract is itself the named proof target
-
- ## Test Quality Criteria [MANDATORY]
+ # Testing
- 1. **Literal expectations**: Use hardcoded literal values in assertions — expected value ≠ mock return value (implementation processes data)
- 2. **Result verification**: Assert return values and state, not call order
- 3. **Meaningful assertions**: Every test MUST have at least one assertion — a test without assertions provides zero value
- 4. **Boundary fidelity**: Mock only boundaries intentionally isolated at the selected test level; keep every dependency named as real by the proof obligation or Design Doc boundary decision
- 5. **Boundary coverage**: Include empty/zero/max/error cases with happy paths
+ ## Reference
- **ENFORCEMENT**: Tests violating ANY criterion MUST be rewritten
+ Read [references/typescript.md](references/typescript.md) only for TypeScript tests in web frontend work, including React, RTL, MSW, and Playwright tests. It does not apply to backend or non-web TypeScript tests.
- ## Verification Requirements [MANDATORY for VERIFY phase]
+ ## Governing Sources
- ### Before Commit Checklist
+ Use test commands, thresholds, boundaries, fixtures, and conventions from the repository, task, Work Plan, Design Doc, or test skeleton. Do not invent defaults when these sources are silent.
- ☐ All tests pass
- ☐ No tests skipped or commented
- ☐ No debug code left in tests
- ☐ Coverage threshold passes when the project, task file, work plan, or Design Doc defines one
- ☐ Tests run in reasonable time
+ ## TDD Gate
- ### Zero Tolerance Policy
+ For behavior-changing implementation:
- - **Zero failing tests**: Fix immediately
- - **Zero skipped tests**: Delete or fix
- - **Zero flaky tests**: Make deterministic
- - **Zero unreviewed slow tests**: When a test or suite exceeds the project's accepted feedback window, optimize it, split it, move it to the appropriate test level, or record the accepted exception in the governing task or Design Doc
+ 1. **RED**: Add or select a test that observes the required behavior. Run it and confirm it fails for the targeted missing behavior or regression.
+ 2. **GREEN**: Make the smallest implementation change that satisfies the behavior. Run the focused test.
+ 3. **REFACTOR**: Improve the changed code without changing the contract. Re-run the focused test.
+ 4. **VERIFY**: Run the repository's required quality command and every wider check required by the task boundary.
- **ENFORCEMENT**: Cannot proceed with task completion if ANY quality check fails
+ Documentation, pure configuration, and disposable exploration do not require a RED phase. A productionized spike must gain tests before completion.
- ## Common Anti-Patterns
+ ## Proof Selection
- Detect and eliminate these patterns immediately:
+ - Test through the public or externally observable boundary named by the acceptance criterion or proof obligation.
+ - Keep a dependency real when that dependency is the boundary under proof; isolate external I/O only when the selected test level permits it.
+ - Use integration or E2E tests for persistence, process, browser, service, or cross-component claims that unit tests cannot observe.
+ - A broader test does not replace a required focused check, and a focused test does not prove a wider boundary.
- - Tests that test nothing (always pass)
- - Tests that depend on execution order
- - Tests that depend on external state
- - Tests with complex logic (tests that need their own tests)
- - Testing implementation details instead of observable behavior
- - Excessive mocking (mock boundaries only, use real internals)
- - Test code duplication
+ ## Capability Probe Postconditions
- ### Flaky Tests
+ Evidence is substantive only when an executed assertion observes the exact consumer-visible postcondition.
- Eliminate tests that fail intermittently:
- - Remove timing dependencies
- - Use deterministic data instead of random values
- - Ensure proper cleanup
- - Fix race conditions
- - Make all tests deterministic
+ - A helper result, mock call, source match, build success, or zero-test run does not prove behavior unless that is the named claim.
+ - Intentional absence is substantive when absence is the expected postcondition.
+ - State-changing behavior must assert relevant before → action → after state, including persistence or rollback semantics when applicable.
+ - For a changed route or alternate input path, exercise that path explicitly; evidence from another route is not interchangeable.
- ## Regression Testing
+ ## Test Integrity
- - Add test for every bug fix
- - Maintain comprehensive test suite
- - Run full suite regularly
- - Delete a test only when the covered behavior no longer exists or the same behavior is covered by a stronger test at the correct level
+ - Keep tests deterministic, isolated, and active.
+ - Use meaningful assertions on results, state, or observable effects.
+ - Do not weaken, skip, delete, or replace a test merely to make a run pass.
+ - Change an existing expectation only when a cited governing source changed the expected behavior.
+ - Use project-scoped setup and guaranteed cleanup for mutated state or external resources.
+ - Treat coverage as diagnostic unless a governing source defines a threshold.
- ### Legacy Code
+ ## Completion Gate
- - Add characterization tests before refactoring
- - Test existing behavior first
- - Gradually improve coverage
- - Refactor with confidence
+ - [ ] Focused tests pass; when the TDD Gate applies, RED failed for the targeted missing behavior or regression; otherwise RED is recorded as not applicable with the reason
+ - [ ] Required integration/E2E boundary is exercised
+ - [ ] Every cited behavior has a consumer-visible capability probe
+ - [ ] Repository-defined quality commands pass
+ - [ ] No required test is skipped, hollow, or dependent on execution order
+ - [ ] Mutated state and resources are restored