Files
smart-home-control-system/scripts/acceptance.sh
T
sambu 4c23a82a02 test(acceptance): A7 网页接口冒烟细化为 13 项抽检(静态三件套/只读接口/写接口/离线负例/404 兜底)
验收脚本总计 19 项断言全通过;采样有效性断言可回归 HAL I2C 字节序修复
2026-09-14 20:19:22 +08:00

69 lines
4.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 验收脚本:一键跑通 A1~A7 验收项并打印结论
# 用法:bash scripts/acceptance.sh
set -uo pipefail
cd "$(dirname "$0")/.."
PASS=0; FAIL=0
ok() { printf ' [通过] %s\n' "$1"; PASS=$((PASS+1)); }
bad() { printf ' [失败] %s\n' "$1"; FAIL=$((FAIL+1)); }
echo "== A1 debug 预设构建(告警即错误) =="
if cmake --preset debug >/dev/null && cmake --build --preset debug >/tmp/shs_build.log 2>&1; then
if grep -qE 'warning:' /tmp/shs_build.log; then bad "构建出现告警(见 /tmp/shs_build.log)"; else ok "零告警构建成功"; fi
else bad "构建失败(见 /tmp/shs_build.log)"; fi
echo "== A2 release 预设构建 =="
if cmake --preset release >/dev/null && cmake --build --preset release >/tmp/shs_build_rel.log 2>&1; then ok "release 构建成功"; else bad "release 构建失败"; fi
echo "== A3 头文件接口检查 =="
if bash tools/header_check.sh >/tmp/shs_headers.log 2>&1; then ok "全部头文件可独立编译"; else bad "头文件检查失败"; fi
echo "== A4 CTest 全量测试 =="
if ctest --preset debug --output-on-failure >/tmp/shs_ctest.log 2>&1; then
ok "$(grep -cE 'Test #' /tmp/shs_ctest.log || echo 0) 个测试全部通过"
else bad "测试未全部通过(见 /tmp/shs_ctest.log)"; fi
echo "== A5 ASan/UBSan 预设 =="
if cmake --preset asan >/dev/null && cmake --build --preset asan >/tmp/shs_asan_build.log 2>&1 && ctest --preset asan >/tmp/shs_asan.log 2>&1; then
ok "ASan/UBSan 下测试通过"
else bad "ASan/UBSan 检查失败"; fi
echo "== A6 回放演示 =="
if ./build/debug/smart_home --replay scripts/demo.txt --data /tmp/shs_demo_data >/tmp/shs_demo.log 2>&1; then
ok "演示脚本执行完成(输出 /tmp/shs_demo.log)"
else bad "演示脚本执行失败"; fi
echo "== A7 网页接口冒烟(17 条路由抽检) =="
./build/debug/smart_home --web --port 18080 --data /tmp/shs_web_data >/tmp/shs_web.log 2>&1 &
WEB_PID=$!; sleep 1.5
WEB=http://127.0.0.1:18080
code() { curl -s -o /tmp/shs_body.txt -w '%{http_code}' --noproxy '*' "$1"; }
size() { curl -s -o /tmp/shs_body.txt -w '%{size_download}' --noproxy '*' "$1"; }
# 静态三件套
if [ "$(code $WEB/)" = "200" ] && grep -q "<!DOCTYPE html>" /tmp/shs_body.txt; then ok "首页 HTML 可访问"; else bad "首页不可访问"; fi
if [ "$(size $WEB/app.js)" -gt 3000 ]; then ok "内嵌脚本 /app.js 完整下发(>3KB,分段字面量拼装生效)"; else bad "/app.js 体积异常"; fi
if [ "$(size $WEB/style.css)" -gt 1000 ]; then ok "内嵌样式 /style.css 完整下发"; else bad "/style.css 体积异常"; fi
# 只读接口
if curl -s --noproxy '*' $WEB/api/devices | grep -q '"客厅主灯"' && curl -s --noproxy '*' $WEB/api/devices | grep -q '"count":8'; then ok "GET /api/devices 返回 8 台设备"; else bad "/api/devices 数据异常"; fi
if curl -s --noproxy '*' $WEB/api/sensors | grep -q '"valid":true'; then ok "GET /api/sensors 采样有效(HAL I2C 字节序正确)"; else bad "/api/sensors 采样无效"; fi
for path in /api/log /api/report /api/config /api/selftest; do
if [ "$(code $WEB$path)" = "200" ]; then ok "GET $path 返回 200"; else bad "GET $path 异常"; fi
done
# 写接口
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"id":1,"on":true}' $WEB/api/device/power | grep -q '"power":true'; then ok "POST /api/device/power 生效"; else bad "电源控制接口异常"; fi
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"dev":1,"cmd":2,"value":1}' $WEB/api/frame | grep -q '"crc_ok":true'; then ok "POST /api/frame 帧 CRC 校验通过"; else bad "协议帧接口异常"; fi
# 负例:离线设备拒绝开机、未知路径 404
if curl -s --noproxy '*' -X POST -H 'Content-Type: application/json' -d '{"id":8,"on":true}' $WEB/api/device/power | grep -q '"ok":false'; then ok "负例:离线设备开机被拒(返回 ok:false)"; else bad "离线设备负例未生效"; fi
if [ "$(code $WEB/api/nonexistent)" = "404" ]; then ok "负例:未知路径返回 404"; else bad "404 兜底异常"; fi
kill $WEB_PID 2>/dev/null || true
wait $WEB_PID 2>/dev/null || true
echo
echo "验收汇总:通过 $PASS 项,失败 $FAIL 项"
[ "$FAIL" -eq 0 ]