Change headless -> run_key, Add previews, _wrap_literal -> _wrap

This commit is contained in:
2025-05-06 22:56:45 -04:00
parent b5da6b9647
commit 5d96d6d3d9
8 changed files with 161 additions and 110 deletions

View File

@ -33,7 +33,8 @@ class DummyInputAction(BaseIOAction):
# --- Tests ---
def test_command_creation():
@pytest.mark.asyncio
async def test_command_creation():
"""Test if Command can be created with a callable."""
action = Action("test_action", dummy_action)
cmd = Command(key="TEST", description="Test Command", action=action)
@ -41,12 +42,15 @@ def test_command_creation():
assert cmd.description == "Test Command"
assert cmd.action == action
result = await cmd()
assert result == "ok"
assert cmd.result == "ok"
def test_command_str():
"""Test if Command string representation is correct."""
action = Action("test_action", dummy_action)
cmd = Command(key="TEST", description="Test Command", action=action)
print(cmd)
assert (
str(cmd)
== "Command(key='TEST', description='Test Command' action='Action(name='test_action', action=dummy_action, args=(), kwargs={}, retry=False)')"
@ -233,3 +237,26 @@ def test_chain_retry_all_not_base_action():
with pytest.raises(Exception) as exc_info:
assert cmd.action.retry_policy.enabled is False
assert "'function' object has no attribute 'retry_policy'" in str(exc_info.value)
@pytest.mark.asyncio
async def test_command_exception_handling():
"""Test if Command handles exceptions correctly."""
async def bad_action():
raise ZeroDivisionError("This is a test exception")
cmd = Command(key="TEST", description="Test Command", action=bad_action)
with pytest.raises(ZeroDivisionError):
await cmd()
assert cmd.result is None
assert isinstance(cmd._context.exception, ZeroDivisionError)
def test_command_bad_action():
"""Test if Command raises an exception when action is not callable."""
with pytest.raises(TypeError) as exc_info:
Command(key="TEST", description="Test Command", action="not_callable")
assert str(exc_info.value) == "Action must be a callable or an instance of BaseAction"

View File

@ -1,45 +0,0 @@
import pytest
from falyx import Action, Falyx
@pytest.mark.asyncio
async def test_headless():
"""Test if Falyx can run in headless mode."""
falyx = Falyx("Headless Test")
# Add a simple command
falyx.add_command(
key="T",
description="Test Command",
action=lambda: "Hello, World!",
)
# Run the CLI
result = await falyx.headless("T")
assert result == "Hello, World!"
@pytest.mark.asyncio
async def test_headless_recovery():
"""Test if Falyx can recover from a failure in headless mode."""
falyx = Falyx("Headless Recovery Test")
state = {"count": 0}
async def flaky():
if not state["count"]:
state["count"] += 1
raise RuntimeError("Random failure!")
return "ok"
# Add a command that raises an exception
falyx.add_command(
key="E",
description="Error Command",
action=Action("flaky", flaky),
retry=True,
)
result = await falyx.headless("E")
assert result == "ok"