File size: 7,295 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""The W8 union surface: runtime state bound by the bootstrap.

The core union's bound half - db/var/master/fns/misc - degrades to ``None``
unbound (a library import never raises), warns loudly on a *different-object*
rebind (an import-time capture desyncing from the live object is the bug the
re-point pass exists to prevent), and derives the client/role/dispatcher
surface from the bound master.
"""

from __future__ import annotations

import logging
import types

import pytest


def _load_core_union():
    """Import the composition root (``pyUltroid.ultroid``) against a stubbed root."""
    import importlib
    import sys

    saved = {
        n: m
        for n, m in sys.modules.items()
        if n.split(".")[0] in ("pyUltroid", "telethon")
    }
    for n in saved:
        sys.modules.pop(n, None)
    try:
        root = types.ModuleType("pyUltroid")
        root.__path__ = ["pyUltroid"]
        root.LOGS = logging.getLogger("core-union-tests")
        sys.modules["pyUltroid"] = root
        startup = types.ModuleType("pyUltroid.startup")
        logger = types.ModuleType("pyUltroid.startup.logger")
        logger.LOGS = root.LOGS
        sys.modules["pyUltroid.startup"] = startup
        sys.modules["pyUltroid.startup.logger"] = logger
        return importlib.import_module("pyUltroid.ultroid")
    finally:
        for n in [n for n in sys.modules if n.split(".")[0] in ("pyUltroid", "telethon")]:
            sys.modules.pop(n, None)
        sys.modules.update(saved)


@pytest.fixture
def union():
    module = _load_core_union()
    fresh = module.Ultroid()
    return types.SimpleNamespace(module=module, core=fresh)


class FakeDB:
    def __init__(self) -> None:
        self.data = {}

    def get_key(self, key):
        return self.data.get(key)

    def set_key(self, key, value):
        self.data[key] = value

    def del_key(self, key):
        self.data.pop(key, None)


class FakeClient:
    def __init__(self, name: str) -> None:
        self.name = name
        self.dispatcher = types.SimpleNamespace(name=f"dispatcher-{name}")


class FakeMaster:
    def __init__(self, bot=None, user=None, app=None) -> None:
        self.bot = bot
        self.user = user
        self.app = app
        self._by_role = {"user": [], "bot": [], "business": [], "vc": []}
        for role, client in (("user", user), ("bot", bot), ("vc", app)):
            if client is not None:
                self._by_role[role].append(client)

    def clients_by_role(self, role):
        return tuple(self._by_role.get(role, ()))

    @property
    def telegram_clients(self):
        seen = []
        for role in ("user", "bot", "vc"):
            for client in self._by_role[role]:
                if client not in seen:
                    seen.append(client)
        return tuple(seen)

    def route(self):
        return self.user or self.bot


# ── degradation ───────────────────────────────────────────────────────────


def test_unbound_reads_degrade_to_none(union):
    core = union.core
    assert core.db is None and core.var is None and core.master is None
    assert core.fns is None and core.misc is None
    assert core.bot is None and core.user is None and core.app is None
    assert core.clients == () and core.dispatchers == ()
    assert core.available_roles() == ()
    assert core.has_role("user") is False
    assert core.clients_by_role("bot") == ()
    assert core.route_client() is None
    assert core.modules is None
    # db verbs are no-ops (returns) rather than raises
    assert core.db_get("x", "default") == "default"
    core.db_set("x", 1)
    core.db_del("x")


def test_status_reports_unbound_state(union):
    status = union.core.status()
    assert status["bound"] == {
        "db": False,
        "var": False,
        "master": False,
        "fns": False,
        "misc": False,
    }
    assert status["plugins"] == 0 and status["modules"] == 0
    assert status["roles"] == () and status["clients"] == 0


# ── binding ──────────────────────────────────────────────────────────────


def test_bind_db_and_verbs(union):
    core = union.core
    db = FakeDB()
    core.bind_db(db)
    assert core.db is db
    core.db_set("k", "v")
    assert core.db_get("k") == "v" and db.data["k"] == "v"
    core.db_del("k")
    assert core.db_get("k") is None


def test_rebind_same_object_is_silent(union, caplog):
    core = union.core
    db = FakeDB()
    core.bind_db(db)
    with caplog.at_level(logging.WARNING):
        core.bind_db(db)
    assert not [r for r in caplog.records if "rebound" in r.message]


def test_rebind_different_object_warns_loudly(union, caplog):
    core = union.core
    core.bind_db(FakeDB())
    with caplog.at_level(logging.WARNING):
        core.bind_db(FakeDB())
    assert any(
        "runtime rebind: core.db" in r.message
        for r in caplog.records
    ), "a different-object rebind must warn: captures would desync"


def test_bind_none_is_a_no_op(union):
    core = union.core
    db = FakeDB()
    core.bind_db(db)
    core.bind_db(None)
    assert core.db is db


# ── client surface ────────────────────────────────────────────────────────


def test_client_surface_derives_from_master(union):
    core = union.core
    bot, user = FakeClient("bot"), FakeClient("user")
    master = FakeMaster(bot=bot, user=user)
    core.bind_master(master)

    assert core.bot is bot and core.user is user and core.app is None
    assert set(core.clients) == {bot, user}
    assert {d.name for d in core.dispatchers} == {"dispatcher-bot", "dispatcher-user"}
    assert core.available_roles() == ("user", "bot")
    assert core.has_role("user") is True and core.has_role("vc") is False
    assert core.clients_by_role("bot") == (bot,)
    assert core.route_client() is user

    master._by_role["vc"].append(FakeClient("vc"))
    assert core.available_roles() == ("user", "bot", "vc")


def test_modules_registry_via_misc(union):
    core = union.core
    sentinel = types.SimpleNamespace(registry="the-module-registry")
    core.bind_misc(sentinel)
    assert core.modules == "the-module-registry"
    assert core.misc is sentinel


def test_status_reports_bound_state(union):
    core = union.core
    core.bind_db(FakeDB())
    core.bind_master(FakeMaster(bot=FakeClient("bot")))
    core.bind_misc(types.SimpleNamespace(registry=[object()]))
    status = core.status()
    assert status["bound"] == {
        "db": True,
        "var": False,
        "master": True,
        "fns": False,
        "misc": True,
    }
    assert status["roles"] == ("bot",) and status["clients"] == 1
    assert status["dispatchers"] == 1


# ── the constructed half is untouched ────────────────────────────────────


def test_constructed_half_still_seven(union):
    assert len(union.core.components()) == 7
    assert all(c is not None for c in union.core.components())