File size: 27,516 Bytes
8ca1978 9c77d16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | """
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
|