Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

9.4 KB
Raw Blame
#!/usr/bin/env bash
# Behavioural coverage for scripts/mode-switch.sh.
#
# scripts/README.md and docs/MODE-SWITCH.md both point operators at this
# script, but nothing exercised it. `--status` in particular is the read-only
# entry point people reach for first, and it has to survive a .env that is
# missing or does not pin ODS_MODE.
#
# Run: bash tests/test-mode-switch-status.sh

set -uo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MODE_SWITCH="$ROOT_DIR/scripts/mode-switch.sh"

PASS=0
FAIL=0

pass() { echo "[PASS] $1"; PASS=$((PASS + 1)); }
fail() { echo "[FAIL] $1"; echo "       $2"; FAIL=$((FAIL + 1)); }

check_eq() {
    local label="$1" expected="$2" actual="$3"
    if [[ "$expected" == "$actual" ]]; then
        pass "$label"
    else
        fail "$label" "expected [$expected] got [$actual]"
    fi
}

check_contains() {
    local label="$1" needle="$2" haystack="$3"
    if [[ "$haystack" == *"$needle"* ]]; then
        pass "$label"
    else
        fail "$label" "missing [$needle] in: $haystack"
    fi
}

WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT

# mode-switch.sh resolves .env as <script dir>/../.env, so give it a fixture
# install root to operate on.
new_root() {
    local root
    root="$(mktemp -d "$WORKDIR/root.XXXXXX")"
    mkdir -p "$root/scripts"
    cp "$MODE_SWITCH" "$root/scripts/"
    printf '%s' "$root"
}

run_mode() {
    local root="$1"; shift
    ( cd "$root/scripts" && bash ./mode-switch.sh "$@" ) 2>&1
}

run_rc() {
    local root="$1"; shift
    ( cd "$root/scripts" && bash ./mode-switch.sh "$@" ) >/dev/null 2>&1
    echo $?
}

current_mode_line() {
    grep -m1 "^Current mode:" <<< "$1" || true
}

# ── 1. No .env at all ─────────────────────────────────────────────────────
#
# The regression: `current=$(grep ... | cut ...)` under `set -euo pipefail`
# aborted the script when grep matched nothing, so --status printed nothing
# and exited non-zero. The `${current:-local}` default was unreachable.

ROOT="$(new_root)"
OUT="$(run_mode "$ROOT" --status)"
check_eq "no .env: exits 0" "0" "$(run_rc "$ROOT" --status)"
check_eq "no .env: falls back to the default mode" "Current mode: local" "$(current_mode_line "$OUT")"
check_contains "no .env: still lists the available modes" "Available modes:" "$OUT"
check_contains "no .env: says why the value is a default" ".env not found" "$OUT"

# ── 2. .env present but ODS_MODE not pinned ───────────────────────────────

ROOT="$(new_root)"
printf 'LLM_API_URL=http://llama-server:8080\n' > "$ROOT/.env"
OUT="$(run_mode "$ROOT" --status)"
check_eq "no ODS_MODE key: exits 0" "0" "$(run_rc "$ROOT" --status)"
check_eq "no ODS_MODE key: falls back to the default mode" "Current mode: local" "$(current_mode_line "$OUT")"

# ── 3. ODS_MODE pinned ────────────────────────────────────────────────────

ROOT="$(new_root)"
printf 'ODS_MODE=cloud\n' > "$ROOT/.env"
check_eq "pinned mode is reported" "Current mode: cloud" "$(current_mode_line "$(run_mode "$ROOT" --status)")"

ROOT="$(new_root)"
printf 'ODS_MODE="hybrid"\n' > "$ROOT/.env"
check_eq "quoted mode is reported unquoted" "Current mode: hybrid" "$(current_mode_line "$(run_mode "$ROOT" --status)")"

ROOT="$(new_root)"
printf 'ODS_MODE=cloud\r\n' > "$ROOT/.env"
check_eq "CRLF .env does not leak a carriage return" "Current mode: cloud" "$(current_mode_line "$(run_mode "$ROOT" --status)")"

# ── 4. Bare invocation defaults to status ─────────────────────────────────

ROOT="$(new_root)"
printf 'ODS_MODE=cloud\n' > "$ROOT/.env"
check_eq "no argument defaults to status" "Current mode: cloud" "$(current_mode_line "$(run_mode "$ROOT")")"

# ── 5. Switching still works ──────────────────────────────────────────────

ROOT="$(new_root)"
printf 'ODS_MODE=local\nLLM_API_URL=http://llama-server:8080\n' > "$ROOT/.env"
run_mode "$ROOT" cloud > /dev/null
check_eq "switch updates ODS_MODE" "ODS_MODE=cloud" "$(grep -m1 '^ODS_MODE=' "$ROOT/.env")"
check_eq "switch repoints LLM_API_URL" "LLM_API_URL=http://litellm:4000" "$(grep -m1 '^LLM_API_URL=' "$ROOT/.env")"
check_eq "switched mode is reported back" "Current mode: cloud" "$(current_mode_line "$(run_mode "$ROOT" --status)")"

ROOT="$(new_root)"
printf 'ODS_MODE=cloud\nODS_MODEL_SWITCHBOARD=enabled\n' > "$ROOT/.env"
run_mode "$ROOT" local > /dev/null
check_eq "switching back to local repoints LLM_API_URL" "LLM_API_URL=http://llama-server:8080" "$(grep -m1 '^LLM_API_URL=' "$ROOT/.env")"
check_eq "local switchboard routes Hermes through model-router" "HERMES_LLM_BASE_URL=http://model-router:9099/v1" "$(grep -m1 '^HERMES_LLM_BASE_URL=' "$ROOT/.env")"
check_eq "local switchboard gives Hermes the local router key" "HERMES_LLM_API_KEY=no-key" "$(grep -m1 '^HERMES_LLM_API_KEY=' "$ROOT/.env")"

# Host-native llama-server (Windows Portal) keeps the in-stack llama-server
# off; local mode must keep routing through LiteLLM, as the installer does.
ROOT="$(new_root)"
printf 'ODS_MODE=cloud
NATIVE_LLM_BASE_URL=http://127.0.0.1:8080
LITELLM_KEY=sk-ods-abc
' > "$ROOT/.env"
run_mode "$ROOT" local > /dev/null
check_eq "native local keeps LLM_API_URL on LiteLLM" "LLM_API_URL=http://litellm:4000" "$(grep -m1 '^LLM_API_URL=' "$ROOT/.env")"
check_eq "native local keeps Hermes on LiteLLM" "HERMES_LLM_BASE_URL=http://litellm:4000/v1" "$(grep -m1 '^HERMES_LLM_BASE_URL=' "$ROOT/.env")"
check_eq "native local gives Hermes the LiteLLM key" "HERMES_LLM_API_KEY=sk-ods-abc" "$(grep -m1 '^HERMES_LLM_API_KEY=' "$ROOT/.env")"

# ── 6. External topology cannot be partially overwritten ─────────────────

ROOT="$(new_root)"
cat > "$ROOT/.env" <<'EOF'
ODS_MODE=local
LLM_BACKEND=external
LLM_API_URL=http://host.docker.internal:11434
EXTERNAL_LLM_URL=http://127.0.0.1:11434
EOF
BEFORE="$(cat "$ROOT/.env")"
OUT="$(run_mode "$ROOT" cloud)"
check_eq "external backend rejects direct mode switch" "1" "$(run_rc "$ROOT" cloud)"
check_contains "external backend points to the transactional reset" "--no-external-llm" "$OUT"
check_eq "rejected external mode switch leaves .env unchanged" "$BEFORE" "$(cat "$ROOT/.env")"

# ── 7. Bad input is rejected ──────────────────────────────────────────────

ROOT="$(new_root)"
printf 'ODS_MODE=local\n' > "$ROOT/.env"
check_eq "unknown mode exits 1" "1" "$(run_rc "$ROOT" banana)"
check_contains "unknown mode explains itself" "Unknown mode" "$(run_mode "$ROOT" banana)"

ROOT="$(new_root)"
check_eq "switching without .env exits 1" "1" "$(run_rc "$ROOT" cloud)"

# ── 8. Case-insensitive backend and switchboard values ──────────────────

ROOT="$(new_root)"
cat > "$ROOT/.env" <<'EOF'
ODS_MODE=local
LLM_BACKEND=EXTERNAL
LLM_API_URL=http://host.docker.internal:11434
EXTERNAL_LLM_URL=http://127.0.0.1:11434
EOF
BEFORE="$(cat "$ROOT/.env")"
OUT="$(run_mode "$ROOT" cloud)"
check_eq "uppercase EXTERNAL backend rejects direct mode switch" "1" "$(run_rc "$ROOT" cloud)"
check_contains "uppercase EXTERNAL backend points to reset" "--no-external-llm" "$OUT"
check_eq "rejected uppercase EXTERNAL mode switch leaves .env unchanged" "$BEFORE" "$(cat "$ROOT/.env")"

ROOT="$(new_root)"
printf 'ODS_MODE=cloud\nODS_MODEL_SWITCHBOARD=ENABLED\n' > "$ROOT/.env"
run_mode "$ROOT" local > /dev/null
check_eq "uppercase ENABLED switchboard routes Hermes through model-router" "HERMES_LLM_BASE_URL=http://model-router:9099/v1" "$(grep -m1 '^HERMES_LLM_BASE_URL=' "$ROOT/.env")"
check_eq "uppercase ENABLED switchboard gives Hermes the local router key" "HERMES_LLM_API_KEY=no-key" "$(grep -m1 '^HERMES_LLM_API_KEY=' "$ROOT/.env")"

# ── 9. Bash 3.2 and POSIX compatibility ───────────────────────────────────

# Ensure mode-switch.sh contains no Bash 4 case-modification parameter expansions (${var,,} or ${var^^})
if grep -E -q '\$\{[a-zA-Z_0-9]+,,|\$\{[a-zA-Z_0-9]+\^\^' "$MODE_SWITCH"; then
    fail "Bash 3.2 compat: mode-switch.sh contains non-portable Bash 4 case expansion" "found: $(grep -nE '\$\{[a-zA-Z_0-9]+,,|\$\{[a-zA-Z_0-9]+\^\^' "$MODE_SWITCH")"
else
    pass "Bash 3.2 compat: no non-portable case expansion syntax in mode-switch.sh"
fi

# When /bin/bash exists (macOS Bash 3.2 or Linux), verify direct execution under /bin/bash
if [[ -x /bin/bash ]]; then
    ROOT="$(new_root)"
    printf 'ODS_MODE=local\nLLM_API_URL=http://llama-server:8080\n' > "$ROOT/.env"
    ( cd "$ROOT/scripts" && PATH=/usr/bin:/bin /bin/bash ./mode-switch.sh cloud ) >/dev/null 2>&1
    check_eq "/bin/bash mode switch exits 0" "0" "$?"
    check_eq "/bin/bash switch updates ODS_MODE" "ODS_MODE=cloud" "$(grep -m1 '^ODS_MODE=' "$ROOT/.env")"
    check_eq "/bin/bash switch updates LLM_API_URL" "LLM_API_URL=http://litellm:4000" "$(grep -m1 '^LLM_API_URL=' "$ROOT/.env")"
fi

# ── Summary ───────────────────────────────────────────────────────────────

echo ""
echo "Passed: $PASS  Failed: $FAIL"
[[ "$FAIL" -eq 0 ]] || exit 1
echo "[PASS] mode-switch status and switching"