#!/usr/bin/env bash set -euo pipefail fail() { printf 'Control Center services contract: %s\n' "$1" >&2 exit 1 } cleanup() { qs ipc call kdeconnect reset >/dev/null 2>&1 || true qs ipc call home-assistant reset >/dev/null 2>&1 || true qs ipc call status-events reset >/dev/null 2>&1 || true qs ipc call quicksettings close >/dev/null 2>&1 || true } trap cleanup EXIT qs ipc show | rg -q '^target kdeconnect$' \ || fail 'KDE Connect IPC target is missing' qs ipc show | rg -q '^target home-assistant$' \ || fail 'Home Assistant IPC target is missing' qs ipc call kdeconnect fixture reachable >/dev/null jq -e ' .fixture == true and .available == true and .reachable == true and .actionCount == 4 and .transferActive == false and .ongoingCount == 0 ' <<<"$(qs ipc call kdeconnect status)" >/dev/null \ || fail 'reachable phone fixture is malformed' qs ipc call kdeconnect fixture offline >/dev/null jq -e ' .fixture == true and .available == true and .reachable == false and .pairedCount == 1 and .ongoingCount == 0 ' <<<"$(qs ipc call kdeconnect status)" >/dev/null \ || fail 'offline phone fixture is malformed' qs ipc call kdeconnect fixture transfer >/dev/null jq -e ' .fixture == true and .transferActive == true and .transferFileName == "Fixture document.pdf" and .ongoingCount == 1 ' <<<"$(qs ipc call kdeconnect status)" >/dev/null \ || fail 'phone transfer did not enter Ongoing' qs ipc call kdeconnect cancel >/dev/null jq -e '.transferActive == false and .ongoingCount == 0' \ <<<"$(qs ipc call kdeconnect status)" >/dev/null \ || fail 'phone transfer did not leave Ongoing' qs ipc call home-assistant fixture ready >/dev/null jq -e ' .fixture == true and .phase == "ready" and .configuredCount == 7 and .visibleCount == 4 and .stale == false and .lastError == "" ' <<<"$(qs ipc call home-assistant status)" >/dev/null \ || fail 'ready Home fixture is malformed' qs ipc call home-assistant fixture stale >/dev/null jq -e ' .fixture == true and .phase == "degraded" and .configuredCount == 7 and .visibleCount == 4 and .stale == true and .lastError == "unreachable" ' <<<"$(qs ipc call home-assistant status)" >/dev/null \ || fail 'stale Home fixture did not retain entities' qs ipc call home-assistant fixture unavailable >/dev/null jq -e ' .fixture == true and .phase == "unavailable" and .configuredCount == 0 and .visibleCount == 0 and .lastError == "not-configured" ' <<<"$(qs ipc call home-assistant status)" >/dev/null \ || fail 'unavailable Home fixture is malformed' trap - EXIT cleanup printf 'Control Center services contract: PASS\n'