Initial commit: APIcord Discord API Emulator

- Core infrastructure: config, state management, models
- REST API server (aiohttp) with Discord-compatible endpoints
- WebSocket Gateway server (websockets) with Discord Gateway protocol
- State management with guilds, channels, users, messages, interactions
- Pydantic models for Discord objects (User, Guild, Channel, Message, etc.)
- CLI with start, test, reset, export, validate commands
- YAML-based test scenario framework
- Basic test scenario for /server_status command flow
- Unit tests for models and state manager (19 passing)
- GPL-3.0 license
This commit is contained in:
k.eaven committed 2026-10-31 11:43:57 +00:00
commit 5669048f89
38 files changed
+2871

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
"""Tests for Pydantic Models"""
import pytest
from apicord.models.discord import (
User,
BotUser,
Role,
Channel,
ChannelType,
Guild,
Message,
Interaction,
InteractionType,
Snowflake,
)
class TestSnowflake:
def test_snowflake_from_string(self):
sf = Snowflake("123456789012345678")
assert sf == "123456789012345678"
def test_snowflake_from_int(self):
sf = Snowflake(123456789012345678)
assert sf == "123456789012345678"
def test_snowflake_invalid(self):
with pytest.raises(ValueError):
Snowflake.validate("invalid", None)
class TestUser:
def test_user_creation(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
bot=False,
)
assert user.id == "123456789012345678"
assert user.username == "TestUser"
assert user.discriminator == "1234"
assert user.bot is False
def test_user_with_guild_roles(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
guild_roles={"111111111111111111": ["222222222222222222"]},
)
assert "111111111111111111" in user.guild_roles
assert "222222222222222222" in user.guild_roles["111111111111111111"]
class TestBotUser:
def test_bot_user_creation(self):
bot = BotUser(
id="444444444444444444",
username="TestBot",
discriminator="0000",
token="test_token_123",
)
assert bot.id == "444444444444444444"
assert bot.bot is True
assert bot.token == "test_token_123"
class TestRole:
def test_role_creation(self):
role = Role(
id="222222222222222222",
name="Game Server User",
color=3447003,
permissions="104324161",
)
assert role.name == "Game Server User"
assert role.color == 3447003
class TestChannel:
def test_text_channel(self):
channel = Channel(
id="111111111111111111",
type=ChannelType.GUILD_TEXT,
guild_id="123456789012345678",
name="general",
)
assert channel.type == ChannelType.GUILD_TEXT
assert channel.guild_id == "123456789012345678"
assert channel.name == "general"
def test_voice_channel(self):
channel = Channel(
id="111111111111111112",
type=ChannelType.GUILD_VOICE,
guild_id="123456789012345678",
name="voice-chat",
)
assert channel.type == ChannelType.GUILD_VOICE
class TestGuild:
def test_guild_creation(self):
guild = Guild(
id="123456789012345678",
name="Test Guild",
owner_id="111111111111111111",
)
assert guild.name == "Test Guild"
assert guild.owner_id == "111111111111111111"
assert guild.roles == []
assert guild.channels == []
class TestMessage:
def test_message_creation(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
)
message = Message(
id="999999999999999999",
channel_id="111111111111111111",
author=user,
content="Hello, world!",
timestamp="2024-01-01T00:00:00.000000+00:00",
)
assert message.content == "Hello, world!"
assert message.author.username == "TestUser"
class TestInteraction:
def test_interaction_creation(self):
interaction = Interaction(
id="888888888888888888",
type=InteractionType.APPLICATION_COMMAND,
application_id="444444444444444444",
token="interaction_token_123",
)
assert interaction.type == InteractionType.APPLICATION_COMMAND
assert interaction.token == "interaction_token_123"
assert interaction.responded is False
+83
View File
@@ -0,0 +1,83 @@
"""Tests for State Manager"""
import pytest
from apicord.config import EmulatorConfig
from apicord.state.manager import StateManager
from apicord.models.discord import User, Role, Channel, ChannelType, Guild
class TestStateManager:
@pytest.fixture
def config(self):
return EmulatorConfig(
bot_token="test_token_123",
bot_id="444444444444444444",
persist_state=False,
)
@pytest.fixture
async def state_manager(self, config):
manager = StateManager(config)
await manager.create_default_state(guilds=1, channels_per_guild=2, users=3)
return manager
@pytest.mark.asyncio
async def test_create_default_state(self, state_manager):
assert len(state_manager.guilds) == 1
assert len(state_manager.channels) == 2
assert len(state_manager.users) == 4 # 3 test users + bot
@pytest.mark.asyncio
async def test_bot_user_created(self, state_manager):
bot = state_manager.get_bot_user()
assert bot is not None
assert bot.id == "444444444444444444"
assert bot.username == "TestBot"
assert bot.bot is True
@pytest.mark.asyncio
async def test_get_guild(self, state_manager):
guild = state_manager.get_guild(list(state_manager.guilds.keys())[0])
assert guild is not None
assert isinstance(guild, Guild)
@pytest.mark.asyncio
async def test_get_channel(self, state_manager):
channel = state_manager.get_channel(list(state_manager.channels.keys())[0])
assert channel is not None
assert isinstance(channel, Channel)
@pytest.mark.asyncio
async def test_get_user(self, state_manager):
user = state_manager.get_user(list(state_manager.users.keys())[0])
assert user is not None
assert isinstance(user, User)
@pytest.mark.asyncio
async def test_add_message(self, state_manager):
from apicord.models.discord import Message
from datetime import datetime
channel_id = list(state_manager.channels.keys())[0]
message = Message(
id="999999999999999999",
channel_id=channel_id,
author=state_manager.get_bot_user(),
content="Test message",
timestamp=datetime.utcnow().isoformat() + "Z",
)
state_manager.add_message(message)
retrieved = state_manager.get_message(message.id)
assert retrieved is not None
assert retrieved.content == "Test message"
@pytest.mark.asyncio
async def test_state_summary(self, state_manager):
summary = state_manager.get_state_summary()
assert "guilds" in summary
assert "channels" in summary
assert "users" in summary
assert summary["guilds"] == 1
assert summary["channels"] == 2
assert summary["users"] == 4 # 3 + bot