Skip to content

Image Pipeline Playground

test(backend/engine): test DAG execution ordering and caching

Mohammad Rajaei2df33d1ec78e
diff --git a/backend/tests/test_cache.py b/backend/tests/test_cache.py
new file mode 100644
index 0000000..f931c76
--- /dev/null
+++ b/backend/tests/test_cache.py
@@ -0,0 +1,31 @@
+"""Tests for content-addressed cache manager."""
+
+from __future__ import annotations
+
+import numpy as np
+
+from app.engine.cache import CacheManager
+
+
+def test_cache_put_get_roundtrip(tmp_path) -> None:
+ cache = CacheManager(tmp_path)
+ image = np.zeros((8, 8, 3), dtype=np.uint8)
+ image[:, :] = (10, 20, 30)
+ key = cache.make_key("blur", {"k": 3}, {"image": cache.hash_image(image)}, seed=1)
+ assert not cache.has(key)
+ cache.put(key, image, meta={"node": "blur"})
+ assert cache.has(key)
+ loaded = cache.get(key)
+ assert loaded is not None
+ assert loaded.shape == image.shape
+ assert np.array_equal(loaded, image)
+
+
+def test_cache_key_changes_with_params(tmp_path) -> None:
+ cache = CacheManager(tmp_path)
+ inputs = {"image": "abc"}
+ key_a = cache.make_key("blur", {"ksize": 3}, inputs, seed=0)
+ key_b = cache.make_key("blur", {"ksize": 5}, inputs, seed=0)
+ key_c = cache.make_key("blur", {"ksize": 3}, inputs, seed=1)
+ assert key_a != key_b
+ assert key_a != key_c
diff --git a/backend/tests/test_executor.py b/backend/tests/test_executor.py
new file mode 100644
index 0000000..bb756c0
--- /dev/null
+++ b/backend/tests/test_executor.py
@@ -0,0 +1,150 @@
+"""Tests for DAG topological sort and cached execution."""
+
+from __future__ import annotations
+
+from typing import Any
+
+import numpy as np
+import pytest
+
+from app.engine.executor import DagExecutor, DagValidationError, topological_sort, validate_graph
+from app.engine.registry import BaseNode, NodeRegistry
+from app.models.graph import (
+ Edge,
+ ExecuteRequest,
+ Graph,
+ NodeInstance,
+ ParamField,
+ PortDirection,
+ PortSpec,
+)
+
+
+class _SourceNode(BaseNode):
+ type = "source"
+ label = "Source"
+ category = "test"
+ ports = [PortSpec(id="image", name="Image", direction=PortDirection.OUTPUT)]
+ params = [ParamField(name="value", label="Value", type="number", default=1)]
+
+ def execute(
+ self,
+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],
+ params: dict[str, Any],
+ seed: int = 0,
+ ) -> dict[str, np.ndarray | list[np.ndarray]]:
+ image = np.full((4, 4), int(params["value"]) + seed, dtype=np.uint8)
+ return {"image": image}
+
+ def emit_python(
+ self,
+ node_id: str,
+ params: dict[str, Any],
+ input_vars: dict[str, str],
+ output_vars: dict[str, str],
+ ) -> list[str]:
+ return [f"{output_vars['image']} = np.full((4, 4), {params['value']})"]
+
+
+class _AddNode(BaseNode):
+ type = "add"
+ label = "Add"
+ category = "test"
+ ports = [
+ PortSpec(id="image", name="Image", direction=PortDirection.INPUT),
+ PortSpec(id="image", name="Image", direction=PortDirection.OUTPUT),
+ ]
+ params = [ParamField(name="delta", label="Delta", type="number", default=1)]
+
+ def execute(
+ self,
+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],
+ params: dict[str, Any],
+ seed: int = 0,
+ ) -> dict[str, np.ndarray | list[np.ndarray]]:
+ image = inputs["image"]
+ assert isinstance(image, np.ndarray)
+ return {"image": image + int(params["delta"])}
+
+ def emit_python(
+ self,
+ node_id: str,
+ params: dict[str, Any],
+ input_vars: dict[str, str],
+ output_vars: dict[str, str],
+ ) -> list[str]:
+ return [f"{output_vars['image']} = {input_vars['image']} + {params['delta']}"]
+
+
+def _registry() -> NodeRegistry:
+ reg = NodeRegistry()
+ reg.register(_SourceNode())
+ reg.register(_AddNode())
+ return reg
+
+
+def test_topological_sort_order() -> None:
+ graph = Graph(
+ nodes=[
+ NodeInstance(id="a", type="source"),
+ NodeInstance(id="b", type="add"),
+ NodeInstance(id="c", type="add"),
+ ],
+ edges=[
+ Edge(id="e1", source="a", target="b"),
+ Edge(id="e2", source="b", target="c"),
+ ],
+ )
+ assert topological_sort(graph) == ["a", "b", "c"]
+
+
+def test_cycle_detection() -> None:
+ graph = Graph(
+ nodes=[
+ NodeInstance(id="a", type="source"),
+ NodeInstance(id="b", type="add"),
+ ],
+ edges=[
+ Edge(id="e1", source="a", target="b"),
+ Edge(id="e2", source="b", target="a"),
+ ],
+ )
+ with pytest.raises(DagValidationError, match="cycle"):
+ topological_sort(graph)
+
+
+def test_unknown_node_validation() -> None:
+ graph = Graph(nodes=[NodeInstance(id="a", type="missing")], edges=[])
+ with pytest.raises(DagValidationError, match="Unknown node type"):
+ validate_graph(graph, _registry())
+
+
+def test_executor_order_and_cache(tmp_path) -> None:
+ reg = _registry()
+ executor = DagExecutor(tmp_path, node_registry=reg)
+ request = ExecuteRequest(
+ graph=Graph(
+ nodes=[
+ NodeInstance(id="a", type="source", params={"value": 5}),
+ NodeInstance(id="b", type="add", params={"delta": 2}),
+ ],
+ edges=[Edge(id="e1", source="a", target="b")],
+ ),
+ seed=0,
+ sample_count=1,
+ cache=True,
+ )
+ events: list[str] = []
+ first = executor.execute(request, on_event=lambda event: events.append(event.type.value))
+ assert first["order"] == ["a", "b"]
+ assert "done" in events
+ assert "preview" in events
+
+ second_events: list[bool | None] = []
+ executor.execute(
+ request,
+ on_event=lambda event: second_events.append(event.cache_hit)
+ if event.type.value == "preview"
+ else None,
+ )
+ assert any(hit is True for hit in second_events)
diff --git a/backend/tests/test_registry.py b/backend/tests/test_registry.py
index 38b2d6f..73069a1 100644
--- a/backend/tests/test_registry.py
+++ b/backend/tests/test_registry.py
@@ -20,7 +20,14 @@ class _PassthroughNode(BaseNode):
PortSpec(id="image", name="Image", direction=PortDirection.OUTPUT),
]
params = [
- ParamField(name="gain", label="Gain", type="number", default=1.0, minimum=0.0, maximum=10.0),
+ ParamField(
+ name="gain",
+ label="Gain",
+ type="number",
+ default=1.0,
+ minimum=0.0,
+ maximum=10.0,
+ ),
]
def execute(