# Duplicate Prevention Testing Guide

**Last Updated:** 2026-01-28

## Quick Test Commands

### 1. Test File Locking & In-Memory Tracking

```bash
php v2/scripts/test-concurrent-submissions.php --dry-run
```

**Expected Output:**

- ✅ Lock acquisition/release works
- ✅ In-memory tracking works
- ✅ Lock cleanup works

### 2. Monitor Recent Submissions

```bash
php v2/scripts/monitor-event-form-submissions.php --days=1
```

**Checks:**

- Duplicates within 5 minutes
- Race conditions (identical timestamps within 1 second)
- Invalid Interested In values

### 3. Investigate Specific Duplicate

```bash
php v2/scripts/investigate-duplicate-submissions.php --email=maxine@testmail.de --event="Intergastra 26"
```

**Shows:**

- Detailed comparison of duplicate submissions
- Time difference
- Lock file status
- Log entries

## Browser Testing Checklist

### Test 1: Rapid Double-Click

1. Fill out form
2. Rapidly double-click submit button
3. **Expected:** Only one submission, form disabled during submission

### Test 2: Multiple Tabs

1. Open form in Tab 1
2. Open same form in Tab 2 (same email)
3. Submit in Tab 1
4. Immediately submit in Tab 2
5. **Expected:** Tab 2 shows "Submission already in progress" error

### Test 3: Network Retry

1. Fill out form
2. Submit form
3. Simulate network error (DevTools → Network → Offline)
4. Form should queue for offline sync
5. **Expected:** No duplicate when sync completes

### Test 4: Form Reset

1. Submit form successfully
2. Click "Weitere Kontakt erfassen" (Add Another)
3. **Expected:** Form resets with preserved owner, ready for new submission

### Test 5: Visual Indicators

1. Submit form
2. **Expected:**
   - Form opacity reduces to 0.7
   - Form inputs disabled
   - Submit button shows loading state

## HubSpot Verification

### Check for Duplicates

1. Go to HubSpot → Contacts
2. Filter by `sign_up_type__c` = "Event Lead Capture"
3. Sort by Created Date
4. Look for contacts with:
   - Same email
   - Same event name (`content` field)
   - Created within 1 second of each other

### Verify Request IDs

1. Check submission logs: `v2/logs/event-lead-capture-{date}.log`
2. Search for `request_id`
3. Verify each submission has unique request ID

### Verify Lock Files

```bash
ls -la writable/locks/
```

**Expected:**

- Directory exists
- No lock files (or only very recent ones <60 seconds old)
- Stale locks auto-cleanup

## Load Testing (Advanced)

### Using Apache Bench

```bash
# Create test data file
cat > test-data.json << 'EOF'
{
  "firstname": "Test",
  "lastname": "Concurrent",
  "email": "test-concurrent@example.com",
  "company": "Test Company",
  "customer_type__c": "Neukunde",
  "owner": "TestOwner",
  "event_name": "Test Event",
  "event_id": "test-event-123"
}
EOF

# Run concurrent requests
ab -n 10 -c 5 -p test-data.json -T application/json \
   http://your-domain/v2/api/event-lead-capture.php
```

**Expected:**

- Only 1 successful submission (200/204)
- 9 responses with `duplicate: true` or `message: "Submission already in progress"`

## Monitoring Schedule

### Daily

```bash
php v2/scripts/monitor-event-form-submissions.php --days=1
```

### Weekly

```bash
php v2/scripts/monitor-event-form-submissions.php --days=7
```

Review for:

- Race condition alerts
- Lock file cleanup issues
- Duplicate patterns

## Troubleshooting

### Issue: Test script fails

**Check:**

- PHP syntax: `php -l v2/scripts/test-concurrent-submissions.php`
- File permissions on `writable/locks/`
- PHP `flock()` function available

### Issue: Lock files not cleaning up

**Check:**

- File permissions: `ls -la writable/locks/`
- PHP process can write/delete: `touch writable/locks/test.lock && rm writable/locks/test.lock`
- Stale lock cleanup logic in `acquireSubmissionLock()`

### Issue: Duplicates still occurring

**Investigate:**

1. Run investigation script for specific email
2. Check logs for lock acquisition failures
3. Verify HubSpot API response times
4. Check if file locking is working: `php v2/scripts/test-concurrent-submissions.php`
