from acl_engine.actions.registry import ActionRegistry, register_builtin_actions
from acl_engine.parser import ACLCommand, VariableRef
from acl_engine.validator import ACLValidationError, Validator


def build_validator() -> Validator:
    registry = ActionRegistry()
    register_builtin_actions(registry)
    return Validator(registry)


def test_validator_blocks_unknown_actions() -> None:
    validator = build_validator()

    command = ACLCommand(action="unknown.run", params={}, line_number=2)

    try:
        validator.validate(command)
    except ACLValidationError as error:
        assert "unknown action" in str(error)
    else:  # pragma: no cover
        raise AssertionError("expected ACLValidationError")


def test_validator_requires_parameters() -> None:
    validator = build_validator()

    command = ACLCommand(action="file.write", params={"path": "main.py"}, line_number=3)

    try:
        validator.validate(command)
    except ACLValidationError as error:
        assert "missing required parameters" in str(error)
    else:  # pragma: no cover
        raise AssertionError("expected ACLValidationError")


def test_validator_allows_variable_refs_before_resolution() -> None:
    validator = build_validator()
    command = ACLCommand(
        action="file.write",
        params={"path": "main.py", "content": VariableRef("code")},
        line_number=4,
    )

    validator.validate(command)


def test_validator_applies_schema_defaults() -> None:
    validator = build_validator()
    command = ACLCommand(action="file.list", params={}, line_number=5)

    _, params = validator.validate(command)

    assert params["path"] == "."
