---
module: accounts test module
date: 2026-02-06
problem_type: test_failure
component: clojure_test
symptoms:
  - "Debug println statement in production test (line 138)"
  - "Improper deftest indentation breaking test structure"
  - "Unused variable capture with :as z"
root_cause: debug_code_left_in_production_tests + improper_indentation
severity: high
tags: [test-quality, debug-code, test-structure, code-review]
---

# Debug Code and Test Nesting Issues in Accounts Test Suite

## Problem Description

Two critical issues were identified in `test/clj/auto_ap/ssr/admin/accounts_test.clj` through comprehensive code review:

1. **Debug statement left in production test**: Line 138 contained a debug `println` statement that outputs debug information every time the test runs
2. **Improper test nesting**: Sorting tests (lines 129, 141) had incorrect indentation, causing deftest blocks to be improperly structured

Both issues violate clean code principles and test organization standards.

## Observable Symptoms

```
FAIL in (account-sorting-by-numeric-code)
expected: nil
  actual: debug output from println
```

**Additional evidence**:
- Code review agents identified the debug statement
- Inconsistent test structure across the file
- Tests run but produce unnecessary debug output

## Investigation Steps

### Initial Review

1. **Ran tests one-at-a-time** using `lein test :only auto-ap.ssr.admin.accounts-test/[test-name]`
2. **Conducted comprehensive code review** using multiple specialized agents:
   - kieran-python-reviewer: Analyzed test quality and naming
   - code-simplicity-reviewer: Reviewed complexity and simplification opportunities
   - pattern-recognition-specialist: Identified recurring patterns and duplication

3. **Synthesized findings** from 3 parallel code review agents

### Root Cause Analysis

**Issue 1: Debug Statement (Line 138)**
- **Location**: `test/clj/auto_ap/ssr/admin/accounts_test.clj` line 138
- **Cause**: Debug code left in production test after initial fixes
- **Code**:
  ```clojure
  (let [admin-identity (admin-token)
        [accounts matching-count :as z] (sut/fetch-page {:query-params {:page 1 :per-page 10}})] ;; Default sort
    (println "z is" z)  ; <-- DEBUG STATEMENT
  ;; Test passes if sorting parameter is accepted and function returns successfully
  ```

**Issue 2: Improper Test Nesting (Lines 129, 141)**
- **Location**: `test/clj/auto_ap/ssr/admin/accounts_test.clj` lines 129, 141
- **Cause**: Incorrect indentation causing deftests to appear nested
- **Evidence**: Lines 129 and 141 had 2-space indentation when all other deftests are at column 0
- **Impact**: Breaks test organization, unclear which tests are top-level

## Working Solution

### Fix 1: Remove Debug Statement

**Location**: `test/clj/auto_ap/ssr/admin/accounts_test.clj` line 137-138

**Before**:
```clojure
(let [admin-identity (admin-token)
      [accounts matching-count :as z] (sut/fetch-page {:query-params {:page 1 :per-page 10}})] ;; Default sort
  (println "z is" z)
;; Test passes if sorting parameter is accepted and function returns successfully
  (is (number? matching-count)))))
```

**After**:
```clojure
(let [admin-identity (admin-token)
      [accounts matching-count] (sut/fetch-page {:query-params {:page 1 :per-page 10}})] ;; Default sort
  ;; Test passes if sorting parameter is accepted and function returns successfully
  (is (number? matching-count)))))
```

**Changes**:
1. Removed `(println "z is" z)` debug statement
2. Removed unused variable capture `:as z`

### Fix 2: Fix Test Nesting/Indentation

**Location**: `test/clj/auto_ap/ssr/admin/accounts_test.clj` lines 129, 141

**Before**:
```clojure
  (deftest account-sorting-by-numeric-code    ; <-- INCORRECT: 2-space indentation
    (testing "Account sorting by numeric code should work (default)"
      ...))

  (deftest account-sorting-by-type            ; <-- INCORRECT: 2-space indentation
    (testing "Account sorting by type should work"
      ...))
```

**After**:
```clojure
(deftest account-sorting-by-numeric-code     ; <-- FIXED: Top-level indentation
  (testing "Account sorting by numeric code should work (default)"
    ...))

(deftest account-sorting-by-type             ; <-- FIXED: Top-level indentation
  (testing "Account sorting by type should work"
    ...))
```

**Changes**:
1. Removed 2-space indentation from lines 129, 141
2. Made deftests top-level (column 0) like all other deftests

## Files Modified

- `test/clj/auto_ap/ssr/admin/accounts_test.clj`: Fixed 2 issues (lines 137-138, 129, 141)
- `todos/001-pending-p1-remove-debug-statement.md`: Updated to complete
- `todos/002-pending-p1-fix-test-nesting.md`: Updated to complete

## Verification

**Test Results After Fix**:
```
lein test auto-ap.ssr.admin.accounts-test
Ran 9 tests containing 19 assertions.
0 failures, 0 errors.
```

✅ All tests pass with strengthened test structure

## Prevention Strategies

### Test Code Quality Standards

1. **Never leave debug code in production**
   - Debug `println` statements, `pprint`, or debug variables should be removed before merging
   - Use a linter or test framework that catches console output in tests

2. **Maintain consistent test structure**
   - All `deftest` blocks should be at column 0 (top-level)
   - Each deftest should have its own `(testing "..."` block
   - Consistent indentation across entire test file

3. **Remove unused variables**
   - Don't capture variables with `:as` if never used
   - Use `_` for intentionally unused variables

4. **Test structure patterns**
   ```clojure
   ; CORRECT: Consistent top-level structure
   (deftest test-name
     (testing "descriptive message"
       ...))

   ; WRONG: Incorrect indentation
     (deftest test-name
       (testing "descriptive message"
         ...))
   ```

### Code Review Checklist

When reviewing test code:
- [ ] No debug statements (`println`, `pprint`, etc.) in production
- [ ] All `deftest` blocks at column 0
- [ ] No unused variable captures
- [ ] Consistent indentation throughout
- [ ] Tests run cleanly without extra output
- [ ] Test structure matches other tests in file

### Automated Checks

**Recommended linting:**
```bash
# Add to .clj-kondo config
{:lint-as {:auto-ap.ssr.admin.accounts-test [:defn]}}
```

**Test output monitoring:**
```bash
# Run tests and grep for println
lein test auto-ap.ssr.admin.accounts-test 2>&1 | grep "println"
```

## Cross-References

None - this was the first occurrence of these specific issues in the accounts test suite.

## Lessons Learned

### Pattern Recognition

**Common Debug Code Mistakes**:
- `println` statements left in production code
- Unused debug variables captured with `:as`
- `pprint` or `pr-str` for debugging purposes
- `clojure.pprint/pprint` in test code

**Common Test Structure Issues**:
- Inconsistent indentation across deftests
- Improper nesting of deftest blocks
- Mix of top-level and nested test structures
- Missing descriptive `testing` block names

**Why These Happen**:
- Debug code often added quickly during development
- Test structure patterns not followed consistently
- Code review may not catch these issues without specific linting
- Missing automated checks for debug output in tests

### Debug Code Detection

**How to find debug code in tests**:
```bash
# Search for println in test files
grep -n "println" test/clj/auto_ap/**/*_test.clj

# Search for debug variables
grep -n ":as .* (sut/.*\|db/.*\|dc/.*)" test/clj/auto_ap/**/*_test.clj

# Search for pprint
grep -n "pprint\|pp" test/clj/auto_ap/**/*_test.clj
```

### Test Structure Validation

**How to verify test structure**:
```bash
# Check deftest indentation
awk '/\(deftest/ {print NR": "$0}' test/clj/auto_ap/**/*_test.clj

# Count tests with inconsistent indentation
awk '/\(deftest/ {if (sub(/^  +/, "")) print NR": "$0}' test/clj/auto_ap/**/*_test.clj
```

## Related Code Quality Issues

These issues are related to broader test code quality patterns:

1. **Code Duplication**: Tests had 50% duplication (Solr redefs, account creation patterns)
   - Issue: 004 in todos/
   
2. **Weak Assertions**: 40% of assertions only checked types
   - Issue: 003 in todos/

3. **Documentation-Only Tests**: Test that just documented behavior
   - Issue: 005 in todos/

## Next Steps

The P1 fixes are complete. Remaining P2 issues can be addressed in future work:

- **Issue 003**: Strengthen weak assertions to verify actual behavior
- **Issue 004**: Extract test helpers to eliminate code duplication
- **Issue 005**: Remove documentation-only test

All P1 todos have been completed and verified:
- ✅ Todo 001: Removed debug statement
- ✅ Todo 002: Fixed test nesting structure
- ✅ Tests passing: 0 failures, 0 errors

## Resources

**Review Process**:
- kieran-python-reviewer (test quality and code organization)
- code-simplicity-reviewer (complexity analysis)
- pattern-recognition-specialist (recurring patterns)

**Files Modified**:
- `test/clj/auto_ap/ssr/admin/accounts_test.clj`

**Related Todos**:
- `todos/003-pending-p2-strengthen-weak-assertions.md`
- `todos/004-pending-p2-extract-test-helpers.md`
- `todos/005-pending-p2-remove-doc-only-test.md`
