Files
gree-controller/scripts/smoke.sh
T
2026-08-23 23:32:26 +02:00

78 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
BINARY="${GREE_CONTROLLER_TEST_BINARY:-$ROOT/target/debug/gree-controller}"
[[ -x "$BINARY" ]] || cargo build >/dev/null
TMP="$(mktemp -d)"
PORT="${GREE_CONTROLLER_TEST_PORT:-$((19000 + RANDOM % 1000))}"
LOG="$TMP/server.log"
PID=""
cleanup() {
if [[ -n "$PID" ]] && kill -0 "$PID" 2>/dev/null; then kill "$PID" 2>/dev/null || true; wait "$PID" 2>/dev/null || true; fi
rm -rf "$TMP"
}
trap cleanup EXIT
GREE_CONTROLLER_BIND="127.0.0.1:$PORT" \
GREE_CONTROLLER_DATABASE="$TMP/test.db" \
GREE_CONTROLLER_SIMULATE=true \
GREE_CONTROLLER_AUTO_SEED=true \
GREE_CONTROLLER_POLL_INTERVAL_SECONDS=2 \
GREE_CONTROLLER_ZONE_INTERVAL_SECONDS=2 \
GREE_CONTROLLER_APP_TOKEN="" \
RUST_LOG=warn \
"$BINARY" >"$LOG" 2>&1 &
PID=$!
for _ in $(seq 1 80); do
if curl -fsS "http://127.0.0.1:$PORT/api/health" >"$TMP/health.json"; then break; fi
if ! kill -0 "$PID" 2>/dev/null; then cat "$LOG" >&2; exit 1; fi
sleep 0.1
done
grep -q '"status":"ok"' "$TMP/health.json"
curl -fsS "http://127.0.0.1:$PORT/api/bootstrap" >"$TMP/bootstrap.json"
grep -q 'sim-salon' "$TMP/bootstrap.json"
curl -fsS -X POST -H 'Content-Type: application/json' \
-d '{"power":true,"mode":"cool","target_temperature":22}' \
"http://127.0.0.1:$PORT/api/devices/sim-salon/command" >"$TMP/command.json"
grep -q '"power":true' "$TMP/command.json"
curl -fsS -X POST -H 'Content-Type: application/json' \
-d '{"name":"Test","device_id":"sim-salon","enabled":true,"mode":"cool","setpoint":23,"hysteresis":0.6,"min_on_seconds":0,"min_off_seconds":0,"sensor_source":"device"}' \
"http://127.0.0.1:$PORT/api/zones" >"$TMP/zone.json"
grep -q '"name":"Test"' "$TMP/zone.json"
ZONE_ID="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["id"])' "$TMP/zone.json")"
sleep 3
curl -fsS "http://127.0.0.1:$PORT/api/history?zone_id=$ZONE_ID&hours=1" >"$TMP/history.json"
grep -q '"readings"' "$TMP/history.json"
grep -q '"control_temperature"' "$TMP/history.json"
curl -fsS "http://127.0.0.1:$PORT/api/readings?device_id=sim-salon&hours=1" >"$TMP/readings.json"
grep -q '"readings"' "$TMP/readings.json"
# Home Assistant gets its own generated, restricted controller token.
curl -fsS -X POST -H 'Content-Type: application/json' \
-d '{"name":"Smoke Home Assistant"}' \
"http://127.0.0.1:$PORT/api/access-tokens" >"$TMP/access-token.json"
HA_ACCESS_TOKEN="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["token"])' "$TMP/access-token.json")"
[[ "$HA_ACCESS_TOKEN" == gree_controller_* ]]
if curl -fsS "http://127.0.0.1:$PORT/api/integrations/home-assistant/devices" >/dev/null 2>&1; then
echo "Restricted Home Assistant API unexpectedly accepted a request without a token" >&2
exit 1
fi
curl -fsS -H "Authorization: Bearer $HA_ACCESS_TOKEN" \
"http://127.0.0.1:$PORT/api/integrations/home-assistant/devices" >"$TMP/ha-devices.json"
grep -q 'sim-salon' "$TMP/ha-devices.json"
curl -fsS -X POST -H "Authorization: Bearer $HA_ACCESS_TOKEN" -H 'Content-Type: application/json' \
-d '{"power":false}' \
"http://127.0.0.1:$PORT/api/integrations/home-assistant/devices/sim-salon/command" >"$TMP/ha-command.json"
grep -q '"power":false' "$TMP/ha-command.json"
echo "Smoke test OK (port $PORT)"