paper-dynasty-discord/cogs/dev_tools.py
Cal Corum 777e6de3de feat: add CleanupView for refractor test data
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 07:06:37 -05:00

78 lines
2.3 KiB
Python

"""Dev-only tools for testing Paper Dynasty systems.
This cog is only loaded when DATABASE != prod. It provides commands
for integration testing that create and clean up synthetic test data.
"""
import logging
import discord
from discord import app_commands
from discord.ext import commands
from api_calls import db_delete
logger = logging.getLogger(__name__)
class CleanupView(discord.ui.View):
"""Post-test buttons to clean up or keep synthetic game data."""
def __init__(
self, owner_id: int, game_id: int, embed: discord.Embed, timeout: float = 300.0
):
super().__init__(timeout=timeout)
self.owner_id = owner_id
self.game_id = game_id
self.embed = embed
@discord.ui.button(
label="Clean Up Test Data", style=discord.ButtonStyle.danger, emoji="🧹"
)
async def cleanup_btn(
self, interaction: discord.Interaction, button: discord.ui.Button
):
if interaction.user.id != self.owner_id:
return
try:
await db_delete("decisions/game", self.game_id)
await db_delete("plays/game", self.game_id)
await db_delete("games", self.game_id)
self.embed.add_field(
name="",
value=f"🧹 Test data cleaned up (game #{self.game_id} removed)",
inline=False,
)
except Exception as e:
self.embed.add_field(
name="",
value=f"❌ Cleanup failed: {e}",
inline=False,
)
self.clear_items()
await interaction.response.edit_message(embed=self.embed, view=self)
self.stop()
@discord.ui.button(
label="Keep Test Data", style=discord.ButtonStyle.secondary, emoji="📌"
)
async def keep_btn(
self, interaction: discord.Interaction, button: discord.ui.Button
):
if interaction.user.id != self.owner_id:
return
self.embed.add_field(
name="",
value=f"📌 Test data kept (game #{self.game_id})",
inline=False,
)
self.clear_items()
await interaction.response.edit_message(embed=self.embed, view=self)
self.stop()
async def on_timeout(self):
self.clear_items()