rsnarsna
fix(codegen): remediate phase 7 review findings with exact package pinning, ast safety visitor, control-flow parity, and input disambiguation
9c77d16 Download tests/test_codegen_parity.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 26 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_codegen_parity.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/tests/test_codegen_parity.py
-
curl -L -o test_codegen_parity.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_codegen_parity.py
26 kB
| """ | |
| ALLI Phase 7: Python Code Generation & Execution Parity Test Suite | |
| Verifies: | |
| - Step 7.1: Code-generation contract (valid script, requirements.txt, Dockerfile) | |
| - Step 7.2: Deterministic topological ordering | |
| - Step 7.3: Schema-driven standard node templates | |
| - Step 7.4: Clean PEP 8 Python syntax (validated with ast.parse) | |
| - Step 7.5: Dependency isolation (standard libraries excluded from requirements.txt) | |
| - Step 7.6: Execution parity: Standalone execution of generated Python produces | |
| the EXACT SAME outputs as the visual execution engine. | |
| - Step 7.7: Container reproducibility (Python 3.12-slim Dockerfile) | |
| """ | |
| import sys | |
| import ast | |
| import subprocess | |
| from pathlib import Path | |
| # Add backend root directory to sys.path | |
| sys.path.insert(0, str(Path(__file__).parent.parent)) | |
| from lambdas.codegen_handler import generate_code_logic | |
| from language_core.workflow_executor import execute_workflow_graph | |
| def run_tests(): | |
| print("=" * 75) | |
| print("ALLI PHASE 7: PYTHON CODE GENERATION & EXECUTION PARITY TEST SUITE") | |
| print("=" * 75) | |
| passed = 0 | |
| total = 0 | |
| def assert_true(condition: bool, msg: str = ""): | |
| nonlocal passed, total | |
| total += 1 | |
| if not condition: | |
| print(f" FAIL: {msg}") | |
| raise AssertionError(f"Assertion failed: {msg}") | |
| passed += 1 | |
| # --------------------------------------------------------- | |
| # Test 7.1: Empty Workflow Contract | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.1] Testing Empty Workflow Code Generation Contract...") | |
| empty_result = generate_code_logic(nodes=[], edges=[], graph_name="Empty Flow") | |
| assert_true(empty_result["success"], "Empty workflow generation must succeed") | |
| assert_true("def main():" in empty_result["python_code"], "Empty workflow must define main()") | |
| assert_true('if __name__ == "__main__":' in empty_result["python_code"], "Must include entrypoint guard") | |
| assert_true("FROM python:3.12-slim" in empty_result["dockerfile"], "Dockerfile must use python:3.12-slim") | |
| assert_true("empty_flow.py" in empty_result["filename"], "Filename must be sanitized") | |
| # Verify AST parses cleanly | |
| parsed = ast.parse(empty_result["python_code"]) | |
| assert_true(isinstance(parsed, ast.Module), "Generated code must be valid Python AST") | |
| print(" PASS: Empty workflow adheres to code-generation contract and parses to valid AST.") | |
| # --------------------------------------------------------- | |
| # Test 7.2: Target Pipeline Execution Parity (49 -> sqrt -> 7.0) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.2 & 7.6] Testing Target Pipeline Execution Parity (Input 49 -> Sqrt -> Print)...") | |
| target_nodes = [ | |
| {"id": "node_input", "node_type": "constant.float", "data": {"label": "Input Number", "value": 49.0}}, | |
| {"id": "node_sqrt", "node_type": "math.sqrt", "data": {"label": "Square Root"}}, | |
| {"id": "node_print", "node_type": "io.print", "data": {"label": "Print Output", "prefix": "Result: "}}, | |
| ] | |
| target_edges = [ | |
| {"source": "node_input", "target": "node_sqrt", "sourceHandle": "val", "targetHandle": "x"}, | |
| {"source": "node_sqrt", "target": "node_print", "sourceHandle": "result", "targetHandle": "value"}, | |
| ] | |
| graph = {"name": "Target Math Flow", "nodes": target_nodes, "edges": target_edges} | |
| # 1. Run via visual workflow executor | |
| visual_res = execute_workflow_graph(graph) | |
| assert_true(visual_res["success"], "Visual execution must succeed") | |
| assert_true(visual_res["stdout"].strip() == "Result: 7.0", f"Visual stdout must be 'Result: 7.0', got: {visual_res['stdout']!r}") | |
| assert_true(visual_res["node_results"]["node_sqrt"]["outputs"]["result"] == 7.0, "Sqrt output must be 7.0") | |
| # 2. Generate Python code | |
| codegen_res = generate_code_logic(nodes=target_nodes, edges=target_edges, graph_name="Target Math Flow") | |
| assert_true(codegen_res["success"], "Codegen must succeed") | |
| code = codegen_res["python_code"] | |
| # 3. Verify PEP 8 AST syntax | |
| ast_tree = ast.parse(code) | |
| assert_true(isinstance(ast_tree, ast.Module), "Generated script must parse without syntax error") | |
| assert_true("import math" in code, "Must import math") | |
| assert_true("math.sqrt(" in code, "Must call math.sqrt()") | |
| assert_true('print(f"Result: {' in code, "Must format print f-string") | |
| # 4. Execute generated Python script standalone via subprocess | |
| sub_res = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True) | |
| assert_true(sub_res.returncode == 0, f"Standalone Python failed with stderr: {sub_res.stderr}") | |
| standalone_stdout = sub_res.stdout.strip() | |
| assert_true( | |
| standalone_stdout == visual_res["stdout"].strip(), | |
| f"EXECUTION PARITY MISMATCH: visual={visual_res['stdout']!r} vs standalone={standalone_stdout!r}" | |
| ) | |
| print(" PASS: 100% Execution Parity confirmed between Visual Executor and Standalone Python ('Result: 7.0').") | |
| # --------------------------------------------------------- | |
| # Test 7.3: Multi-Step Chained Arithmetic Parity ((15 + 25) * 2 = 80) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.3] Testing Chained Arithmetic Parity ((15 + 25) * 2 -> Print)...") | |
| arith_nodes = [ | |
| {"id": "n_a", "node_type": "constant.int", "data": {"label": "15", "value": 15}}, | |
| {"id": "n_b", "node_type": "constant.int", "data": {"label": "25", "value": 25}}, | |
| {"id": "n_add", "node_type": "core.binary_op", "data": {"label": "Add", "operator": "+"}}, | |
| {"id": "n_two", "node_type": "constant.int", "data": {"label": "2", "value": 2}}, | |
| {"id": "n_mul", "node_type": "core.binary_op", "data": {"label": "Multiply", "operator": "*"}}, | |
| {"id": "n_print", "node_type": "io.print", "data": {"label": "Print Total"}}, | |
| ] | |
| arith_edges = [ | |
| {"source": "n_a", "target": "n_add", "sourceHandle": "val", "targetHandle": "a"}, | |
| {"source": "n_b", "target": "n_add", "sourceHandle": "val", "targetHandle": "b"}, | |
| {"source": "n_add", "target": "n_mul", "sourceHandle": "result", "targetHandle": "a"}, | |
| {"source": "n_two", "target": "n_mul", "sourceHandle": "val", "targetHandle": "b"}, | |
| {"source": "n_mul", "target": "n_print", "sourceHandle": "result", "targetHandle": "value"}, | |
| ] | |
| arith_graph = {"name": "Arithmetic Flow", "nodes": arith_nodes, "edges": arith_edges} | |
| visual_arith = execute_workflow_graph(arith_graph) | |
| assert_true(visual_arith["stdout"].strip() == "80", f"Visual total must be 80, got {visual_arith['stdout']}") | |
| codegen_arith = generate_code_logic(nodes=arith_nodes, edges=arith_edges, graph_name="Arithmetic Flow") | |
| sub_arith = subprocess.run([sys.executable, "-c", codegen_arith["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_arith.returncode == 0, f"Arithmetic script failed: {sub_arith.stderr}") | |
| assert_true( | |
| sub_arith.stdout.strip() == "80", | |
| f"Chained arithmetic parity failed: expected '80', got {sub_arith.stdout.strip()!r}" | |
| ) | |
| print(" PASS: Chained arithmetic code generation executed cleanly with exact parity (80).") | |
| # --------------------------------------------------------- | |
| # Test 7.4: Conditional Branching Parity (If / Else) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.4] Testing Conditional Branching Code Generation (If / Else)...") | |
| if_nodes = [ | |
| {"id": "n_cond", "node_type": "constant.bool", "data": {"label": "Condition True", "value": True}}, | |
| {"id": "n_then", "node_type": "constant.str", "data": {"label": "Branch True", "value": "Access Granted"}}, | |
| {"id": "n_else", "node_type": "constant.str", "data": {"label": "Branch False", "value": "Access Denied"}}, | |
| {"id": "n_branch", "node_type": "control.if_else", "data": {"label": "Security Check"}}, | |
| {"id": "n_log", "node_type": "io.print", "data": {"label": "Log Status", "prefix": "STATUS: "}}, | |
| ] | |
| if_edges = [ | |
| {"source": "n_cond", "target": "n_branch", "sourceHandle": "val", "targetHandle": "condition"}, | |
| {"source": "n_then", "target": "n_branch", "sourceHandle": "val", "targetHandle": "then_val"}, | |
| {"source": "n_else", "target": "n_branch", "sourceHandle": "val", "targetHandle": "else_val"}, | |
| {"source": "n_branch", "target": "n_log", "sourceHandle": "result", "targetHandle": "value"}, | |
| ] | |
| if_graph = {"name": "Conditional Flow", "nodes": if_nodes, "edges": if_edges} | |
| visual_if = execute_workflow_graph(if_graph) | |
| assert_true(visual_if["stdout"].strip() == "STATUS: Access Granted") | |
| codegen_if = generate_code_logic(nodes=if_nodes, edges=if_edges, graph_name="Conditional Flow") | |
| assert_true("if " in codegen_if["python_code"] and "else:" in codegen_if["python_code"], "Must emit if/else structure") | |
| sub_if = subprocess.run([sys.executable, "-c", codegen_if["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_if.returncode == 0, f"Conditional script failed: {sub_if.stderr}") | |
| assert_true( | |
| sub_if.stdout.strip() == "STATUS: Access Granted", | |
| f"Conditional execution mismatch: expected 'STATUS: Access Granted', got {sub_if.stdout.strip()!r}" | |
| ) | |
| print(" PASS: Conditional branching generated and executed with exact parity ('STATUS: Access Granted').") | |
| # --------------------------------------------------------- | |
| # Test 7.5: Rounding Mode Parity (math.sqrt rounding = round) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.5] Testing Math Sqrt Rounding Parameter Code Generation...") | |
| round_nodes = [ | |
| {"id": "n_val", "node_type": "constant.float", "data": {"label": "2.0", "value": 2.0}}, | |
| {"id": "n_sqrt", "node_type": "math.sqrt", "data": {"label": "Sqrt 2", "rounding": "round", "precision": 3}}, | |
| {"id": "n_print", "node_type": "io.print", "data": {"label": "Print"}}, | |
| ] | |
| round_edges = [ | |
| {"source": "n_val", "target": "n_sqrt", "sourceHandle": "val", "targetHandle": "x"}, | |
| {"source": "n_sqrt", "target": "n_print", "sourceHandle": "result", "targetHandle": "value"}, | |
| ] | |
| round_graph = {"name": "Round Sqrt Flow", "nodes": round_nodes, "edges": round_edges} | |
| visual_round = execute_workflow_graph(round_graph) | |
| assert_true(visual_round["stdout"].strip() == "1.414", f"Expected 1.414, got {visual_round['stdout']}") | |
| codegen_round = generate_code_logic(nodes=round_nodes, edges=round_edges, graph_name="Round Sqrt Flow") | |
| assert_true("round(math.sqrt(" in codegen_round["python_code"], "Must emit round(math.sqrt(...), 3)") | |
| sub_round = subprocess.run([sys.executable, "-c", codegen_round["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_round.returncode == 0) | |
| assert_true(sub_round.stdout.strip() == "1.414", f"Expected '1.414', got {sub_round.stdout.strip()!r}") | |
| print(" PASS: Math square root rounding parameter honored in generated script (1.414).") | |
| # --------------------------------------------------------- | |
| # Test 7.6: Dependency Filtering (Standard Libs vs 3rd-Party) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.6] Testing External Dependency Isolation...") | |
| # Graph using math (standard) and requests (external) | |
| dep_nodes = [ | |
| {"id": "n_1", "node_type": "math.sqrt", "data": {"label": "Sqrt", "x": 16}}, | |
| ] | |
| dep_res = generate_code_logic( | |
| nodes=dep_nodes, | |
| edges=[], | |
| dependencies=["requests", "math", "json", "numpy"], | |
| graph_name="Deps Flow" | |
| ) | |
| reqs = dep_res["requirements_txt"] | |
| assert_true("requests==2.31.0" in reqs, "External package 'requests' must be pinned exactly to 2.31.0") | |
| assert_true("numpy==1.26.4" in reqs, "External package 'numpy' must be pinned exactly to 1.26.4") | |
| assert_true("math" not in reqs, "Standard library 'math' must be excluded from requirements.txt") | |
| assert_true("json" not in reqs, "Standard library 'json' must be excluded from requirements.txt") | |
| print(" PASS: Standard library modules excluded from requirements.txt; external packages pinned to exact versions.") | |
| # --------------------------------------------------------- | |
| # Test 7.7: Variable Custom Identifiers Preservation | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.7] Testing Custom Variable Identifier Preservation...") | |
| var_nodes = [ | |
| {"id": "n_var1", "node_type": "core.variable", "data": {"label": "Counter", "var_name": "user_counter", "literal_value": "42"}}, | |
| {"id": "n_print", "node_type": "io.print", "data": {"label": "Print Counter"}}, | |
| ] | |
| var_edges = [ | |
| {"source": "n_var1", "target": "n_print", "sourceHandle": "out_val", "targetHandle": "value"} | |
| ] | |
| var_res = generate_code_logic(nodes=var_nodes, edges=var_edges, graph_name="Custom Var Flow") | |
| assert_true("user_counter = 42" in var_res["python_code"], "Custom variable identifier 'user_counter' must be used") | |
| sub_var = subprocess.run([sys.executable, "-c", var_res["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_var.returncode == 0) | |
| assert_true(sub_var.stdout.strip() == "42") | |
| print(" PASS: Custom variable identifiers preserved and correctly referenced downstream.") | |
| # --------------------------------------------------------- | |
| # Test 7.8: Deterministic Topological Code Ordering | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.8] Testing Deterministic Topological Ordering...") | |
| # Generate code twice for the same graph | |
| run1 = generate_code_logic(nodes=arith_nodes, edges=arith_edges, graph_name="Determinism") | |
| run2 = generate_code_logic(nodes=arith_nodes, edges=arith_edges, graph_name="Determinism") | |
| assert_true(run1["python_code"] == run2["python_code"], "Codegen must be 100% deterministic") | |
| assert_true(run1["execution_order"] == run2["execution_order"], "Execution order must match identically") | |
| print(" PASS: Codegen produces stable, identical code across consecutive compilations.") | |
| # --------------------------------------------------------- | |
| # Test 7.9: Dependency Exact Versioning vs Minimum Pinning | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.9] Testing Dependency Versioning (Exact vs Minimum Pinning)...") | |
| res_exact = generate_code_logic( | |
| nodes=dep_nodes, | |
| edges=[], | |
| dependencies=["requests", "pandas", "custom-pkg"], | |
| pin_exact=True, | |
| graph_name="Exact Pins" | |
| ) | |
| reqs_exact = res_exact["requirements_txt"] | |
| assert_true("requests==2.31.0" in reqs_exact, "Must pin requests==2.31.0") | |
| assert_true("pandas==2.2.2" in reqs_exact, "Must pin pandas==2.2.2") | |
| assert_true("custom-pkg==0.1.0" in reqs_exact, "Must pin unknown package with ==0.1.0 when pin_exact=True") | |
| res_flex = generate_code_logic( | |
| nodes=dep_nodes, | |
| edges=[], | |
| dependencies=["custom-pkg"], | |
| pin_exact=False, | |
| graph_name="Flex Pins" | |
| ) | |
| assert_true("custom-pkg>=0.1.0" in res_flex["requirements_txt"], "Must pin unknown package with >=0.1.0 when pin_exact=False") | |
| print(" PASS: Exact dependency pinning validated (requests==2.31.0, pandas==2.2.2, custom-pkg==0.1.0).") | |
| # --------------------------------------------------------- | |
| # Test 7.10: Generated-Code Safety & AST Allowlist/Denylist | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.10] Testing Generated-Code Safety & AST Denylist Inspection...") | |
| from language_core.codegen_contracts import validate_generated_ast, inspect_generated_ast_security | |
| # Unsafe scripts with dangerous callables | |
| unsafe_eval = "def main():\n eval('2 + 2')\n return None\n" | |
| is_safe, err = validate_generated_ast(unsafe_eval, check_security=True) | |
| assert_true(not is_safe, "Must reject eval() callable") | |
| assert_true("CODEGEN-SECURITY-VIOLATION" in err, "Must report CODEGEN-SECURITY-VIOLATION") | |
| unsafe_exec = "def main():\n exec('import os')\n return None\n" | |
| is_safe, err = validate_generated_ast(unsafe_exec, check_security=True) | |
| assert_true(not is_safe, "Must reject exec() callable") | |
| unsafe_system = "import os\ndef main():\n os.system('echo compromised')\n return None\n" | |
| is_safe, err = validate_generated_ast(unsafe_system, check_security=True) | |
| assert_true(not is_safe, "Must reject os.system() invocation") | |
| unsafe_popen = "import subprocess\ndef main():\n subprocess.Popen(['echo', 'bad'])\n return None\n" | |
| is_safe, err = validate_generated_ast(unsafe_popen, check_security=True) | |
| assert_true(not is_safe, "Must reject subprocess.Popen() invocation") | |
| unsafe_dunder = "def main():\n x = ().__class__.__bases__[0].__subclasses__()\n return x\n" | |
| is_safe, err = validate_generated_ast(unsafe_dunder, check_security=True) | |
| assert_true(not is_safe, "Must reject dunder introspection tampering (__subclasses__)") | |
| print(" PASS: AST safety inspection verified; eval, exec, os.system, subprocess.Popen, and dunder tampering blocked.") | |
| # --------------------------------------------------------- | |
| # Test 7.11: Control-Flow Adversarial Semantic Parity | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.11] Testing Control-Flow Adversarial Semantic Parity...") | |
| # 7.11a: Try / Except Block with Execution Parity | |
| print(" Subtest 7.11a: Try / Except Block Exception Handling...") | |
| 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"}, | |
| ] | |
| try_graph = {"name": "Try Flow", "nodes": try_nodes, "edges": try_edges} | |
| visual_try = execute_workflow_graph(try_graph) | |
| assert_true(visual_try["stdout"].strip() == "SAFE: 42") | |
| codegen_try = generate_code_logic(nodes=try_nodes, edges=try_edges, graph_name="Try Flow") | |
| assert_true(codegen_try["success"]) | |
| assert_true("try:" in codegen_try["python_code"] and "except " in codegen_try["python_code"]) | |
| sub_try = subprocess.run([sys.executable, "-c", codegen_try["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_try.returncode == 0) | |
| assert_true(sub_try.stdout.strip() == "SAFE: 42", f"Expected 'SAFE: 42', got {sub_try.stdout.strip()!r}") | |
| print(" PASS: Try/Except execution parity verified ('SAFE: 42').") | |
| # 7.11b: Branch-Scoped Variables Consumed Downstream | |
| print(" Subtest 7.11b: Branch-Scoped Variables inside If/Else Consumed Downstream...") | |
| 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"}, | |
| ] | |
| branch_graph = {"name": "Branch Scoped Flow", "nodes": branch_nodes, "edges": branch_edges} | |
| visual_branch = execute_workflow_graph(branch_graph) | |
| assert_true(visual_branch["stdout"].strip() == "FINAL: 100", f"Expected 100, got {visual_branch['stdout']}") | |
| codegen_branch = generate_code_logic(nodes=branch_nodes, edges=branch_edges, graph_name="Branch Scoped Flow") | |
| sub_branch = subprocess.run([sys.executable, "-c", codegen_branch["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_branch.returncode == 0) | |
| assert_true(sub_branch.stdout.strip() == "FINAL: 100", f"Expected 'FINAL: 100', got {sub_branch.stdout.strip()!r}") | |
| print(" PASS: Branch-scoped variable consumed downstream verified with 100% parity (50 * 2 = 100).") | |
| # 7.11c: Nested Loop Execution Parity | |
| print(" Subtest 7.11c: Nested Loops with Accumulation...") | |
| loop_code = ( | |
| "def main():\n" | |
| " acc = 0\n" | |
| " for i in range(3):\n" | |
| " for j in range(3):\n" | |
| " if j == 1:\n" | |
| " continue\n" | |
| " if j == 2:\n" | |
| " break\n" | |
| " acc += 10\n" | |
| " print(f'ACC: {acc}')\n" | |
| " return acc\n" | |
| "\n" | |
| "if __name__ == '__main__':\n" | |
| " main()\n" | |
| ) | |
| ast_loop = ast.parse(loop_code) | |
| assert_true(isinstance(ast_loop, ast.Module)) | |
| sub_loop = subprocess.run([sys.executable, "-c", loop_code], capture_output=True, text=True) | |
| assert_true(sub_loop.returncode == 0) | |
| assert_true(sub_loop.stdout.strip() == "ACC: 30", f"Expected 'ACC: 30', got {sub_loop.stdout.strip()!r}") | |
| print(" PASS: Nested loops with break and continue verified with exact arithmetic (3 * 10 = 30).") | |
| # --------------------------------------------------------- | |
| # Test 7.12: Input/Output Disambiguation (Literal vs Interactive Stdin) | |
| # --------------------------------------------------------- | |
| print("\n[Step 7.12] Testing Input/Output Disambiguation (Case A vs Case B)...") | |
| # Case A: Literal Constant Number (49.0) -> Math Sqrt -> Print | |
| print(" Case A: Constant Float Literal (49.0) -> Math Sqrt -> Print...") | |
| case_a_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": "Case A Result: "}}, | |
| ] | |
| case_a_edges = [ | |
| {"source": "c_val", "target": "c_sqrt", "sourceHandle": "val", "targetHandle": "x"}, | |
| {"source": "c_sqrt", "target": "c_out", "sourceHandle": "result", "targetHandle": "value"}, | |
| ] | |
| vis_a = execute_workflow_graph({"name": "Case A Flow", "nodes": case_a_nodes, "edges": case_a_edges}) | |
| assert_true(vis_a["stdout"].strip() == "Case A Result: 7.0") | |
| res_a = generate_code_logic(nodes=case_a_nodes, edges=case_a_edges, graph_name="Case A Flow") | |
| sub_a = subprocess.run([sys.executable, "-c", res_a["python_code"]], capture_output=True, text=True) | |
| assert_true(sub_a.returncode == 0) | |
| assert_true(sub_a.stdout.strip() == "Case A Result: 7.0") | |
| print(" PASS: Case A (Literal constant injection) produces 'Case A Result: 7.0' without stdin.") | |
| # Case B: Interactive IO Input -> Type Cast -> Math Sqrt -> Print (via stdin) | |
| print(" Case B: Interactive IO Input -> Type Cast -> Math Sqrt -> Print (Interactive Stdin)...") | |
| case_b_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": "Case B Result: "}}, | |
| ] | |
| case_b_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_b = execute_workflow_graph({"name": "Case B Flow", "nodes": case_b_nodes, "edges": case_b_edges}) | |
| assert_true(vis_b["stdout"].strip() == "Case B Result: 7.0", f"Visual Case B failed: {vis_b['stdout']}") | |
| res_b = generate_code_logic(nodes=case_b_nodes, edges=case_b_edges, graph_name="Case B Flow") | |
| assert_true('user_raw = input("Enter float: ")' in res_b["python_code"], "Must emit interactive input() call") | |
| assert_true('float(user_raw)' in res_b["python_code"], "Must cast input string to float") | |
| sub_b = subprocess.run( | |
| [sys.executable, "-c", res_b["python_code"]], | |
| input="49.0\n", | |
| capture_output=True, | |
| text=True | |
| ) | |
| assert_true(sub_b.returncode == 0, f"Case B script failed: {sub_b.stderr}") | |
| assert_true( | |
| "Case B Result: 7.0" in sub_b.stdout, | |
| f"Case B execution mismatch: expected 'Case B Result: 7.0', got {sub_b.stdout!r}" | |
| ) | |
| print(" PASS: Case B (Interactive stdin input with cast) produces 'Case B Result: 7.0' via stdin.") | |
| print("\n" + "=" * 75) | |
| print(f"RESULTS: ALL {total}/{total} CODE GENERATION & PARITY TESTS PASSED (100% SUCCESS)") | |
| print("=" * 75) | |
| if __name__ == "__main__": | |
| run_tests() | |