auto / tests /test_phase9_programming_fundamentals.py
rsnarsna
feat(phase9): implement visual programming fundamentals across 11 core areas
b7c0740
Raw History Blame Contribute Delete
16.3 kB
"""
ALLI Phase 9 - Programming Fundamentals Test Suite
Verifies schema compliance, deterministic execution, type compatibility,
bounded safety guards, and PEP 8 Python AST code generation across all 11 core areas:
1. Input / Output
2. Variables & Constants
3. Data Types & Type Casting
4. Operators (Arithmetic, Comparison, Logical)
5. Math Functions
6. If-Else (Conditional Branching)
7. For Loop & While Loop (Bounded Iterations)
8. Functions & Scope
9. Collection & String Operations
10. Exception Handling (Try / Except)
11. Classes & Object-Oriented Programming (OOP)
"""
import ast
import time
import pytest
from language_core.node_schema import default_registry
from language_core.workflow_executor import execute_workflow_graph
from lambdas.codegen_handler import generate_code_logic
# ─── 1. INPUT & OUTPUT ────────────────────────────────────────────────────────
def test_p9_01_input_and_output():
"""Verifies io.input and io.print schemas, execution, and stdout telemetry."""
graph = {
"run_id": "test_p9_io_01",
"nodes": [
{
"id": "in1",
"type": "io.input",
"data": {"value": "Hello ALLI"},
},
{
"id": "pr1",
"type": "io.print",
"data": {"message": "Hello ALLI"},
},
],
"edges": [
{"id": "e1", "source": "in1", "target": "pr1", "sourceHandle": "val", "targetHandle": "value"}
],
}
res = execute_workflow_graph(graph)
assert res["success"] is True
assert "Hello ALLI" in res["stdout"] or "Hello ALLI" in str(res["final_outputs"])
# Codegen verification
cg = generate_code_logic(graph)
assert cg["success"] is True
ast.parse(cg["python_code"])
# ─── 2. VARIABLES & CONSTANTS ─────────────────────────────────────────────────
def test_p9_02_variables_and_constants():
"""Verifies typed constants (int, float, str, bool) and variable mutability."""
for ctype, def_val in [("int", 100), ("float", 2.718), ("str", "obsidian"), ("bool", True)]:
node_type = f"constant.{ctype}"
assert default_registry.has(node_type)
schema = default_registry.get(node_type)
assert schema.category == "Variables & Types"
graph = {
"run_id": "test_p9_vars_02",
"nodes": [
{
"id": "c_int",
"type": "constant.int",
"data": {"parameters": {"literal_value": "42", "const_type": "int"}},
},
{
"id": "var1",
"type": "core.variable",
"data": {"parameters": {"var_name": "counter", "default_value": "0", "var_type": "int"}},
},
],
"edges": [
{"id": "e1", "source": "c_int", "target": "var1", "sourceHandle": "out", "targetHandle": "in_val"}
],
}
res = execute_workflow_graph(graph)
assert res["success"] is True
assert res["node_results"]["c_int"]["outputs"]["out"] == 42
assert res["node_results"]["var1"]["outputs"]["val"] == 42
# ─── 3. DATA TYPES & CASTING ──────────────────────────────────────────────────
def test_p9_03_data_types_and_casting():
"""Verifies list, dict, tuple, set, and type casting."""
assert default_registry.has("data.list")
assert default_registry.has("data.dict")
assert default_registry.has("data.tuple")
assert default_registry.has("data.set")
assert default_registry.has("core.type_cast")
# Test type cast float to int
cast_graph = {
"run_id": "test_p9_cast_03",
"nodes": [
{
"id": "c_fl",
"type": "constant.float",
"data": {"parameters": {"literal_value": "99.8"}},
},
{
"id": "cast1",
"type": "core.type_cast",
"data": {"parameters": {"target_type": "int"}},
},
],
"edges": [
{"id": "e1", "source": "c_fl", "target": "cast1", "sourceHandle": "out", "targetHandle": "value"}
],
}
res = execute_workflow_graph(cast_graph)
assert res["success"] is True
assert res["node_results"]["cast1"]["outputs"]["result"] == 99
# ─── 4. OPERATORS ─────────────────────────────────────────────────────────────
def test_p9_04_operators_arithmetic_comparison_logic():
"""Verifies binary operators, comparison, and boolean logic."""
graph = {
"run_id": "test_p9_ops_04",
"nodes": [
{
"id": "op_add",
"type": "core.binary_op",
"data": {"parameters": {"a": 20, "b": 22, "operator": "+"}},
},
{
"id": "cmp1",
"type": "core.compare",
"data": {"parameters": {"operator": "=="}},
},
{
"id": "target_val",
"type": "constant.int",
"data": {"parameters": {"literal_value": "42"}},
},
],
"edges": [
{"id": "e1", "source": "op_add", "target": "cmp1", "sourceHandle": "result", "targetHandle": "a"},
{"id": "e2", "source": "target_val", "target": "cmp1", "sourceHandle": "out", "targetHandle": "b"},
],
}
res = execute_workflow_graph(graph)
assert res["success"] is True
assert res["node_results"]["op_add"]["outputs"]["result"] == 42
assert res["node_results"]["cmp1"]["outputs"]["result"] is True
# ─── 5. EXPANDED MATH FUNCTIONS ───────────────────────────────────────────────
def test_p9_05_math_functions_expansion():
"""Verifies math.pow, math.abs, math.round, math.floor, math.ceil, math.min, math.max."""
for mtype in ["math.pow", "math.abs", "math.round", "math.floor", "math.ceil", "math.min", "math.max"]:
assert default_registry.has(mtype)
# 1. math.pow (2^5 = 32)
res_pow = default_registry.execute("math.pow", {"x": 2.0, "y": 5.0}, {})
assert res_pow["result"] == 32.0
# 2. math.abs (abs(-7.5) = 7.5)
res_abs = default_registry.execute("math.abs", {"x": -7.5}, {})
assert res_abs["result"] == 7.5
# 3. math.round (round(3.14159, 2) = 3.14)
res_round = default_registry.execute("math.round", {"x": 3.14159}, {"precision": 2})
assert res_round["result"] == 3.14
# 4. math.floor (floor(4.9) = 4)
res_floor = default_registry.execute("math.floor", {"x": 4.9}, {})
assert res_floor["result"] == 4
# 5. math.ceil (ceil(4.1) = 5)
res_ceil = default_registry.execute("math.ceil", {"x": 4.1}, {})
assert res_ceil["result"] == 5
# 6. math.min (min(12, 8) = 8)
res_min = default_registry.execute("math.min", {"a": 12.0, "b": 8.0}, {})
assert res_min["result"] == 8.0
# 7. math.max (max(12, 8) = 12)
res_max = default_registry.execute("math.max", {"a": 12.0, "b": 8.0}, {})
assert res_max["result"] == 12.0
# ─── 6. IF-ELSE CONDITIONAL BRANCHING ─────────────────────────────────────────
def test_p9_06_if_else_conditional_branching():
"""Verifies conditional execution routing between true and false branches."""
graph = {
"run_id": "test_p9_cond_06",
"nodes": [
{
"id": "cond_node",
"type": "control.if_else",
"data": {"parameters": {"condition": True, "then_val": "success_branch", "else_val": "fail_branch"}},
},
],
"edges": [],
}
res = execute_workflow_graph(graph)
assert res["success"] is True
assert res["node_results"]["cond_node"]["outputs"]["result"] == "success_branch"
# ─── 7. FOR LOOP & WHILE LOOP (BOUNDED ITERATION) ──────────────────────────────
def test_p9_07_loops_and_bounded_iteration_safety():
"""Verifies loop iteration and bounded iteration safety limits."""
# 1. Normal for-loop over list
res_for = default_registry.execute("control.for_loop", {"iterable": [1, 2, 3, 4, 5]}, {})
assert res_for["iteration_count"] == 5
assert res_for["completed"] is True
# 2. While loop bounded limit breach prevention
overflow_graph = {
"run_id": "test_p9_loop_overflow",
"nodes": [
{
"id": "loop_huge",
"type": "control.for_loop",
"data": {
"parameters": {"range_count": 25000},
"settings": {"max_iterations": 500},
},
}
],
"edges": [],
}
res_overflow = execute_workflow_graph(overflow_graph)
assert res_overflow["success"] is False
assert "ERR-LOOP-OVERFLOW" in str(res_overflow["node_results"]["loop_huge"]["error"])
# ─── 8. FUNCTIONS & SCOPE ─────────────────────────────────────────────────────
def test_p9_08_functions_and_callable_invocation():
"""Verifies function definition and call execution."""
res_def = default_registry.execute("function.def", {}, {"func_name": "triple", "params_list": "x", "return_expr": "x * 3"})
fn = res_def["callable_fn"]
assert callable(fn)
assert fn(10) == 30
res_call = default_registry.execute("function.call", {"callable_fn": fn, "arg": 7}, {"func_name": "triple"})
assert res_call["result"] == 21
# ─── 9. STRING & COLLECTION OPERATIONS ────────────────────────────────────────
def test_p9_09_string_and_collection_operations():
"""Verifies string.transform, string.concat, and string.split."""
# 1. string.transform (upper, title, strip)
t_upper = default_registry.execute("string.transform", {"text_in": "hello alli"}, {"operation": "upper"})
assert t_upper["text_out"] == "HELLO ALLI"
assert t_upper["length"] == 10
t_title = default_registry.execute("string.transform", {"text_in": "hello alli"}, {"operation": "title"})
assert t_title["text_out"] == "Hello Alli"
# 2. string.concat
t_concat = default_registry.execute("string.concat", {"a": "Visual", "b": "Programming"}, {"separator": " "})
assert t_concat["result"] == "Visual Programming"
assert t_concat["length"] == 18
# 3. string.split
t_split = default_registry.execute("string.split", {"text": "red,green,blue"}, {"delimiter": ","})
assert t_split["items"] == ["red", "green", "blue"]
assert t_split["count"] == 3
# ─── 10. EXCEPTION HANDLING (TRY / EXCEPT) ────────────────────────────────────
def test_p9_10_try_except_error_guard():
"""Verifies try_except node captures errors and returns fallback."""
# Success path
res_ok = default_registry.execute("control.try_except", {"try_val": "primary_data"}, {})
assert res_ok["result"] == "primary_data"
assert res_ok["has_error"] is False
# Fallback path
res_fb = default_registry.execute("control.try_except", {"try_val": None, "fallback_val": "safe_default"}, {})
assert res_fb["result"] == "safe_default"
# ─── 11. CLASSES & OBJECT-ORIENTED PROGRAMMING (OOP) ──────────────────────────
def test_p9_11_classes_and_oop():
"""Verifies oop.class_def, oop.instance, and oop.method_call."""
# 1. Class Definition
cls_res = default_registry.execute(
"oop.class_def",
{},
{"class_name": "Student", "attributes": "name, score"}
)
assert cls_res["class_name"] == "Student"
assert cls_res["class_ref"].__name__ == "Student"
# 2. Create Instance
inst_res = default_registry.execute(
"oop.instance",
{"class_in": cls_res["class_ref"], "arg_1": "Bob", "arg_2": 95},
{"class_name": "Student"}
)
instance = inst_res["instance"]
assert instance["name"] == "Bob"
assert instance["name"] == "Bob"
assert instance["score"] == 95
# 3. Method Call (Mutation: increment score)
method_res = default_registry.execute(
"oop.method_call",
{"instance_in": instance, "arg": 5},
{"method_name": "add_bonus", "attribute_to_modify": "score", "operation": "increment"}
)
assert method_res["result"] == 100
assert method_res["instance_out"]["score"] == 100
# ─── 12. END-TO-END PIPELINE & PYTHON CODE GENERATION ─────────────────────────
def test_p9_12_end_to_end_multinode_dag_and_codegen():
"""
Builds an end-to-end DAG combining Math, String, Variables, and Logic,
executes it deterministically, and validates PEP 8 code synthesis.
"""
run_id = f"run_p9_e2e_{int(time.time()*1000)}"
graph = {
"run_id": run_id,
"nodes": [
{
"id": "c_str",
"type": "string.concat",
"data": {"parameters": {"a": "alli", "b": "studio", "separator": "_"}},
},
{
"id": "str_up",
"type": "string.transform",
"data": {"parameters": {"operation": "upper"}},
},
{
"id": "m_pow",
"type": "math.pow",
"data": {"parameters": {"x": 3.0, "y": 4.0}},
},
{
"id": "c_oop",
"type": "oop.class_def",
"data": {"parameters": {"class_name": "Account", "attributes": "owner, balance"}},
},
{
"id": "inst_oop",
"type": "oop.instance",
"data": {"parameters": {"class_name": "Account", "arg_1_val": "Alice", "arg_2_val": 500}},
},
{
"id": "m_call",
"type": "oop.method_call",
"data": {"parameters": {"method_name": "deposit", "attribute_to_modify": "balance", "operation": "increment", "default_arg": 150}},
},
],
"edges": [
{"id": "e1", "source": "c_str", "target": "str_up", "sourceHandle": "result", "targetHandle": "text_in"},
{"id": "e2", "source": "c_oop", "target": "inst_oop", "sourceHandle": "class_ref", "targetHandle": "class_in"},
{"id": "e3", "source": "inst_oop", "target": "m_call", "sourceHandle": "instance", "targetHandle": "instance_in"},
],
}
# 1. Execute full workflow DAG
res = execute_workflow_graph(graph)
assert res["success"] is True
# Validate String pipeline
assert res["node_results"]["str_up"]["outputs"]["text_out"] == "ALLI_STUDIO"
assert res["node_results"]["str_up"]["outputs"]["length"] == 11
# Validate Math pipeline
assert res["node_results"]["m_pow"]["outputs"]["result"] == 81.0
# Validate OOP pipeline
assert res["node_results"]["m_call"]["outputs"]["result"] == 650
assert res["node_results"]["m_call"]["outputs"]["instance_out"]["balance"] == 650
# 2. Validate Python Code Generation
cg_res = generate_code_logic(graph)
assert cg_res["success"] is True
py_code = cg_res["python_code"]
# Must be valid PEP 8 Python AST
tree = ast.parse(py_code)
assert isinstance(tree, ast.Module)
# Must execute standalone with identical outputs
sandbox_locals = {}
exec(compile(tree, filename="<workflow_test>", mode="exec"), {}, sandbox_locals)
assert "main" in sandbox_locals
ret = sandbox_locals["main"]()
assert "3.0 ** 4.0" in py_code or "math.pow" in py_code
assert ret is not None