# Setup Test Data and Run Tests

This document explains how to use the automated setup script to create test data and run the full test suite.

## Quick Start

Run the setup script which will:
1. Create a new company
2. Create a collection agent
3. Create line and area entries
4. Create a customer
5. Create a loan entry
6. Run the full test suite

```bash
npm run test:setup
```

Or directly:
```bash
node setup_test_data.js
```

## What Gets Created

### 1. Company
- **Name**: "Test Day Loan Company"
- **Email**: "test@dayloan.com"

### 2. Collection Agent
- **Name**: "Test Collection Agent"
- **Mobile**: "9876543210"
- **Line**: "Line-1"
- **Status**: "active"
- **Report Time**: 9 AM - 5 PM

### 3. Line Entries
- Line: "Line-1" with areas: "Area-A", "Area-B", "Area-C"

### 4. Area Entries
- Areas: "Area-A", "Area-B", "Area-C"

### 5. Customer
- **Name**: "Test Customer"
- **Mobile**: "9876543211"
- **Area**: "Area-A"
- Complete address and identification details

### 6. Loan Entry
- **Loan ID**: "TEST-LOAN-001"
- **Amount**: ₹10,000
- **Total Amount**: ₹12,500
- **Dues**: 30 days
- **Next Due Date**: Yesterday (to ensure it's due for collection)
- **Per Due Amount**: ₹333.33

## Script Features

### Idempotent Operations
The script checks if data already exists before creating:
- If company exists → uses existing company
- If agent exists → updates if needed, otherwise uses existing
- If customer exists → uses existing customer
- If loan exists → uses existing loan

This means you can run the script multiple times safely.

### Automatic Test Execution
After creating test data, the script automatically:
1. Sets environment variables with test data IDs
2. Waits for data to be committed
3. Runs the full test suite

## Test Data Configuration

You can modify the test data in `setup_test_data.js`:

```javascript
const TEST_DATA = {
  company: {
    com_name: 'Test Day Loan Company',
    email: 'test@dayloan.com'
  },
  agent: {
    name: 'Test Collection Agent',
    mobile_no: '9876543210',
    // ... other fields
  },
  // ... other test data
};
```

## Environment Variables

The script sets these environment variables for the test suite:
- `TEST_MOBILE_NO`: Collection agent mobile number
- `TEST_LOAN_ID`: Loan ID for testing
- `TEST_COM_ID`: Company ID

## Manual Cleanup

If you want to clean up test data manually:

```sql
-- Delete test loan receipts
DELETE FROM loan_receipt WHERE loan_id = 'TEST-LOAN-001';

-- Delete test expenses
DELETE FROM expense WHERE agent_id IN (
  SELECT id FROM collection_agent WHERE mobile_no = '9876543210'
);

-- Delete test loan entry
DELETE FROM loan_entry WHERE loan_id = 'TEST-LOAN-001';

-- Delete test customer
DELETE FROM customers WHERE cust_mobile = '9876543211';

-- Delete test line entries
DELETE FROM line_entry WHERE line = 'Line-1' AND com_id = (
  SELECT com_id FROM collection_agent WHERE mobile_no = '9876543210' LIMIT 1
);

-- Delete test area entries
DELETE FROM area WHERE com_id = (
  SELECT com_id FROM collection_agent WHERE mobile_no = '9876543210' LIMIT 1
);

-- Delete test collection agent
DELETE FROM collection_agent WHERE mobile_no = '9876543210';

-- Delete test company (only if no other data depends on it)
DELETE FROM company WHERE com_name = 'Test Day Loan Company';
```

## Troubleshooting

### Error: "Company already exists"
- This is normal if you've run the script before
- The script will use the existing company

### Error: "Agent already exists"
- The script will update the agent's company_id and line if needed
- Or use the existing agent

### Error: "Database connection failed"
- Make sure your database is running
- Check `config/database.js` for correct credentials
- Verify database exists

### Error: "Table doesn't exist"
- Run database migrations if available
- Or create tables manually based on model definitions

### Tests Fail After Setup
- Check that the server is running: `npm start`
- Verify the API URL in test scripts matches your server
- Check server logs for errors

## Running Tests Separately

If you only want to run tests without setup:

```bash
npm test
```

Make sure you have:
- Valid test data in database
- Environment variables set (or update TEST_CONFIG in test script)

## Custom Test Data

To use different test data:

1. Modify `TEST_DATA` object in `setup_test_data.js`
2. Or create your own setup script based on this one
3. Update test configuration in `test_collection_agent_api.js`

## Notes

- The script creates real database records
- Test data persists after tests complete
- You may want to clean up test data periodically
- The script is safe to run multiple times (idempotent)

