Files
Advanced-Community-Bot/tests/test_space_creation_simple.py
T
Dome edd3eee178 feat: native matrix URI pills for {user}/{room} + major rendering & codebase refactor
This change introduces native `matrix:` URI-based rendering for `{user}` and `{room}` placeholders,
replacing previous plaintext and matrix.to-based links. Users and rooms are now rendered as clickable
pills in supporting clients, with a clean display using display names and room names (no @/# prefixes).

Reporting, moderation, and auto-redaction messages have been updated to use the same rendering logic.
Inspect and event links now also use native `matrix:` URIs for direct in-client navigation.

Internally, URI generation and rendering logic have been unified via central helper functions,
ensuring consistent handling of user IDs, room IDs, aliases, and event IDs.

This commit also includes a broader refactor of the codebase:
- decomposed complex flows (e.g. join handling) into smaller helpers
- moved mutable class-level state to instance-level
- reduced duplicate API calls and redundant logic
- improved overall structure and maintainability

Test coverage has been extended for URI helpers and rendering logic to prevent regressions.

No breaking changes to existing template parameters like `{user_link}` or `{room_link}`.
2026-04-11 20:21:33 +02:00

125 lines
4.6 KiB
Python

"""Simple tests for space creation functionality."""
import pytest
from unittest.mock import Mock, AsyncMock
class TestSpaceCreationSimple:
"""Simple tests for space creation functionality."""
def test_get_space_roomlist_empty_parent_room(self):
"""Test get_space_roomlist with empty parent room."""
from community.bot import CommunityBot
# Create a mock bot instance
bot = Mock(spec=CommunityBot)
bot.config = {"parent_room": ""}
bot.log = Mock()
bot.client = Mock()
# Mock the get_space_roomlist method
bot.get_space_roomlist = AsyncMock(return_value=[])
# Test that empty parent room returns empty list
import asyncio
result = asyncio.run(bot.get_space_roomlist())
assert result == []
def test_get_space_roomlist_with_parent_room(self):
"""Test get_space_roomlist with configured parent room."""
from community.bot import CommunityBot
# Create a mock bot instance
bot = Mock(spec=CommunityBot)
bot.config = {"parent_room": "!space:example.com"}
bot.log = Mock()
bot.client = Mock()
bot.client.get_state = AsyncMock(return_value=[])
# Mock the get_space_roomlist method
bot.get_space_roomlist = AsyncMock(return_value=["!room1:example.com", "!room2:example.com"])
# Test that configured parent room returns room list
import asyncio
result = asyncio.run(bot.get_space_roomlist())
assert result == ["!room1:example.com", "!room2:example.com"]
def test_space_creation_parameters(self):
"""Test that space creation parameters are correct."""
# Test that the space creation logic uses correct parameters
creation_content = {
"type": "m.space",
"m.federate": True,
"m.room.history_visibility": "joined"
}
# Verify the creation content has the correct space type
assert creation_content["type"] == "m.space"
assert creation_content["m.federate"] is True
assert creation_content["m.room.history_visibility"] == "joined"
def test_power_level_verification_modern_room(self):
"""Test power level verification for modern room versions."""
# Test that modern room version verification logic is correct
room_version = "12"
is_modern = int(room_version) >= 12
assert is_modern is True
# For modern rooms, creators have unlimited power and don't appear in power levels
power_levels = {"users": {}}
bot_power_level = power_levels.get("users", {}).get("@bot:example.com")
# Bot should not have a power level in modern rooms (unlimited power)
assert bot_power_level is None
def test_power_level_verification_legacy_room(self):
"""Test power level verification for legacy room versions."""
# Test that legacy room version verification logic is correct
room_version = "1"
is_modern = int(room_version) >= 12
assert is_modern is False
# For legacy rooms, bot should have power level 1000
power_levels = {"users": {"@bot:example.com": 1000}}
bot_power_level = power_levels.get("users", {}).get("@bot:example.com")
assert bot_power_level == 1000
def test_space_type_verification(self):
"""Test space type verification logic."""
# Mock state events
state_events = [
Mock(type="m.room.create", content={"type": "m.space"}),
Mock(type="m.room.power_levels", content={})
]
# Find the room create event
space_type_set = False
for event in state_events:
if event.type == "m.room.create":
space_type = event.content.get("type")
space_type_set = (space_type == "m.space")
break
assert space_type_set is True
def test_space_type_not_set(self):
"""Test space type verification when type is not set."""
# Mock state events with wrong type
state_events = [
Mock(type="m.room.create", content={"type": "m.room"}),
Mock(type="m.room.power_levels", content={})
]
# Find the room create event
space_type_set = False
for event in state_events:
if event.type == "m.room.create":
space_type = event.content.get("type")
space_type_set = (space_type == "m.space")
break
assert space_type_set is False