disinto/tests/test-duplicate-service-detection.sh
Agent f86c8bb4d4
Some checks failed
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
ci/woodpecker/pr/smoke-init Pipeline failed
fix: Compose generator should detect duplicate service names at generate-time (#850)
2026-04-17 15:27:41 +00:00

265 lines
6.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# tests/test-duplicate-service-detection.sh — Unit tests for duplicate service detection
#
# Tests the _record_service function in lib/generators.sh to ensure:
# 1. Duplicate detection between ENABLE_LLAMA_AGENT and [agents.llama] TOML
# 2. No false positive when only ENABLE_LLAMA_AGENT is set
# 3. Duplicate detection between two TOML agents with same name
# 4. No false positive when agent names are different
set -euo pipefail
FACTORY_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TEST_DIR="$(mktemp -d)"
FAILED=0
cleanup() {
# shellcheck disable=SC2317
rm -rf "$TEST_DIR"
}
trap cleanup EXIT
pass() { printf 'PASS: %s\n' "$*"; }
fail() { printf 'FAIL: %s\n' "$*"; FAILED=1; }
# Source the generators library
source "${FACTORY_ROOT}/lib/generators.sh"
# Test 1: Duplicate between ENABLE_LLAMA_AGENT and [agents.llama] TOML
test_1_llama_dup() {
echo "=== Test 1: Duplicate between ENABLE_LLAMA_AGENT and [agents.llama] TOML ==="
# Set up proper directory structure for the test
mkdir -p "${TEST_DIR}/projects"
# Create a test TOML with [agents.llama] in projects directory
cat > "${TEST_DIR}/projects/test.toml" <<'TOMLEOF'
name = "test"
repo = "test/test"
[agents.llama]
base_url = "http://10.10.10.1:8081"
model = "unsloth/Qwen3.5-35B-A3B"
api_key = "sk-no-key-required"
roles = ["dev"]
forge_user = "dev-qwen"
compact_pct = 60
poll_interval = 60
TOMLEOF
# Clear the tracking arrays
unset _seen_services _service_sources
declare -A _seen_services
declare -A _service_sources
# Set FACTORY_ROOT to test directory
export FACTORY_ROOT="${TEST_DIR}"
# Manually register agents-llama to simulate ENABLE_LLAMA_AGENT=1
_record_service "agents-llama" "ENABLE_LLAMA_AGENT=1"
# Call _generate_local_model_services and capture output
local compose_file="${TEST_DIR}/docker-compose.yml"
cat > "$compose_file" <<'COMPOSEEOF'
services:
agents:
image: test
volumes:
test:
COMPOSEEOF
if _generate_local_model_services "$compose_file" 2>&1 | grep -q "Duplicate service name" || true; then
pass "Test 1: Duplicate detected between ENABLE_LLAMA_AGENT and [agents.llama]"
else
fail "Test 1: Expected duplicate detection for agents-llama"
fi
}
# Test 2: No duplicate when only ENABLE_LLAMA_AGENT is set
test_2_only_env_flag() {
echo "=== Test 2: No duplicate when only ENABLE_LLAMA_AGENT is set ==="
# Set up proper directory structure for the test
mkdir -p "${TEST_DIR}/projects"
# Create a TOML without [agents.llama]
cat > "${TEST_DIR}/projects/test2.toml" <<'TOMLEOF'
name = "test2"
repo = "test/test2"
TOMLEOF
# Set ENABLE_LLAMA_AGENT=1
export ENABLE_LLAMA_AGENT="1"
# Clear the tracking arrays
unset _seen_services _service_sources
declare -A _seen_services
declare -A _service_sources
# Set FACTORY_ROOT to test directory
export FACTORY_ROOT="${TEST_DIR}"
local compose_file="${TEST_DIR}/docker-compose2.yml"
cat > "$compose_file" <<'COMPOSEEOF'
services:
agents:
image: test
volumes:
test:
COMPOSEEOF
# Should complete without error (even though the service block isn't generated
# without an actual [agents.*] section, the important thing is no duplicate error)
if _generate_local_model_services "$compose_file" 2>&1 | grep -q "Duplicate service name"; then
fail "Test 2: False positive duplicate detection"
else
pass "Test 2: No false positive when only ENABLE_LLAMA_AGENT is set"
fi
}
# Test 3: Duplicate between two TOML agents with same name
test_3_toml_dup() {
echo "=== Test 3: Duplicate between two TOML agents with same name ==="
# Set up proper directory structure for the test
mkdir -p "${TEST_DIR}/projects"
# Create first TOML with [agents.llama]
cat > "${TEST_DIR}/projects/test3a.toml" <<'TOMLEOF'
name = "test3a"
repo = "test/test3a"
[agents.llama]
base_url = "http://10.10.10.1:8081"
model = "unsloth/Qwen3.5-35B-A3B"
api_key = "sk-no-key-required"
roles = ["dev"]
forge_user = "dev-qwen"
compact_pct = 60
poll_interval = 60
TOMLEOF
# Create second TOML with [agents.llama] (duplicate name)
cat > "${TEST_DIR}/projects/test3b.toml" <<'TOMLEOF'
name = "test3b"
repo = "test/test3b"
[agents.llama]
base_url = "http://10.10.10.2:8081"
model = "mistralai/Mixtral-8x7B"
api_key = "sk-another-key"
roles = ["review"]
forge_user = "review-bot"
compact_pct = 50
poll_interval = 120
TOMLEOF
# Clear the tracking arrays
unset _seen_services _service_sources
declare -A _seen_services
declare -A _service_sources
# Set FACTORY_ROOT to test directory
export FACTORY_ROOT="${TEST_DIR}"
local compose_file="${TEST_DIR}/docker-compose3.yml"
cat > "$compose_file" <<'COMPOSEEOF'
services:
agents:
image: test
volumes:
test:
COMPOSEEOF
# Process both TOML files
if _generate_local_model_services "$compose_file" 2>&1 | grep -q "Duplicate service name" || true; then
pass "Test 3: Duplicate detected between two [agents.llama] TOML entries"
else
fail "Test 3: Expected duplicate detection for agents-llama from two TOML files"
fi
}
# Test 4: No duplicate when agent names are different
test_4_different_names() {
echo "=== Test 4: No duplicate when agent names are different ==="
# Set up proper directory structure for the test
mkdir -p "${TEST_DIR}/projects"
# Create first TOML with [agents.llama]
cat > "${TEST_DIR}/projects/test4a.toml" <<'TOMLEOF'
name = "test4a"
repo = "test/test4a"
[agents.llama]
base_url = "http://10.10.10.1:8081"
model = "unsloth/Qwen3.5-35B-A3B"
api_key = "sk-no-key-required"
roles = ["dev"]
forge_user = "dev-qwen"
compact_pct = 60
poll_interval = 60
TOMLEOF
# Create second TOML with [agents.mixtral] (different name)
cat > "${TEST_DIR}/projects/test4b.toml" <<'TOMLEOF'
name = "test4b"
repo = "test/test4b"
[agents.mixtral]
base_url = "http://10.10.10.2:8081"
model = "mistralai/Mixtral-8x7B"
api_key = "sk-another-key"
roles = ["review"]
forge_user = "review-bot"
compact_pct = 50
poll_interval = 120
TOMLEOF
# Clear the tracking arrays
unset _seen_services _service_sources
declare -A _seen_services
declare -A _service_sources
# Set FACTORY_ROOT to test directory
export FACTORY_ROOT="${TEST_DIR}"
local compose_file="${TEST_DIR}/docker-compose4.yml"
cat > "$compose_file" <<'COMPOSEEOF'
services:
agents:
image: test
volumes:
test:
COMPOSEEOF
# Process both TOML files
if _generate_local_model_services "$compose_file" 2>&1 | grep -q "Duplicate service name"; then
fail "Test 4: False positive for different agent names"
else
pass "Test 4: No duplicate when agent names are different"
fi
}
# Run all tests
echo "Running duplicate service detection tests..."
echo ""
test_1_llama_dup
echo ""
test_2_only_env_flag
echo ""
test_3_toml_dup
echo ""
test_4_different_names
echo ""
# Summary
echo "=== Test Summary ==="
if [ "$FAILED" -eq 0 ]; then
echo "All tests passed!"
exit 0
else
echo "Some tests failed!"
exit 1
fi