Files
smart-home-control-system/scripts/acceptance.sh
T
sambu 3e10ee8488 test(S6): 补齐 app 层与 HTTP 第二组集成测试,行覆盖率 72.2%→80.3%(NFR-05 达标)
新增
- tests/integration/test_menu.c:菜单 14 项全分派、命令全集正/负例、参数解析、脚本化 stdin 主循环
- tests/integration/test_http_api.c:其余 11 个接口 + 错误路径(400/404/协议层 400)
- src/app/app_options.c:app_parse_args/app_print_usage 从 main.c 迁入静态库(app.h 声明此前无实现)
- app_clear_stop():复用主循环时清除退出标志
- tools/coverage.sh:A10 覆盖率门限自动判定;acceptance 扩到 A1~A10 共 25 项

修复(本轮集成测试暴露)
- menu_render 漏渲染首项「设备列表」(行号 0 既当标题又当表项 0)
- http_server_stop 只置标志不关套接字,导致同进程无法二次启动(聚合运行 70 项失败)
- POST /api/config 未匹配到字段时假成功(现返回 400)
- CRC/I2C 字节序、采样路数、离线设备负例等此前已在集成阶段修复

实测:CTest 19/19;聚合运行 19 套件 2450 断言 0 失败;ASan 19/19;覆盖率 80.3%
2026-09-14 22:23:17 +08:00

115 lines
6.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~A9 验收项并打印结论
# 用法: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
total=$(ctest --preset debug -N 2>/dev/null | sed -n 's/^Total Tests: //p' | tr -d ' ')
ok "${total:-?} 个测试全部通过(ctest 输出:$(grep -oE '[0-9]+% tests passed' /tmp/shs_ctest.log | head -1))"
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 "== A8 优雅退出与数据落盘 =="
A8DIR=$(mktemp -d)
./build/debug/smart_home --web --port 18585 --data "$A8DIR/data" >/tmp/shs_a8.log 2>&1 &
A8PID=$!; sleep 1.5
curl -s -m 3 --noproxy '*' -o /dev/null "http://127.0.0.1:18585/api/devices"
kill -INT "$A8PID" 2>/dev/null
for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$A8PID" 2>/dev/null || break; sleep 0.2; done
wait "$A8PID" 2>/dev/null
A8RC=$?
if [ "$A8RC" -eq 0 ]; then ok "网页模式 SIGINT 优雅退出(退出码 0)"; else bad "优雅退出失败(退出码 $A8RC)"; fi
if [ -f "$A8DIR/data/devices.dat" ] && [ -f "$A8DIR/data/system.log" ] && [ -f "$A8DIR/data/smart_home.ini" ]; then
ok "退出时设备表 / 日志 / 配置均已落盘"
else
bad "退出未正确落盘($A8DIR/data)"
fi
rm -rf "$A8DIR"
echo "== A9 门锁密码校验与冷却 =="
L9DIR=$(mktemp -d)
L9OUT=$(./build/debug/smart_home --data "$L9DIR/data" <<'EOF' 2>&1
lock 4 9999
lock 4 9999
lock 4 9999
lock 4 1234
EOF
)
echo "$L9OUT" | grep -q "密码错误:还可重试 2 次" && ok "错误密码被拒并给出剩余次数" || bad "未给出剩余重试次数"
echo "$L9OUT" | grep -q "门锁冷却中" && ok "连错 3 次进入 30 秒冷却" || bad "未进入冷却"
L9OUT2=$(./build/debug/smart_home --data "$L9DIR/data2" <<'EOF' 2>&1
lock 4 1234
EOF
)
echo "$L9OUT2" | grep -q "密码正确,已上锁" && ok "正确密码上锁成功" || bad "正确密码未生效"
rm -rf "$L9DIR"
echo "== A10 行覆盖率 ≥ 80%(NFR-05)=="
COV_LINE=$(bash tools/coverage.sh 2>/dev/null | grep "行覆盖率" | tail -1)
COV_PCT=$(printf '%s' "$COV_LINE" | sed -n 's/.*行覆盖率 \([0-9.]*\)%.*/\1/p')
if [ -n "$COV_PCT" ] && awk -v v="$COV_PCT" 'BEGIN { exit !(v >= 80.0) }'; then
ok "行覆盖率 $COV_PCT%(目标 ≥80%)"
else
bad "行覆盖率未达标:$COV_LINE"
fi
echo
echo "验收汇总:通过 $PASS 项,失败 $FAIL 项"
[ "$FAIL" -eq 0 ]