#!/bin/bash

set -e
set -x

# Create test directory structure
TEST_DIR="$HOME/test-archive"
mkdir -p "$TEST_DIR"

# Test Suite
run_tests() {
    echo "==> Running test suite"

    echo "==> Test 1: Basic archive sync"
    ./archive.sh
    
    echo "==> Test 2: Verify archive structure"
    test -d "$TEST_DIR/repos" || (echo "repos directory missing" && exit 1)
    test -d "$TEST_DIR/packages" || (echo "packages directory missing" && exit 1)
    
    echo "==> Test 3: Test archive-cleaner"
    ./archive-cleaner --repo "$TEST_DIR/repos/last" --archive "$TEST_DIR" --keep-years 1
    
    echo "==> Test 4: Test incremental sync"
    # Add new test package
    echo "new package" > /srv/archive/repos/2024/03/20/core/os/x86_64/test-2.0-1-x86_64.pkg.tar.zst
    ./archive.sh
    
    echo "==> Test 5: Test hardlinking"
    # Check if hardlinks are created
    find "$TEST_DIR" -type f -links +1 || (echo "No hardlinks found" && exit 1)
}

# Setup
echo "==> Setting up test environment"
cp test-archive.conf /tmp/archive.conf
export ARCHIVE_CONFIG=/tmp/archive.conf

# Run tests
run_tests

echo "==> All tests passed!" 