auto / tests /test_phase7_codegen.py
rsnarsna
fix(codegen): remediate phase 7 review findings with exact package pinning, ast safety visitor, control-flow parity, and input disambiguation
9c77d16
Raw History Blame Contribute Delete
27.5 kB
"""
ALLI Phase 7: Python Code Generation Acceptance Test Suite
Verifies:
1. Empty graphs and minimal valid graphs.
2. Connected Input -> math.sqrt -> Output pipeline with AST validity and execution parity.
3. Multiple dependencies and deterministic topological ordering.
4. Numeric widening and supported type conversions.
5. String escaping, Unicode, collections, and literal serialization.
6. Invalid connections, missing required parameters, and incompatible types (fail-closed).
7. Unsupported node types, non-finite values, and banned module imports.
8. Cycle detection (DAG-001) and duplicate node IDs (VAL-DUPLICATE-NODE-ID).
9. Stable, deterministic output for repeated generations (SHA-256 parity).
10. Graph mutation and fingerprint change tracking.
11. Python syntax validation (ast.parse) without code execution.
12. FastAPI endpoint parity across /codegen, /api/codegen, /graph/codegen, /api/graph/codegen.
"""
import ast
import json
import math
import subprocess
import sys
import pytest
from fastapi.testclient import TestClient
from main import app
from language_core.codegen_contracts import (
STANDARD_LIBS,
BANNED_IMPORTS,
PINNED_PACKAGE_VERSIONS,
format_literal,
sanitize_identifier,
compute_codegen_hash,
validate_generated_ast,
)
from lambdas.codegen_handler import generate_code_logic
@pytest.fixture
def client():
return TestClient(app)
# ---------------------------------------------------------------------------
# 1. Empty graphs and minimal valid graphs
# ---------------------------------------------------------------------------
def test_empty_workflow_generation():
"""Empty graph generates clean PEP 8 template, valid AST, and Dockerfile."""
res = generate_code_logic(nodes=[], edges=[], graph_name="Empty Test")
assert res["success"] is True
assert "def main():" in res["python_code"]
assert 'if __name__ == "__main__":' in res["python_code"]
assert "return None" in res["python_code"]
assert "FROM python:3.12-slim" in res["dockerfile"]
assert "USER appuser" in res["dockerfile"]
assert res["dependencies"] == []
assert res["execution_order"] == []
# AST validation
tree = ast.parse(res["python_code"])
assert isinstance(tree, ast.Module)
def test_minimal_single_node_generation():
"""Minimal single constant integer node."""
nodes = [
{"id": "c1", "node_type": "constant.int", "data": {"literal_value": 42, "const_name": "answer"}}
]
res = generate_code_logic(nodes=nodes, edges=[], graph_name="Single Node")
assert res["success"] is True
assert "answer = 42" in res["python_code"]
assert "return answer" in res["python_code"]
tree = ast.parse(res["python_code"])
assert isinstance(tree, ast.Module)
# ---------------------------------------------------------------------------
# 2. Connected Input -> math.sqrt -> Output graph
# ---------------------------------------------------------------------------
def test_input_math_sqrt_output_pipeline():
"""Connected Input (49.0) -> math.sqrt -> Print with execution parity."""
nodes = [
{"id": "n_in", "node_type": "constant.float", "data": {"literal_value": 49.0, "const_name": "input_val"}},
{"id": "n_sqrt", "node_type": "math.sqrt", "data": {"rounding": "none"}},
{"id": "n_out", "node_type": "io.print", "data": {"prefix": "Result: "}},
]
edges = [
{"source": "n_in", "target": "n_sqrt", "sourceHandle": "val", "targetHandle": "x"},
{"source": "n_sqrt", "target": "n_out", "sourceHandle": "result", "targetHandle": "value"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Sqrt Pipeline")
assert res["success"] is True
code = res["python_code"]
# Verify AST parses cleanly
tree = ast.parse(code)
assert isinstance(tree, ast.Module)
# Standard lib import verification: math must be imported, but not in requirements.txt
assert "import math" in code
assert "math" not in res["requirements_txt"]
# Run standalone Python via subprocess to verify observable output
proc = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)
assert proc.returncode == 0, f"Execution failed:\n{proc.stderr}\nCode:\n{code}"
assert "Result: 7.0" in proc.stdout.strip()
# ---------------------------------------------------------------------------
# 3. Multiple dependencies and deterministic ordering
# ---------------------------------------------------------------------------
def test_multi_dependency_diamond_dag_ordering():
"""Diamond DAG: Root -> (BranchA, BranchB) -> Merge -> Print."""
nodes = [
{"id": "root", "node_type": "constant.int", "data": {"literal_value": 10, "const_name": "x"}},
{"id": "branch_a", "node_type": "core.binary_op", "data": {"operator": "+", "b": 5}},
{"id": "branch_b", "node_type": "core.binary_op", "data": {"operator": "*", "b": 2}},
{"id": "merge", "node_type": "core.binary_op", "data": {"operator": "+"}},
{"id": "out", "node_type": "io.print", "data": {}},
]
edges = [
{"source": "root", "target": "branch_a", "sourceHandle": "out", "targetHandle": "a"},
{"source": "root", "target": "branch_b", "sourceHandle": "out", "targetHandle": "a"},
{"source": "branch_a", "target": "merge", "sourceHandle": "out", "targetHandle": "a"},
{"source": "branch_b", "target": "merge", "sourceHandle": "out", "targetHandle": "b"},
{"source": "merge", "target": "out", "sourceHandle": "out", "targetHandle": "value"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Diamond Flow")
assert res["success"] is True
order = res["execution_order"]
# Root must precede branches; branches must precede merge; merge must precede out
assert order.index("root") < order.index("branch_a")
assert order.index("root") < order.index("branch_b")
assert order.index("branch_a") < order.index("merge")
assert order.index("branch_b") < order.index("merge")
assert order.index("merge") < order.index("out")
# Verify execution parity ((10 + 5) + (10 * 2) = 15 + 20 = 35)
code = res["python_code"]
proc = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)
assert proc.returncode == 0
assert proc.stdout.strip() == "35"
# ---------------------------------------------------------------------------
# 4. Numeric widening and supported type conversions
# ---------------------------------------------------------------------------
def test_numeric_widening_and_type_casting():
"""Integer feeding into float cast and string conversion."""
nodes = [
{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 100, "const_name": "raw_int"}},
{"id": "n2", "node_type": "core.type_cast", "data": {"target_type": "float"}},
{"id": "n3", "node_type": "core.type_cast", "data": {"target_type": "str"}},
{"id": "n4", "node_type": "io.print", "data": {}},
]
edges = [
{"source": "n1", "target": "n2", "sourceHandle": "out", "targetHandle": "value"},
{"source": "n2", "target": "n3", "sourceHandle": "out", "targetHandle": "value"},
{"source": "n3", "target": "n4", "sourceHandle": "out", "targetHandle": "value"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Widening Test")
assert res["success"] is True
code = res["python_code"]
assert "float(" in code
assert "str(" in code
proc = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)
assert proc.returncode == 0
assert proc.stdout.strip() == "100.0"
# ---------------------------------------------------------------------------
# 5. String escaping, Unicode, collections, and literal serialization
# ---------------------------------------------------------------------------
def test_string_escaping_and_unicode_literals():
"""Handles double quotes, newlines, tabs, and Unicode without syntax errors."""
complex_str = 'Special "quotes", \nnewline, \ttab, and Unicode: 🚀 こんにちは'
nodes = [
{"id": "n_str", "node_type": "constant.str", "data": {"literal_value": complex_str, "const_name": "msg"}},
{"id": "n_out", "node_type": "io.print", "data": {}},
]
edges = [
{"source": "n_str", "target": "n_out", "sourceHandle": "out", "targetHandle": "value"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Unicode Test")
assert res["success"] is True
code = res["python_code"]
tree = ast.parse(code)
assert isinstance(tree, ast.Module)
import os
proc = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
encoding="utf-8",
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
)
assert proc.returncode == 0
assert "🚀 こんにちは" in proc.stdout
def test_collections_synthesis():
"""Lists, dicts, tuples, and sets correctly synthesized into valid Python."""
nodes = [
{"id": "n_list", "node_type": "data.list", "data": {"initial_items": "[1, 2, 3]"}},
{"id": "n_dict", "node_type": "data.dict", "data": {"initial_dict": '{"alpha": 1, "beta": 2}'}},
{"id": "n_tuple", "node_type": "data.tuple", "data": {"item_1": 10, "item_2": 20, "item_3": 30}},
{"id": "n_set", "node_type": "data.set", "data": {"initial_elements": "[1, 2, 2, 3]"}},
]
res = generate_code_logic(nodes=nodes, edges=[], graph_name="Collections Flow")
assert res["success"] is True
code = res["python_code"]
tree = ast.parse(code)
assert isinstance(tree, ast.Module)
assert "list_" in code
assert "dict_" in code
assert "tuple_" in code
assert "set_" in code
# ---------------------------------------------------------------------------
# 6. Invalid connections, missing required parameters, and incompatible types
# ---------------------------------------------------------------------------
def test_incompatible_port_types_fails_closed():
"""Incompatible type wiring (string output into float target) fails closed with TYPE-001."""
nodes = [
{"id": "n1", "node_type": "constant.str", "data": {"literal_value": "not_a_number"}},
{"id": "n2", "node_type": "math.sqrt", "data": {}},
]
edges = [
{"source": "n1", "target": "n2", "sourceHandle": "val", "targetHandle": "x"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Incompatible Types", validate=True)
assert res["success"] is False
assert res["python_code"] == ""
diags = res["diagnostics"]
assert any(d.get("code") in ("TYPE-001", "VAL-TYPE-MISMATCH") for d in diags)
def test_missing_required_parameter_in_strict_mode():
"""Missing required parameter in strict validation mode fails closed."""
graph = {
"name": "Missing Required",
"nodes": [
{"id": "n_cmp", "node_type": "core.compare", "data": {"operator": "=="}},
],
"edges": [],
}
res = generate_code_logic(graph=graph, validate=True, strict_validation=True)
assert res["success"] is False
assert res["python_code"] == ""
assert any("REQUIRED" in d.get("code", "") for d in res["diagnostics"])
# ---------------------------------------------------------------------------
# 7. Unsupported node types, non-finite values, and banned module imports
# ---------------------------------------------------------------------------
def test_banned_module_import_rejected():
"""Attempting to import internal ALLI categories produces diagnostic error."""
nodes = [
{"id": "n1", "node_type": "core.banned_submodule.run", "data": {}},
]
res = generate_code_logic(nodes=nodes, edges=[], graph_name="Banned Import")
code = res["python_code"]
assert "import core" not in code
assert "import language_core" not in code
def test_non_finite_float_rejected():
"""format_literal raises ValueError on inf or nan."""
with pytest.raises(ValueError, match="Non-finite float values are not supported"):
format_literal(float("inf"), "float")
with pytest.raises(ValueError, match="Non-finite float values are not supported"):
format_literal(float("nan"), "float")
# ---------------------------------------------------------------------------
# 8. Cycles and invalid graph structures
# ---------------------------------------------------------------------------
def test_cycle_detection_fails_closed():
"""Direct cyclic feedback fails closed with DAG-001."""
nodes = [
{"id": "n1", "node_type": "core.variable", "data": {"var_name": "a"}},
{"id": "n2", "node_type": "core.variable", "data": {"var_name": "b"}},
]
edges = [
{"source": "n1", "target": "n2", "sourceHandle": "val", "targetHandle": "in"},
{"source": "n2", "target": "n1", "sourceHandle": "val", "targetHandle": "in"},
]
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Cyclic Flow", validate=True)
assert res["success"] is False
assert res["python_code"] == ""
diags = res["diagnostics"]
assert any(d.get("code") == "DAG-001" for d in diags)
def test_duplicate_node_ids_fails_closed():
"""Duplicate node identifiers fail closed with VAL-DUPLICATE-NODE-ID."""
nodes = [
{"id": "dup_node", "node_type": "constant.int", "data": {"literal_value": 1}},
{"id": "dup_node", "node_type": "constant.int", "data": {"literal_value": 2}},
]
res = generate_code_logic(nodes=nodes, edges=[], graph_name="Duplicate IDs")
assert res["success"] is False
assert res["python_code"] == ""
assert any(d.get("code") == "VAL-DUPLICATE-NODE-ID" for d in res["diagnostics"])
# ---------------------------------------------------------------------------
# 9. Stable output for repeated generation (Deterministic SHA-256 Parity)
# ---------------------------------------------------------------------------
def test_deterministic_output_stability():
"""Consecutive generations of the same graph produce byte-identical code and identical hash."""
nodes = [
{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 25, "const_name": "val_a"}},
{"id": "n2", "node_type": "math.sqrt", "data": {"rounding": "none"}},
{"id": "n3", "node_type": "io.print", "data": {"prefix": "Ans: "}},
]
edges = [
{"source": "n1", "target": "n2", "sourceHandle": "val", "targetHandle": "x"},
{"source": "n2", "target": "n3", "sourceHandle": "result", "targetHandle": "value"},
]
res1 = generate_code_logic(nodes=nodes, edges=edges, graph_name="Stable Flow")
res2 = generate_code_logic(nodes=nodes, edges=edges, graph_name="Stable Flow")
assert res1["python_code"] == res2["python_code"]
assert res1["metadata"]["hash"] == res2["metadata"]["hash"]
assert len(res1["metadata"]["hash"]) == 64
# ---------------------------------------------------------------------------
# 10. Graph mutation and stale generated-code detection
# ---------------------------------------------------------------------------
def test_graph_mutation_fingerprint_change():
"""Mutating graph node values changes fingerprint and artifact hash."""
nodes_v1 = [
{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 10}},
]
nodes_v2 = [
{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 20}},
]
res1 = generate_code_logic(nodes=nodes_v1, edges=[], graph_name="Mutation Flow")
res2 = generate_code_logic(nodes=nodes_v2, edges=[], graph_name="Mutation Flow")
assert res1["graph_fingerprint"] != res2["graph_fingerprint"]
assert res1["metadata"]["hash"] != res2["metadata"]["hash"]
# ---------------------------------------------------------------------------
# 11. Python syntax validation (ast.parse)
# ---------------------------------------------------------------------------
def test_validate_generated_ast_utility():
"""Validates that ast syntax checker reports clean syntax vs syntax error."""
valid_code = "def main():\n x = 10\n return x\n"
is_valid, err = validate_generated_ast(valid_code)
assert is_valid is True
assert err is None
invalid_code = "def main():\n x = (10 +\n"
is_valid, err = validate_generated_ast(invalid_code)
assert is_valid is False
assert "SyntaxError" in err
# ---------------------------------------------------------------------------
# 12. FastAPI Endpoints Parity (/codegen, /api/codegen, /graph/codegen, /api/graph/codegen)
# ---------------------------------------------------------------------------
def test_fastapi_endpoints_parity(client):
"""Verifies that all 4 codegen route paths accept CodegenRequest and return identical artifacts."""
payload = {
"name": "Endpoint Parity",
"nodes": [
{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 49}},
{"id": "n2", "node_type": "math.sqrt", "data": {}},
],
"edges": [
{"source": "n1", "target": "n2", "sourceHandle": "val", "targetHandle": "x"},
],
}
endpoints = ["/codegen", "/api/codegen", "/graph/codegen", "/api/graph/codegen"]
results = []
for ep in endpoints:
resp = client.post(ep, json=payload)
assert resp.status_code == 200, f"Endpoint {ep} failed with status {resp.status_code}"
data = resp.json()
assert data["success"] is True
assert "math.sqrt" in data["python_code"]
assert len(data["metadata"]["hash"]) == 64
results.append(data["metadata"]["hash"])
# All 4 endpoints return byte-identical artifact hashes
assert len(set(results)) == 1, "All 4 codegen endpoints must return identical artifact hashes"
# ---------------------------------------------------------------------------
# 13. Exact Pinned Package Versions (Review Issue 1)
# ---------------------------------------------------------------------------
def test_exact_pinned_versions():
"""External packages are pinned to exact versions when pin_exact=True."""
res = generate_code_logic(
nodes=[{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 1}}],
edges=[],
dependencies=["requests", "pandas", "numpy", "custom_pkg"],
pin_exact=True,
graph_name="Pinned Versions"
)
reqs = res["requirements_txt"]
assert "requests==2.31.0" in reqs
assert "pandas==2.2.2" in reqs
assert "numpy==1.26.4" in reqs
assert "custom_pkg==0.1.0" in reqs
def test_flexible_pinned_versions():
"""Unknown packages use >= when pin_exact=False."""
res = generate_code_logic(
nodes=[{"id": "n1", "node_type": "constant.int", "data": {"literal_value": 1}}],
edges=[],
dependencies=["custom_pkg"],
pin_exact=False,
graph_name="Flex Versions"
)
reqs = res["requirements_txt"]
assert "custom_pkg>=0.1.0" in reqs
# ---------------------------------------------------------------------------
# 14. AST Security Allowlist / Denylist Inspection (Review Issue 3)
# ---------------------------------------------------------------------------
def test_ast_security_blocks_eval_and_exec():
"""Security AST visitor blocks eval, exec, and compile callables."""
from language_core.codegen_contracts import validate_generated_ast, inspect_generated_ast_security
code_eval = "def main():\n eval('2+2')\n return None\n"
is_safe, err = validate_generated_ast(code_eval, check_security=True)
assert is_safe is False
assert "CODEGEN-SECURITY-VIOLATION" in err
code_exec = "def main():\n exec('import sys')\n return None\n"
is_safe, err = validate_generated_ast(code_exec, check_security=True)
assert is_safe is False
diags = inspect_generated_ast_security(code_eval)
assert len(diags) > 0
assert diags[0]["code"] == "CODEGEN-SECURITY-VIOLATION"
def test_ast_security_blocks_os_and_subprocess_arbitrary_commands():
"""Security AST visitor blocks os.system, os.popen, subprocess.Popen."""
from language_core.codegen_contracts import validate_generated_ast
code_os = "import os\ndef main():\n os.system('id')\n return None\n"
is_safe, err = validate_generated_ast(code_os, check_security=True)
assert is_safe is False
assert "CODEGEN-SECURITY-VIOLATION" in err
code_sub = "import subprocess\ndef main():\n subprocess.Popen(['ls'])\n return None\n"
is_safe, err = validate_generated_ast(code_sub, check_security=True)
assert is_safe is False
def test_ast_security_blocks_dunder_introspection_tampering():
"""Security AST visitor blocks access to __subclasses__ and __builtins__."""
from language_core.codegen_contracts import validate_generated_ast
code_dunder = "def main():\n x = ().__class__.__bases__[0].__subclasses__()\n return x\n"
is_safe, err = validate_generated_ast(code_dunder, check_security=True)
assert is_safe is False
assert "CODEGEN-SECURITY-VIOLATION" in err
# ---------------------------------------------------------------------------
# 15. Control-Flow Adversarial Semantic Parity (Review Issue 2)
# ---------------------------------------------------------------------------
def test_control_flow_try_except_parity():
"""Try/except node falls back cleanly on error with exact output."""
from language_core.workflow_executor import execute_workflow_graph
try_nodes = [
{"id": "n_safe", "node_type": "constant.int", "data": {"label": "Fallback 42", "value": 42}},
{"id": "n_try", "node_type": "control.try_except", "data": {"label": "Try Block", "default_fallback": "42"}},
{"id": "n_print", "node_type": "io.print", "data": {"label": "Print Caught", "prefix": "SAFE: "}},
]
try_edges = [
{"source": "n_safe", "target": "n_try", "sourceHandle": "val", "targetHandle": "fallback_val"},
{"source": "n_try", "target": "n_print", "sourceHandle": "result", "targetHandle": "value"},
]
res_vis = execute_workflow_graph({"name": "Try Flow", "nodes": try_nodes, "edges": try_edges})
assert res_vis["stdout"].strip() == "SAFE: 42"
res_code = generate_code_logic(nodes=try_nodes, edges=try_edges, graph_name="Try Flow")
assert res_code["success"] is True
sub_res = subprocess.run([sys.executable, "-c", res_code["python_code"]], capture_output=True, text=True)
assert sub_res.returncode == 0
assert sub_res.stdout.strip() == "SAFE: 42"
def test_control_flow_branch_scoped_variables_downstream():
"""Variables defined in if/else branches are correctly consumed downstream."""
from language_core.workflow_executor import execute_workflow_graph
branch_nodes = [
{"id": "n_cond", "node_type": "constant.bool", "data": {"label": "Cond", "value": True}},
{"id": "n_val_true", "node_type": "constant.int", "data": {"label": "50", "value": 50}},
{"id": "n_val_false", "node_type": "constant.int", "data": {"label": "5", "value": 5}},
{"id": "n_if", "node_type": "control.if_else", "data": {"label": "If Node"}},
{"id": "n_mult_two", "node_type": "constant.int", "data": {"label": "Two", "value": 2}},
{"id": "n_mult", "node_type": "core.binary_op", "data": {"label": "Double", "operator": "*"}},
{"id": "n_out", "node_type": "io.print", "data": {"label": "Print Out", "prefix": "FINAL: "}},
]
branch_edges = [
{"source": "n_cond", "target": "n_if", "sourceHandle": "val", "targetHandle": "condition"},
{"source": "n_val_true", "target": "n_if", "sourceHandle": "val", "targetHandle": "then_val"},
{"source": "n_val_false", "target": "n_if", "sourceHandle": "val", "targetHandle": "else_val"},
{"source": "n_if", "target": "n_mult", "sourceHandle": "result", "targetHandle": "a"},
{"source": "n_mult_two", "target": "n_mult", "sourceHandle": "val", "targetHandle": "b"},
{"source": "n_mult", "target": "n_out", "sourceHandle": "result", "targetHandle": "value"},
]
vis_res = execute_workflow_graph({"name": "Branch Flow", "nodes": branch_nodes, "edges": branch_edges})
assert vis_res["stdout"].strip() == "FINAL: 100"
code_res = generate_code_logic(nodes=branch_nodes, edges=branch_edges, graph_name="Branch Flow")
assert code_res["success"] is True
sub_res = subprocess.run([sys.executable, "-c", code_res["python_code"]], capture_output=True, text=True)
assert sub_res.returncode == 0
assert sub_res.stdout.strip() == "FINAL: 100"
# ---------------------------------------------------------------------------
# 16. Input/Output Disambiguation (Review Issue 5)
# ---------------------------------------------------------------------------
def test_input_disambiguation_case_a_literal():
"""Case A: Literal Constant Number (49.0) -> Math Sqrt -> Print."""
from language_core.workflow_executor import execute_workflow_graph
nodes = [
{"id": "c_val", "node_type": "constant.float", "data": {"label": "Literal 49.0", "value": 49.0}},
{"id": "c_sqrt", "node_type": "math.sqrt", "data": {"label": "Sqrt"}},
{"id": "c_out", "node_type": "io.print", "data": {"label": "Print", "prefix": "Result: "}},
]
edges = [
{"source": "c_val", "target": "c_sqrt", "sourceHandle": "val", "targetHandle": "x"},
{"source": "c_sqrt", "target": "c_out", "sourceHandle": "result", "targetHandle": "value"},
]
vis = execute_workflow_graph({"name": "Case A", "nodes": nodes, "edges": edges})
assert vis["stdout"].strip() == "Result: 7.0"
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Case A")
sub = subprocess.run([sys.executable, "-c", res["python_code"]], capture_output=True, text=True)
assert sub.returncode == 0
assert sub.stdout.strip() == "Result: 7.0"
def test_input_disambiguation_case_b_interactive_stdin():
"""Case B: Interactive IO Input -> Type Cast -> Math Sqrt -> Print (via stdin)."""
from language_core.workflow_executor import execute_workflow_graph
nodes = [
{"id": "in_user", "node_type": "io.input", "data": {"label": "User Input", "prompt": "Enter float: ", "default_value": "49.0", "var_name": "user_raw"}},
{"id": "cast_node", "node_type": "core.type_cast", "data": {"label": "Cast to Float", "target_type": "float"}},
{"id": "b_sqrt", "node_type": "math.sqrt", "data": {"label": "Sqrt"}},
{"id": "b_out", "node_type": "io.print", "data": {"label": "Print", "prefix": "Result: "}},
]
edges = [
{"source": "in_user", "target": "cast_node", "sourceHandle": "result", "targetHandle": "value"},
{"source": "cast_node", "target": "b_sqrt", "sourceHandle": "result", "targetHandle": "x"},
{"source": "b_sqrt", "target": "b_out", "sourceHandle": "result", "targetHandle": "value"},
]
vis = execute_workflow_graph({"name": "Case B", "nodes": nodes, "edges": edges})
assert vis["stdout"].strip() == "Result: 7.0"
res = generate_code_logic(nodes=nodes, edges=edges, graph_name="Case B")
sub = subprocess.run(
[sys.executable, "-c", res["python_code"]],
input="49.0\n",
capture_output=True,
text=True
)
assert sub.returncode == 0
assert "Result: 7.0" in sub.stdout