|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Comprehensive test script for Copybuffer |
| 4 | +# This script tests all major features to ensure they work correctly |
| 5 | + |
| 6 | +# Don't exit on error - we want to count failures |
| 7 | +# set -e # Exit on error |
| 8 | + |
| 9 | +echo "======================================" |
| 10 | +echo " Copybuffer Comprehensive Test" |
| 11 | +echo "======================================" |
| 12 | +echo "" |
| 13 | + |
| 14 | +# Colors for output |
| 15 | +GREEN='\033[0;32m' |
| 16 | +RED='\033[0;31m' |
| 17 | +YELLOW='\033[1;33m' |
| 18 | +NC='\033[0m' # No Color |
| 19 | + |
| 20 | +# Test counter |
| 21 | +TESTS_PASSED=0 |
| 22 | +TESTS_FAILED=0 |
| 23 | + |
| 24 | +# Function to run a test |
| 25 | +run_test() { |
| 26 | + local test_name="$1" |
| 27 | + local command="$2" |
| 28 | + |
| 29 | + echo -n "Testing: $test_name... " |
| 30 | + |
| 31 | + if eval "$command" > /tmp/test_output.log 2>&1; then |
| 32 | + echo -e "${GREEN}PASSED${NC}" |
| 33 | + ((TESTS_PASSED++)) |
| 34 | + else |
| 35 | + echo -e "${RED}FAILED${NC}" |
| 36 | + echo " Error output:" |
| 37 | + cat /tmp/test_output.log | head -5 |
| 38 | + ((TESTS_FAILED++)) |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Function to verify output contains text |
| 43 | +verify_output() { |
| 44 | + local test_name="$1" |
| 45 | + local command="$2" |
| 46 | + local expected="$3" |
| 47 | + |
| 48 | + echo -n "Testing: $test_name... " |
| 49 | + |
| 50 | + if eval "$command" 2>&1 | grep -q "$expected"; then |
| 51 | + echo -e "${GREEN}PASSED${NC}" |
| 52 | + ((TESTS_PASSED++)) |
| 53 | + else |
| 54 | + echo -e "${RED}FAILED${NC}" |
| 55 | + echo " Expected to find: $expected" |
| 56 | + ((TESTS_FAILED++)) |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +# Ensure clean state |
| 61 | +rm -rf ~/.copybuffer |
| 62 | +rm -f /tmp/test-*.json |
| 63 | + |
| 64 | +echo "1. Build Tests" |
| 65 | +echo "--------------" |
| 66 | +run_test "TypeScript compilation" "npm run build" |
| 67 | +run_test "ESLint validation" "npm run lint" |
| 68 | +echo "" |
| 69 | + |
| 70 | +echo "2. CLI Tests" |
| 71 | +echo "------------" |
| 72 | +run_test "CLI help command" "node dist/cli.js --help" |
| 73 | +run_test "CLI version command" "node dist/cli.js --version" |
| 74 | +echo "" |
| 75 | + |
| 76 | +echo "3. Configuration Tests" |
| 77 | +echo "---------------------" |
| 78 | +verify_output "Default config creation" "node dist/cli.js config" "dataDir" |
| 79 | +verify_output "Config contains maxHistorySize" "node dist/cli.js config" "maxHistorySize" |
| 80 | +verify_output "Config contains hotkeys" "node dist/cli.js config" "hotkeys" |
| 81 | +run_test "Set config value" "node dist/cli.js config-set maxHistorySize 500" |
| 82 | +verify_output "Verify config update" "node dist/cli.js config" '"maxHistorySize": "500"' |
| 83 | +echo "" |
| 84 | + |
| 85 | +echo "4. Storage Tests" |
| 86 | +echo "---------------" |
| 87 | + |
| 88 | +# Create test data |
| 89 | +cat > /tmp/test-import.json << 'EOF' |
| 90 | +[ |
| 91 | + { |
| 92 | + "id": "test-001", |
| 93 | + "content": "Test entry 1", |
| 94 | + "timestamp": 1698601200000, |
| 95 | + "type": "text" |
| 96 | + }, |
| 97 | + { |
| 98 | + "id": "test-002", |
| 99 | + "content": "Test entry 2 with keyword", |
| 100 | + "timestamp": 1698601260000, |
| 101 | + "type": "text" |
| 102 | + }, |
| 103 | + { |
| 104 | + "id": "test-003", |
| 105 | + "content": "Another test entry", |
| 106 | + "timestamp": 1698601320000, |
| 107 | + "type": "text", |
| 108 | + "tags": ["test", "example"] |
| 109 | + } |
| 110 | +] |
| 111 | +EOF |
| 112 | + |
| 113 | +run_test "Import history" "node dist/cli.js import /tmp/test-import.json" |
| 114 | +verify_output "List shows entries" "node dist/cli.js list" "Test entry 1" |
| 115 | +run_test "Export history" "node dist/cli.js export /tmp/test-export.json" |
| 116 | +run_test "Exported file exists" "test -f /tmp/test-export.json" |
| 117 | +echo "" |
| 118 | + |
| 119 | +echo "5. Search Tests" |
| 120 | +echo "--------------" |
| 121 | +verify_output "Search finds entry" "node dist/cli.js search 'keyword'" "Test entry 2" |
| 122 | +verify_output "Search with limit" "node dist/cli.js search 'test' --limit 2" "Test entry" |
| 123 | +verify_output "Search no results" "node dist/cli.js search 'nonexistent'" "No results found" |
| 124 | +echo "" |
| 125 | + |
| 126 | +echo "6. Delete Tests" |
| 127 | +echo "--------------" |
| 128 | +run_test "Delete entry" "node dist/cli.js delete test-001" |
| 129 | +verify_output "Entry deleted" "node dist/cli.js search 'Test entry 1'" "No results found" |
| 130 | +echo "" |
| 131 | + |
| 132 | +echo "7. List Tests" |
| 133 | +echo "------------" |
| 134 | +verify_output "List default limit" "node dist/cli.js list" "Showing" |
| 135 | +verify_output "List with custom limit" "node dist/cli.js list --limit 5" "Showing" |
| 136 | +echo "" |
| 137 | + |
| 138 | +echo "8. Clear Tests" |
| 139 | +echo "-------------" |
| 140 | +run_test "Clear history" "node dist/cli.js clear --yes" |
| 141 | +verify_output "History cleared" "node dist/cli.js list" "No clipboard history found" |
| 142 | +echo "" |
| 143 | + |
| 144 | +echo "9. File Structure Tests" |
| 145 | +echo "----------------------" |
| 146 | +run_test "Config directory exists" "test -d ~/.copybuffer" |
| 147 | +run_test "Config file exists" "test -f ~/.copybuffer/config.json" |
| 148 | +run_test "History file exists" "test -f ~/.copybuffer/history.json" |
| 149 | +echo "" |
| 150 | + |
| 151 | +echo "10. Module Tests" |
| 152 | +echo "---------------" |
| 153 | + |
| 154 | +# Create a simple test script to verify imports |
| 155 | +cat > /tmp/test-imports.js << 'EOF' |
| 156 | +const path = require('path'); |
| 157 | +const projectDir = '/home/runner/work/Copybuffer/Copybuffer'; |
| 158 | +const { storageManager, searchManager, configManager } = require(path.join(projectDir, 'dist/exports')); |
| 159 | +
|
| 160 | +// Test that modules export correctly |
| 161 | +console.log('storageManager:', typeof storageManager); |
| 162 | +console.log('searchManager:', typeof searchManager); |
| 163 | +console.log('configManager:', typeof configManager); |
| 164 | +
|
| 165 | +// Test basic functionality |
| 166 | +const config = configManager.getConfig(); |
| 167 | +console.log('Config loaded:', config.dataDir ? 'yes' : 'no'); |
| 168 | +
|
| 169 | +const history = storageManager.loadHistory(); |
| 170 | +console.log('History loaded:', Array.isArray(history) ? 'yes' : 'no'); |
| 171 | +
|
| 172 | +const recent = searchManager.getRecent(10); |
| 173 | +console.log('Recent retrieved:', Array.isArray(recent) ? 'yes' : 'no'); |
| 174 | +EOF |
| 175 | + |
| 176 | +run_test "Module imports work" "node /tmp/test-imports.js" |
| 177 | +echo "" |
| 178 | + |
| 179 | +echo "======================================" |
| 180 | +echo " Test Summary" |
| 181 | +echo "======================================" |
| 182 | +echo "" |
| 183 | +echo -e "Tests Passed: ${GREEN}${TESTS_PASSED}${NC}" |
| 184 | +echo -e "Tests Failed: ${RED}${TESTS_FAILED}${NC}" |
| 185 | +echo "" |
| 186 | + |
| 187 | +if [ $TESTS_FAILED -eq 0 ]; then |
| 188 | + echo -e "${GREEN}All tests passed!${NC}" |
| 189 | + exit 0 |
| 190 | +else |
| 191 | + echo -e "${RED}Some tests failed!${NC}" |
| 192 | + exit 1 |
| 193 | +fi |
0 commit comments