File size: 3,579 Bytes
5cac97d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa696e8
5cac97d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Literal
from unittest.mock import MagicMock

import pytest

from src.orchestrators.simple import Orchestrator
from src.utils.models import AssessmentDetails, JudgeAssessment


def make_assessment(
    mechanism: int,
    clinical: int,
    drug_candidates: list[str],
    sufficient: bool = False,
    recommendation: Literal["continue", "synthesize"] = "continue",
    confidence: float = 0.8,
) -> JudgeAssessment:
    return JudgeAssessment(
        details=AssessmentDetails(
            mechanism_score=mechanism,
            mechanism_reasoning="reasoning is sufficient for testing purposes",
            clinical_evidence_score=clinical,
            clinical_reasoning="reasoning is sufficient for testing purposes",
            drug_candidates=drug_candidates,
            key_findings=["finding"],
        ),
        sufficient=sufficient,
        confidence=confidence,
        recommendation=recommendation,
        next_search_queries=[],
        reasoning="reasoning is sufficient for testing purposes",
    )


@pytest.fixture
def orchestrator():
    search = MagicMock()
    judge = MagicMock()
    return Orchestrator(search, judge)


@pytest.mark.unit
def test_should_synthesize_high_scores(orchestrator):
    """High scores with drug candidates triggers synthesis."""
    assessment = make_assessment(mechanism=7, clinical=6, drug_candidates=["Testosterone"])

    # Access the private method via name mangling or just call it if it was public.
    # Since I made it private _should_synthesize, I access it directly.
    should_synth, reason = orchestrator._should_synthesize(
        assessment, iteration=3, max_iterations=10, evidence_count=50
    )

    assert should_synth is True
    assert reason == "high_scores_with_candidates"


@pytest.mark.unit
def test_should_synthesize_late_iteration(orchestrator):
    """Late iteration with acceptable scores triggers synthesis."""
    assessment = make_assessment(mechanism=5, clinical=4, drug_candidates=[])
    should_synth, reason = orchestrator._should_synthesize(
        assessment, iteration=9, max_iterations=10, evidence_count=80
    )

    assert should_synth is True
    assert reason in ["late_iteration_acceptable", "emergency_synthesis"]


@pytest.mark.unit
def test_should_not_synthesize_early_low_scores(orchestrator):
    """Early iteration with low scores continues searching."""
    assessment = make_assessment(mechanism=3, clinical=2, drug_candidates=[])
    should_synth, reason = orchestrator._should_synthesize(
        assessment, iteration=2, max_iterations=10, evidence_count=20
    )

    assert should_synth is False
    assert reason == "continue_searching"


@pytest.mark.unit
def test_judge_approved_overrides_all(orchestrator):
    """If judge explicitly says synthesize with good scores, do it."""
    assessment = make_assessment(
        mechanism=6, clinical=5, drug_candidates=[], sufficient=True, recommendation="synthesize"
    )
    should_synth, reason = orchestrator._should_synthesize(
        assessment, iteration=2, max_iterations=10, evidence_count=20
    )

    assert should_synth is True
    assert reason == "judge_approved"


@pytest.mark.unit
def test_max_evidence_threshold(orchestrator):
    """Force synthesis if we have tons of evidence."""
    assessment = make_assessment(mechanism=2, clinical=2, drug_candidates=[])
    should_synth, reason = orchestrator._should_synthesize(
        assessment, iteration=5, max_iterations=10, evidence_count=150
    )

    assert should_synth is True
    assert reason == "max_evidence_reached"