from __future__ import annotations

import unittest

from tools.collection.collect_arxiv import ArxivSearchParser, classify


SEARCH_RESULT = """
<ol>
  <li class="arxiv-result">
    <p class="list-title is-inline-block">
      <a href="https://arxiv.org/abs/2607.12345v2">arXiv:2607.12345</a>
    </p>
    <p class="title is-5 mathjax">
      A Trace-Grounded Benchmark for Tool-Using Agents
    </p>
    <p class="authors">
      <span>Authors:</span>
      <a>Alice Example</a>, <a>Bob Example</a>
    </p>
    <div class="tags">
      <span class="tag is-small is-link tooltip">cs.AI</span>
      <span class="tag is-small is-link tooltip">cs.LG</span>
    </div>
    <span class="abstract-full has-text-grey-dark mathjax">
      We evaluate an LLM agent with tools and deterministic verification.
      <a>Less</a>
    </span>
    <p class="is-size-7">Submitted 24 July, 2026; originally announced July 2026.</p>
  </li>
</ol>
"""


class ArxivSearchParserTest(unittest.TestCase):
    def test_parses_search_result_into_api_compatible_record(self) -> None:
        parser = ArxivSearchParser()
        parser.feed(SEARCH_RESULT)

        self.assertEqual(len(parser.records), 1)
        record = parser.records[0]
        self.assertEqual(record["id"], "2607.12345")
        self.assertEqual(record["url"], "https://arxiv.org/abs/2607.12345")
        self.assertEqual(record["published"], "2026-07-24")
        self.assertEqual(record["updated"], "2026-07-24")
        self.assertEqual(record["authors"], ["Alice Example", "Bob Example"])
        self.assertEqual(record["categories"], ["cs.AI", "cs.LG"])
        self.assertNotIn("Less", record["summary"])

    def test_parsed_record_uses_existing_classifier(self) -> None:
        parser = ArxivSearchParser()
        parser.feed(SEARCH_RESULT)
        topics, score, relevance = classify(parser.records[0], {"agent-evaluation", "tool-use"})

        self.assertIn("agent-evaluation", topics)
        self.assertIn("tool-use", topics)
        self.assertGreaterEqual(score, 7)
        self.assertIn(relevance, {"medium", "high"})


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