aboutsummaryrefslogtreecommitdiffstats
path: root/tests/conftest.py
blob: 6b7c3bca98732d68333cddae157ce4bb4b36ea5f (plain) (blame)
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
228
import asyncio
import os
import sys
import tempfile
from unittest.mock import AsyncMock, MagicMock
import pytest

import discord
from discord.ext import commands

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))


@pytest.fixture(autouse=True, scope="function")
def setup_test_env(monkeypatch):
    test_db = os.path.join(tempfile.gettempdir(), "test_senator.db")
    if os.path.exists(test_db):
        os.remove(test_db)
    monkeypatch.setenv("SENATOR_DB_PATH", test_db)
    reload_database()
    yield
    if os.path.exists(test_db):
        os.remove(test_db)


def reload_database():
    import cogs.utils.database as db_module
    db_module._initialized = False


class MockUser:
    def __init__(self, id=12345, name="testuser", discriminator="0001"):
        self.id = id
        self.name = name
        self.discriminator = discriminator
        self.mention = f"@{name}"
        self.display_name = name
        self.avatar = None
        self.bot = False
        self.guild = None
        self._state = MagicMock()

    def __str__(self):
        return f"{self.name}#{self.discriminator}"


class MockChannel:
    def __init__(self, id=11111, name="test-channel", type=discord.ChannelType.text):
        self.id = id
        self.name = name
        self.type = type
        self.mention = f"#{name}"
        self.guild = None
        self.category = None
        self._state = MagicMock()

    def send(self, *args, **kwargs):
        return MagicMock()


class MockTextChannel(MockChannel):
    def __init__(self, id=11111, name="test-channel"):
        super().__init__(id, name, discord.ChannelType.text)


class MockVoiceChannel(MockChannel):
    def __init__(self, id=11112, name="test-voice"):
        super().__init__(id, name, discord.ChannelType.voice)
        self.voice_states = {}


class MockGuild:
    def __init__(self, id=99999, name="Test Server"):
        self.id = id
        self.name = name
        self.channels = []
        self.roles = []
        self.emojis = []
        self.me = MockUser(id=111111, name="Sen Bot")
        self._state = MagicMock()
        self._default_role = None

    def get_channel(self, channel_id):
        for ch in self.channels:
            if ch.id == channel_id:
                return ch
        return None

    def get_role(self, role_id):
        for role in self.roles:
            if role.id == role_id:
                return role
        return None

    def get_member(self, member_id):
        return None


class MockMessage:
    def __init__(self, id=22222, content="test message", author=None, channel=None, guild=None):
        self.id = id
        self.content = content
        self.author = author or MockUser()
        self.channel = channel or MockChannel()
        self.guild = guild
        self.attachments = []
        self.embeds = []
        self.jump_url = "https://discord.com/channels/..."
        self.created_at = None

    async def reply(self, *args, **kwargs):
        return MagicMock()
        # Stub for reply - kept async to match Discord interface signature


class MockInteraction:
    def __init__(self, user=None, channel=None, guild=None):
        self.user = user or MockUser()
        self.channel = channel or MockChannel()
        self.guild = guild or MockGuild()
        self.message = None
        self.response = AsyncMock()
        self.followup = AsyncMock()
        self.client = None
        self.id = 33333
        self.token = "test_token"
        self._state = MagicMock()

    async def defer(self, *args, **kwargs):
        pass

    async def respond(self, *args, **kwargs):
        pass

    async def delete_original_response(self, *args, **kwargs):
        pass

    async def send(self, *args, **kwargs):
        return MagicMock()


# Stub for Discord interaction - test implementation only
class MockApplicationContext:
    def __init__(self, bot=None, interaction=None, user=None, channel=None, guild=None):
        self.bot = bot
        self.interaction = interaction or MockInteraction(user=user, channel=channel, guild=guild)
        self.user = user or MockUser()
        self.channel = channel or MockChannel()
        self.guild = guild or MockGuild()
        self.message = None
        self.response = AsyncMock()
        self.followup = AsyncMock()
        self.client = self.bot
        self.id = 33333
        self.token = "test_token"
        self._state = self.interaction._state
        self.author = self.user

    async def defer(self, *args, **kwargs):
        pass
        # Stub for defer - tests don't verify interaction state

    async def respond(self, *args, **kwargs):
        pass
        # Stub for respond - tests mock the response

    async def delete_original_response(self, *args, **kwargs):
        pass
        # Stub for delete_original_response - not used in tests

    async def send(self, *args, **kwargs):
        return MagicMock()
        # Stub for send - not used in tests

    async def reply(self, *args, **kwargs):
        return MagicMock()
        # Stub for reply - not used in tests

    async def send_followup(self, *args, **kwargs):
        return MagicMock()
        # Stub for send_followup - not used in tests


@pytest.fixture
def mock_guild():
    return MockGuild()


@pytest.fixture
def mock_channel(mock_guild):
    channel = MockTextChannel()
    channel.guild = mock_guild
    return channel


@pytest.fixture
def mock_voice_channel(mock_guild):
    channel = MockVoiceChannel()
    channel.guild = mock_guild
    return channel


@pytest.fixture
def mock_user():
    return MockUser()


@pytest.fixture
def mock_member(mock_guild):
    member = MockUser()
    member.guild = mock_guild
    return member


@pytest.fixture
def mock_message(mock_channel, mock_user, mock_guild):
    return MockMessage(channel=mock_channel, author=mock_user, guild=mock_guild)


@pytest.fixture
def mock_interaction(mock_channel, mock_user, mock_guild):
    return MockInteraction(user=mock_user, channel=mock_channel, guild=mock_guild)


@pytest.fixture
def bot():
    intents = discord.Intents.all()
    return commands.Bot(intents=intents, command_prefix="!", loop=asyncio.new_event_loop())