# Testing Guide - Filesystem Discovery Implementation

## Quick Test

```bash
cd ~/workspace/plagis_aumentum

# Test the service layer
python3 test_filesystem_discovery.py
```

**Expected Output**:
```
Document ID: 10000000013787
  Expected Page Count: 1
  Actual Images Found: 1
  ✅ MATCH! Found all 1 pages

Document ID: 10000000013791
  Expected Page Count: 46
  Actual Images Found: 46
  ✅ MATCH! Found all 46 pages

Document ID: 10000000013800
  Expected Page Count: 2
  Actual Images Found: 2
  ✅ MATCH! Found all 2 pages
```

## Full API Test

### 1. Start the API Server
```bash
python3 aumentum_api.py
```

### 2. Test Document Metadata Endpoint
```bash
curl "http://localhost:8001/documents/by-document-number?document_number=PL11089" | jq
```

**Expected**: Should show `available_images: 46` for document ID 10000000013791

### 3. Test PDF Generation (Single Page)
```bash
curl "http://localhost:8001/documents/pdf-by-document-number?document_number=PL11089&document_id=10000000013787" \
  -o PL11089_1page.pdf
```

**Expected**: 1-page PDF

### 4. Test PDF Generation (46 Pages)
```bash
curl "http://localhost:8001/documents/pdf-by-document-number?document_number=PL11089&document_id=10000000013791" \
  -o PL11089_46pages.pdf
```

**Expected**: 46-page PDF (this is the main test!)

### 5. Verify PDF
```bash
# Check page count
pdfinfo PL11089_46pages.pdf | grep Pages

# Expected output:
# Pages: 46
```

## Troubleshooting

### Issue: "Filesystem discovery found 1 pages"
**Cause**: Reference file not found or directory doesn't exist
**Fix**: 
```bash
# Verify contentstore is mounted
ls -la /mnt/aumentum_contentstore/contentstore/2015/3/26/15/8/
```

### Issue: "MISMATCH! Expected 46, found 30"
**Cause**: Not enough files in timestamp range
**Fix**: This is expected for some documents. The system returns as many as it finds.

### Issue: "Permission denied"
**Cause**: No read access to contentstore
**Fix**:
```bash
sudo chmod -R +r /mnt/aumentum_contentstore/contentstore/
```

## Performance Test

Test with multiple documents:

```python
#!/usr/bin/env python3
from aumentum_browser_service import AumentumBrowserService, DEFAULT_DB_CONFIG, DEFAULT_CONTENTSTORE_BASE
import time

service = AumentumBrowserService(DEFAULT_DB_CONFIG, DEFAULT_CONTENTSTORE_BASE)

documents = ["PL11089", "BP703", "PL035"]  # Add your test documents

for doc_num in documents:
    start = time.time()
    result = service.resolve_store_urls_by_document_number(doc_num)
    elapsed = time.time() - start
    
    for doc in result:
        print(f"{doc_num} (ID {doc['document_id']}): {len(doc['images'])} pages in {elapsed:.3f}s")
```

**Expected Performance**: <100ms per document

## Integration Test

Test the complete flow from browser extension:

1. Open Aumentum Web Access in browser
2. Navigate to document PL11089
3. Click "View PDF" button in extension
4. Verify 46-page PDF opens correctly

## Regression Test

Ensure single-page documents still work:

```bash
# Test a single-page document
curl "http://localhost:8001/documents/by-document-number?document_number=SINGLE_PAGE_DOC" | jq
```

**Expected**: `available_images: 1` (no filesystem discovery triggered)

## Success Criteria

- ✅ Multi-page documents show correct `available_images` count
- ✅ PDF generation produces files with correct page count
- ✅ Single-page documents still work (no regression)
- ✅ Response time < 100ms for directory listing
- ✅ No errors in logs
- ✅ Cached PDFs work on second request

## Logs to Monitor

```bash
# Watch for filesystem discovery triggers
tail -f aumentum_api.log | grep "filesystem"

# Expected log lines:
# 🔍 Document ID 10000000013791: Expected 46 pages, found 1 in DB
#    Using filesystem-based discovery...
# ✅ Filesystem discovery found 46 pages
```

