84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
"""L1: Formula Evidence search uses only the typed, published Evidence surface."""
|
|
|
|
import json
|
|
from types import SimpleNamespace
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from tht.cli import app, search_cmd
|
|
from tht.evidence.search import EvidenceSearchOutcome
|
|
|
|
|
|
def test_formula_search_uses_typed_evidence_with_a_formula_constraint():
|
|
class Searcher:
|
|
vector_generation = "gen:" + "a" * 32
|
|
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def search(self, embedding, **kwargs):
|
|
self.calls.append((embedding, kwargs))
|
|
return [SimpleNamespace(
|
|
id="fragment:formula",
|
|
similarity=0.9,
|
|
content="formula excerpt",
|
|
title="Fascia pediatrica",
|
|
metadata={
|
|
"evidence_id": "evidence:fascia-pediatrica",
|
|
"evidence_kind": "formula",
|
|
"document_id": "doc:formula",
|
|
"ordinal": 0,
|
|
"source_uri": "file:///curated/formula/fascia-pediatrica.md",
|
|
"provenance": {},
|
|
},
|
|
)]
|
|
|
|
class Embedder:
|
|
def embed_query(self, query):
|
|
return [0.25]
|
|
|
|
searcher = Searcher()
|
|
outcome = search_cmd.search_formula_evidence(
|
|
"fascia pediatrica", searcher=searcher, embedder=Embedder(), top=3,
|
|
)
|
|
|
|
assert outcome.status == "available"
|
|
assert [result.evidence_id for result in outcome.results] == [
|
|
"evidence:fascia-pediatrica",
|
|
]
|
|
assert searcher.calls[0][1]["metadata_filter"]["required_kinds"] == ["formula"]
|
|
|
|
|
|
def test_formula_search_json_is_pristine_and_human_output_uses_only_published_evidence(
|
|
monkeypatch,
|
|
):
|
|
monkeypatch.setattr(
|
|
search_cmd,
|
|
"_load_config_or_exit",
|
|
lambda _path: SimpleNamespace(embeddings=object()),
|
|
)
|
|
monkeypatch.setattr(search_cmd, "workspace_id_for_config", lambda _cfg, _path: "workspace-a")
|
|
monkeypatch.setattr(
|
|
search_cmd,
|
|
"search_formula_evidence",
|
|
lambda *args, **kwargs: EvidenceSearchOutcome("available", "gen:" + "a" * 32),
|
|
)
|
|
monkeypatch.setattr("tht.cli.vector_cmd.require_vector_cfg", lambda _cfg: None)
|
|
monkeypatch.setattr("tht.cli.vector_cmd.open_searcher", lambda _cfg: object())
|
|
monkeypatch.setattr("tht.cli.vector_cmd.make_embedder", lambda _cfg: object())
|
|
monkeypatch.setattr("tht.evidence.active_searcher", lambda *args, **kwargs: object())
|
|
monkeypatch.setattr("tht.evidence.validate_corpus_workspace", lambda _cfg, _workspace: None)
|
|
|
|
json_result = CliRunner().invoke(
|
|
app, ["search", "find", "fascia pediatrica", "--kind", "formula", "--top", "1", "--json"],
|
|
)
|
|
human_result = CliRunner().invoke(
|
|
app, ["search", "find", "fascia pediatrica", "--kind", "formula", "--top", "1"],
|
|
)
|
|
|
|
assert json_result.exit_code == 0
|
|
assert json.loads(json_result.stdout) == []
|
|
assert human_result.exit_code == 0
|
|
assert "ATTENZIONE" not in human_result.output
|
|
assert "Nessuna formula" in human_result.output
|