130 lines
4.8 KiB
Python
130 lines
4.8 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from pipewright import Graph, load_graph
|
|
from pipewright.check import check
|
|
from pipewright.errors import ConfigError
|
|
from pipewright.nodes.builtin import FilterExpr
|
|
from pipewright.patch import apply_ops, ops_schema
|
|
from pipewright.registry import registry
|
|
|
|
from conftest import FAKE
|
|
|
|
|
|
def small_graph() -> Graph:
|
|
g = Graph("t")
|
|
src = g.add("src", "source.inline", kind="issue", records=[{"id": "a", "body": "x" * 50}, {"id": "b", "body": "y"}])
|
|
f = g.add("long", FilterExpr.Params(expr="len(payload.body) > 10"), input=src)
|
|
g.add("lab", "llm.label", input=f, model="m", prompt="{{ payload.body }}",
|
|
labels={"c": {"type": "string", "enum": ["x", "y"]}})
|
|
g.add("out", "sink.jsonl", input="lab", path="out.jsonl")
|
|
return g
|
|
|
|
|
|
def codes(diags, level=None):
|
|
return [d.code for d in diags if level is None or d.level == level]
|
|
|
|
|
|
def test_builder_roundtrip(tmp_path):
|
|
g = small_graph()
|
|
g.save(tmp_path / "g.yaml")
|
|
g.save(tmp_path / "g.json")
|
|
a, b = load_graph(tmp_path / "g.yaml"), load_graph(tmp_path / "g.json")
|
|
assert a == b == g.to_spec()
|
|
assert a.nodes["lab"].inputs == {"in": ["long.out"]} or a.nodes["lab"].inputs == {"in": ["long"]}
|
|
|
|
|
|
def test_builder_validates_params_early():
|
|
g = Graph("t")
|
|
from pydantic import ValidationError
|
|
|
|
with pytest.raises(ValidationError):
|
|
g.add("x", "filter.expr", exprr="1")
|
|
|
|
|
|
def test_clean_graph_checks_ok(make_project):
|
|
proj = make_project(FAKE)
|
|
diags = check(small_graph().to_spec(), proj)
|
|
assert not codes(diags, "error"), [d.format() for d in diags]
|
|
assert "PW014" in codes(diags) # budget estimate
|
|
|
|
|
|
def test_check_finds_problems(make_project):
|
|
proj = make_project(FAKE)
|
|
spec = small_graph().to_spec()
|
|
spec.nodes["lab"].type = "llm.lable"
|
|
spec.nodes["out"].inputs = {"in": ["lab.outt"]}
|
|
spec.nodes["long"].params["expr"] = "len(payload.body) >"
|
|
diags = check(spec, proj)
|
|
msgs = {d.code: d for d in diags if d.level == "error"}
|
|
assert "llm.label" in msgs["PW001"].hint
|
|
assert msgs["PW002"].node == "long"
|
|
spec2 = small_graph().to_spec()
|
|
spec2.nodes["lab"].params["model"] = "mm"
|
|
spec2.nodes["out"].inputs = {"in": ["long.nope"]}
|
|
d2 = {d.code: d for d in check(spec2, proj) if d.level == "error"}
|
|
assert "PW011" in d2 and "'m'" in d2["PW011"].hint
|
|
assert "PW004" in d2 and "out" in d2["PW004"].hint
|
|
|
|
|
|
def test_check_kind_mismatch_cycle_and_required_input(make_project):
|
|
from pipewright.nodes.base import NodeType, Port
|
|
from pipewright.registry import register_node
|
|
|
|
@register_node
|
|
class OnlyTasks(NodeType):
|
|
name = "test.only_tasks"
|
|
kind = "map"
|
|
inputs = {"in": Port(kind="task")}
|
|
|
|
async def process(self, item, ctx):
|
|
return item
|
|
|
|
proj = make_project(FAKE)
|
|
spec = small_graph().to_spec()
|
|
spec.nodes["lab"].type = "test.only_tasks"
|
|
spec.nodes["lab"].params = {}
|
|
assert "PW007" in codes(check(spec, proj), "error")
|
|
spec = small_graph().to_spec()
|
|
spec.nodes["long"].inputs = {"in": ["lab"]}
|
|
assert "PW008" in codes(check(spec, proj), "error")
|
|
spec = small_graph().to_spec()
|
|
spec.nodes["long"].inputs = {}
|
|
assert "PW006" in codes(check(spec, proj), "error")
|
|
|
|
|
|
def test_patch_ops(make_project):
|
|
proj = make_project(FAKE)
|
|
spec = small_graph().to_spec()
|
|
new = apply_ops(spec, [
|
|
{"op": "add_node", "id": "sum", "type": "llm.generate", "inputs": {"in": "lab"},
|
|
"params": {"model": "m", "prompt": "{{ payload.body }}", "output_field": "s"}},
|
|
{"op": "disconnect", "from": "lab", "to": "out.in"},
|
|
{"op": "connect", "from": "sum", "to": "out"},
|
|
{"op": "set_param", "node": "long", "path": "expr", "value": "len(payload.body) > 5"},
|
|
{"op": "rename_node", "id": "long", "new_id": "longish"},
|
|
])
|
|
assert new.nodes["out"].inputs == {"in": ["sum.out"]}
|
|
assert new.nodes["lab"].inputs == {"in": ["longish.out"]}
|
|
assert not codes(check(new, proj), "error")
|
|
removed = apply_ops(new, [{"op": "remove_node", "id": "sum", "rewire": True}])
|
|
assert removed.nodes["out"].inputs == {"in": ["lab.out"]}
|
|
with pytest.raises(ConfigError):
|
|
apply_ops(spec, [{"op": "connect", "from": "ghost", "to": "out"}])
|
|
with pytest.raises(ConfigError):
|
|
apply_ops(spec, [{"op": "explode"}])
|
|
assert "oneOf" in json.dumps(ops_schema()) or "discriminator" in json.dumps(ops_schema())
|
|
|
|
|
|
def test_every_node_type_example_validates():
|
|
from pipewright.ops import nodes_describe
|
|
|
|
for name in registry.node_types.names():
|
|
cls = registry.node_types[name]
|
|
if name.startswith("test."):
|
|
continue
|
|
cls.Params.model_validate(cls.example)
|
|
d = nodes_describe(name)
|
|
assert d["params_schema"] and d["example_yaml"] and d["description"]
|