from __future__ import annotations

import ast
import unittest
from pathlib import Path


MODULE_PATH = Path("/opt/jarina/userbot/plugins/nanoagent/commands.py")


class AgentOneshotTriggerRetentionTests(unittest.TestCase):
    def test_agent_oneshot_does_not_delete_trigger_message(self):
        tree = ast.parse(MODULE_PATH.read_text())

        agent_oneshot = next(
            node
            for node in tree.body
            if isinstance(node, ast.AsyncFunctionDef) and node.name == "agent_oneshot"
        )

        forbidden_calls: list[str] = []
        for node in ast.walk(agent_oneshot):
            if not isinstance(node, ast.Call):
                continue
            try:
                rendered = ast.unparse(node)
            except Exception:
                rendered = "<unparseable>"
            if "session_delete_messages" in rendered:
                forbidden_calls.append(rendered)

        self.assertEqual(
            forbidden_calls,
            [],
            f"agent_oneshot must not delete the trigger message: {forbidden_calls}",
        )


if __name__ == "__main__":
    unittest.main()
