fix(bot): fix raw_content round-trip and parsing consistency
- Remove 'or (empty)' fallback in build_ping_embed to preserve empty strings in raw_content - Fix parse_ping_embed to use direct indexing for all required fields (modality, raw_content, captured_at) - Remove unused timezone import - Add test case for empty raw_content round-trip to catch future regressions Fixes code review findings: empty raw_content was being converted to literal "(empty)" string, breaking lossless round-trip requirement needed by Task 7's thread-open handler.
This commit is contained in:
parent
af547ab02c
commit
9971632cd1
2 changed files with 18 additions and 4 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
MODALITY_LABELS = {
|
MODALITY_LABELS = {
|
||||||
|
|
@ -24,15 +24,15 @@ def build_ping_embed(
|
||||||
embed.add_field(name="modality", value=modality, inline=True)
|
embed.add_field(name="modality", value=modality, inline=True)
|
||||||
embed.add_field(name="sender_id", value=sender_id or "", inline=True)
|
embed.add_field(name="sender_id", value=sender_id or "", inline=True)
|
||||||
embed.add_field(name="captured_at", value=captured_at.isoformat(), inline=True)
|
embed.add_field(name="captured_at", value=captured_at.isoformat(), inline=True)
|
||||||
embed.add_field(name="raw_content", value=raw_content or "(empty)", inline=False)
|
embed.add_field(name="raw_content", value=raw_content, inline=False)
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
def parse_ping_embed(embed: discord.Embed) -> dict:
|
def parse_ping_embed(embed: discord.Embed) -> dict:
|
||||||
values = {field.name: field.value for field in embed.fields}
|
values = {field.name: field.value for field in embed.fields}
|
||||||
return {
|
return {
|
||||||
"modality": values.get("modality"),
|
"modality": values["modality"],
|
||||||
"sender_id": values.get("sender_id") or None,
|
"sender_id": values.get("sender_id") or None,
|
||||||
"raw_content": values.get("raw_content"),
|
"raw_content": values["raw_content"],
|
||||||
"captured_at": datetime.fromisoformat(values["captured_at"]),
|
"captured_at": datetime.fromisoformat(values["captured_at"]),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,17 @@ def test_build_ping_embed_handles_missing_sender():
|
||||||
)
|
)
|
||||||
parsed = ping.parse_ping_embed(embed)
|
parsed = ping.parse_ping_embed(embed)
|
||||||
assert parsed["sender_id"] is None
|
assert parsed["sender_id"] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_and_parse_empty_raw_content():
|
||||||
|
"""Verify empty raw_content round-trips correctly without being converted to '(empty)'."""
|
||||||
|
captured_at = datetime(2026, 7, 13, 9, 0, 0, tzinfo=timezone.utc)
|
||||||
|
embed = ping.build_ping_embed(
|
||||||
|
modality="bh_email",
|
||||||
|
sender_id="test@example.com",
|
||||||
|
snippet="Empty message",
|
||||||
|
raw_content="",
|
||||||
|
captured_at=captured_at,
|
||||||
|
)
|
||||||
|
parsed = ping.parse_ping_embed(embed)
|
||||||
|
assert parsed["raw_content"] == "", "Empty raw_content should round-trip as empty string, not '(empty)'"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue