# LazyDomain Test Suite

This directory contains integration and regression tests for LazyDomain's domain checking functionality.

## Test Structure

```
tests/
├── README.md           # This file
└── rdap_checking.rs    # RDAP domain checking regression tests
```

## RDAP Regression Tests

The `rdap_checking.rs` file contains comprehensive regression tests for RDAP (Registration Data Access Protocol) domain checking.

### What These Tests Cover

#### 1. **Known Taken Domains**
Tests verify that well-known domains correctly show as "Taken":
- `google.com` / `google.net` / `google.org` / `google.io`
- `microsoft.com` / `microsoft.net`
- `amazon.com`
- `facebook.com`

**Critical Regression:** The `test_google_net_is_taken()` test specifically guards against a bug where `google.net` incorrectly showed as "Available" due to using the wrong RDAP endpoint.

#### 2. **TLD Registry Correctness**
Tests ensure each TLD uses its correct RDAP server:
- **Verisign** - `.com` and `.net` (separate endpoints!)
- **Public Interest Registry** - `.org`
- **Identity Digital** - `.io`, `.co`, `.app`, `.dev`, etc.

#### 3. **Multi-TLD Independence**
The `test_tld_endpoints_are_independent()` test verifies that:
- `.com` and `.net` use different RDAP endpoints
- Each TLD's results are independent
- No cross-contamination between TLD checks

#### 4. **DNS Verification Fallback**
Tests verify DNS verification when RDAP returns 404:
- `google.co` / `amazon.co` / `twitter.co` - Real domains with incomplete RDAP data
- DNS resolution confirms domain is actually taken
- Truly unregistered domains correctly show as available

**Critical Feature:** Some RDAP servers (like `.co`) have incomplete data and return 404 even for registered domains. We use DNS resolution (A/AAAA record lookup) as a fallback to verify if the domain is actually registered.

#### 5. **Error Handling**
Tests verify proper error handling for:
- Unsupported TLDs
- Invalid domain names
- Empty domain names or TLDs

#### 6. **Real-World Scenarios**
Tests simulate actual usage patterns:
- Checking multiple TLDs for the same domain
- Checking popular tech company domains
- Verifying well-known domains across multiple TLDs

## Running the Tests

### Run All RDAP Tests
```bash
just test-rdap
```

### Run Specific Test
```bash
cargo test test_google_net_is_taken
```

### Run Tests with Output
```bash
cargo test test_google_net_is_taken -- --nocapture
```

### Run All Tests
```bash
just test
```

## Important Notes

### Network Dependency
⚠️ **These tests make real network calls to RDAP servers!**

- Tests require internet connection
- Tests may be slow (network latency)
- Tests may fail if RDAP servers are down
- Consider using `--test-threads=1` to avoid rate limiting

### Rate Limiting
Some RDAP servers may rate-limit requests. If tests fail due to rate limiting:

```bash
# Run tests sequentially
cargo test --test rdap_checking -- --test-threads=1

# Run with delays between tests
cargo test --test rdap_checking -- --test-threads=1 --nocapture
```

### CI/CD Considerations
For CI/CD pipelines, consider:
1. Caching test results
2. Running tests less frequently (e.g., nightly)
3. Using mock RDAP servers for faster tests
4. Separating network-dependent tests from unit tests

## Test Categories

### Regression Tests
Tests that guard against specific bugs that were fixed:
- ✅ `test_google_net_is_taken()` - Guards against wrong .net RDAP endpoint
- ✅ `test_tld_endpoints_are_independent()` - Ensures .com and .net use different endpoints
- ✅ `test_google_co_dns_verification()` - Guards against false "Available" for domains with incomplete RDAP data
- ✅ `test_amazon_co_dns_verification()` - Verifies DNS fallback for .co domains
- ✅ `test_twitter_co_dns_verification()` - Additional .co DNS verification test

### Smoke Tests
Tests that verify basic functionality works:
- ✅ `test_google_com_is_taken()`
- ✅ `test_microsoft_com_is_taken()`
- ✅ `test_verisign_com_tld()`

### Integration Tests
Tests that verify multiple components work together:
- ✅ `test_multiple_tlds_for_same_domain()`
- ✅ `test_popular_tech_companies()`
- ✅ `test_well_known_domains_across_tlds()`

## Adding New Tests

When adding new RDAP regression tests:

1. **Name tests descriptively**: `test_<what>_<expected_behavior>`
2. **Add comments explaining WHY**: Especially for regression tests
3. **Use well-known domains**: Google, Microsoft, etc. are stable test fixtures
4. **Test edge cases**: Empty strings, invalid TLDs, etc.
5. **Document bugs fixed**: Add comments like `// REGRESSION: <bug description>`

### Example Test Template

```rust
#[tokio::test]
async fn test_example_domain_behavior() {
    // REGRESSION: <If this guards against a specific bug, describe it>
    let status = check_domain_status("example", "com").await;
    assert_eq!(
        status,
        CheckStatus::Taken,
        "example.com should be taken (reason)"
    );
}
```

## Troubleshooting

### Tests Failing?

1. **Check internet connection** - Tests require network access
2. **Check RDAP server status** - Servers may be temporarily down
3. **Check for rate limiting** - Run with `--test-threads=1`
4. **Check domain status** - Rarely, test domains may change status

### Adding Debug Output

```rust
#[tokio::test]
async fn test_with_debug() {
    let client = create_client();
    let domain = Domain::new("example", "com").unwrap();
    let result = client.check_domain(&domain).await.unwrap();
    
    eprintln!("Domain: {}.{}", result.domain, result.tld);
    eprintln!("Status: {:?}", result.status);
    eprintln!("Registrar: {:?}", result.registrar);
    
    assert_eq!(result.status, CheckStatus::Taken);
}
```

## Future Improvements

- [ ] Add mock RDAP server for faster unit tests
- [ ] Add performance benchmarks
- [ ] Add tests for concurrent domain checking
- [ ] Add tests for caching behavior
- [ ] Add tests for retry logic
- [ ] Separate network tests from pure logic tests
