Files
app/backend/tests/flow/test_library.py
T
stroblmeandClaude Opus 5 640654bd66 Rename the import package app to fluksio
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>
2026-08-21 21:48:05 +02:00

97 lines
3.0 KiB
Python

"""Nodes shared across flows: one source, many instances."""
from pathlib import Path
import pytest
from fluksio.flow.messages import MessageSpec
from fluksio.flow.schemas import FlowDef, NodeDef
from fluksio.flow.store import FlowStore, LibExists, LibNotFound
SOURCE = "def process():\n return {'temp': 1}\n"
@pytest.fixture
def store(tmp_path: Path) -> FlowStore:
return FlowStore(tmp_path / "flows")
def a_flow(name: str = "heating") -> FlowDef:
return FlowDef(
name=name,
nodes=[NodeDef(id="sensor", provides=[MessageSpec(name="temp")])],
)
def test_sharing_moves_the_source_and_points_the_node_at_it(store: FlowStore):
store.write_flow(a_flow())
store.write_node_source("heating", "sensor", SOURCE)
store.share_node("heating", "sensor", "read_temp")
assert store.list_lib() == ["read_temp"]
assert store.read_lib_source("read_temp") == SOURCE
node = store.read_flow("heating", draft=True).nodes[0]
assert node.source_ref == "read_temp"
# The private copy is gone; the library one is what it runs.
assert not (store.root / "heating" / "nodes" / "sensor.py").exists()
def test_the_library_is_not_mistaken_for_a_flow(store: FlowStore):
store.write_flow(a_flow())
store.write_node_source("heating", "sensor", SOURCE)
store.share_node("heating", "sensor", "read_temp")
assert store.list_flows() == ["heating"]
assert [flow.name for flow in store.read_all()] == ["heating"]
def test_a_second_flow_can_use_the_same_source(store: FlowStore):
store.write_flow(a_flow())
store.write_node_source("heating", "sensor", SOURCE)
store.share_node("heating", "sensor", "read_temp")
store.write_flow(
FlowDef(
name="cooling",
nodes=[
NodeDef(
id="sensor",
source_ref="read_temp",
provides=[MessageSpec(name="temp")],
)
],
)
)
assert store.usages("read_temp") == ["cooling.sensor", "heating.sensor"]
def test_a_shared_name_is_not_taken_twice(store: FlowStore):
store.write_flow(a_flow())
store.write_node_source("heating", "sensor", SOURCE)
store.share_node("heating", "sensor", "read_temp")
store.write_flow(FlowDef(name="cooling", nodes=[NodeDef(id="sensor")]))
with pytest.raises(LibExists):
store.share_node("cooling", "sensor", "read_temp")
def test_unsharing_takes_a_private_copy_back(store: FlowStore):
store.write_flow(a_flow())
store.write_node_source("heating", "sensor", SOURCE)
store.share_node("heating", "sensor", "read_temp")
store.unshare_node("heating", "sensor")
node = store.read_flow("heating", draft=True).nodes[0]
assert node.source_ref is None
assert store.read_node_source("heating", "sensor", draft=True) == SOURCE
# The library keeps its copy for whoever else is using it.
assert store.list_lib() == ["read_temp"]
def test_a_missing_shared_source_is_reported(store: FlowStore):
with pytest.raises(LibNotFound):
store.read_lib_source("nothing_here")