ChatGPT-Next-Web/test_indexeddb.sh
2025-07-07 21:37:16 +07:00

177 lines
6.2 KiB
Bash

#!/bin/bash
# IndexedDB Storage Testing Script
# This script provides comprehensive testing for the IndexedDB storage fixes
echo "🗄️ IndexedDB Storage Testing Script"
echo "===================================="
echo ""
# Function to print section headers
print_section() {
echo ""
echo "📋 $1"
echo "----------------------------------------"
}
# Function to run a test and capture results
run_test() {
local test_name="$1"
local test_command="$2"
echo "🧪 Testing: $test_name"
echo "Command: $test_command"
# Execute the test (this would typically be run in browser console)
echo " ⚠️ Run this in browser console: $test_command"
echo ""
}
print_section "Test Environment Setup"
echo "1. Start the development server:"
echo " npm run dev"
echo ""
echo "2. Open browser and go to: http://localhost:3000"
echo ""
echo "3. Open browser developer tools (F12)"
echo ""
echo "4. Go to Console tab"
echo ""
print_section "IndexedDB Health Check Tests"
run_test "Check Storage Health" "await debugStorage.checkStorageHealth()"
run_test "List All Store Data" "await debugStorage.listAllStoreData()"
run_test "Validate Store Integrity" "await debugStorage.validateStoreIntegrity()"
print_section "Chat Store Specific Tests"
echo "🧪 Chat Store Tests (run in console):"
echo ""
echo "// Test 1: Check if chat store exists and is properly structured"
echo "const chatData = await debugStorage.listAllStoreData();"
echo "console.log('Chat Store:', chatData['chat-next-web-store']);"
echo ""
echo "// Test 2: Create a new chat session"
echo "const { useChatStore } = await import('/app/store/chat');"
echo "const chatStore = useChatStore.getState();"
echo "console.log('Current sessions:', chatStore.sessions.length);"
echo "console.log('Current session index:', chatStore.currentSessionIndex);"
echo ""
echo "// Test 3: Add a message and verify persistence"
echo "chatStore.onUserInput('Test message for IndexedDB');"
echo "await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for persistence"
echo "await debugStorage.listAllStoreData(); // Check if saved"
echo ""
print_section "Storage Migration Tests"
run_test "Migrate Data" "await debugStorage.migrateData()"
run_test "Create Backup" "await debugStorage.backupStores()"
print_section "Error Handling Tests"
echo "🧪 Error Handling Tests (run in console):"
echo ""
echo "// Test 1: Simulate IndexedDB failure"
echo "// (This requires manually disabling IndexedDB in browser settings)"
echo ""
echo "// Test 2: Test with corrupted data"
echo "localStorage.setItem('test-corrupt', '{invalid json}');"
echo "const testStorage = await import('/app/utils/indexedDB-storage');"
echo "try {"
echo " await testStorage.indexedDBStorage.getItem('test-corrupt');"
echo "} catch (error) {"
echo " console.log('Handled corrupted data:', error);"
echo "}"
echo ""
print_section "Store Key Consistency Tests"
echo "🧪 Store Key Tests (run in console):"
echo ""
echo "// Test 1: Verify all store keys are accessible"
echo "const { StoreKey } = await import('/app/constant');"
echo "console.log('Available Store Keys:', Object.values(StoreKey));"
echo ""
echo "// Test 2: Check each store exists"
echo "for (const key of Object.values(StoreKey)) {"
echo " const data = await debugStorage.indexedDBStorage.getItem(key);"
echo " console.log(\`Store \${key}: \${data ? 'EXISTS' : 'MISSING'}\`);"
echo "}"
echo ""
print_section "Performance Tests"
echo "🧪 Performance Tests (run in console):"
echo ""
echo "// Test 1: Large data storage performance"
echo "const largeData = { state: { messages: new Array(1000).fill({id: 'test', content: 'test message', date: new Date().toISOString()}), _hasHydrated: true } };"
echo "console.time('Large data storage');"
echo "await debugStorage.indexedDBStorage.setItem('performance-test', JSON.stringify(largeData));"
echo "console.timeEnd('Large data storage');"
echo ""
echo "// Test 2: Retrieval performance"
echo "console.time('Large data retrieval');"
echo "const retrieved = await debugStorage.indexedDBStorage.getItem('performance-test');"
echo "console.timeEnd('Large data retrieval');"
echo "console.log('Retrieved data size:', retrieved?.length || 0, 'characters');"
echo ""
print_section "Cleanup Tests"
run_test "Clear Test Data" "await debugStorage.indexedDBStorage.removeItem('performance-test')"
run_test "Clear All Stores (CAREFUL!)" "await debugStorage.clearAllStores()"
print_section "Manual Testing Checklist"
echo "✅ Manual Tests to Perform:"
echo ""
echo "1. Chat Functionality:"
echo " - Create a new chat session"
echo " - Send multiple messages"
echo " - Refresh the page"
echo " - Verify messages persist"
echo ""
echo "2. Settings Persistence:"
echo " - Change app settings"
echo " - Refresh the page"
echo " - Verify settings persist"
echo ""
echo "3. Cross-tab Sync:"
echo " - Open app in multiple tabs"
echo " - Make changes in one tab"
echo " - Verify changes appear in other tabs"
echo ""
echo "4. Storage Fallback:"
echo " - Disable IndexedDB in browser settings"
echo " - Refresh the page"
echo " - Verify app still works with localStorage"
echo ""
print_section "Expected Results"
echo "✅ Success Indicators:"
echo " - No console errors related to storage"
echo " - Chat messages persist across page refreshes"
echo " - IndexedDB health check returns { indexedDB: true, localStorage: true }"
echo " - Store integrity validation returns empty issues array"
echo " - All store keys have valid data structure"
echo ""
echo "❌ Failure Indicators:"
echo " - 'skip setItem' warnings in console"
echo " - Messages lost after page refresh"
echo " - Health check shows IndexedDB: false"
echo " - Store integrity issues found"
echo " - JSON parse errors in console"
echo ""
print_section "Troubleshooting"
echo "🔧 Common Issues & Solutions:"
echo ""
echo "Issue: 'skip setItem' warnings"
echo "Solution: Fixed with improved hydration logic"
echo ""
echo "Issue: Data not persisting"
echo "Solution: Check _hasHydrated flag and storage availability"
echo ""
echo "Issue: IndexedDB unavailable"
echo "Solution: App should fallback to localStorage automatically"
echo ""
echo "Issue: Corrupted data"
echo "Solution: Use debugStorage.clearStore(StoreKey.Chat) to reset"
echo ""
echo "🎯 Test completed! Review results in browser console."