mihomo-kit/tools/test.sh
2026-10-31 18:44:17 +03:00

50 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
# Render every fixture with router/mihomo-kit.sh and compare with tests/expected/,
# validate with `mihomo -t` when MIHOMO points to a mihomo binary, check
# luci/servers.js (share links, YAML) against the outputs of mihomo's own Go code,
# and luci/graph.js (the config graph) on the rendered profiles.
# tools/test.sh
# UPDATE=1 tools/test.sh rewrite tests/expected/ (review the diff)
# MIHOMO=/path/to/mihomo tools/test.sh
set -u
cd "$(dirname "$0")/.."
out=$(mktemp -d)
fail=0
if [ -n "${MIHOMO:-}" ]; then
# rule-sets are fetched over http: put a local copy of each in place for an offline check
mkdir -p "$out/home/ruleset"
echo '+.example.ru' > "$out/domains.txt"
for r in $(sed -n 's|^ *path: \./ruleset/\(.*\.mrs\)$|\1|p' tests/expected/*.yaml | sort -u); do
"$MIHOMO" convert-ruleset domain text "$out/domains.txt" "$out/home/ruleset/$r" >/dev/null
done
fi
mkdir -p tests/expected
for f in tests/fixtures/*.env; do
n=$(basename "$f" .env)
[ "$n" = common ] && continue
sh router/mihomo-kit.sh render tests/fixtures/common.env "$f" > "$out/$n.yaml" || { echo "FAIL $n: render"; fail=1; continue; }
if [ "${UPDATE:-0}" = 1 ]; then
cp "$out/$n.yaml" "tests/expected/$n.yaml"
elif ! diff -u "tests/expected/$n.yaml" "$out/$n.yaml"; then
echo "FAIL $n: differs from tests/expected/$n.yaml (UPDATE=1 if intended)"
fail=1
continue
fi
if [ -n "${MIHOMO:-}" ]; then
cp "$out/$n.yaml" "$out/home/$n.yaml"
if ! "$MIHOMO" -t -d "$out/home" -f "$out/home/$n.yaml" >"$out/$n.log" 2>&1; then
echo "FAIL $n: mihomo -t"; tail -3 "$out/$n.log"; fail=1; continue
fi
fi
echo "ok $n"
done
if command -v node >/dev/null 2>&1; then
node tools/test-servers.js || fail=1
node tools/test-graph.js || fail=1
node tools/test-fixed-services.js || fail=1
else
echo "skip luci/servers.js: no node"
fi
rm -rf "$out"
exit $fail