File size: 6,408 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for astrbot.core.star.base module."""

import pytest
from unittest.mock import AsyncMock, MagicMock, patch


class TestStarBase:
    """Test cases for the Star base class."""

    def test_star_class_exists(self):
        """Test that Star class can be imported."""
        from astrbot.core.star import Star

        assert Star is not None

    def test_star_init_with_context(self):
        """Test Star initialization with a context-like object."""
        from astrbot.core.star import Star

        # Create a mock context with get_config method
        mock_context = MagicMock()
        mock_context.get_config.return_value = MagicMock()

        # Create a concrete Star subclass for testing
        class TestStar(Star):
            name = "test_star"
            author = "test_author"

        star = TestStar(context=mock_context)

        assert star.context is mock_context

    @pytest.mark.asyncio
    async def test_text_to_image_with_config(self):
        """Test text_to_image method with valid config."""
        from astrbot.core.star import Star

        mock_context = MagicMock()
        mock_config = MagicMock()
        mock_config.get.return_value = "default_template"
        mock_context.get_config.return_value = mock_config

        class TestStar(Star):
            name = "test_star"
            author = "test_author"

        star = TestStar(context=mock_context)

        with patch(
            "astrbot.core.star.base.html_renderer.render_t2i",
            new_callable=AsyncMock,
        ) as mock_render:
            mock_render.return_value = "http://example.com/image.png"
            result = await star.text_to_image("test text", return_url=True)

            mock_render.assert_called_once_with(
                "test text",
                return_url=True,
                template_name="default_template",
            )
            assert result == "http://example.com/image.png"

    @pytest.mark.asyncio
    async def test_text_to_image_without_config(self):
        """Test text_to_image method when get_config returns None."""
        from astrbot.core.star import Star

        mock_context = MagicMock()
        mock_context.get_config.return_value = None

        class TestStar(Star):
            name = "test_star"
            author = "test_author"

        star = TestStar(context=mock_context)

        with patch(
            "astrbot.core.star.base.html_renderer.render_t2i",
            new_callable=AsyncMock,
        ) as mock_render:
            mock_render.return_value = "http://example.com/image.png"
            result = await star.text_to_image("test text", return_url=False)

            mock_render.assert_called_once_with(
                "test text",
                return_url=False,
                template_name=None,
            )
            assert result == "http://example.com/image.png"

    @pytest.mark.asyncio
    async def test_html_render(self):
        """Test html_render method."""
        from astrbot.core.star import Star

        mock_context = MagicMock()

        class TestStar(Star):
            name = "test_star"
            author = "test_author"

        star = TestStar(context=mock_context)

        with patch(
            "astrbot.core.star.base.html_renderer.render_custom_template",
            new_callable=AsyncMock,
        ) as mock_render:
            mock_render.return_value = "http://example.com/rendered.png"
            result = await star.html_render(
                "<html>{{ data }}</html>",
                {"data": "test"},
                return_url=True,
            )

            mock_render.assert_called_once_with(
                "<html>{{ data }}</html>",
                {"data": "test"},
                return_url=True,
                options=None,
            )
            assert result == "http://example.com/rendered.png"

    @pytest.mark.asyncio
    async def test_initialize_and_terminate(self):
        """Test that initialize and terminate methods can be overridden."""
        from astrbot.core.star import Star

        class TestStar(Star):
            name = "test_star"
            author = "test_author"

            async def initialize(self) -> None:
                self.initialized = True

            async def terminate(self) -> None:
                self.terminated = True

        mock_context = MagicMock()
        star = TestStar(context=mock_context)

        await star.initialize()
        assert star.initialized is True

        await star.terminate()
        assert star.terminated is True

    def test_star_metadata_registration(self):
        """Test that Star subclass is automatically registered."""
        from astrbot.core.star import star_map, star_registry
        from astrbot.core.star.star import StarMetadata

        # Clear any previous registration for this test module
        module_path = __name__

        class UniqueTestStar:
            """Not a Star subclass, should not be registered."""
            pass

        # Verify Star subclass gets registered
        initial_count = len(star_registry)

        # Note: This test verifies the __init_subclass__ mechanism
        # The actual registration happens when a class inherits from Star
        assert len(star_registry) >= initial_count


class TestNoCircularImports:
    """Test that there are no circular import issues."""

    def test_import_star_module(self):
        """Test that star module can be imported without circular import errors."""
        import astrbot.core.star

        assert astrbot.core.star is not None

    def test_import_pipeline_module(self):
        """Test that pipeline module can be imported without circular import errors."""
        import astrbot.core.pipeline

        assert astrbot.core.pipeline is not None

    def test_import_both_modules(self):
        """Test that both modules can be imported together."""
        import astrbot.core.pipeline
        import astrbot.core.star

        # Verify key exports are available
        from astrbot.core.star import Context, Star, PluginManager

        assert Context is not None
        assert Star is not None
        assert PluginManager is not None

    def test_import_pipeline_context(self):
        """Test that PipelineContext can be imported."""
        from astrbot.core.pipeline.context import PipelineContext

        assert PipelineContext is not None