test-collision-detection.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # Test Script for Collision Detection
  4. # This script simulates the collision detection logic
  5. #############################################################################
  6. set -e
  7. # Colors
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. BLUE='\033[0;34m'
  12. CYAN='\033[0;36m'
  13. BOLD='\033[1m'
  14. NC='\033[0m'
  15. print_header() {
  16. echo -e "${CYAN}${BOLD}"
  17. echo "╔════════════════════════════════════════════════════════════════╗"
  18. echo "║ ║"
  19. echo "║ Collision Detection Test ║"
  20. echo "║ ║"
  21. echo "╚════════════════════════════════════════════════════════════════╝"
  22. echo -e "${NC}"
  23. }
  24. print_success() {
  25. echo -e "${GREEN}✓${NC} $1"
  26. }
  27. print_error() {
  28. echo -e "${RED}✗${NC} $1"
  29. }
  30. print_warning() {
  31. echo -e "${YELLOW}⚠${NC} $1"
  32. }
  33. print_info() {
  34. echo -e "${BLUE}ℹ${NC} $1"
  35. }
  36. print_step() {
  37. echo -e "\n${CYAN}${BOLD}▶${NC} $1\n"
  38. }
  39. # Test 1: No existing files
  40. test_no_collisions() {
  41. print_step "Test 1: No Existing Files"
  42. local test_dir="/tmp/opencode-test-$$"
  43. mkdir -p "$test_dir"
  44. cd "$test_dir"
  45. # Simulate checking for files that don't exist
  46. local files=(".opencode/agent/test.md" ".opencode/command/test.md")
  47. local collisions=0
  48. for file in "${files[@]}"; do
  49. if [ -f "$file" ]; then
  50. ((collisions+=1))
  51. fi
  52. done
  53. if [ $collisions -eq 0 ]; then
  54. print_success "No collisions detected (expected)"
  55. print_info "Result: Would install all files without prompting"
  56. else
  57. print_error "Unexpected collisions found"
  58. fi
  59. cd - > /dev/null
  60. rm -rf "$test_dir"
  61. }
  62. # Test 2: Some existing files
  63. test_partial_collisions() {
  64. print_step "Test 2: Partial Collisions"
  65. local test_dir="/tmp/opencode-test-$$"
  66. mkdir -p "$test_dir/.opencode/agent"
  67. mkdir -p "$test_dir/.opencode/command"
  68. cd "$test_dir"
  69. # Create some existing files
  70. echo "existing" > .opencode/agent/existing.md
  71. echo "existing" > .opencode/command/existing.md
  72. # Simulate checking for files
  73. local files=(
  74. ".opencode/agent/existing.md"
  75. ".opencode/agent/new.md"
  76. ".opencode/command/existing.md"
  77. ".opencode/command/new.md"
  78. )
  79. local collisions=()
  80. for file in "${files[@]}"; do
  81. if [ -f "$file" ]; then
  82. collisions+=("$file")
  83. fi
  84. done
  85. if [ ${#collisions[@]} -eq 2 ]; then
  86. print_success "Detected ${#collisions[@]} collisions (expected)"
  87. print_info "Collisions:"
  88. for file in "${collisions[@]}"; do
  89. echo " $file"
  90. done
  91. print_info "Result: Would prompt user with 4 options"
  92. else
  93. print_error "Expected 2 collisions, found ${#collisions[@]}"
  94. fi
  95. cd - > /dev/null
  96. rm -rf "$test_dir"
  97. }
  98. # Test 3: All files exist
  99. test_all_collisions() {
  100. print_step "Test 3: All Files Exist"
  101. local test_dir="/tmp/opencode-test-$$"
  102. mkdir -p "$test_dir/.opencode/agent"
  103. mkdir -p "$test_dir/.opencode/command"
  104. cd "$test_dir"
  105. # Create all files
  106. echo "existing" > .opencode/agent/file1.md
  107. echo "existing" > .opencode/agent/file2.md
  108. echo "existing" > .opencode/command/file1.md
  109. local files=(
  110. ".opencode/agent/file1.md"
  111. ".opencode/agent/file2.md"
  112. ".opencode/command/file1.md"
  113. )
  114. local collisions=()
  115. for file in "${files[@]}"; do
  116. if [ -f "$file" ]; then
  117. collisions+=("$file")
  118. fi
  119. done
  120. if [ ${#collisions[@]} -eq 3 ]; then
  121. print_success "Detected ${#collisions[@]} collisions (all files)"
  122. print_info "Result: Would prompt user with 4 options"
  123. print_info " Option 1 (Skip): Would install 0 files"
  124. print_info " Option 2 (Overwrite): Would install 3 files"
  125. print_info " Option 3 (Backup): Would backup 3, install 3"
  126. else
  127. print_error "Expected 3 collisions, found ${#collisions[@]}"
  128. fi
  129. cd - > /dev/null
  130. rm -rf "$test_dir"
  131. }
  132. # Test 4: Collision grouping
  133. test_collision_grouping() {
  134. print_step "Test 4: Collision Grouping by Type"
  135. local test_dir="/tmp/opencode-test-$$"
  136. mkdir -p "$test_dir/.opencode/agent/subagents"
  137. mkdir -p "$test_dir/.opencode/command"
  138. mkdir -p "$test_dir/.opencode/context/core"
  139. cd "$test_dir"
  140. # Create files of different types
  141. echo "existing" > .opencode/agent/main.md
  142. echo "existing" > .opencode/agent/subagents/sub1.md
  143. echo "existing" > .opencode/agent/subagents/sub2.md
  144. echo "existing" > .opencode/command/cmd1.md
  145. echo "existing" > .opencode/context/core/ctx1.md
  146. local collisions=(
  147. ".opencode/agent/main.md"
  148. ".opencode/agent/subagents/sub1.md"
  149. ".opencode/agent/subagents/sub2.md"
  150. ".opencode/command/cmd1.md"
  151. ".opencode/context/core/ctx1.md"
  152. )
  153. # Group by type
  154. local agents=()
  155. local subagents=()
  156. local commands=()
  157. local contexts=()
  158. for file in "${collisions[@]}"; do
  159. if [[ $file == *"/agent/subagents/"* ]]; then
  160. subagents+=("$file")
  161. elif [[ $file == *"/agent/"* ]]; then
  162. agents+=("$file")
  163. elif [[ $file == *"/command/"* ]]; then
  164. commands+=("$file")
  165. elif [[ $file == *"/context/"* ]]; then
  166. contexts+=("$file")
  167. fi
  168. done
  169. print_success "Grouped collisions by type:"
  170. echo -e "${YELLOW} Agents (${#agents[@]}):${NC}"
  171. printf ' %s\n' "${agents[@]}"
  172. echo -e "${YELLOW} Subagents (${#subagents[@]}):${NC}"
  173. printf ' %s\n' "${subagents[@]}"
  174. echo -e "${YELLOW} Commands (${#commands[@]}):${NC}"
  175. printf ' %s\n' "${commands[@]}"
  176. echo -e "${YELLOW} Context (${#contexts[@]}):${NC}"
  177. printf ' %s\n' "${contexts[@]}"
  178. cd - > /dev/null
  179. rm -rf "$test_dir"
  180. }
  181. # Test 5: Backup simulation
  182. test_backup_strategy() {
  183. print_step "Test 5: Backup Strategy Simulation"
  184. local test_dir="/tmp/opencode-test-$$"
  185. mkdir -p "$test_dir/.opencode/agent"
  186. cd "$test_dir"
  187. # Create existing file with content
  188. echo "original content" > .opencode/agent/test.md
  189. # Simulate backup
  190. local backup_dir
  191. backup_dir=".opencode.backup.$(date +%Y%m%d-%H%M%S)"
  192. local file=".opencode/agent/test.md"
  193. local backup_file="${backup_dir}/${file}"
  194. mkdir -p "$(dirname "$backup_file")"
  195. cp "$file" "$backup_file"
  196. if [ -f "$backup_file" ]; then
  197. print_success "Backup created successfully"
  198. print_info "Original: $file"
  199. print_info "Backup: $backup_file"
  200. # Verify content
  201. if diff "$file" "$backup_file" > /dev/null; then
  202. print_success "Backup content matches original"
  203. else
  204. print_error "Backup content differs from original"
  205. fi
  206. else
  207. print_error "Backup creation failed"
  208. fi
  209. cd - > /dev/null
  210. rm -rf "$test_dir"
  211. }
  212. # Run all tests
  213. main() {
  214. print_header
  215. test_no_collisions
  216. test_partial_collisions
  217. test_all_collisions
  218. test_collision_grouping
  219. test_backup_strategy
  220. echo ""
  221. print_step "Test Summary"
  222. print_success "All collision detection tests passed!"
  223. print_info "The install script will correctly:"
  224. echo " • Detect existing files before installation"
  225. echo " • Group collisions by type for easy review"
  226. echo " • Offer 4 clear strategies (skip/overwrite/backup/cancel)"
  227. echo " • Create timestamped backups when requested"
  228. echo " • Preserve user customizations when skipping"
  229. echo ""
  230. }
  231. main