# Test Artifacts Organization

## Overview

This document explains how test artifacts (temporary files, SQLite databases, etc.) are organized and automatically cleaned up.

---

## 📁 Directory Structure

### Before (❌ Disorganized)
```
storage/
├── testing_wave_failure_68d75d4066e19.sqlite    ❌ Root level
├── testing_wave_failure_68d75d40559a9.sqlite    ❌ Root level
├── testing_wave_failure_68d75d40749ad.sqlite    ❌ Root level
├── app/
│   └── temp-test-68ded617baa40/                 ❌ Not in temp/
```

### After (✅ Organized)
```
storage/
├── app/
│   └── temp/
│       ├── .gitignore                           ✅ Ignore all temp files
│       └── testing/                             ✅ Test artifacts here
│           ├── test-68ded617baa40/              ✅ Temp test directories
│           └── service-test-68ded617baa41/      ✅ Service test directories
├── framework/
│   └── testing/
│       ├── .gitignore                           ✅ Ignore all test files
│       ├── wave_failure_68d75d4066e19.sqlite    ✅ SQLite test databases
│       └── wave_failure_68d75d40559a9.sqlite    ✅ SQLite test databases
```

---

## 🧹 Automatic Cleanup

### Configuration

**File:** `config/maintenance.php`

```php
'temp_cleanup' => [
    'paths' => [
        storage_path('app/temp'),              // Temp files & test directories
        storage_path('framework/testing'),     // SQLite test databases
    ],
    'retention_hours' => 24,                   // Clean files older than 24h
    'safety_window_minutes' => 10,             // Skip files < 10 min old
],
```

### Scheduler

**Runs:** Hourly at minute 15  
**Command:** `php artisan maintenance:cleanup-temp`

```php
// src/App/Console/Kernel.php
$schedule->command('maintenance:cleanup-temp')
    ->hourlyAt(15)
    ->withoutOverlapping();
```

---

## 📝 Test File Updates

### 1. MaintenanceTempCleanUpTest

**Before:**
```php
$this->testPath = storage_path('app/temp-test-' . uniqid());
```

**After:**
```php
$testingRoot = storage_path('app/temp/testing');
$this->testPath = $testingRoot . '/test-' . uniqid();
```

### 2. LocalStorageServiceTest

**Before:**
```php
$this->testRoot = storage_path('framework/testing/local-storage-service');
```

**After:**
```php
$testingRoot = storage_path('app/temp/testing');
$this->testRoot = $testingRoot . '/service-test-' . uniqid();
```

### 3. WaveCoordinatorFailureTest

**Before:**
```php
$testDatabase = storage_path('testing_wave_failure_' . uniqid() . '.sqlite');
```

**After:**
```php
$testingDir = storage_path('framework/testing');
$testDatabase = $testingDir . '/wave_failure_' . uniqid() . '.sqlite';
```

---

## 🎯 Benefits

### 1. **Organization**
- ✅ All test artifacts in dedicated directories
- ✅ Clear separation between production and test files
- ✅ Easy to identify and manage test files

### 2. **Automatic Cleanup**
- ✅ Test files cleaned automatically after 24 hours
- ✅ No manual intervention required
- ✅ Prevents storage buildup

### 3. **Git Hygiene**
- ✅ `.gitignore` prevents test artifacts from being committed
- ✅ Only directory structure is tracked
- ✅ Clean repository

### 4. **Performance**
- ✅ Reduced storage usage
- ✅ Faster filesystem operations
- ✅ No clutter in storage root

---

## 🔍 Monitoring

### Check Current Test Artifacts

```bash
# Check temp test directories
ls -lah storage/app/temp/testing/

# Check SQLite test databases
ls -lah storage/framework/testing/*.sqlite
```

### Manual Cleanup

```bash
# Dry-run (preview what would be deleted)
php artisan maintenance:cleanup-temp --dry-run

# Actual cleanup
php artisan maintenance:cleanup-temp

# Clean specific path
php artisan maintenance:cleanup-temp --path=storage/framework/testing

# Custom retention (7 days)
php artisan maintenance:cleanup-temp --older-than=7d
```

### Statistics

```bash
# Check disk usage
du -sh storage/app/temp/
du -sh storage/framework/testing/

# Count files
find storage/app/temp/testing -type f | wc -l
find storage/framework/testing -name "*.sqlite" | wc -l
```

---

## 🚨 Troubleshooting

### Test Artifacts Not Being Cleaned

**Check:**
1. Verify paths in `config/maintenance.php`
2. Check file modification times: `ls -lh storage/framework/testing/`
3. Run with `--dry-run` to see what would be deleted
4. Check scheduler is running: `php artisan schedule:list`

### Tests Creating Files in Wrong Location

**Fix:**
1. Update test file to use new directory structure
2. Ensure `storage/app/temp/testing` or `storage/framework/testing` exists
3. Run tests again

### Storage Full Despite Cleanup

**Solutions:**
1. Reduce retention period: `'retention_hours' => 12`
2. Run cleanup more frequently: `->everyThirtyMinutes()`
3. Manually clean old files: `php artisan maintenance:cleanup-temp --older-than=1h`

---

## 📋 Migration Checklist

- [x] Create `storage/app/temp/testing/` directory
- [x] Create `storage/framework/testing/` directory
- [x] Add `.gitignore` files
- [x] Update `MaintenanceTempCleanUpTest`
- [x] Update `LocalStorageServiceTest`
- [x] Update `WaveCoordinatorFailureTest`
- [x] Move existing SQLite files to new location
- [x] Update `config/maintenance.php` paths
- [x] Verify tests pass
- [x] Verify cleanup command detects files

---

## 📊 Impact

| Metric | Before | After | Improvement |
|--------|--------|-------|-------------|
| **Files in storage root** | 16 SQLite | 0 | ✅ 100% cleaner |
| **Organized directories** | 0 | 2 | ✅ Better structure |
| **Auto-cleanup paths** | 1 | 2 | ✅ More coverage |
| **Git-ignored files** | Manual | Automatic | ✅ Better hygiene |

---

## 🔄 Future Improvements

1. **Add metrics tracking**
   - Track cleanup frequency
   - Monitor storage trends
   - Alert on unusual growth

2. **Extend to other test types**
   - Integration test artifacts
   - Performance test data
   - Mock data files

3. **Configurable per-test retention**
   - Different retention for different test types
   - Priority-based cleanup
   - Adaptive retention based on storage

4. **Test artifact archiving**
   - Archive old test files before deletion
   - Compress archived files
   - S3/cloud storage for long-term retention

