# Testing PDF Sending from Chrome's Integrated PDF Viewer

This guide explains how to test the new PDF viewer integration that allows sending PDFs directly from Chrome's built-in PDF viewer to the Binect API.

## What Changed

The extension now detects PDFs in two ways:

1. **PDF Downloads** (Original) - Detects when you download a PDF file
2. **PDF Viewer** (New) - Detects when you're viewing a PDF in Chrome's integrated viewer

The popup will prioritize showing PDFs from the current tab viewer over previously downloaded PDFs.

## Testing the PDF Viewer Integration

### Setup

1. **Reload the Extension**
   ```
   chrome://extensions/ → Find BinectChrome → Click reload icon
   ```

2. **Check Permissions**
   - The extension now requires `activeTab` and `<all_urls>` permissions
   - Chrome will ask you to approve these new permissions
   - Click "Allow" when prompted

### Test Scenario 1: Open a PDF in a New Tab

**Steps:**
1. Find any PDF URL on the web (examples below)
2. Right-click the PDF link → "Open link in new tab"
3. Chrome will open the PDF in its integrated viewer
4. Click the BinectChrome extension icon
5. The popup should show the PDF details with "Send PDF to Binect" button

**Test PDF URLs:**
```
https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf
https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf
https://www.africau.edu/images/default/sample.pdf
```

**Expected Results:**
- PDF filename extracted from URL or tab title
- Domain shows the source domain
- Size shows "Size unknown" (normal for viewed PDFs)
- Timestamp shows "Just now"
- "Send PDF to Binect" button is active

### Test Scenario 2: Navigate Directly to a PDF URL

**Steps:**
1. Copy a PDF URL (use test URLs above)
2. Paste it into Chrome's address bar and press Enter
3. Chrome loads the PDF in the viewer
4. Click the BinectChrome extension icon
5. The popup should detect and show the PDF

**Expected Results:**
- Same as Scenario 1

### Test Scenario 3: View PDF from Google Drive / Cloud Storage

**Steps:**
1. Go to Google Drive, Dropbox, or any cloud storage
2. Click on a PDF file to view it
3. Wait for the PDF to load in the viewer
4. Click the BinectChrome extension icon

**Expected Results:**
- Extension detects the PDF
- Domain shows the cloud provider's domain
- File can be sent to Binect

**Note:** Some cloud providers use blob URLs or viewer applications that may not be directly detected. If this happens, download the PDF instead (it will be detected via download detection).

### Test Scenario 4: Send PDF to Binect API

**Steps:**
1. Open a PDF in Chrome's viewer (use Scenario 1 or 2)
2. Click the BinectChrome extension icon
3. If not signed in:
   - Enter your Binect credentials
   - Click "Sign In"
4. Once authenticated, the PDF should be shown
5. Click "Send PDF to Binect"

**Expected Results:**
- Status shows "Uploading..."
- After successful upload: "Success! Document ID: [id]"
- Tracking entry is created with the transfer details
- After 3 seconds, the popup clears

**Expected Errors (if testing with invalid credentials):**
- Authentication errors show in red
- Network errors are displayed
- Failed transfers are tracked with error messages

## API Integration Testing

### Test with Real Binect API

If you have Binect API credentials:

1. Sign in with valid credentials
2. Open a test PDF
3. Send it to Binect
4. Check the tracking info (click "?" button) to see the transfer log
5. Verify the document appears in your Binect account

### Test with Invalid Credentials

1. Sign in with invalid credentials
2. Should show "Invalid credentials" error
3. Extension should return to login screen

### Test with Network Errors

1. Disconnect from internet
2. Try to authenticate or send PDF
3. Should show "Network error" message

### Test Session Expiry

1. Sign in successfully
2. Wait for token to expire (or manipulate storage to simulate)
3. Try to send PDF
4. Should show "Session expired. Please sign in again."
5. Should automatically log out after 2 seconds

## Debugging Tips

### Check Console Logs

**Popup Console:**
1. Click extension icon to open popup
2. Right-click inside popup → "Inspect"
3. Check console for errors or debug messages

**Background Service Worker Console:**
1. Go to `chrome://extensions/`
2. Find BinectChrome
3. Click "service worker" link
4. Check console for detection logs

### Common Issues

**Issue: PDF not detected**
- **Solution 1:** The URL might not end with `.pdf` - this is normal for some cloud services
- **Solution 2:** Try refreshing the PDF tab
- **Solution 3:** Download the PDF instead (download detection should work)

**Issue: "Failed to fetch PDF: 403 Forbidden"**
- **Cause:** The PDF URL requires authentication/cookies that the extension can't access
- **Solution:** Download the PDF instead

**Issue: "Failed to fetch PDF: CORS error"**
- **Cause:** The server doesn't allow cross-origin requests
- **Solution:** Download the PDF instead

**Issue: Extension shows "No PDF detected"**
- **Check:** Is the current tab actually showing a PDF?
- **Check:** Does the URL end with `.pdf` or contain PDF indicators?
- **Try:** Download the PDF to test the download detection

## Verifying API Requests

### Using Chrome DevTools Network Tab

1. Open popup with DevTools open (right-click → Inspect)
2. Go to Network tab
3. Send a PDF
4. Look for requests to `https://api.binect.de/`
5. Check request details:
   - **POST /auth/login** - Authentication request
   - **POST /documents/upload** - PDF upload request

**Authentication Request:**
```json
POST https://api.binect.de/auth/login
Content-Type: application/json

{
  "username": "your-username",
  "password": "your-password"
}
```

**Upload Request:**
```
POST https://api.binect.de/documents/upload
Authorization: Bearer [token]
Content-Type: multipart/form-data

[PDF file data]
```

### Expected API Responses

**Successful Authentication:**
```json
{
  "token": "eyJhbGc...",
  "expiresAt": "2024-01-15T12:00:00Z"
}
```

**Successful Upload:**
```json
{
  "documentId": "doc_abc123",
  "status": "uploaded",
  "uploadedAt": "2024-01-14T12:00:00Z"
}
```

**Authentication Error (401):**
```json
{
  "error": "Invalid credentials"
}
```

**Upload Error (400):**
```json
{
  "error": "Invalid file format"
}
```

## Tracking Data

After sending PDFs, check the tracking data:

1. Click the "?" button in the extension popup
2. Opens tracking page
3. Shows list of all transfer attempts
4. Includes:
   - Timestamp
   - Source domain
   - PDF size
   - Result (success/failure)
   - Error message (if failed)

## Comparison: Download Detection vs. Viewer Detection

| Feature | Download Detection | PDF Viewer Detection |
|---------|-------------------|---------------------|
| **Trigger** | PDF file download completes | User opens PDF in browser |
| **File Size** | Known (from download) | Unknown (estimated after fetch) |
| **Reliability** | High | Depends on URL format |
| **Use Case** | Downloading PDFs from web | Viewing PDFs directly in browser |
| **Badge** | Shows "1" after download | No badge (on-demand) |

## Next Steps

Once you've verified the PDF viewer integration works:

1. Test with various PDF sources (Google Drive, Dropbox, direct links)
2. Verify all error cases are handled gracefully
3. Check that tracking data is accurate
4. Test the download detection still works alongside viewer detection
5. Consider edge cases:
   - Very large PDFs (10MB+)
   - PDFs with special characters in filename
   - PDFs from authenticated sources
   - PDFs from blob URLs

## Known Limitations

1. **Blob URLs**: Some web apps create temporary blob URLs that can't be re-fetched
   - **Workaround**: Download the PDF instead
2. **Authenticated PDFs**: PDFs behind login walls may not be accessible
   - **Workaround**: Download the PDF instead
3. **Embedded PDFs**: PDFs embedded in iframes may not be detected
   - **Workaround**: Open the PDF in a new tab or download it
4. **Size Unknown**: PDF size is not known until fetch, so tracking may show 0 initially
   - **Note**: Actual size is recorded after successful upload

## Support

If you encounter issues:
1. Check the console logs (popup and service worker)
2. Verify the PDF URL format
3. Try downloading the PDF as an alternative
4. Report issues to bernd.worsch@binect.de with:
   - PDF URL (if public)
   - Error message
   - Console logs
   - Tracking data export
