File size: 3,349 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
"""The core union: seven subsystems, one process object, attribute access.

The import-side facts come from ``tests/_core_probe.py`` in a clean
interpreter, because the core modules drag in the loader, the queue and the
webupload chain and the rest of the suite leaves its own stubs behind. The
source-side facts are read with ast, which needs no imports.
"""

from __future__ import annotations

import ast
import json
import subprocess
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]
CORE_DIR = ROOT / "pyUltroid" / "_core"

SINGLETONS = (
    "_default_loader",
    "plugin_help",
    "registry",
    "manifest_registry",
    "toggle_registry",
    "inline_cache",
    "queue_manager",
)


@pytest.fixture(scope="module")
def probe() -> dict:
    done = subprocess.run(
        [sys.executable, str(ROOT / "tests" / "_core_probe.py")],
        cwd=ROOT,
        capture_output=True,
        text=True,
        timeout=300,
    )
    assert done.returncode == 0, done.stderr
    return json.loads(done.stdout.strip().splitlines()[-1])


def test_core_owns_seven_constructed_subsystems(probe):
    """The constructed half: the seven plugin-runtime registries."""
    assert probe["components"] == 7
    assert probe["all_distinct"] is True
    assert probe["types"] == [
        "AsyncPluginLoader",
        "InlineResultCache",
        "ManifestRegistry",
        "PluginHelp",
        "PluginRegistry",
        "QueueManager",
        "ToggleRegistry",
    ]


def test_core_is_assembled_from_the_domain_mixins(probe):
    """W9: the union visibly composes - one class from the domain mixins."""
    assert probe["mro"] == [
        "Ultroid",
        "DBMixin",
        "ServiceMixin",
        "ClientMixin",
        "RegistryMixin",
        "RuntimeMixin",
        "object",
    ]


def test_core_slots_cover_constructed_and_bound_halves(probe):
    """W8: the runtime-state slots join the constructed ones."""
    assert probe["attributes"] == sorted(
        (
            # constructed (import time)
            "inline_cache",
            "loader",
            "manifest",
            "plugin_help",
            "queue",
            "registry",
            "toggles",
            # bound (bootstrap time)
            "db",
            "fns",
            "master",
            "misc",
            "var",
            # the client domain
            "_voice",
            # the lifecycle (W9 runtime)
            "_boot_at",
            "_hosted_on",
            "_prefix",
            "_universal_lifecycle",
        )
    )


def test_no_singleton_is_reexported(probe):
    assert probe["lazy_singletons"] == []


@pytest.mark.parametrize(
    "module,indicator",
    [
        ("loader.py", ("_default_loader", "plugin_help", "registry")),
        ("manifest.py", ("manifest_registry",)),
        ("toggles.py", ("toggle_registry",)),
        ("events.py", ("inline_cache",)),
        ("queue.py", ("queue_manager",)),
    ],
)
def test_no_module_singleton_is_assigned(module, indicator):
    tree = ast.parse((CORE_DIR / module).read_text())
    assigned = {
        t.id
        for node in tree.body
        if isinstance(node, ast.Assign)
        for t in node.targets
        if isinstance(t, ast.Name)
    }
    assert not assigned & set(indicator), f"{module} still builds a singleton"