# tests/test_config.py
import yaml
from agentsh_secure_sandbox.core.types import SecureConfig, ThreatFeed, ThreatFeedsConfig
from agentsh_secure_sandbox.core.config import generate_server_config


def test_default_config():
    cfg = SecureConfig()
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["server"]["http"]["addr"] == "127.0.0.1:18080"
    assert result["auth"]["type"] == "none"
    assert result["policies"]["system_dir"] == "/etc/agentsh/system"
    assert result["policies"]["dir"] == "/etc/agentsh"
    assert result["policies"]["default"] == "policy"
    assert result["workspace"] == "/workspace"
    assert result["sandbox"]["enabled"] is True


def test_custom_workspace():
    cfg = SecureConfig(workspace="/home/agent")
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["workspace"] == "/home/agent"


def test_custom_policy_name():
    cfg = SecureConfig(policy_name="my-policy")
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["policies"]["default"] == "my-policy"


def test_default_threat_feeds():
    cfg = SecureConfig()
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["threat_feeds"]["enabled"] is True
    assert len(result["threat_feeds"]["feeds"]) == 2


def test_threat_feeds_disabled():
    cfg = SecureConfig(threat_feeds=False)
    result = yaml.safe_load(generate_server_config(cfg))
    assert "threat_feeds" not in result


def test_custom_threat_feeds():
    cfg = SecureConfig(
        threat_feeds=ThreatFeedsConfig(
            feeds=[ThreatFeed(name="custom", url="https://example.com/list", format="domain-list")],
            action="audit",
        )
    )
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["threat_feeds"]["action"] == "audit"
    assert result["threat_feeds"]["feeds"][0]["name"] == "custom"


def test_real_paths_included():
    cfg = SecureConfig(real_paths=True)
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["sandbox"]["real_paths"] is True


def test_enforce_redirects():
    cfg = SecureConfig(enforce_redirects=True)
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["sandbox"]["enforce_redirects"] is True


def test_watchtower():
    cfg = SecureConfig(watchtower="https://watchtower.example.com")
    result = yaml.safe_load(generate_server_config(cfg))
    assert result["watchtower"]["url"] == "https://watchtower.example.com"
