mirror of
https://github.com/zap-proto/spec.git
synced 2026-10-30 15:04:03 +00:00
Add test_client/test_gateway/test_server and extend test_config to exercise the async stub modules (connect/list/call, add/remove/list servers, run() startup) and Config.load + non-Darwin default_path. Lifts total coverage 85% -> 91%, giving the 80% gate real headroom across the 3.10/3.11/3.12 matrix. Ignore .coverage/coverage.xml so build artifacts stay out of the tree. 129 passed; client/config/gateway/server at 100%, identity 92%, crypto 82%.
116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
"""Tests for zap_schema.config module."""
|
|
|
|
import pytest
|
|
|
|
from zap_schema.config import Config, ServerConfig, Transport
|
|
|
|
|
|
class TestConfig:
|
|
"""Tests for Config class."""
|
|
|
|
def test_default_config(self):
|
|
"""Test creating default config."""
|
|
config = Config()
|
|
assert config.listen == "0.0.0.0"
|
|
assert config.port == 9999
|
|
assert config.log_level == "info"
|
|
assert config.servers == []
|
|
|
|
def test_custom_config(self):
|
|
"""Test creating custom config."""
|
|
config = Config(listen="127.0.0.1", port=8888, log_level="debug")
|
|
assert config.listen == "127.0.0.1"
|
|
assert config.port == 8888
|
|
assert config.log_level == "debug"
|
|
|
|
def test_config_from_dict(self):
|
|
"""Test creating config from dict."""
|
|
data = {
|
|
"listen": "localhost",
|
|
"port": 9000,
|
|
"log_level": "warn",
|
|
"servers": [
|
|
{"name": "test", "url": "http://localhost:8080"}
|
|
]
|
|
}
|
|
config = Config.from_dict(data)
|
|
assert config.listen == "localhost"
|
|
assert config.port == 9000
|
|
assert config.log_level == "warn"
|
|
assert len(config.servers) == 1
|
|
assert config.servers[0].name == "test"
|
|
|
|
def test_config_default_path(self):
|
|
"""Test default config path."""
|
|
path = Config.default_path()
|
|
assert "zap" in str(path)
|
|
assert "config.toml" in str(path)
|
|
|
|
def test_config_from_dict_with_transport(self):
|
|
"""Transport string in a server entry is parsed into the enum."""
|
|
config = Config.from_dict(
|
|
{"servers": [{"name": "ws", "url": "ws://x", "transport": "websocket"}]}
|
|
)
|
|
assert config.servers[0].transport == Transport.WEBSOCKET
|
|
|
|
def test_config_load_from_file(self, tmp_path):
|
|
"""Config.load reads and parses a TOML file."""
|
|
toml = tmp_path / "config.toml"
|
|
toml.write_text(
|
|
'listen = "127.0.0.1"\n'
|
|
"port = 7777\n"
|
|
'log_level = "debug"\n'
|
|
"[[servers]]\n"
|
|
'name = "alpha"\n'
|
|
'url = "http://alpha:1"\n'
|
|
)
|
|
config = Config.load(toml)
|
|
assert config.listen == "127.0.0.1"
|
|
assert config.port == 7777
|
|
assert config.log_level == "debug"
|
|
assert config.servers[0].name == "alpha"
|
|
|
|
def test_default_path_non_darwin(self, monkeypatch):
|
|
"""Non-macOS platforms resolve to an XDG-style or Windows path."""
|
|
import platform as platform_mod
|
|
|
|
monkeypatch.setattr(platform_mod, "system", lambda: "Linux")
|
|
assert str(Config.default_path()).endswith(".config/zap/config.toml")
|
|
monkeypatch.setattr(platform_mod, "system", lambda: "Windows")
|
|
assert "zap" in str(Config.default_path())
|
|
|
|
|
|
class TestServerConfig:
|
|
"""Tests for ServerConfig class."""
|
|
|
|
def test_server_config(self):
|
|
"""Test creating server config."""
|
|
config = ServerConfig(name="test", url="http://localhost:8080")
|
|
assert config.name == "test"
|
|
assert config.url == "http://localhost:8080"
|
|
assert config.transport == Transport.STDIO
|
|
assert config.timeout == 30000
|
|
|
|
def test_server_config_with_transport(self):
|
|
"""Test server config with transport."""
|
|
config = ServerConfig(
|
|
name="test",
|
|
url="ws://localhost:8080",
|
|
transport=Transport.WEBSOCKET,
|
|
timeout=60000
|
|
)
|
|
assert config.transport == Transport.WEBSOCKET
|
|
assert config.timeout == 60000
|
|
|
|
|
|
class TestTransport:
|
|
"""Tests for Transport enum."""
|
|
|
|
def test_transport_values(self):
|
|
"""Test transport enum values."""
|
|
assert Transport.STDIO.value == "stdio"
|
|
assert Transport.HTTP.value == "http"
|
|
assert Transport.WEBSOCKET.value == "websocket"
|
|
assert Transport.ZAP.value == "zap"
|
|
assert Transport.UNIX.value == "unix"
|