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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
from __future__ import annotations

from pathlib import Path

import pytest

from gcmd_classifier.classification import (
    TermCandidate,
    build_term_candidates,
    build_topic_candidates,
    route_terms,
    route_topics,
    validate_term_candidate_relationship,
)
from gcmd_classifier.config import ModelSettings
from gcmd_classifier.errors import StructuredModelResponseError, UnknownCandidateIDError
from gcmd_classifier.llm import FakeModelClient
from gcmd_classifier.llm.prompts import PromptCandidate
from gcmd_classifier.models import ArticleRecord, SupportType
from gcmd_classifier.vocabulary import build_vocabulary_index, load_vocabulary

FIXTURE_PATH = Path("tests/fixtures/gcmd_hierarchy_small.json")
FULL_HIERARCHY_PATH = Path("data/gcmd_hierarchy.json")


def _index():
    return load_vocabulary(FIXTURE_PATH)


def _article(
    abstract: str = "Atmospheric chemistry and weather events are discussed.",
) -> ArticleRecord:
    return ArticleRecord(
        DOI="10.example/term-routing",
        Title="Atmospheric chemistry and weather observations",
        Year=2025,
        Abstract=abstract,
    )


def _decision(candidate_id: str, confidence: float | None = 0.82) -> dict:
    return {
        "candidate_id": candidate_id,
        "confidence": confidence,
        "evidence": "The article supports this Term.",
        "support_type": "explicit",
        "reason": "Primary subject is represented by this Term.",
    }


def _topic_branch(topic_index: int = 0):
    index = _index()
    topic_candidate = build_topic_candidates(index)[topic_index]
    client = FakeModelClient([{"selected": [_decision(topic_candidate.candidate_id)]}])
    return route_topics(
        article=_article(),
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    ).branches[0]


def _no_term_index():
    data = {
        "level": "Category",
        "name": "EARTH SCIENCE",
        "children": [
            {
                "level": "Topic",
                "name": "LAND SURFACE",
                "UUID": "topic-land-surface",
            }
        ],
    }
    return build_vocabulary_index(data, vocabulary_version="no-term-test")


def test_term_candidates_are_built_only_from_selected_topic_direct_children() -> None:
    index = _index()
    topic = index.get("topic-atmosphere")
    candidates = build_term_candidates(index, topic_uuid=topic.UUID)

    assert [candidate.term_uuid for candidate in candidates] == list(topic.child_uuids)
    assert [index.get(candidate.term_uuid).level for candidate in candidates] == ["Term", "Term"]


def test_terms_from_sibling_topics_are_not_included() -> None:
    index = _index()
    candidates = build_term_candidates(index, topic_uuid="topic-atmosphere")

    assert "term-ocean-chemistry" not in [candidate.term_uuid for candidate in candidates]


def test_term_candidate_ids_are_unique_within_prompt() -> None:
    candidates = build_term_candidates(_index(), topic_uuid="topic-atmosphere")
    candidate_ids = [candidate.candidate_id for candidate in candidates]

    assert candidate_ids == ["term_0001", "term_0002"]
    assert len(candidate_ids) == len(set(candidate_ids))


def test_term_candidate_ids_map_to_uuid_bearing_term_records() -> None:
    index = _index()

    for candidate in build_term_candidates(index, topic_uuid="topic-atmosphere"):
        record = index.get(candidate.term_uuid)
        assert record.UUID
        assert record.level == "Term"
        assert candidate.prompt_candidate.candidate_id == candidate.candidate_id


def test_term_candidate_construction_does_not_hard_code_term_names_or_uuids() -> None:
    index = _index()
    candidates = build_term_candidates(index, topic_uuid="topic-atmosphere")
    direct_terms = index.terms_for_topic("topic-atmosphere")

    assert [candidate.term_uuid for candidate in candidates] == [term.UUID for term in direct_terms]
    assert [candidate.prompt_candidate.name for candidate in candidates] == [
        term.name for term in direct_terms
    ]


def test_selected_topic_with_no_direct_term_children_is_handled_explicitly() -> None:
    index = _no_term_index()
    topic = index.topics()[0]

    candidates = build_term_candidates(index, topic_uuid=topic.UUID)

    assert candidates == ()


def test_one_term_selected_under_selected_topic() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.selected_count == 1
    assert result.stopped_at_topic is False
    assert result.term_branches[0].term_uuid == term_candidate.term_uuid


def test_multiple_terms_selected_under_one_topic() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidates = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)
    client = FakeModelClient(
        [
            {
                "selected": [
                    _decision(term_candidates[0].candidate_id),
                    _decision(term_candidates[1].candidate_id, confidence=None),
                ],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert [branch.term_uuid for branch in result.term_branches] == [
        term_candidates[0].term_uuid,
        term_candidates[1].term_uuid,
    ]
    assert result.term_branches[1].confidence is None


def test_stop_at_topic_with_stop_at_parent_true() -> None:
    topic_branch = _topic_branch()
    client = FakeModelClient(
        [
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "The Topic is supported, but no child Term is supported.",
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.term_branches == ()
    assert result.stopped_at_topic is True
    assert result.stop_at_topic is not None
    assert result.stop_at_topic.topic_uuid == topic_branch.topic_uuid
    assert result.stop_at_topic.stop_reason == (
        "The Topic is supported, but no child Term is supported."
    )


def test_topic_with_no_supported_term_returns_stop_at_topic_result() -> None:
    index = _no_term_index()
    topic = index.topics()[0]
    topic_branch = _manual_topic_branch(topic.UUID, topic.name, topic.canonical_path)
    client = FakeModelClient(
        [
            {
                "selected": [],
                "stop_at_parent": True,
                "stop_reason": "No direct Term candidates are available or supported.",
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.stopped_at_topic is True
    assert result.term_branches == ()


def test_invalid_term_candidate_id_is_rejected() -> None:
    client = FakeModelClient(
        [
            {
                "selected": [_decision("unknown-term")],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    with pytest.raises(UnknownCandidateIDError):
        route_terms(
            article=_article(),
            topic_branch=_topic_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_duplicate_term_candidate_id_is_rejected() -> None:
    topic_branch = _topic_branch()
    candidate_id = build_term_candidates(
        _index(),
        topic_uuid=topic_branch.topic_uuid,
    )[0].candidate_id
    client = FakeModelClient(
        [
            {
                "selected": [_decision(candidate_id), _decision(candidate_id)],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    with pytest.raises(UnknownCandidateIDError):
        route_terms(
            article=_article(),
            topic_branch=topic_branch,
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_empty_or_malformed_term_candidate_id_is_rejected_by_structured_validation() -> None:
    client = FakeModelClient(
        [
            {
                "selected": [_decision("")],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    with pytest.raises(StructuredModelResponseError):
        route_terms(
            article=_article(),
            topic_branch=_topic_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_selected_term_branch_seed_is_populated_from_vocabulary_index() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    term = index.get(term_candidate.term_uuid)
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id, confidence=0.74)],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(model_name="unit-test-model", prompt_version_term="term-test"),
    )
    branch = result.term_branches[0]

    assert branch.branch_id == f"{topic_branch.branch_id}/term:{term_candidate.candidate_id}"
    assert branch.parent_topic_uuid == topic_branch.topic_uuid
    assert branch.parent_topic_name == topic_branch.topic_name
    assert branch.term_uuid == term.UUID
    assert branch.term_name == term.name
    assert branch.term_level == "Term"
    assert branch.term_canonical_path == term.canonical_path
    assert branch.evidence == "The article supports this Term."
    assert branch.support_type is SupportType.EXPLICIT
    assert branch.confidence == 0.74
    assert branch.reason == "Primary subject is represented by this Term."
    assert branch.candidate_id == term_candidate.candidate_id
    assert branch.prompt_version == "term-test"
    assert branch.model_name == "unit-test-model"


def test_confidence_is_preserved_as_uncalibrated_metadata_only() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id, confidence=0.01)],
                "stop_at_parent": False,
                "ambiguous_alternatives": [],
            }
        ]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.term_branches[0].confidence == 0.01
    assert result.term_branches[0].term_uuid == term_candidate.term_uuid


def test_no_fake_fallback_term_is_created() -> None:
    topic_branch = _topic_branch()
    client = FakeModelClient(
        [{"selected": [], "stop_at_parent": True, "stop_reason": "Stop here."}]
    )

    result = route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=_index(),
        model_client=client,
        settings=ModelSettings(),
    )

    assert result.term_branches == ()
    assert "FALLBACK" not in str(result.model_dump())


def test_no_variable_routing_is_performed() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
            }
        ]
    )

    route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert len(client.requests) == 1
    assert client.requests[0].stage.value == "term"


def test_term_selected_from_correct_topic_succeeds() -> None:
    index = _index()
    topic_branch = _topic_branch()
    candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]

    validate_term_candidate_relationship(
        candidate,
        selected_topic_uuid=topic_branch.topic_uuid,
        index=index,
    )


def test_candidate_from_sibling_topic_cannot_be_accepted() -> None:
    client = FakeModelClient(
        [
            {
                "selected": [_decision("term-ocean-chemistry")],
                "stop_at_parent": False,
            }
        ]
    )

    with pytest.raises(UnknownCandidateIDError):
        route_terms(
            article=_article(),
            topic_branch=_topic_branch(),
            vocabulary=_index(),
            model_client=client,
            settings=ModelSettings(),
        )


def test_manually_invalid_candidate_to_record_mapping_is_rejected() -> None:
    index = _index()
    invalid_candidate = TermCandidate(
        candidate_id="term_0001",
        topic_uuid="topic-atmosphere",
        term_uuid="term-ocean-chemistry",
        prompt_candidate=PromptCandidate(
            candidate_id="term_0001",
            name="OCEAN CHEMISTRY",
            level="Term",
        ),
    )

    with pytest.raises(ValueError):
        validate_term_candidate_relationship(
            invalid_candidate,
            selected_topic_uuid="topic-atmosphere",
            index=index,
        )


def test_term_router_never_evaluates_all_terms_globally() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
            }
        ]
    )

    route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    prompt = client.requests[0].prompt
    assert "ATMOSPHERIC CHEMISTRY" in prompt
    assert "WEATHER EVENTS" in prompt
    assert "OCEAN CHEMISTRY" not in prompt


def test_term_router_uses_provider_neutral_model_interface() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
            }
        ]
    )

    route_terms(
        article=_article(),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(provider="fake-provider", model_name="fake-model"),
    )

    request = client.requests[0]
    assert request.provider == "fake-provider"
    assert request.model_name == "fake-model"
    assert request.response_schema.__name__ == "TermResponse"


def test_fake_model_receives_article_fields_parent_context_and_direct_term_candidates() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    article = _article()
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
            }
        ]
    )

    route_terms(
        article=article,
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    prompt = client.requests[0].prompt
    assert article.DOI in prompt
    assert article.Title in prompt
    assert article.Abstract in prompt
    assert topic_branch.topic_name in prompt
    assert topic_branch.topic_canonical_path in prompt
    assert term_candidate.candidate_id in prompt
    assert index.get(term_candidate.term_uuid).name in prompt


def test_empty_abstract_remains_valid_and_is_passed_to_term_prompt() -> None:
    index = _index()
    topic_branch = _topic_branch()
    term_candidate = build_term_candidates(index, topic_uuid=topic_branch.topic_uuid)[0]
    client = FakeModelClient(
        [
            {
                "selected": [_decision(term_candidate.candidate_id)],
                "stop_at_parent": False,
            }
        ]
    )

    route_terms(
        article=_article(abstract=""),
        topic_branch=topic_branch,
        vocabulary=index,
        model_client=client,
        settings=ModelSettings(),
    )

    assert "<ABSTRACT>\n\n</ABSTRACT>" in client.requests[0].prompt


def test_full_data_current_vocabulary_has_144_direct_term_candidates() -> None:
    index = load_vocabulary(FULL_HIERARCHY_PATH)
    all_candidates = [
        candidate
        for topic in index.topics()
        for candidate in build_term_candidates(index, topic_uuid=topic.UUID)
    ]
    term_uuids = [candidate.term_uuid for candidate in all_candidates]

    assert len(all_candidates) == 144
    assert len(term_uuids) == len(set(term_uuids))
    assert all(index.get(candidate.term_uuid).UUID for candidate in all_candidates)
    assert all(index.get(candidate.term_uuid).level == "Term" for candidate in all_candidates)


def _manual_topic_branch(topic_uuid: str, topic_name: str, canonical_path: str):
    from gcmd_classifier.classification import TopicBranchSeed

    return TopicBranchSeed(
        branch_id="topic:manual",
        topic_uuid=topic_uuid,
        topic_name=topic_name,
        topic_level="Topic",
        topic_canonical_path=canonical_path,
        evidence="The Topic is supported.",
        support_type="explicit",
        confidence=0.8,
        reason="Manual test branch.",
        candidate_id="topic_manual",
        prompt_version="topic-test",
        model_provider="fake",
        model_name="fake-model",
        retry_count=0,
    )