Skip to content

Add unit tests for phase-executor.ts #378

Closed

Description

@admarble

Context

src/lib/workflow/phase-executor.ts is a core orchestration module (handles phase execution, cold-start retry, MCP fallback, QA verdict parsing) but has no dedicated test file. Discovered during QA of #371.

Goal

Add src/lib/workflow/phase-executor.test.ts covering the module's key functions.

Acceptance Criteria

  • Test parseQaVerdict() — all verdict formats (markdown header, bold, plain), case insensitivity, null on no match
  • Test formatDuration() — seconds-only, minutes+seconds edge cases
  • Test getPhasePrompt() — issue number substitution, AGENTS.md inclusion when present, graceful null when absent
  • Test executePhaseWithRetry() — cold-start retry logic, MCP fallback, retry disabled path (use injected executePhaseFn)

Notes

  • executePhaseWithRetry already accepts an injected executePhaseFn parameter for testing — the seam exists, just no tests use it yet
  • parseQaVerdict and formatDuration are already exported
  • getPhasePrompt is private — test indirectly via executePhaseWithRetry or export for testing

Priority

P3 — core module, but currently covered by integration tests (1562 pass)

Activity

  1. admarble commented on Mar 25, 2026

    @admarble
    ContributorAuthor

    Spec Plan — #378: Add unit tests for phase-executor.ts

    AC Checklist with Verification Criteria

    AC-1: Test parseQaVerdict()

    Verification Method: Unit Test

    • Given: Various verdict string formats (markdown header, bold, plain, bold-wrapped value)
    • When: parseQaVerdict(input) is called
    • Then: Returns correct QaVerdict value, case-insensitively; returns null for empty/no-match

    AC-2: Test formatDuration()

    Verification Method: Unit Test

    • Given: Seconds values below 60, at 60, and above 60
    • When: formatDuration(seconds) is called
    • Then: Returns "Xs" for <60, "Xm Ys" for ≥60; handles 0, fractional, exact minutes

    AC-3: Test getPhasePrompt()

    Verification Method: Unit Test

    • Given: Phase, issue number, optional agent; AGENTS.md present or absent
    • When: getPhasePrompt() is called
    • Then: Returns prompt with {issue} substituted; includes AGENTS.md when present; uses AIDER_PHASE_PROMPTS for non-claude agents

    AC-4: Test executePhaseWithRetry()

    Verification Method: Unit Test (injected executePhaseFn)

    • Cold-start retry: failures under 60s trigger up to 2 retries
    • MCP fallback: after cold-start retries exhausted, retries with mcp: false
    • Retry disabled: config.retry === false → single call, no retry
    • Genuine failure: duration ≥60s → no retry (not cold-start)

    AC-5 (Derived): Export getPhasePrompt with @internal JSDoc tag

    Verification Method: Code review — matches executePhaseWithRetry precedent

    AC-6 (Derived): Test NEEDS_VERIFICATION verdict

    Verification Method: Unit Test — 4th QaVerdict value coverage


    Implementation Plan

    Phase 1: Pure function tests

    1. Create src/lib/workflow/phase-executor.test.ts
    2. Test parseQaVerdict() — all 4 verdict values × 4 formats + null cases
    3. Test formatDuration() — boundary at 60s, edge cases

    Phase 2: getPhasePrompt tests
    4. Export getPhasePrompt with @internal tag in phase-executor.ts
    5. Mock readAgentsMd via vi.mock("../agents-md.js")
    6. Test issue substitution, AGENTS.md present/absent, agent prompt selection

    Phase 3: executePhaseWithRetry tests
    7. Test cold-start retry, MCP fallback, retry disabled, genuine failure, first-attempt success


    Design Decisions

    • Export getPhasePrompt with @internal tag rather than testing indirectly — simpler, more focused tests, matches existing executePhaseWithRetry precedent
    • Use injected executePhaseFn for retry tests — the seam already exists, no new mocking infrastructure needed
    • Mock readAgentsMd via vi.mock — follows agents-md.test.ts pattern

    Non-Goals

    • Testing executePhase() directly (private, complex driver dependencies)
    • Integration testing with real Claude Code SDK
    • Testing PhaseSpinner or ShutdownManager (have their own test files)

    Recommended Workflow

    Phases: exec → qa | Quality Loop: disabled

  2. added
    planned
    on Mar 25, 2026
  3. added a commit that references this issue on Mar 25, 2026
  4. admarble commented on Mar 25, 2026

    @admarble
    ContributorAuthor

    Exec Phase — Progress Update

    AC Coverage

    AC Source Description Status Evidence
    AC-1 Original Test parseQaVerdict() ✅ Implemented 17 tests: all 4 verdicts × 4 formats + case insensitivity + null cases
    AC-2 Original Test formatDuration() ✅ Implemented 7 tests: 0s, <60s, 60s, >60s, fractional, exact minutes
    AC-3 Original Test getPhasePrompt() ✅ Implemented 7 tests: issue substitution, AGENTS.md present/absent, agent selection
    AC-4 Original Test executePhaseWithRetry() ✅ Implemented 8 tests: cold-start retry, MCP fallback, retry disabled, genuine failure
    AC-5 Derived Export getPhasePrompt with @internal ✅ Implemented phase-executor.ts:151
    AC-6 Derived Test NEEDS_VERIFICATION verdict ✅ Implemented Covered in parseQaVerdict verdict iteration tests

    Files Changed

    File Change
    src/lib/workflow/phase-executor.ts Export getPhasePrompt with @internal JSDoc tag
    src/lib/workflow/phase-executor.test.ts New — 44 unit tests

    Quality Gates

    Check Status
    npm run build ✅ Passed
    npm run lint ✅ Passed (0 errors, 0 warnings)
    npm test ✅ 44 new tests pass

    PR

    Created: #433

    Quality Plan Alignment: N/A — No quality plan in issue

    CHANGELOG: N/A (test-only change)

  5. admarble commented on Mar 25, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #378

    AC Coverage

    AC Source Description Status Notes
    AC-1 Original Test parseQaVerdict() MET 22 tests: all 4 verdicts × 4 formats (markdown header, bold, bold-wrapped, plain) + case insensitivity + null cases + multi-line extraction
    AC-2 Original Test formatDuration() MET 7 tests: 0s, <60s, fractional, exactly 60s, >60s, large values, exact minutes
    AC-3 Original Test getPhasePrompt() MET 7 tests: issue substitution, all-occurrences, AGENTS.md present/absent, aider vs claude-code vs undefined agent
    AC-4 Original Test executePhaseWithRetry() MET 8 tests: first-attempt success, cold-start retry (2 retries), MCP fallback, retry disabled, genuine failure (≥60s), MCP-already-off, original error preserved, sessionId
    Derived ACs
    AC-5 Derived (Code Quality) Export getPhasePrompt with @internal tag MET Exported at line 147, @internal JSDoc at line 145
    AC-6 Derived (Test Coverage) Test NEEDS_VERIFICATION verdict MET Covered in all 4 format iteration tests

    Coverage: 6/6 AC items fully met
    Original ACs: 4/4 met
    Derived ACs: 2/2 met


    Quality Plan Verification

    N/A — No quality plan found in issue comments


    CI Status

    Check Conclusion Impact
    build (20.x) SUCCESS ✅ MET
    build (22.x) SUCCESS ✅ MET
    typecheck SUCCESS ✅ MET
    validate-skills SUCCESS ✅ MET
    validate-plugin SUCCESS ✅ MET

    CI Summary: 5 passed, 0 pending, 0 failed


    Stale Branch Check

    Check Value
    Commits behind main 0
    Status ✅ OK

    Quality Metrics

    Metric Value Status
    Type issues (any) 0 OK
    Deleted tests 0 OK
    Files changed 2 (in #378 commit) OK
    Lines added +409 -
    Lines deleted -2 -

    Test Coverage Analysis

    Changed File Tier Has Tests? Test File
    src/lib/workflow/phase-executor.ts Standard ✅ Yes src/lib/workflow/phase-executor.test.ts
    src/lib/workflow/phase-executor.test.ts Optional (test file) N/A Self

    Coverage: 1/1 changed source files have corresponding tests


    Code Review

    Strengths:

    • Well-structured test organization: describe blocks map 1:1 to AC items
    • Excellent use of the existing executePhaseFn injection seam — no new mocking infrastructure needed
    • vi.mock("../agents-md.js") follows established patterns (agents-md.test.ts)
    • Loop-driven verdict format tests avoid repetitive boilerplate while maintaining clarity
    • Minimal source change: only exported getPhasePrompt with @internal tag — clean, low-risk
    • makeResult() helper keeps executePhaseWithRetry tests readable

    Issues:

    • None

    Suggestions:

    • None — test scope is appropriately focused on the AC

    Test Quality Review

    Category Status Notes
    Behavior vs Implementation ✅ OK Tests assert on outputs (return values, call counts), not internals
    Coverage Depth ✅ OK Error paths covered: null/empty input, cold-start exhaustion, MCP fallback failure
    Mock Hygiene ✅ OK Only readAgentsMd mocked (external I/O); executePhaseFn injected via existing seam
    Test Reliability ✅ OK No timing dependencies, all deterministic

    Anti-Pattern Detection

    Dependency Audit

    N/A — No dependency changes

    Code Patterns

    No anti-patterns detected in changed files.


    Call-Site Review

    New exported functions detected: 1 (getPhasePrompt)

    Function Call Sites Loop? Conditions AC Match
    getPhasePrompt() phase-executor.ts:181 (internal) No Unconditional in executePhase ✅ @internal — exported for testing only, no new external callers

    CHANGELOG Verification

    Result: N/A — test-only change, no user-facing changes


    Execution Evidence

    N/A — No executable changes (test-only addition + minor export change)


    Self-Evaluation

    • Verified working: Yes — ran npm test -- --run src/lib/workflow/phase-executor.test.ts and all 44 tests pass
    • Test efficacy: High — tests directly exercise the functions under test with specific assertions on return values; cold-start retry logic is thoroughly exercised through the injected executePhaseFn seam
    • Likely failure mode: If PHASE_PROMPTS or AIDER_PHASE_PROMPTS templates change, getPhasePrompt tests that assert on specific substrings (e.g., /spec 42, gh issue view) may need updating — this is expected and desirable (tests should break when prompt structure changes)
    • Verdict confidence: High — clean, focused test-only PR with full CI green

    Verdict: READY_FOR_MERGE

    All 6 AC items fully met. 44 unit tests covering parseQaVerdict, formatDuration, getPhasePrompt, and executePhaseWithRetry. Clean code review, no issues found. All CI checks pass.

    Documentation

    • N/A — No documentation needed (test-only change)

    Next Steps

    1. Merge PR feat(#378): add unit tests for phase-executor.ts #433
  6. added
    ready-for-review
    on Mar 25, 2026
  7. added a commit that references this issue on Mar 25, 2026
  8. admarble commented on Mar 25, 2026

    @admarble
    ContributorAuthor

    Documentation Generated

    File: docs/features/phase-executor-tests.md

    Sections Included:

    • What's Tested — parseQaVerdict, formatDuration, getPhasePrompt, executePhaseWithRetry
    • Coverage details — verdict formats, duration edge cases, retry/fallback scenarios
    • Running the Tests — vitest command
    • Troubleshooting — prompt template changes, mock setup

    Next Steps:

    1. Review generated documentation for accuracy
    2. Merge PR to deploy documentation

    Ready to merge!

  9. added a commit that references this issue on Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plannedready-for-review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions