Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/gw2/cogs/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,10 @@ async def session(ctx):
embed.add_field(name=gw2_messages.ACCOUNT_NAME, value=chat_formatting.inline(acc_name))
embed.add_field(name=gw2_messages.SERVER, value=chat_formatting.inline(gw2_server))

# Play time from API age (actual in-game time) — only when both snapshots have age
if "age" in rs_start and "age" in rs_end:
play_time_seconds = rs_end["age"] - rs_start["age"]
if play_time_seconds > 0:
play_time_str = gw2_utils.format_seconds_to_time(play_time_seconds)
else:
play_time_str = str(time_passed.timedelta)
# Play time from API age (actual in-game time)
play_time_seconds = rs_end["age"] - rs_start["age"]
if play_time_seconds > 0:
play_time_str = gw2_utils.format_seconds_to_time(play_time_seconds)
else:
play_time_str = str(time_passed.timedelta)
embed.add_field(name=gw2_messages.PLAY_TIME, value=chat_formatting.inline(play_time_str))
Expand Down Expand Up @@ -218,15 +215,16 @@ def _add_deaths_field(embed: discord.Embed, rs_chars_start: list[dict], rs_chars
profession = char_start["profession"]
time_deaths = int(char_end["deaths"]) - int(char_start["deaths"])
total_deaths += time_deaths
death_lines.append(f"{profession}: {name} - {time_deaths}")
death_lines.append(f"{name} ({profession}): {time_deaths}")

if death_lines:
death_lines.append(f"Total: {total_deaths}")
value = "\n".join(death_lines)
# Truncate if it would exceed Discord's 1024-char field value limit
if len(value) > 1020:
value = value[:1017] + "..."
embed.add_field(
name=f"{gw2_messages.TIMES_YOU_DIED}: {total_deaths}",
name=gw2_messages.TIMES_YOU_DIED,
value=chat_formatting.inline(value),
inline=False,
)
Expand Down
48 changes: 17 additions & 31 deletions tests/unit/gw2/cogs/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ async def test_session_play_time_from_api_age(self, mock_ctx, sample_api_key_dat
assert "2h 30m" in play_time_field.value

@pytest.mark.asyncio
async def test_session_play_time_fallback_when_no_age(self, mock_ctx, sample_api_key_data, sample_time_passed):
"""Test that play time falls back to timedelta when age is 0/missing."""
async def test_session_play_time_fallback_when_zero_age(self, mock_ctx, sample_api_key_data, sample_time_passed):
"""Test that play time falls back to timedelta when age diff is 0."""
session_data = _make_session_data(
start_overrides={"age": 0},
end_overrides={"age": 0},
start_overrides={"age": 5000000},
end_overrides={"age": 5000000},
)
runner = self._run_session(mock_ctx, sample_api_key_data, session_data, sample_time_passed)
async with runner.run() as r:
Expand All @@ -365,22 +365,6 @@ async def test_session_play_time_fallback_when_no_age(self, mock_ctx, sample_api
assert play_time_field is not None
assert "2:30:00" in play_time_field.value

@pytest.mark.asyncio
async def test_session_play_time_fallback_when_age_missing_from_start(
self, mock_ctx, sample_api_key_data, sample_time_passed
):
"""Test that play time falls back to wall-clock when age key is missing from start."""
# Remove age from start entirely to simulate stale data
session_data = _make_session_data(end_overrides={"age": 5009000})
del session_data[0]["start"]["age"]
runner = self._run_session(mock_ctx, sample_api_key_data, session_data, sample_time_passed)
async with runner.run() as r:
await session(mock_ctx)
embed = r.mock_send.call_args[0][1]
play_time_field = next((f for f in embed.fields if f.name == "Play time"), None)
assert play_time_field is not None
assert "2:30:00" in play_time_field.value

# === Gold tests ===

@pytest.mark.asyncio
Expand Down Expand Up @@ -441,11 +425,11 @@ async def test_session_characters_with_deaths(self, mock_ctx, sample_api_key_dat
r.mock_chars_dal.get_all_end_characters = AsyncMock(return_value=chars_end)
await session(mock_ctx)
embed = r.mock_send.call_args[0][1]
deaths_field = next((f for f in embed.fields if "Times you died" in f.name), None)
deaths_field = next((f for f in embed.fields if f.name == "Times you died"), None)
assert deaths_field is not None
assert "Warrior" in deaths_field.value
assert "TestChar" in deaths_field.value
assert ": 5" in deaths_field.name
assert "Total: 5" in deaths_field.value

@pytest.mark.asyncio
async def test_session_no_deaths_when_unchanged(self, mock_ctx, sample_api_key_data, sample_time_passed):
Expand All @@ -459,7 +443,7 @@ async def test_session_no_deaths_when_unchanged(self, mock_ctx, sample_api_key_d
r.mock_chars_dal.get_all_end_characters = AsyncMock(return_value=chars_end)
await session(mock_ctx)
embed = r.mock_send.call_args[0][1]
deaths_field = next((f for f in embed.fields if "Times you died" in f.name), None)
deaths_field = next((f for f in embed.fields if f.name == "Times you died"), None)
assert deaths_field is None

@pytest.mark.asyncio
Expand All @@ -480,9 +464,9 @@ async def test_session_multiple_character_deaths(self, mock_ctx, sample_api_key_
r.mock_chars_dal.get_all_end_characters = AsyncMock(return_value=chars_end)
await session(mock_ctx)
embed = r.mock_send.call_args[0][1]
deaths_field = next((f for f in embed.fields if "Times you died" in f.name), None)
deaths_field = next((f for f in embed.fields if f.name == "Times you died"), None)
assert deaths_field is not None
assert ": 6" in deaths_field.name
assert "Total: 6" in deaths_field.value
assert "Warrior" in deaths_field.value
assert "Mesmer" in deaths_field.value

Expand Down Expand Up @@ -883,8 +867,9 @@ def test_deaths_changed(self):
with patch("src.gw2.cogs.sessions.chat_formatting.inline", side_effect=lambda x: f"`{x}`"):
_add_deaths_field(embed, start, end)
assert len(embed.fields) == 1
assert ": 5" in embed.fields[0].name
assert "Warrior: Char1 - 5" in embed.fields[0].value
assert embed.fields[0].name == "Times you died"
assert "Char1 (Warrior): 5" in embed.fields[0].value
assert "Total: 5" in embed.fields[0].value

def test_no_deaths(self):
embed = discord.Embed()
Expand Down Expand Up @@ -917,7 +902,7 @@ def test_duplicate_end_chars_deduplicated(self):
with patch("src.gw2.cogs.sessions.chat_formatting.inline", side_effect=lambda x: f"`{x}`"):
_add_deaths_field(embed, start, end)
assert len(embed.fields) == 1
assert ": 5" in embed.fields[0].name
assert "Total: 5" in embed.fields[0].value
# Should only show the character once
assert embed.fields[0].value.count("Warrior") == 1

Expand All @@ -935,9 +920,10 @@ def test_multiple_characters_per_line_format(self):
with patch("src.gw2.cogs.sessions.chat_formatting.inline", side_effect=lambda x: f"`{x}`"):
_add_deaths_field(embed, start, end)
assert len(embed.fields) == 1
assert ": 3" in embed.fields[0].name
assert "Necromancer: I Hadesz I - 1" in embed.fields[0].value
assert "Mesmer: Hàdész - 2" in embed.fields[0].value
assert embed.fields[0].name == "Times you died"
assert "I Hadesz I (Necromancer): 1" in embed.fields[0].value
assert "Hàdész (Mesmer): 2" in embed.fields[0].value
assert "Total: 3" in embed.fields[0].value


class TestAddWvwStats:
Expand Down