---
name: step-08-run-tests
description: Run Final Test Sweep until 100% pass — fix CODE not tests
model: opus
prev_step: steps/step-07-tests.md
next_step: COMPLETE
---

# Step 8: Run Final Test Sweep

**CRITICAL:** Fix the CODE, not the tests. Tests represent expected behavior.
Target 100% pass for Sonar quality gate. If stuck after max retries, skip with TODO and escalate.

---

## 1. Build

```bash
dotnet build --no-restore
# Note: WSL bin\Debug cleanup handled by PostToolUse hook (wsl-dotnet-cleanup.sh)
```

**MUST PASS before running tests.**

---

## 2. Run Full Test Suite (backend + frontend in parallel)

```bash
# Run backend and frontend tests in parallel when both exist
WEB_DIR=$(find . -name "vitest.config.ts" -not -path "*/node_modules/*" -exec dirname {} \; 2>/dev/null | head -1)

if [ -n "$WEB_DIR" ]; then
  # Both exist → run in parallel
  dotnet test --no-build --verbosity normal &
  BACKEND_PID=$!
  (cd "$WEB_DIR" && npm run test) &
  FRONTEND_PID=$!
  wait $BACKEND_PID; BACKEND_RC=$?
  wait $FRONTEND_PID; FRONTEND_RC=$?
  [ $BACKEND_RC -ne 0 ] || [ $FRONTEND_RC -ne 0 ] && echo "FAILURES DETECTED"
else
  # Backend only
  dotnet test --no-build --verbosity normal
fi
```

---

## 3. Analyze Results

**If ALL PASS:** Skip to step 5 (Summary).

**If FAILURES:** For each failing test:

```markdown
### Failing Test: {test_name}
**File:** {test_file}:{line}
**Error:** {error_message}

**Root Cause Analysis:**
- [ ] Code bug: implementation doesn't match expected behavior
- [ ] Missing dependency: service/mock not configured
- [ ] Data issue: test data setup incomplete
```

---

## 4. Fix Loop (max 5 iterations)

> **Max 5 iterations (vs max 3 inline).** This catches cross-layer problems that inline tests missed.

```
WHILE tests failing AND iteration < 5:
  1. Identify root cause (ALWAYS code bug, not test bug)
  2. Fix the production CODE via appropriate skill/MCP
  3. dotnet build --no-restore
  4. dotnet test --no-build
  5. Log result
```

**Rules:**
- Fix CODE, never modify test assertions
- Use the same skill/MCP that created the original code
- After fix, rebuild before retesting
- If stuck after 3 iterations on same test, ask user

**If stuck (auto_mode = false):**

```yaml
questions:
  - header: "Test Fix"
    question: "Test is still failing after multiple attempts. How should we proceed?"
    options:
      - label: "Try alternative fix (Recommended)"
        description: "Attempt a different approach to fix the code"
      - label: "Skip this test"
        description: "Mark as skipped with TODO comment"
      - label: "Discuss the issue"
        description: "Need help understanding the failure"
    multiSelect: false
```

---

## 5. Typecheck (if applicable)

> **Note:** Frontend tests already ran in parallel with backend tests in section 2.

```bash
npm run typecheck
```

**MUST PASS.** If frontend tests failed in section 2, apply the same fix loop as backend (fix CODE, not tests, max 5 iterations).

---

## 6. Commit Tests

```
test({module}): security tests and coverage sweep
```

---

## 7. Save + Summary

If save_mode: write to `{output_dir}/08-run-tests.md`.

Display: total tests, passing count, iterations to green, fixes applied, coverage %.

## 8. Route to Next Step

If pr_mode: create PR via `gh pr create`, show URL.
Otherwise: show final summary (task, context, files, quality checks, commits, next steps).
