warpgate/tests/09-cli/test-preset-cli.sh
grabbit faf9d80824 feat: fill implementation gaps — preset unification, cron, adaptive bw, update cmd, tests
Step 1 — Unify preset logic (eliminate dual implementation)
- src/cli/preset.rs: add missing fields (chunk_limit, multi_thread_streams,
  multi_thread_cutoff), fix Office buffer_size 64M→128M, implement FromStr
- src/web/api.rs: post_preset() now calls Preset::apply() — no more inlined
  params; Office write_back unified to 5s (was 3s in API)

Step 2 — Fix setup.rs connection test: warn→bail
- All 4 "Warning: Could not connect/resolve" prints replaced with anyhow::bail!
  matching deploy/setup.rs behavior

Step 3 — Web UI: add [web] and [notifications] edit sections
- templates/web/tabs/config.html: new collapsible Web UI (password) and
  Notifications (webhook_url, cache_threshold_pct, nas_offline_minutes,
  writeback_depth) sections, both tagged "No restart"
- Also adds [log] section (file path + level select, "Full restart")

Step 4 — Full cron expression support in warmup scheduler
- Cargo.toml: add cron = "0.12", chrono = "0.4"
- supervisor.rs: normalize_cron_schedule() converts 5-field standard cron to
  7-field cron crate format; replaces naive hour-only matching

Step 5 — Adaptive bandwidth algorithm
- supervisor.rs: extract compute_adaptive_limit() pure function; sliding
  window of 6 samples, cv>0.3→congested (−25%, floor 1MiB/s), stable
  near-limit→maintain, under-utilizing→+10% (capped at limit_up)

Step 6 — warpgate update command
- src/cli/update.rs: query GitHub Releases API, compare with CARGO_PKG_VERSION
- src/main.rs: add Update{apply}, SetupWifi, CloneMac{interface} commands
- src/cli/wifi.rs: TODO stub for WiFi AP setup

Unit tests (+35, total 188→223)
- cli/preset.rs: 10 tests — FromStr, all fields for each preset, idempotency,
  connection/share isolation, write_back consistency regression
- supervisor.rs: 14 tests — normalize_cron_schedule (5 cases),
  compute_adaptive_limit (9 cases: congestion, floor, stable, under-utilizing,
  cap, zero-current, zero-max, empty window)
- config.rs: 11 tests — WebConfig (3), NotificationsConfig (4), LogConfig (4)

Shell tests (+4 scripts)
- tests/09-cli/test-preset-cli.sh: preset CLI without daemon; checks all
  three presets write correct values including unified buffer_size/write_back
- tests/09-cli/test-update-command.sh: update command; skips on no-network
- tests/10-scheduled/test-cron-warmup-schedule.sh: "* * * * *" fires in <90s
- tests/10-scheduled/test-adaptive-bandwidth.sh: adaptive loop stability
- tests/harness/config-gen.sh: add warmup.warmup_schedule override support
- tests/run-all.sh: add 10-scheduled category

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-19 16:55:00 +08:00

145 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Test: `warpgate preset <name>` applies correct values to config file.
#
# Verifies that each preset writes the expected cache.max_size to the config,
# that CLI and API presets are unified (same source of truth), and that the
# command exits 0 for valid presets and non-zero for unknown ones.
#
# Does NOT require a running warpgate daemon — only needs a config file.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/../harness/helpers.sh"
setup_test_env
trap teardown_test_env EXIT
# Generate a minimal config pointing at a fake NAS (we don't connect to it)
gen_config "nas_host=127.0.0.1"
# ── photographer preset ──────────────────────────────────────────────────────
output=$("$WARPGATE_BIN" preset photographer -c "$TEST_CONFIG" 2>&1) || {
echo "FAIL: 'warpgate preset photographer' exited non-zero"
echo " output: $output"
exit 1
}
assert_output_contains "$output" "photographer"
# Verify cache.max_size was written as 500G
if ! grep -q 'max_size = "500G"' "$TEST_CONFIG"; then
echo "FAIL: photographer preset did not write cache.max_size = \"500G\""
echo " config: $(grep max_size "$TEST_CONFIG" || echo '(not found)')"
exit 1
fi
# Verify chunk_size = 256M
if ! grep -q 'chunk_size = "256M"' "$TEST_CONFIG"; then
echo "FAIL: photographer preset did not write chunk_size = \"256M\""
exit 1
fi
# Verify chunk_limit = 1G (field added in this round of fixes)
if ! grep -q 'chunk_limit = "1G"' "$TEST_CONFIG"; then
echo "FAIL: photographer preset did not write chunk_limit = \"1G\""
exit 1
fi
# Verify multi_thread_streams = 4
if ! grep -q 'multi_thread_streams = 4' "$TEST_CONFIG"; then
echo "FAIL: photographer preset did not write multi_thread_streams = 4"
exit 1
fi
# Verify webdav is disabled for photographer
if grep -q 'enable_webdav = true' "$TEST_CONFIG"; then
echo "FAIL: photographer preset should NOT enable WebDAV"
exit 1
fi
# ── video preset ─────────────────────────────────────────────────────────────
gen_config "nas_host=127.0.0.1"
output=$("$WARPGATE_BIN" preset video -c "$TEST_CONFIG" 2>&1) || {
echo "FAIL: 'warpgate preset video' exited non-zero"
echo " output: $output"
exit 1
}
if ! grep -q 'max_size = "1T"' "$TEST_CONFIG"; then
echo "FAIL: video preset did not write cache.max_size = \"1T\""
exit 1
fi
if ! grep -q 'chunk_size = "512M"' "$TEST_CONFIG"; then
echo "FAIL: video preset did not write chunk_size = \"512M\""
exit 1
fi
if ! grep -q 'chunk_limit = "2G"' "$TEST_CONFIG"; then
echo "FAIL: video preset did not write chunk_limit = \"2G\""
exit 1
fi
if ! grep -q 'multi_thread_streams = 2' "$TEST_CONFIG"; then
echo "FAIL: video preset did not write multi_thread_streams = 2"
exit 1
fi
# ── office preset ─────────────────────────────────────────────────────────────
gen_config "nas_host=127.0.0.1"
output=$("$WARPGATE_BIN" preset office -c "$TEST_CONFIG" 2>&1) || {
echo "FAIL: 'warpgate preset office' exited non-zero"
echo " output: $output"
exit 1
}
if ! grep -q 'max_size = "50G"' "$TEST_CONFIG"; then
echo "FAIL: office preset did not write cache.max_size = \"50G\""
exit 1
fi
# office buffer_size must be 128M (not 64M — unified in Step 1 fix)
if ! grep -q 'buffer_size = "128M"' "$TEST_CONFIG"; then
echo "FAIL: office preset should write buffer_size = \"128M\", got:"
grep buffer_size "$TEST_CONFIG" || echo " (not found)"
exit 1
fi
# office should enable WebDAV
if ! grep -q 'enable_webdav = true' "$TEST_CONFIG"; then
echo "FAIL: office preset should enable WebDAV"
exit 1
fi
# office write_back should be 5s (unified; was incorrectly 3s in API before fix)
if ! grep -q 'write_back = "5s"' "$TEST_CONFIG"; then
echo "FAIL: office preset should write write_back = \"5s\""
exit 1
fi
# ── unknown preset returns non-zero ──────────────────────────────────────────
gen_config "nas_host=127.0.0.1"
if "$WARPGATE_BIN" preset bad-preset -c "$TEST_CONFIG" 2>&1; then
echo "FAIL: unknown preset should exit non-zero"
exit 1
fi
# ── config remains parseable after preset ─────────────────────────────────────
gen_config "nas_host=127.0.0.1"
"$WARPGATE_BIN" preset photographer -c "$TEST_CONFIG" > /dev/null 2>&1
# `warpgate status` parses the config; it will fail the mount check but not
# the config parse — ensure it doesn't error on config parsing
status_out=$("$WARPGATE_BIN" status -c "$TEST_CONFIG" 2>&1) || true
if echo "$status_out" | grep -qi "failed to parse\|toml\|invalid"; then
echo "FAIL: config written by preset is not parseable"
echo " output: $status_out"
exit 1
fi
echo "PASS: $(basename "$0" .sh)"