audit_site.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. # 全站验收:起服务 → 逐个页面真实加载 → 查正文/控制台错误/图片可达
  3. set -u
  4. PROJ=/opt/data/projects/hardware-lab
  5. SITE=$PROJ/site
  6. PORT=${PORT:-8599}
  7. CHROME=/tmp/pwbrowsers/chromium_headless_shell-1255/chrome-headless-shell-linux64/chrome-headless-shell
  8. cd "$SITE" || exit 1
  9. # 起服务(若未起)
  10. if ! curl -s -o /dev/null -m 2 "http://127.0.0.1:$PORT/index.html"; then
  11. (python3 -m http.server $PORT --bind 127.0.0.1 >/dev/null 2>&1 &) ; sleep 1.5
  12. fi
  13. IDS=$(ls data/products/*.json 2>/dev/null | grep -v INDEX | xargs -n1 basename | sed 's/.json//')
  14. ROUTES="#/ "
  15. for id in $IDS; do ROUTES="$ROUTES #/p/$id #/lab/$id"; done
  16. echo "=== 静态资源可达性 ==="
  17. for f in index.html app.js assets/theme.css assets/app.css \
  18. vendor/lit/lit/index.js vendor/babylon/babylon.js; do
  19. printf "%-32s " "$f"; curl -s -o /dev/null -w "%{http_code}\n" "http://127.0.0.1:$PORT/$f"
  20. done
  21. printf "%-32s " "data/products/INDEX.json"; curl -s -o /dev/null -w "%{http_code}\n" "http://127.0.0.1:$PORT/data/products/INDEX.json"
  22. echo
  23. echo "=== 主 KV 与配图 ==="
  24. for f in assets/kv/*.png assets/detail/*.png; do
  25. [ -f "$SITE/$f" ] || continue
  26. printf "%-46s " "$(basename $f)"; curl -s -o /dev/null -w "%{http_code}\n" "http://127.0.0.1:$PORT/$f"
  27. done | awk '{print} $2!=200{bad++} END{if(bad)print " ✗ 不可达 " bad " 个"}'
  28. echo
  29. echo "=== 页面渲染检查 ==="
  30. FAILED=0
  31. printf "%-26s %-8s %-8s %s\n" "ROUTE" "正文长度" "错误数" "判定"
  32. for r in $ROUTES; do
  33. OUT=/tmp/route-dom.html; ERR=/tmp/route-err.log
  34. timeout 45 "$CHROME" --no-sandbox --disable-gpu --disable-dev-shm-usage \
  35. --headless --use-gl=swiftshader --enable-unsafe-swiftshader --virtual-time-budget=12000 --enable-logging=stderr --v=0 \
  36. --dump-dom "http://127.0.0.1:$PORT/index.html$r" > "$OUT" 2>"$ERR"
  37. LEN=$(python3 -c "
  38. import re
  39. h=open('$OUT',encoding='utf-8',errors='replace').read()
  40. t=re.sub(r'<(script|style).*?</\1>','',h,flags=re.S)
  41. t=re.sub(r'<[^>]+>',' ',t); t=re.sub(r'\s+',' ',t).strip()
  42. print(len(t))")
  43. ERRS=$(grep -i -E "Uncaught|SyntaxError|TypeError|ReferenceError|Failed to (load|fetch)|404" "$ERR" \
  44. | grep -v -i -E "gpu|vulkan|dbus|font|sandbox|DevTools|GCM" | wc -l)
  45. if [ "$LEN" -lt 200 ] || [ "$ERRS" -gt 0 ]; then V="✗"; FAILED=$((FAILED+1)); else V="✓"; fi
  46. printf "%-26s %-8s %-8s %s\n" "$r" "$LEN" "$ERRS" "$V"
  47. done
  48. echo
  49. echo "=== 结论:$FAILED 个路由未通过 ==="
  50. exit $FAILED