# Persistent Connection Test Plan

## Overview
This test plan verifies that the Chrome extension implements persistent connection management as requested:
- Connections remain active after manual connect until explicit disconnect
- Connections persist across popup open/close cycles
- Connections survive browser events (tab switches, window focus changes, idle periods)
- No automatic disconnection after tool operations

## Prerequisites
1. **Remote Server Running**: Start the remote server on `ws://localhost:3001/chrome`
   ```bash
   cd app/remote-server
   npm run dev
   ```

2. **Extension Loaded**: Load the built extension from `app/chrome-extension/.output/chrome-mv3`

## Test Cases

### Test 1: Basic Persistent Connection
**Objective**: Verify connection persists after popup close

**Steps**:
1. Open Chrome extension popup
2. Click "Connect" button
3. Verify connection status shows "Connected (Persistent)" with green checkmark
4. Note the persistent connection indicator: "🔗 Persistent: Active"
5. Close the popup window
6. Wait 30 seconds
7. Reopen the popup

**Expected Result**: 
- Connection status still shows "Connected (Persistent)"
- Connection time continues counting from original connection
- No reconnection attempts logged

### Test 2: Connection Persistence Across Browser Events
**Objective**: Verify connection survives browser lifecycle events

**Steps**:
1. Establish connection (Test 1)
2. Switch between multiple tabs
3. Change window focus to different applications
4. Minimize/restore browser window
5. Open new browser windows
6. Check popup status after each event

**Expected Result**: 
- Connection remains active throughout all events
- No disconnection or reconnection attempts
- Status consistently shows "Connected (Persistent)"

### Test 3: Tool Execution Without Disconnection
**Objective**: Verify connection persists during and after tool operations

**Steps**:
1. Establish connection
2. Use Cherry Studio or another MCP client to send tool requests:
   - `chrome_navigate` to navigate to a website
   - `chrome_screenshot` to take screenshots
   - `chrome_extract_content` to extract page content
3. Execute multiple tool operations in sequence
4. Check connection status after each operation

**Expected Result**:
- All tool operations complete successfully
- Connection remains active after each operation
- No automatic disconnection after tool completion

### Test 4: Extended Idle Period Test
**Objective**: Verify connection survives long idle periods

**Steps**:
1. Establish connection
2. Leave browser idle for 30 minutes
3. Do not interact with the extension or browser
4. After 30 minutes, check popup status
5. Test tool functionality by sending a simple request

**Expected Result**:
- Connection remains active after idle period
- Tool operations work immediately without reconnection
- Connection time shows full duration including idle time

### Test 5: Manual Disconnect Only
**Objective**: Verify connection only terminates on explicit disconnect

**Steps**:
1. Establish connection
2. Perform various activities (tabs, tools, idle time)
3. Click "Disconnect" button in popup
4. Verify disconnection
5. Close and reopen popup

**Expected Result**:
- Connection terminates only when "Disconnect" is clicked
- After disconnect, status shows "Disconnected - Click Connect for persistent connection"
- Popup reopening shows disconnected state
- No automatic reconnection attempts

### Test 6: Browser Restart Connection Restoration
**Objective**: Verify connection state restoration after browser restart

**Steps**:
1. Establish connection
2. Close entire browser (all windows)
3. Restart browser
4. Open extension popup immediately

**Expected Result**:
- Extension attempts to restore previous connection
- If server is still running, connection is re-established automatically
- If successful, shows "Connected (Persistent)" status

### Test 7: Server Reconnection Behavior
**Objective**: Verify robust reconnection when server becomes unavailable

**Steps**:
1. Establish connection
2. Stop the remote server
3. Wait for connection loss detection
4. Restart the remote server
5. Monitor reconnection attempts

**Expected Result**:
- Extension detects connection loss
- Automatic reconnection attempts begin
- Connection is restored when server comes back online
- Persistent connection behavior resumes

## Verification Points

### UI Indicators
- ✅ Status shows "Connected (Persistent)" when connected
- ✅ Persistent badge shows "🔗 Persistent: Active"
- ✅ Info text: "🔗 Connection will persist until manually disconnected"
- ✅ Connection time continues counting accurately
- ✅ Disconnect button changes to "Disconnect" when connected

### Console Logs
Monitor browser console for these log messages:
- `Background: Remote server client initialized (not connected)`
- `Background: Browser event listeners initialized for connection persistence`
- `Background: Connection health check started (5-minute intervals)`
- `RemoteServerClient: Connection state saved`
- `Background: Tab switched to X, connection maintained`
- `Background: Window focus changed to X, connection maintained`

### Connection State Persistence
- Connection state is saved to chrome.storage.local
- State includes: wasConnected, connectionTime, serverUrl, lastSaveTime
- State is restored on extension startup if recent (within 24 hours)

## Success Criteria
All test cases pass with:
1. ✅ Connections persist until manual disconnect
2. ✅ No automatic disconnection after tool operations
3. ✅ Connections survive all browser lifecycle events
4. ✅ UI clearly indicates persistent connection status
5. ✅ Robust reconnection when server connectivity is restored
6. ✅ Connection state restoration after browser restart

## Troubleshooting
If tests fail, check:
1. Remote server is running on correct port (3001)
2. Extension has proper permissions
3. Browser console for error messages
4. Chrome storage for saved connection state
5. Network connectivity between extension and server
