|
| 1 | +#!/bin/bash |
| 2 | +# test-preset-diff.sh |
| 3 | +# Tests preset diff command |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +DREAM_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 9 | +DREAM_CLI="$DREAM_DIR/dream-cli" |
| 10 | + |
| 11 | +# Test counters |
| 12 | +PASS=0 |
| 13 | +FAIL=0 |
| 14 | + |
| 15 | +# Test helper functions |
| 16 | +pass() { echo "✓ $1"; PASS=$((PASS + 1)); } |
| 17 | +fail() { echo "✗ $1"; FAIL=$((FAIL + 1)); } |
| 18 | + |
| 19 | +# Setup test environment |
| 20 | +setup() { |
| 21 | + export DREAM_HOME="$DREAM_DIR" |
| 22 | + export INSTALL_DIR="$DREAM_DIR" |
| 23 | + export SCRIPT_DIR="$DREAM_DIR" |
| 24 | + # PRESETS_DIR is set by dream-cli to ${INSTALL_DIR}/presets |
| 25 | + mkdir -p "$DREAM_DIR/presets" |
| 26 | + touch "$DREAM_DIR/docker-compose.base.yml" 2>/dev/null || true |
| 27 | +} |
| 28 | + |
| 29 | +# Cleanup |
| 30 | +cleanup() { |
| 31 | + rm -rf "$DREAM_DIR/presets/test-preset-"* 2>/dev/null || true |
| 32 | + # Don't remove docker-compose.base.yml if it existed before tests |
| 33 | +} |
| 34 | + |
| 35 | +# Test 1: Diff command requires two arguments |
| 36 | +test_diff_requires_args() { |
| 37 | + local output |
| 38 | + output=$("$DREAM_CLI" preset diff 2>&1 || true) |
| 39 | + if echo "$output" | grep -q "Usage:"; then |
| 40 | + pass "Diff requires two arguments" |
| 41 | + else |
| 42 | + fail "Diff should require two arguments" |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +# Test 2: Diff fails if preset doesn't exist |
| 47 | +test_diff_nonexistent_preset() { |
| 48 | + local output |
| 49 | + output=$("$DREAM_CLI" preset diff nonexistent1 nonexistent2 2>&1 || true) |
| 50 | + if echo "$output" | grep -q "not found"; then |
| 51 | + pass "Diff fails for nonexistent presets" |
| 52 | + else |
| 53 | + fail "Diff should fail for nonexistent presets" |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# Test 3: Diff shows no differences for identical presets |
| 58 | +test_diff_identical() { |
| 59 | + local presets_dir="$DREAM_DIR/presets" |
| 60 | + mkdir -p "$presets_dir/test-preset-a" "$presets_dir/test-preset-b" |
| 61 | + echo "TIER=3" > "$presets_dir/test-preset-a/env" |
| 62 | + echo "TIER=3" > "$presets_dir/test-preset-b/env" |
| 63 | + echo "enabled:llama-server" > "$presets_dir/test-preset-a/extensions.list" |
| 64 | + echo "enabled:llama-server" > "$presets_dir/test-preset-b/extensions.list" |
| 65 | + |
| 66 | + local output |
| 67 | + output=$("$DREAM_CLI" preset diff test-preset-a test-preset-b 2>&1 || true) |
| 68 | + if echo "$output" | grep -q "no differences"; then |
| 69 | + pass "Diff shows no differences for identical presets" |
| 70 | + else |
| 71 | + fail "Diff should show no differences for identical presets" |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +# Test 4: Diff detects environment variable changes |
| 76 | +test_diff_env_changes() { |
| 77 | + local presets_dir="$DREAM_DIR/presets" |
| 78 | + mkdir -p "$presets_dir/test-preset-c" "$presets_dir/test-preset-d" |
| 79 | + echo "TIER=3" > "$presets_dir/test-preset-c/env" |
| 80 | + echo "TIER=4" > "$presets_dir/test-preset-d/env" |
| 81 | + |
| 82 | + local output |
| 83 | + output=$("$DREAM_CLI" preset diff test-preset-c test-preset-d 2>&1 || true) |
| 84 | + if echo "$output" | grep -q "TIER"; then |
| 85 | + pass "Diff detects environment variable changes" |
| 86 | + else |
| 87 | + fail "Diff should detect environment variable changes" |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +# Test 5: Diff detects service state changes |
| 92 | +test_diff_service_changes() { |
| 93 | + local presets_dir="$DREAM_DIR/presets" |
| 94 | + mkdir -p "$presets_dir/test-preset-e" "$presets_dir/test-preset-f" |
| 95 | + echo "enabled:llama-server" > "$presets_dir/test-preset-e/extensions.list" |
| 96 | + echo "disabled:llama-server" > "$presets_dir/test-preset-f/extensions.list" |
| 97 | + |
| 98 | + local output |
| 99 | + output=$("$DREAM_CLI" preset diff test-preset-e test-preset-f 2>&1 || true) |
| 100 | + if echo "$output" | grep -q "llama-server"; then |
| 101 | + pass "Diff detects service state changes" |
| 102 | + else |
| 103 | + fail "Diff should detect service state changes" |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +# Test 6: Diff masks sensitive values |
| 108 | +test_diff_masks_secrets() { |
| 109 | + local presets_dir="$DREAM_DIR/presets" |
| 110 | + mkdir -p "$presets_dir/test-preset-g" "$presets_dir/test-preset-h" |
| 111 | + echo "API_KEY=secret123" > "$presets_dir/test-preset-g/env" |
| 112 | + echo "API_KEY=secret456" > "$presets_dir/test-preset-h/env" |
| 113 | + echo "enabled:llama-server" > "$presets_dir/test-preset-g/extensions.list" |
| 114 | + echo "enabled:llama-server" > "$presets_dir/test-preset-h/extensions.list" |
| 115 | + |
| 116 | + local output |
| 117 | + output=$("$DREAM_CLI" preset diff test-preset-g test-preset-h 2>&1 || true) |
| 118 | + if echo "$output" | grep -q "API_KEY" && echo "$output" | grep -q "\*\*\*"; then |
| 119 | + pass "Diff masks sensitive values" |
| 120 | + else |
| 121 | + fail "Diff should mask sensitive values" |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +# Run tests |
| 126 | +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 127 | +echo "Preset Diff Tests" |
| 128 | +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 129 | + |
| 130 | +setup |
| 131 | +test_diff_requires_args |
| 132 | +test_diff_nonexistent_preset |
| 133 | +test_diff_identical |
| 134 | +test_diff_env_changes |
| 135 | +test_diff_service_changes |
| 136 | +test_diff_masks_secrets |
| 137 | +cleanup |
| 138 | + |
| 139 | +# Summary |
| 140 | +echo "" |
| 141 | +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 142 | +echo "Test Results: $PASS passed, $FAIL failed" |
| 143 | +[[ $FAIL -eq 0 ]] && exit 0 || exit 1 |
0 commit comments