File size: 3,563 Bytes
862be58
 
 
5b48ad6
862be58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b48ad6
 
 
 
 
 
 
 
 
 
 
862be58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b48ad6
 
 
862be58
 
 
5b48ad6
 
 
 
 
 
 
 
862be58
 
 
 
 
 
 
 
 
 
 
 
5b48ad6
 
 
 
862be58
 
 
 
 
 
 
 
 
 
 
73d3d37
 
862be58
 
5b48ad6
 
862be58
 
 
 
 
 
 
 
 
 
5b48ad6
862be58
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pytest

from agents.processing_agent import ProcessingAgent
from models.state import Passage, State


@pytest.fixture
def make_state():
    def _make(query: str, passages):
        return State(query=query, passages=passages)

    return _make


@pytest.mark.unit
def test_processing_enriches_state_single_passage(make_state):
    state = make_state(
        "test",
        [
            Passage(
                paper_id="abc123",
                text="# Introduction\n" + "A " * 5000 + "\n# Methods\n" + "B " * 5000,
                metadata={
                    "title": "Test Paper",
                    "text_source": "pdf",
                    "year": 2020,
                    "venue": "Journal of Testing",
                    "influentialCitationCount": 2,
                },
            )
        ],
    )

    agent = ProcessingAgent(chunk_size=500, chunk_overlap=50)
    updated_state = agent.process(state)

    assert hasattr(updated_state, "chunks")
    assert updated_state.chunks is not None
    assert len(updated_state.chunks) >= 10
    first_meta = updated_state.chunks[0].metadata
    assert first_meta["paper_id"] == "abc123"
    assert first_meta["paper_title"] == "Test Paper"
    assert first_meta["year"] == 2020
    assert first_meta["venue"] == "Journal of Testing"
    assert first_meta["influential_citation_count"] == 2


@pytest.mark.unit
def test_processing_multiple_passages_generates_chunks(make_state):
    passages = []
    for i in range(3):
        passages.append(
            Passage(
                paper_id=f"p{i}",
                text="# Introduction\n"
                + ("X " * 4000)
                + "\n# Results\n"
                + ("Y " * 4000),
                metadata={
                    "title": f"Paper {i}",
                    "text_source": "pdf",
                    "year": 2019 + i,
                    "venue": "Conf",
                    "influentialCitationCount": i,
                },
            )
        )

    state = make_state("multi", passages)
    agent = ProcessingAgent(chunk_size=500, chunk_overlap=50)
    updated = agent.process(state)

    assert hasattr(updated, "chunks")
    assert len(updated.chunks) >= 30


@pytest.mark.unit
def test_empty_passage_is_skipped(make_state):
    state = make_state(
        "empty",
        [Passage(paper_id="e1", text="", metadata={"text_source": "pdf"})],
    )
    agent = ProcessingAgent(chunk_size=200, chunk_overlap=20)
    updated = agent.process(state)

    assert hasattr(updated, "chunks")
    assert isinstance(updated.chunks, list)
    assert len(updated.chunks) == 0
    # No critical error appended (empty passage skipped)
    assert all("Zero chunks" not in e for e in updated.errors)


@pytest.mark.unit
def test_markdown_parser_handles_plain_text(make_state):
    # No explicit Markdown headers -> MarkdownNodeParser chunks by size
    long_text = "This is a paper without headers. " + ("word " * 3000)
    state = make_state(
        "plaintext",
        [Passage(paper_id="f1", text=long_text, metadata={"text_source": "pdf"})],
    )
    agent = ProcessingAgent(chunk_size=400, chunk_overlap=50)
    updated = agent.process(state)

    assert hasattr(updated, "chunks")
    assert len(updated.chunks) > 0


@pytest.mark.unit
def test_no_passages_triggers_error():
    state = State(query="none", passages=[])
    agent = ProcessingAgent()
    res = agent.process(state)
    assert len(res.errors) > 0
    assert any(
        "No passages available" in e or "Zero chunks generated" in e for e in res.errors
    )