File size: 2,678 Bytes
cd26350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef25ff3
cd26350
 
 
 
 
 
 
 
 
 
ef25ff3
cd26350
ef25ff3
 
 
 
cd26350
 
ef25ff3
cd26350
ef25ff3
 
 
 
cd26350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c3e8bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef25ff3
 
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
import re
from datetime import datetime

import pytest
from pydantic import ValidationError

from models.state import State


@pytest.mark.unit
def test_state_minimal_init():
    s = State(query="test query")
    assert s.query == "test query"
    # request_id present and looks like a uuid string
    assert isinstance(s.request_id, str)
    assert re.match(r"[0-9a-fA-F\-]{36}", s.request_id)
    assert isinstance(s.timestamp, datetime)
    # Optional fields default to None or empty list for errors
    assert s.optimized_query is None
    assert s.papers == []
    assert s.errors == []


@pytest.mark.unit
def test_progressive_updates():
    s = State(query="q")
    s.update_fields(optimized_query="q optimized")
    assert s.optimized_query == "q optimized"

    # simulate retrieval
    papers = [{"paperId": "p1", "title": "Paper 1"}]
    s.update_fields(papers=papers)
    assert len(s.papers) == 1
    # pydantic converts dicts to `Paper` models; verify fields preserved
    assert s.papers[0].paperId == "p1"
    assert s.papers[0].title == "Paper 1"

    # ranking
    ranked = [{"paperId": "p1", "title": "Paper 1", "score": 0.9}]
    s.update_fields(ranked_papers=ranked)
    assert len(s.ranked_papers) == 1
    rp = s.ranked_papers[0].model_dump()
    assert rp.get("paperId") == "p1"
    assert rp.get("score") == 0.9


@pytest.mark.unit
def test_invalid_types_raise():
    with pytest.raises(ValidationError):
        # query is required and must be a str; passing an int should fail
        State(query=123)  # type: ignore


@pytest.mark.unit
def test_error_tracking_and_defaults():
    s = State(query="err")
    s.append_error("first error")
    assert s.errors == ["first error"]

    # defaults regenerated per instance
    s2 = State(query="err2")
    assert s2.request_id != s.request_id


@pytest.mark.unit
def test_synthesis_fields_defaults():
    """Test that answer and citations fields default correctly."""
    s = State(query="synthesis test")
    # answer defaults to None
    assert s.answer is None
    # citations defaults to empty list
    assert s.citations == []
    assert isinstance(s.citations, list)


@pytest.mark.unit
def test_synthesis_fields_assignment():
    """Test that answer and citations can be assigned and updated."""
    s = State(query="synthesis test")

    # Set answer
    s.update_fields(answer="The evidence suggests...")
    assert s.answer == "The evidence suggests..."

    # Set citations as list of integers (paper indices)
    s.update_fields(citations=[0, 2, 5])
    assert s.citations == [0, 2, 5]

    # Verify other existing fields remain intact
    assert s.finalists == []
    assert s.passages == []