diff --git a/src/aipass/drone/apps/drone.py b/src/aipass/drone/apps/drone.py index 896d7e21..adb851c7 100644 --- a/src/aipass/drone/apps/drone.py +++ b/src/aipass/drone/apps/drone.py @@ -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 diff --git a/src/aipass/drone/tests/test_cli_routing.py b/src/aipass/drone/tests/test_cli_routing.py index 9ce948ff..35de1945 100644 --- a/src/aipass/drone/tests/test_cli_routing.py +++ b/src/aipass/drone/tests/test_cli_routing.py @@ -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."""