A wheel whose top-level module is `app` collides with anything else in a user's venv, so the package that is about to be published takes the name it is published under. Only the Python package moves; the repo, the Docker WORKDIR and the compose project keep theirs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
319 lines
9.6 KiB
Python
319 lines
9.6 KiB
Python
"""Dashboards: documents beside the flows, and the values their widgets move."""
|
|
|
|
import pytest
|
|
|
|
from fluksio.flow.dashboards import (
|
|
DashboardDef,
|
|
DashboardNotFound,
|
|
DashboardStore,
|
|
PageDef,
|
|
SectionDef,
|
|
WidgetDef,
|
|
default_dashboard,
|
|
)
|
|
from fluksio.flow.messages import DType, MessageSpec
|
|
from fluksio.flow.nodes import Node
|
|
from fluksio.flow.pipeline import Pipeline
|
|
from fluksio.flow.state import MemoryState
|
|
from fluksio.flow.store import FlowStore, StaleVersion
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp_path) -> DashboardStore:
|
|
return DashboardStore(FlowStore(tmp_path / "flows"))
|
|
|
|
|
|
def chart(message: str, points: int) -> WidgetDef:
|
|
return WidgetDef(
|
|
id="temp",
|
|
type="chart",
|
|
config={"series": [{"message": message}], "history": {"points": points}},
|
|
)
|
|
|
|
|
|
def test_a_dashboard_survives_a_round_trip(store: DashboardStore):
|
|
saved = store.write(default_dashboard("house"))
|
|
|
|
read = store.read("house")
|
|
|
|
assert read.name == "house"
|
|
assert [p.id for p in read.pages] == ["main"]
|
|
assert read.version == saved.version
|
|
|
|
|
|
def test_dashboards_are_invisible_to_the_flow_listing(store: DashboardStore):
|
|
"""They share the repository; they are not flows."""
|
|
store.write(default_dashboard("house"))
|
|
|
|
assert store.flows.list_flows() == []
|
|
assert [d.name for d in store.list()] == ["house"]
|
|
|
|
|
|
def test_a_save_based_on_a_version_someone_moved_past_is_refused(
|
|
store: DashboardStore,
|
|
):
|
|
first = store.write(default_dashboard("house"))
|
|
store.write(first, first.version)
|
|
|
|
with pytest.raises(StaleVersion):
|
|
store.write(first, first.version)
|
|
|
|
|
|
def test_an_edit_reaches_a_panel_only_once_it_is_published(store: DashboardStore):
|
|
"""A wall panel reads the published file; the editor writes beside it."""
|
|
published = store.write(default_dashboard("house"))
|
|
|
|
draft = store.write_draft(
|
|
published.model_copy(update={"title": "Kitchen"}), published.version
|
|
)
|
|
|
|
assert store.has_draft("house")
|
|
assert store.read("house").title == published.title
|
|
assert store.read("house", draft=True).title == "Kitchen"
|
|
|
|
store.publish("house", draft.version)
|
|
|
|
assert store.read("house").title == "Kitchen"
|
|
assert not store.has_draft("house")
|
|
|
|
|
|
def test_discarding_leaves_what_is_published(store: DashboardStore):
|
|
published = store.write(default_dashboard("house"))
|
|
store.write_draft(
|
|
published.model_copy(update={"title": "Kitchen"}), published.version
|
|
)
|
|
|
|
assert store.discard_draft("house").title == published.title
|
|
assert not store.has_draft("house")
|
|
|
|
|
|
def test_a_new_dashboard_is_a_draft_until_it_is_published(store: DashboardStore):
|
|
"""Creating one does not put it on a wall; publishing is what does."""
|
|
created = store.write_draft(default_dashboard("house"), 0)
|
|
|
|
assert not store.is_published("house")
|
|
with pytest.raises(DashboardNotFound):
|
|
store.read("house")
|
|
assert store.read("house", draft=True).title == created.title
|
|
# Listed all the same, so the editor can find what it just made.
|
|
assert [(d.name, d.has_draft, d.version) for d in store.list()] == [
|
|
("house", True, created.version)
|
|
]
|
|
|
|
store.publish("house", created.version)
|
|
|
|
assert store.is_published("house")
|
|
assert store.read("house").title == created.title
|
|
|
|
|
|
def test_an_unpublished_dashboard_can_be_renamed_and_deleted(store: DashboardStore):
|
|
"""And renaming it does not put it on a wall either."""
|
|
store.write_draft(default_dashboard("house"), 0)
|
|
|
|
store.rename("house", "home")
|
|
|
|
assert not store.exists("house")
|
|
assert store.has_draft("home") and not store.is_published("home")
|
|
|
|
store.delete("home")
|
|
assert not store.exists("home")
|
|
|
|
|
|
def test_deleting_and_renaming(store: DashboardStore):
|
|
store.write(default_dashboard("house"))
|
|
|
|
renamed = store.rename("house", "home")
|
|
assert renamed.name == "home"
|
|
assert not store.exists("house")
|
|
|
|
store.delete("home")
|
|
with pytest.raises(DashboardNotFound):
|
|
store.read("home")
|
|
|
|
|
|
def test_the_deepest_chart_decides_how_much_past_is_kept(store: DashboardStore):
|
|
store.write(
|
|
DashboardDef(
|
|
name="house",
|
|
pages=[
|
|
PageDef(
|
|
id="main",
|
|
sections=[
|
|
SectionDef(id="a", widgets=[chart("heating.temp", 400)]),
|
|
SectionDef(
|
|
id="b",
|
|
widgets=[
|
|
chart("heating.temp", 900),
|
|
chart("solar.watts", 100),
|
|
],
|
|
),
|
|
],
|
|
)
|
|
],
|
|
)
|
|
)
|
|
|
|
assert store.history_requirements() == {"heating.temp": 900, "solar.watts": 100}
|
|
|
|
|
|
def test_a_chart_cannot_ask_for_an_unbounded_series(store: DashboardStore):
|
|
store.write(
|
|
DashboardDef(
|
|
name="house",
|
|
pages=[
|
|
PageDef(
|
|
id="main",
|
|
sections=[
|
|
SectionDef(id="a", widgets=[chart("heating.temp", 10**9)])
|
|
],
|
|
)
|
|
],
|
|
)
|
|
)
|
|
|
|
assert store.history_requirements() == {"heating.temp": 5000}
|
|
|
|
|
|
def test_history_is_kept_to_the_depth_a_chart_asked_for():
|
|
state = MemoryState()
|
|
limits = {"f.temp": 300}
|
|
|
|
for i in range(400):
|
|
state.append_history({"f.temp": float(i)}, float(i), limits)
|
|
|
|
assert len(state.history("f.temp")) == 300
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# What an input widget does
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_publishing_a_value_runs_what_consumes_it():
|
|
"""A slider is a value arriving; the graph should not care who sent it."""
|
|
seen: list[float] = []
|
|
|
|
def consume(setpoint, params):
|
|
seen.append(setpoint)
|
|
return {"applied": setpoint}
|
|
|
|
node = Node(
|
|
f=consume,
|
|
requires=[MessageSpec(name="setpoint", port="setpoint", dtype=DType.FLOAT)],
|
|
provides=[MessageSpec(name="applied", port="applied", dtype=DType.FLOAT)],
|
|
name="thermostat",
|
|
)
|
|
node.assign_flow("heating", "thermostat")
|
|
state = MemoryState()
|
|
pipeline = Pipeline(nodes=[node], state=state)
|
|
|
|
pipeline.publish({"heating.setpoint": 21.5})
|
|
|
|
assert seen == [21.5]
|
|
assert state["heating.applied"] == 21.5
|
|
|
|
|
|
def test_publishing_nothing_does_nothing():
|
|
pipeline = Pipeline(nodes=[], state=MemoryState())
|
|
|
|
pipeline.publish({})
|
|
|
|
assert pipeline.values() == {}
|
|
|
|
|
|
def query_chart(**config) -> dict:
|
|
return {
|
|
"source": "query",
|
|
"request": "heating.chart_req",
|
|
"request_dtype": "record",
|
|
"message": "heating.chart_series",
|
|
"dtype": "series",
|
|
"refresh_s": 30,
|
|
**config,
|
|
}
|
|
|
|
|
|
def test_a_widget_refuses_a_dtype_it_cannot_carry():
|
|
WidgetDef(id="s", type="switch", config={"message": "a.b", "dtype": "bool"})
|
|
# A document written before the editor recorded types binds anything.
|
|
WidgetDef(id="s", type="switch", config={"message": "a.b"})
|
|
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(id="s", type="switch", config={"message": "a.b", "dtype": "float"})
|
|
|
|
|
|
def test_the_structured_widgets_bind_their_shapes():
|
|
WidgetDef(id="a", type="agenda", config={"message": "a.b", "dtype": "list"})
|
|
WidgetDef(id="n", type="notification", config={"message": "a.b", "dtype": "record"})
|
|
WidgetDef(id="f", type="forecast", config={"message": "a.b", "dtype": "list"})
|
|
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(id="a", type="agenda", config={"message": "a.b", "dtype": "json"})
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(
|
|
id="n", type="notification", config={"message": "a.b", "dtype": "list"}
|
|
)
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(id="f", type="forecast", config={"message": "a.b", "dtype": "json"})
|
|
|
|
|
|
def test_a_bar_nests_a_second_number():
|
|
WidgetDef(
|
|
id="b",
|
|
type="bar",
|
|
config={
|
|
"message": "a.in",
|
|
"dtype": "float",
|
|
"inner": "a.pv",
|
|
"inner_dtype": "int",
|
|
},
|
|
)
|
|
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(
|
|
id="b",
|
|
type="bar",
|
|
config={"message": "a.in", "dtype": "float", "inner_dtype": "bool"},
|
|
)
|
|
|
|
|
|
def test_a_bar_is_drawn_on_both_readings_it_nests():
|
|
widget = WidgetDef(id="b", type="bar", config={"message": "a.in", "inner": "a.pv"})
|
|
|
|
assert widget.messages == ["a.in", "a.pv"]
|
|
|
|
|
|
def test_a_clock_reads_nothing_and_publishes_nothing():
|
|
widget = WidgetDef(id="c", type="clock", config={"format": "24h"})
|
|
|
|
assert widget.messages == []
|
|
assert widget.target == ""
|
|
|
|
|
|
def test_a_querying_chart_asks_with_a_record_and_draws_a_series():
|
|
widget = WidgetDef(id="c", type="chart", config=query_chart())
|
|
|
|
# It publishes its request and reads the answer, so the canvas draws both.
|
|
assert widget.target == "heating.chart_req"
|
|
assert widget.messages == ["heating.chart_series"]
|
|
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(id="c", type="chart", config=query_chart(dtype="float"))
|
|
with pytest.raises(ValueError):
|
|
WidgetDef(id="c", type="chart", config=query_chart(request_dtype="json"))
|
|
|
|
|
|
def test_a_querying_chart_keeps_no_ring():
|
|
"""The answer carries its own past; a ring would store it twice."""
|
|
widget = WidgetDef(
|
|
id="c", type="chart", config=query_chart(history={"points": 900})
|
|
)
|
|
|
|
assert widget.history_points == 0
|
|
|
|
|
|
def test_a_dashboard_is_cut_into_a_sane_number_of_columns():
|
|
for columns in (0, 49):
|
|
with pytest.raises(ValueError):
|
|
DashboardDef(name="house", columns=columns)
|