89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from hunter.analysis import analyze_materials, answer_question, mock_analyze, validate_idea
|
|
from hunter.db import connect
|
|
|
|
|
|
def test_missing_key_marks_generated_hypotheses_mock(app, material):
|
|
path = app.config["DATABASE"]
|
|
assert analyze_materials(path) == 1
|
|
assert analyze_materials(path) == 1
|
|
with connect(path) as db:
|
|
ideas = db.execute("SELECT * FROM ideas").fetchall()
|
|
assert len(ideas) == 1
|
|
assert (
|
|
db.execute("SELECT count(*) FROM signals WHERE analyzed_at IS NOT NULL").fetchone()[0]
|
|
== 0
|
|
)
|
|
assert ideas[0]["mock"] == 1
|
|
assert ideas[0]["score"] == 50
|
|
assert json.loads(ideas[0]["data"])["signal_ids"] == [material]
|
|
answer = answer_question(path, "умный дом")
|
|
assert answer["mock"]
|
|
assert answer["signal_ids"] == [material]
|
|
|
|
|
|
def test_empty_database_explicitly_reports_insufficient_data(app):
|
|
assert analyze_materials(app.config["DATABASE"]) == 0
|
|
result = answer_question(app.config["DATABASE"], "AI")
|
|
assert result["signal_ids"] == []
|
|
assert "недостаточно данных" in result["text"]
|
|
|
|
|
|
def test_provider_contract_translates_and_scores_real_input(app, material, monkeypatch):
|
|
monkeypatch.setenv("AI_API_KEY", "mock-key")
|
|
|
|
def mock_provider(payload, task):
|
|
result = mock_analyze([{"id": material, "category": "Smart Home"}])
|
|
result["translations"] = [
|
|
{
|
|
"id": material,
|
|
"title": "Исследование умного дома",
|
|
"summary": "Краткое описание",
|
|
"stage": "growing",
|
|
"potential": "high",
|
|
"competition": "unknown",
|
|
}
|
|
]
|
|
result["ideas"][0]["criteria"]["demand"] = 100
|
|
return result
|
|
|
|
analyze_materials(app.config["DATABASE"], ai_provider=mock_provider)
|
|
with connect(app.config["DATABASE"]) as db:
|
|
idea = db.execute("SELECT * FROM ideas").fetchone()
|
|
assert (
|
|
db.execute("SELECT count(*) FROM signals WHERE analyzed_at IS NOT NULL").fetchone()[0]
|
|
== 1
|
|
)
|
|
assert json.loads(idea["data"])["weights"] == [25, 25, 20, 15, 15]
|
|
assert idea["mock"] == 0
|
|
assert idea["score"] == 62.5
|
|
assert (
|
|
db.execute("SELECT translated_title FROM signals").fetchone()[0]
|
|
== "Исследование умного дома"
|
|
)
|
|
|
|
|
|
def test_foreign_citations_bad_scores_and_inline_links_are_rejected(material):
|
|
item = mock_analyze([{"id": material, "category": "AI"}])["ideas"][0]
|
|
for modified in (
|
|
{**item, "signal_ids": [99999]},
|
|
{**item, "criteria": {"demand": 200}},
|
|
{**item, "evidence": "https://unverified.example.com"},
|
|
{**item, "category": "invented"},
|
|
):
|
|
with pytest.raises(ValueError):
|
|
validate_idea(modified, {material})
|
|
|
|
|
|
def test_chat_rejects_unverified_sources(app, material, monkeypatch):
|
|
monkeypatch.setenv("AI_API_KEY", "mock-key")
|
|
with pytest.raises(ValueError):
|
|
answer_question(
|
|
app.config["DATABASE"],
|
|
"smart home",
|
|
ai_provider=lambda payload, task: {"text": "Claim", "signal_ids": [999]},
|
|
)
|