add info about aviable commands in heartbeat

This commit is contained in:
2026-06-24 04:11:31 +03:00
parent 9eb2a06757
commit 25ae509d52
8 changed files with 86 additions and 1 deletions
+19
View File
@@ -14,6 +14,7 @@ from nexus_sync.client.runtime import (
ClientConfigError,
HeartbeatError,
build_heartbeat_request,
list_available_commands,
load_client_config,
main,
run_once,
@@ -84,6 +85,24 @@ def test_build_heartbeat_request_contains_client_state(monkeypatch) -> None:
assert serialized["state"]["uptime_seconds"] is None
def test_list_available_commands_returns_allowed_command_names_and_descriptions() -> None:
commands = list_available_commands(CommandAccessPolicy.allow(["hostname"]))
assert [command.model_dump() for command in commands] == [
{"name": "hostname", "description": "Return system hostname"}
]
def test_build_heartbeat_request_includes_available_commands(monkeypatch) -> None:
monkeypatch.setattr("socket.gethostname", lambda: "macbook-pro.local")
monkeypatch.setattr("platform.system", lambda: "Darwin")
heartbeat = build_heartbeat_request(_config(CommandAccessPolicy.allow(["hostname"])))
assert [command.name for command in heartbeat.available_commands] == ["hostname"]
assert heartbeat.available_commands[0].description == "Return system hostname"
def test_build_heartbeat_request_includes_last_command_result(monkeypatch) -> None:
monkeypatch.setattr("socket.gethostname", lambda: "macbook-pro.local")
monkeypatch.setattr("platform.system", lambda: "Darwin")
+10
View File
@@ -6,6 +6,7 @@ from nexus_sync.common import (
ClientInfo,
ClientPlatform,
ClientState,
ClientCommandCapability,
Command,
CommandKind,
CommandResult,
@@ -28,6 +29,12 @@ def test_heartbeat_request_accepts_payload() -> None:
local_time=datetime(2026, 5, 24, 13, 20, 30, tzinfo=UTC),
uptime_seconds=1200,
),
available_commands=[
ClientCommandCapability(
name="hostname",
description="Return system hostname",
)
],
last_command_result=None,
)
@@ -35,6 +42,9 @@ def test_heartbeat_request_accepts_payload() -> None:
assert serialized["client_id"] == "macbook-pro-01"
assert serialized["client"]["platform"] == "darwin"
assert serialized["available_commands"] == [
{"name": "hostname", "description": "Return system hostname"}
]
assert serialized["last_command_result"] is None
+22
View File
@@ -87,6 +87,28 @@ def test_heartbeat_accepts_state_without_command() -> None:
assert store.clients["macbook-pro-01"].hostname == "macbook-pro.local"
def test_heartbeat_stores_available_commands() -> None:
store = InMemoryStore()
client = _client(store)
payload = _heartbeat_payload()
payload["available_commands"] = [
{"name": "hostname", "description": "Return system hostname"},
{"name": "network_interfaces", "description": "Return network interface information"},
]
response = client.post(
"/api/v1/client/heartbeat",
json=payload,
headers={"Authorization": "Bearer client-token"},
)
assert response.status_code == 200
assert [command.name for command in store.clients["macbook-pro-01"].available_commands] == [
"hostname",
"network_interfaces",
]
def test_heartbeat_delivers_pending_command_and_marks_it_delivered() -> None:
store = InMemoryStore()
store.enqueue_command(