---
status: pending
priority: p2
issue_id: 004
tags: [test-fix, important, code-duplication]
---

# Problem Statement

**Test helper functions need to be extracted to eliminate 50% code duplication**

The test file has significant code duplication with 9 instances of identical `with-redefs [auto-ap.solr/impl ...]` blocks repeated throughout. This violates DRY principles and increases maintenance burden.

**Impact**: Repetitive code is harder to maintain, increases file size, makes test modifications more error-prone, and violates fundamental coding best practices.

## Findings

- **Location**: Lines 20, 47, 65, 77, 92, 106, 122, 133, 146 (9 instances)
- **Duplication pattern**:
  ```clojure
  (with-redefs [auto-ap.solr/impl (auto-ap.solr/->InMemSolrClient (atom {}))]
    ...
  ```

- **Example of duplicated code** (appears 9 times):
  ```clojure
  (with-redefs [auto-ap.solr/impl (auto-ap.solr/->InMemSolrClient (atom {}))]
    (let [admin-identity (admin-token)
          result (sut/account-save {:form-params {:account/numeric-code 12345 ...
                                                    :account/name "New Cash Account"
                                                    :account/type :account-type/asset
                                                    :account/location "B"}
                                   :request-method :post
                                   :identity admin-identity})
          db (dc/db conn)]
      ...
    ))
  ```

- **Additional duplication**: Account creation pattern repeated in 7 tests (lines 49, 67, 81, 94-96, 106, 122, 133, 146)

- **Evidence**: Pattern-recognition-specialist identified this as 50% code duplication

## Proposed Solutions

### Option 1: Extract Solr mock helper (RECOMMENDED FIRST)
**Pros**:
- Reduces LOC by ~18 lines (2 lines per test × 9 tests)
- Simple implementation
- No changes to test logic
- Consistent mocking across all tests

**Cons**:
- None

**Effort**: Small

**Risk**: None

```clojure
;; Helper function to add to test namespace
(defn with-solr-mock [f]
  (with-redefs [auto-ap.solr/impl (auto-ap.solr/->InMemSolrClient (atom {}))]
    (f)))

;; Usage in tests:
(with-solr-mock
  (let [...])
  )
```

### Option 2: Extract account creation helper
**Pros**:
- Reduces LOC by ~20 lines
- Makes test data creation consistent

**Cons**:
- Requires changing test signatures

**Effort**: Medium

**Risk**: Medium

```clojure
;; Helper function
(defn create-account [{:keys [numeric-code name location type]}
                      & {:keys [account-type account-location]
                         :or {account-type :account-type/asset
                              account-location "A"}}]
  (sut/account-save {:form-params {:account/numeric-code numeric-code
                                    :account/name name
                                    :account/type account-type
                                    :account/location account-location}
                     :request-method :post
                     :identity (admin-token)}))

;; Usage:
(create-account {:numeric-code 12345 :name "New Account" :location "B"})
```

### Option 3: Extract full account save helper with all common params
**Pros**:
- Most concise approach

**Cons**:
- Less flexible for test variations
- Requires changing many test signatures

**Effort**: Medium

**Risk**: Medium

### Option 4: Leave as-is
**Pros**:
- No changes needed

**Cons**:
- Maintains duplication
- Violates DRY principle
- Future changes require 9 updates

**Effort**: None

**Risk**: None

## Recommended Action

**Extract Solr mock helper function to eliminate duplication.**

This is the lowest-risk, highest-impact first step. Once implemented, proceed to extract account creation helper if time permits.

## Technical Details

**Affected Component**: Test utilities and helpers

**Affected Files**:
- `test/clj/auto_ap/ssr/admin/accounts_test.clj` (lines 10-8)

**Helper Function Location**: Should be added after the fixture setup at the top of the file, before the first test.

**Database Changes**: None

**API Changes**: None

**Related Code**: `auto-ap.solr/impl` for mocking

## Acceptance Criteria

- [ ] `with-solr-mock` helper function added to test namespace
- [ ] All 9 occurrences of `with-redefs` replaced with helper call
- [ ] All tests still pass after refactoring
- [ ] No duplicate code remains (50% reduction achieved)

## Work Log

- **2026-02-06**: Finding documented through code review agents (pattern-recognition-specialist, code-simplicity-reviewer)

## Resources

- **Review Agents**:
  - pattern-recognition-specialist (task ses_3c9578ae3ffejQg4Wl4GT1ZSAk)
  - code-simplicity-reviewer (task ses_3c958304dffeVRAAPfMSv583Ik)
