/ scripts / test-webdav-versioning-cli.sh
test-webdav-versioning-cli.sh
  1  #!/bin/bash
  2  # Test script for WebDAV versioning CLI functionality
  3  
  4  # Set up colors for output
  5  GREEN='\033[0;32m'
  6  RED='\033[0;31m'
  7  YELLOW='\033[1;33m'
  8  NC='\033[0m' # No Color
  9  
 10  echo -e "${YELLOW}Testing WebDAV Versioning CLI Functionality${NC}"
 11  echo "======================================"
 12  
 13  # Create a test directory
 14  TEST_DIR="./test-webdav-versioning"
 15  mkdir -p $TEST_DIR
 16  echo -e "${GREEN}Created test directory: $TEST_DIR${NC}"
 17  
 18  # Create a test file with initial content
 19  TEST_FILE="$TEST_DIR/test-file.txt"
 20  echo "Initial content - Version 1" > $TEST_FILE
 21  echo -e "${GREEN}Created test file: $TEST_FILE${NC}"
 22  
 23  # Build the CLI
 24  echo -e "${YELLOW}Building the CLI...${NC}"
 25  ./scripts/integrated-build.sh build
 26  if [ $? -ne 0 ]; then
 27      echo -e "${RED}Failed to build the CLI${NC}"
 28      exit 1
 29  fi
 30  echo -e "${GREEN}CLI built successfully${NC}"
 31  
 32  # Create a WebDAV configuration file
 33  CONFIG_FILE="$TEST_DIR/webdav-config.json"
 34  cat > $CONFIG_FILE << EOF
 35  {
 36    "provider": "s3",
 37    "endpoint": "https://example.com/webdav",
 38    "username": "user",
 39    "password": "password",
 40    "versioning_enabled": true,
 41    "diff_versioning_enabled": true,
 42    "auto_version": true,
 43    "max_versions": 10,
 44    "diff_threshold": 1024,
 45    "full_version_interval": 5,
 46    "version_directory": ".versions",
 47    "expiration_days": 30,
 48    "compression_enabled": true,
 49    "compression_type": "gzip",
 50    "compression_level": 6,
 51    "compression_min_size": 1024,
 52    "binary_diff_enabled": true,
 53    "binary_diff_algorithm": "auto",
 54    "binary_diff_min_size": 1024,
 55    "binary_diff_max_size": 104857600,
 56    "binary_diff_similarity_threshold": 0.3
 57  }
 58  EOF
 59  echo -e "${GREEN}Created WebDAV configuration file: $CONFIG_FILE${NC}"
 60  
 61  # Start the daemon
 62  echo -e "${YELLOW}Starting the daemon...${NC}"
 63  ./bin/keepsync --server --sync-dir $TEST_DIR --provider s3 --provider-config $CONFIG_FILE &
 64  DAEMON_PID=$!
 65  echo -e "${GREEN}Daemon started with PID: $DAEMON_PID${NC}"
 66  
 67  # Wait for the daemon to start
 68  echo -e "${YELLOW}Waiting for the daemon to start...${NC}"
 69  sleep 5
 70  
 71  # Initialize the CLI with the WebDAV provider
 72  echo -e "${YELLOW}Initializing the CLI with WebDAV provider...${NC}"
 73  ./bin/keepsync init --provider-config $CONFIG_FILE
 74  if [ $? -ne 0 ]; then
 75      echo -e "${RED}Failed to initialize the CLI${NC}"
 76      exit 1
 77  fi
 78  echo -e "${GREEN}CLI initialized successfully${NC}"
 79  
 80  # Test version list command
 81  echo -e "${YELLOW}Testing version list command...${NC}"
 82  ./bin/keepsync version list $TEST_FILE
 83  if [ $? -ne 0 ]; then
 84      echo -e "${RED}Failed to list versions${NC}"
 85      exit 1
 86  fi
 87  echo -e "${GREEN}Version list command executed successfully${NC}"
 88  
 89  # Modify the test file to create a new version
 90  echo "Modified content - Version 2" > $TEST_FILE
 91  echo -e "${GREEN}Modified test file: $TEST_FILE${NC}"
 92  
 93  # Sync the changes to create a new version
 94  echo -e "${YELLOW}Syncing changes to create a new version...${NC}"
 95  ./bin/keepsync sync $TEST_FILE
 96  if [ $? -ne 0 ]; then
 97      echo -e "${RED}Failed to sync changes${NC}"
 98      exit 1
 99  fi
100  echo -e "${GREEN}Changes synced successfully${NC}"
101  
102  # Test version list command again to see the new version
103  echo -e "${YELLOW}Testing version list command again...${NC}"
104  ./bin/keepsync version list $TEST_FILE
105  if [ $? -ne 0 ]; then
106      echo -e "${RED}Failed to list versions${NC}"
107      exit 1
108  fi
109  echo -e "${GREEN}Version list command executed successfully${NC}"
110  
111  # Get the first version
112  echo -e "${YELLOW}Getting the first version...${NC}"
113  ./bin/keepsync version get $TEST_FILE v1 $TEST_DIR/test-file-v1.txt
114  if [ $? -ne 0 ]; then
115      echo -e "${RED}Failed to get version${NC}"
116      exit 1
117  fi
118  echo -e "${GREEN}Version get command executed successfully${NC}"
119  
120  # Compare the versions
121  echo -e "${YELLOW}Comparing versions...${NC}"
122  ./bin/keepsync version diff $TEST_FILE v1 v2
123  if [ $? -ne 0 ]; then
124      echo -e "${RED}Failed to compare versions${NC}"
125      exit 1
126  fi
127  echo -e "${GREEN}Version diff command executed successfully${NC}"
128  
129  # Add a tag to the first version
130  echo -e "${YELLOW}Adding a tag to the first version...${NC}"
131  ./bin/keepsync version tag add $TEST_FILE v1 "initial"
132  if [ $? -ne 0 ]; then
133      echo -e "${RED}Failed to add tag${NC}"
134      exit 1
135  fi
136  echo -e "${GREEN}Version tag add command executed successfully${NC}"
137  
138  # Search for versions with the tag
139  echo -e "${YELLOW}Searching for versions with the tag...${NC}"
140  ./bin/keepsync version search by-tag $TEST_FILE "initial"
141  if [ $? -ne 0 ]; then
142      echo -e "${RED}Failed to search versions by tag${NC}"
143      exit 1
144  fi
145  echo -e "${GREEN}Version search by-tag command executed successfully${NC}"
146  
147  # Restore the first version
148  echo -e "${YELLOW}Restoring the first version...${NC}"
149  ./bin/keepsync version restore $TEST_FILE v1
150  if [ $? -ne 0 ]; then
151      echo -e "${RED}Failed to restore version${NC}"
152      exit 1
153  fi
154  echo -e "${GREEN}Version restore command executed successfully${NC}"
155  
156  # Verify the content of the restored file
157  CONTENT=$(cat $TEST_FILE)
158  if [ "$CONTENT" != "Initial content - Version 1" ]; then
159      echo -e "${RED}File content does not match the expected content${NC}"
160      echo -e "${RED}Expected: Initial content - Version 1${NC}"
161      echo -e "${RED}Actual: $CONTENT${NC}"
162      exit 1
163  fi
164  echo -e "${GREEN}File content matches the expected content${NC}"
165  
166  # Purge old versions
167  echo -e "${YELLOW}Purging old versions...${NC}"
168  ./bin/keepsync version purge $TEST_FILE 1
169  if [ $? -ne 0 ]; then
170      echo -e "${RED}Failed to purge versions${NC}"
171      exit 1
172  fi
173  echo -e "${GREEN}Version purge command executed successfully${NC}"
174  
175  # Stop the daemon
176  echo -e "${YELLOW}Stopping the daemon...${NC}"
177  kill $DAEMON_PID
178  echo -e "${GREEN}Daemon stopped${NC}"
179  
180  # Clean up
181  echo -e "${YELLOW}Cleaning up...${NC}"
182  rm -rf $TEST_DIR
183  echo -e "${GREEN}Cleaned up test directory${NC}"
184  
185  echo -e "${GREEN}All tests completed successfully!${NC}"