File size: 3,703 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""The Agent union: no collisions, no singletons, correct layer order."""

from __future__ import annotations

import asyncio

import pytest

from pyUltroid.agent import (
    Agent,
    AgentConfig,
    AgentLifecycle,
    AgentRuntime,
    MiniAI,
    ProviderRegistry,
    SessionStore,
    UltroidAgent,
    agent,
    miniai,
)


def test_mro_covers_every_subsystem():
    for cls in (
        AgentRuntime,
        MiniAI,
        UltroidAgent,
        ProviderRegistry,
        AgentLifecycle,
        SessionStore,
        AgentConfig,
    ):
        assert cls in Agent.__mro__


def test_weakest_base_is_last():
    """Otherwise the cooperative __init__ unwinds in the wrong order."""
    mro = Agent.__mro__
    assert mro.index(AgentConfig) > mro.index(SessionStore)
    assert mro.index(SessionStore) > mro.index(AgentLifecycle)
    assert mro.index(AgentLifecycle) > mro.index(ProviderRegistry)
    assert mro.index(ProviderRegistry) > mro.index(MiniAI)


def test_constructs():
    assert isinstance(Agent(), Agent)


@pytest.mark.parametrize(
    "method",
    [
        "config_field", "config_set", "config_status", "config_default_provider",
        "session_list", "session_find", "session_history_key",
        "lifecycle_register", "lifecycle_start", "lifecycle_stop",
        "provider_by_name", "provider_health", "providers_by_tier",
        "providers_configured", "provider_cheapest", "provider_register",
        "mini_respond", "mini_ask", "mini_cache_size", "mini_flush_cache",
        "runtime_prepare", "runtime_run", "runtime_stream", "runtime_executor",
        "respond", "stream", "history", "chat_system", "set_chat_system",
        "status", "set_provider",
    ],
)
def test_every_capability_is_reachable(method):
    assert hasattr(Agent(), method)


def test_the_two_dangerous_collisions_are_gone_by_construction():
    """These pointed at different databases; one silently won under MRO."""
    a = Agent()
    assert a.CONFIG_KEY != a.SESSION_KEY
    assert a.CONFIG_KEY == "AGENT_ENV_MIGRATED"
    assert a.SESSION_KEY.startswith("AGENT_DM_SESSIONS")


def test_no_shadowing_between_layers():
    """Each name resolves to exactly one class in the MRO."""
    mro = Agent.__mro__
    for name in ("status", "register", "respond", "stream", "run", "set_provider"):
        owners = [c.__name__ for c in mro if name in vars(c)]
        assert len(owners) <= 1, f"{name} defined on {owners}"


def test_miniai_returns_an_agent():
    result = asyncio.run(miniai())
    assert isinstance(result, Agent)


def test_the_process_agent_is_one_instance():
    assert agent() is agent()
    assert isinstance(agent(), Agent)


def test_legacy_provider_registry_facade_uses_the_process_agent():
    from pyUltroid.agent import registry

    provider = registry.by_name("gemini")
    assert provider is agent().provider_by_name("gemini")
    registry.register(provider)
    assert registry.by_name("gemini") is provider


def test_there_are_no_module_singletons_left():
    from pyUltroid.agent import _config, _lifecycle, _mini, _providers, _sessions

    for module, gone in (
        (_config, ("agent_config", "get_field", "set_field", "_udb", "_var")),
        (_sessions, ("session_store", "create_session", "sessions_for", "_udb")),
        (_providers, ("registry", "ollama", "cloudflare")),
        (_mini, ("mini",)),
        (_lifecycle, ("engine_lifecycle",)),
    ):
        for name in gone:
            assert not hasattr(module, name), f"{module.__name__}.{name}"


def test_the_config_var_hook_is_patchable():
    """It was a function-local import, so nothing could reach it."""
    from pyUltroid.agent import _config

    assert hasattr(_config, "Var")