TraceStudio-dev/server/tests/test_executor_and_cache.py

64 lines
1.6 KiB
Python
Raw Permalink Normal View History

import asyncio
import time
2026-01-10 19:08:49 +08:00
import sys
from pathlib import Path
2026-01-10 19:08:49 +08:00
# Ensure server root is on sys.path so `app` package can be imported when running tests directly
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
2026-01-10 19:08:49 +08:00
from app.core.workflow_executor import WorkflowExecutor
from app.core.node_registry import register_node, NodeRegistry
from app.core.node_base import TraceNode, NodeType, CachePolicy
2026-01-10 19:08:49 +08:00
# Simple test node
@register_node
class AddNode(TraceNode):
CATEGORY = "Math/Basic"
DISPLAY_NAME = "Add"
NODE_TYPE = NodeType.NORMAL
CACHE_POLICY = CachePolicy.MEMORY
2026-01-10 19:08:49 +08:00
InputSpec = {
"a": ("Number", {"required": True}),
"b": ("Number", {"required": True})
}
OutputSpec = {
"result": ("Number", {})
}
2026-01-10 19:08:49 +08:00
def process(self, inputs, context=None):
a = inputs.get("a", 0)
b = inputs.get("b", 0)
return {"result": a + b}
2026-01-10 19:08:49 +08:00
async def run_executor_test():
executor = WorkflowExecutor(user_id="test_user")
nodes = [
2026-01-10 19:08:49 +08:00
{"id": "n1", "type": "AddNode", "class_name": "AddNode", "params": {}},
]
2026-01-10 19:08:49 +08:00
edges = []
2026-01-10 19:08:49 +08:00
success, report = await executor.execute(nodes=nodes, edges=edges, global_context={})
print("success:", success)
print("report:", report)
2026-01-10 19:08:49 +08:00
assert success is True
assert "nodes" in report
assert "n1" in report["nodes"]
n1 = report["nodes"]["n1"]
assert n1["status"] == "success"
assert "outputs" in n1
assert "result" in n1["outputs"] or isinstance(n1["outputs"], dict)
2026-01-10 19:08:49 +08:00
def test_executor():
asyncio.run(run_executor_test())
if __name__ == "__main__":
2026-01-10 19:08:49 +08:00
asyncio.run(run_executor_test())