feat(system): fix(drone): mail view index bug — display order was reversed from array order

Co-Authored-By: @devpulse <devpulse@aipass>
This commit is contained in:
AIOSAI
2026-04-26 22:44:12 -07:00
co-authored by @devpulse
parent 2f1ede44bb
commit d11bc94ade
2 changed files with 10 additions and 6 deletions
+6 -2
View File
@@ -358,14 +358,18 @@ def _handle_custom_command(args: list[str]) -> int:
def _read_inbox_message_id(inbox: Path, n: int) -> str | None:
"""Return the ID of the Nth message (1-based) from inbox.json, or None."""
"""Return the ID of the Nth message (1-based, display order) from inbox.json.
The inbox display reverses the array (oldest first), so display position 1
is the last element in the JSON array, not the first.
"""
import json as _json
try:
data = _json.loads(inbox.read_text(encoding="utf-8"))
messages = data.get("messages", [])
if 1 <= n <= len(messages):
return messages[n - 1]["id"]
return messages[len(messages) - n]["id"]
except Exception as exc:
logger.warning("Failed to resolve mail index %d: %s", n, exc)
return None
+4 -4
View File
@@ -751,16 +751,16 @@ class TestReadInboxMessageId:
"""_read_inbox_message_id() edge cases."""
def test_valid_index(self, tmp_path: Path) -> None:
"""Returns message ID for valid index."""
"""Returns message ID for valid display-order index (reversed from array)."""
inbox = tmp_path / "inbox.json"
inbox.write_text(
json.dumps({"messages": [{"id": "abc123"}, {"id": "def456"}]}),
json.dumps({"messages": [{"id": "newest"}, {"id": "oldest"}]}),
encoding="utf-8",
)
from aipass.drone.apps.drone import _read_inbox_message_id
assert _read_inbox_message_id(inbox, 1) == "abc123"
assert _read_inbox_message_id(inbox, 2) == "def456"
assert _read_inbox_message_id(inbox, 1) == "oldest"
assert _read_inbox_message_id(inbox, 2) == "newest"
def test_out_of_range(self, tmp_path: Path) -> None:
"""Returns None for out-of-range index."""