Files
app/backend/tests/api/routes/test_flows.py
T
Melvin StroblandClaude Fable 5 fd666743d2 Add flow settings, pulse emitting nodes, and simplify node state
- One dot per node now carries the whole story: primary while running, sage
  after a good run, red when anything is wrong, with the explanation on hover.
  The corner badge is gone, along with the second way of saying the same thing.
- A node that publishes something flashes a ring, so a running flow is legible
  without reading the edge values. Nodes that consume but publish nothing stay
  quiet, which is why the event carries an output count.
- Flow settings open in the same panel its nodes use, from a pencil in the
  dock: the title, the name, and deleting the flow. NodePanel and FlowPanel
  share the panel chrome rather than each drawing their own.
- Renaming is a server operation, because a flow's name is the namespace of its
  messages: the directory moves and every other flow reading `old.message` is
  repointed, instead of being left pointing at a flow that no longer exists.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016WzrvW7rjQbynnhF6pxh6i
2026-08-15 19:46:23 +02:00

209 lines
6.4 KiB
Python

from fastapi.testclient import TestClient
from app.core.config import settings
PREFIX = f"{settings.API_V1_STR}/flows"
WORKING_NODE = """
def process(params):
return {"reading": 21.5}
"""
BROKEN_NODE = """
def process(params):
raise RuntimeError("boom")
"""
def a_flow(name: str = "demo") -> dict:
return {
"name": name,
"title": "Demo",
"nodes": [
{
"id": "sensor",
"type": "python",
"position": {"x": 0, "y": 0},
"provides": [{"name": "reading", "dtype": "float"}],
},
{
"id": "logger",
"type": "python",
"position": {"x": 240, "y": 0},
"requires": [{"name": "reading", "dtype": "float"}],
},
],
}
def test_flows_require_authentication(client: TestClient) -> None:
assert client.get(f"{PREFIX}/").status_code == 401
def test_save_then_read_flow(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
response = client.put(
f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow()
)
assert response.status_code == 200
response = client.get(f"{PREFIX}/demo", headers=superuser_token_headers)
assert response.status_code == 200
body = response.json()
assert body["definition"]["title"] == "Demo"
assert {node["id"] for node in body["definition"]["nodes"]} == {"sensor", "logger"}
def test_name_mismatch_is_rejected(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
response = client.put(
f"{PREFIX}/other", headers=superuser_token_headers, json=a_flow("demo")
)
assert response.status_code == 400
def test_broken_node_is_reported_and_siblings_stay_active(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
client.put(
f"{PREFIX}/demo/nodes/sensor/source",
headers=superuser_token_headers,
json={"code": WORKING_NODE},
)
response = client.put(
f"{PREFIX}/demo/nodes/logger/source",
headers=superuser_token_headers,
json={"code": "def process(reading, params:\n"},
)
assert response.status_code == 200
assert response.json()["status"] == "error"
statuses = client.get(f"{PREFIX}/demo", headers=superuser_token_headers).json()
by_id = {node["id"]: node for node in statuses["nodes"]}
assert by_id["demo.sensor"]["status"] == "active"
assert by_id["demo.logger"]["status"] == "error"
def test_running_a_flow_produces_values(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
client.put(
f"{PREFIX}/demo/nodes/sensor/source",
headers=superuser_token_headers,
json={"code": WORKING_NODE},
)
client.put(
f"{PREFIX}/demo/nodes/logger/source",
headers=superuser_token_headers,
json={"code": "def process(reading, params):\n return {}\n"},
)
response = client.post(
f"{PREFIX}/demo/run", headers=superuser_token_headers, json={"inputs": {}}
)
assert response.status_code == 200
assert response.json()["values"]["demo.reading"]["value"] == 21.5
def test_unconnected_input_is_surfaced(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
flow = a_flow()
flow["nodes"][0]["provides"] = [] # nothing produces "reading" any more
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=flow)
issues = client.post(
f"{PREFIX}/demo/validate", headers=superuser_token_headers
).json()["issues"]
assert any(issue["code"] == "unconnected_input" for issue in issues)
def test_delete_flow(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
assert (
client.delete(f"{PREFIX}/demo", headers=superuser_token_headers).status_code
== 200
)
assert (
client.get(f"{PREFIX}/demo", headers=superuser_token_headers).status_code == 404
)
def test_node_types_are_listed(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
types = client.get(f"{PREFIX}/node-types", headers=superuser_token_headers).json()
by_type = {entry["type"]: entry for entry in types}
assert by_type["python"]["has_source"] is True
assert "properties" in by_type["mqtt"]["params_schema"]
def test_rename_flow(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
response = client.post(
f"{PREFIX}/demo/rename",
headers=superuser_token_headers,
json={"new_name": "demo_renamed"},
)
assert response.status_code == 200
assert response.json()["definition"]["name"] == "demo_renamed"
assert (
client.get(f"{PREFIX}/demo", headers=superuser_token_headers).status_code == 404
)
assert (
client.get(
f"{PREFIX}/demo_renamed", headers=superuser_token_headers
).status_code
== 200
)
# Put it back so the tests that follow find the flow they expect.
client.post(
f"{PREFIX}/demo_renamed/rename",
headers=superuser_token_headers,
json={"new_name": "demo"},
)
def test_rename_onto_a_taken_name_is_refused(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
client.put(
f"{PREFIX}/occupied", headers=superuser_token_headers, json=a_flow("occupied")
)
response = client.post(
f"{PREFIX}/demo/rename",
headers=superuser_token_headers,
json={"new_name": "occupied"},
)
assert response.status_code == 409
client.delete(f"{PREFIX}/occupied", headers=superuser_token_headers)
def test_rename_rejects_an_invalid_name(
client: TestClient, superuser_token_headers: dict[str, str]
) -> None:
client.put(f"{PREFIX}/demo", headers=superuser_token_headers, json=a_flow())
response = client.post(
f"{PREFIX}/demo/rename",
headers=superuser_token_headers,
json={"new_name": "Not A Flow Name"},
)
assert response.status_code == 400