File size: 4,146 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
"""Regression tests for actionable runtime failures seen in the live log."""
from __future__ import annotations

import ast
import os
from pathlib import Path

from conftest import ROOT


def _tree(relative_path: str) -> ast.Module:
    return ast.parse((ROOT / relative_path).read_text(encoding="utf-8"))


def _attribute_path(node: ast.AST) -> list[str]:
    if isinstance(node, ast.Name):
        return [node.id]
    if isinstance(node, ast.Attribute):
        return [*_attribute_path(node.value), node.attr]
    return []


def test_botchat_handlers_use_the_live_database_facade():
    tree = _tree("assistant/pmbot.py")
    imports_db = any(
        isinstance(node, ast.ImportFrom)
        and node.module == "pyUltroid.dB"
        and any(alias.name == "db" for alias in node.names)
        for node in tree.body
    )
    call_paths = [
        _attribute_path(node.func)
        for node in ast.walk(tree)
        if isinstance(node, ast.Call)
    ]
    names = {
        node.id for node in ast.walk(tree) if isinstance(node, ast.Name)
    }

    assert imports_db
    assert ["db", "botchat", "add"] in call_paths
    assert call_paths.count(["db", "botchat", "get_who"]) == 3
    assert "add_stuff" not in names
    assert "get_who" not in names


def test_filename_checker_accepts_legacy_single_path_calls(tmp_path):
    tree = _tree("pyUltroid/fns/tools.py")
    toolkit = next(
        node
        for node in tree.body
        if isinstance(node, ast.ClassDef) and node.name == "ToolKit"
    )
    method = next(
        node
        for node in toolkit.body
        if isinstance(node, ast.FunctionDef) and node.name == "check_filename"
    )
    isolated_class = ast.ClassDef(
        name="ToolKit",
        bases=[],
        keywords=[],
        body=[method],
        decorator_list=[],
    )
    module = ast.fix_missing_locations(
        ast.Module(body=[isolated_class], type_ignores=[])
    )
    namespace = {"os": os}
    exec(compile(module, "pyUltroid/fns/tools.py", "exec"), namespace)

    target = tmp_path / "voice.mp3"
    target.write_bytes(b"test")
    expected = str(tmp_path / "voice_1.mp3")
    assert namespace["ToolKit"].check_filename(str(target)) == expected
    assert namespace["ToolKit"]().check_filename(str(target)) == expected


def test_file_list_plugin_binds_its_byte_formatter():
    tree = _tree("plugins/tools.py")
    assert any(
        isinstance(node, ast.Assign)
        and any(isinstance(target, ast.Name) and target.id == "hb" for target in node.targets)
        and isinstance(node.value, ast.Attribute)
        and node.value.attr == "humanbytes"
        and isinstance(node.value.value, ast.Name)
        and node.value.value.id == "fns"
        for node in tree.body
    )


def test_gpt_addon_calls_the_shared_agent_accessor():
    tree = _tree("addons/gpt.py")
    uses_accessor = any(
        isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and node.func.attr == "respond"
        and isinstance(node.func.value, ast.Call)
        and isinstance(node.func.value.func, ast.Name)
        and node.func.value.func.id == "agent"
        for node in ast.walk(tree)
    )
    calls_function_as_object = any(
        isinstance(node, ast.Attribute)
        and node.attr == "respond"
        and isinstance(node.value, ast.Name)
        and node.value.id == "agent"
        for node in ast.walk(tree)
    )

    assert uses_accessor
    assert not calls_function_as_object


def test_quote_font_loader_falls_back_to_an_installed_bundled_font():
    from PIL import ImageFont

    tree = _tree("addons/quote.py")
    loader = next(
        node
        for node in tree.body
        if isinstance(node, ast.FunctionDef) and node.name == "_quote_font"
    )
    module = ast.fix_missing_locations(
        ast.Module(body=[loader], type_ignores=[])
    )
    namespace = {
        "ImageFont": ImageFont,
        "Path": Path,
        "__file__": str(ROOT / "addons" / "quote.py"),
    }
    exec(compile(module, "addons/quote.py", "exec"), namespace)

    font = namespace["_quote_font"]("missing-font.ttf", 20)
    assert font.getbbox("Quote")