Revise plain estimate answers from inline price zones
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
"""A price zone chosen in a plain AI answer revises that answer in place."""
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def case(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("DB_PATH", str(tmp_path / "chat.db"))
|
||||
monkeypatch.setenv("SESSION_SECRET", "inline-freeform-test")
|
||||
import auth
|
||||
import db
|
||||
import engine_prices
|
||||
import main
|
||||
|
||||
for module in (db, auth, main):
|
||||
importlib.reload(module)
|
||||
db.init_db()
|
||||
for email in ("alice@x", "bob@x"):
|
||||
db.create_user(email, auth.hash_password("Passw0rd!ok"))
|
||||
db.admin_verify_user(email)
|
||||
db.save_message("alice@x", "tab", "user", "Посчитай 100 извещателей в Хабаровском крае")
|
||||
original = ("Выберите ценовую зону:\n"
|
||||
"1. Хабаровский край (1 зона)\n"
|
||||
"2. Хабаровский край (2 зона)")
|
||||
answer_id = db.save_message("alice@x", "tab", "assistant", original)
|
||||
monkeypatch.setattr(engine_prices, "load_catalog", lambda: {"zones": [
|
||||
{"subject_id": 264, "zone_id": 118, "zone_name": "Хабаровский край (1 зона)",
|
||||
"period_id": 428, "period_label": "4 квартал 2026"},
|
||||
{"subject_id": 264, "zone_id": 179, "zone_name": "Хабаровский край (2 зона)",
|
||||
"period_id": 428, "period_label": "4 квартал 2026"},
|
||||
{"subject_id": 265, "zone_id": 300, "zone_name": "Другой край (1 зона)",
|
||||
"period_id": 428, "period_label": "4 квартал 2026"},
|
||||
]})
|
||||
seen = []
|
||||
|
||||
async def generate(user, tab, question, old_answer, zone, period):
|
||||
seen.append((user, tab, question, old_answer, zone, period))
|
||||
return {"answer": "Расчёт для Хабаровский край (2 зона), 4 квартал 2026: 280 ₽.",
|
||||
"usage": {}, "provider": "test", "model": "test"}
|
||||
|
||||
monkeypatch.setattr(main, "_generate_freeform_basis_answer", generate, raising=False)
|
||||
client = TestClient(main.app)
|
||||
alice = {"Authorization": "Bearer " + auth._make_token("alice@x"), "X-Tab-Id": "tab"}
|
||||
bob = {"Authorization": "Bearer " + auth._make_token("bob@x"), "X-Tab-Id": "tab"}
|
||||
return db, client, alice, bob, answer_id, original, seen
|
||||
|
||||
|
||||
def test_plain_answer_switches_in_place_and_rejects_stale_or_foreign(case):
|
||||
db, client, alice, bob, answer_id, original, seen = case
|
||||
url = f"/api/messages/{answer_id}/basis"
|
||||
params = {"label": "Хабаровский край (1 зона)"}
|
||||
assert client.get(url, params=params).status_code == 401
|
||||
assert client.get(url, params=params, headers=bob).status_code == 404
|
||||
options = client.get(url, params=params, headers=alice)
|
||||
assert options.status_code == 200, options.text
|
||||
assert [z["zone_id"] for z in options.json()["zones"]] == [118, 179]
|
||||
payload = {"label": params["label"], "zone_id": 179, "period_id": 428,
|
||||
"revision": options.json()["revision"]}
|
||||
assert client.post(url, json={**payload, "zone_id": 300}, headers=alice).status_code == 400
|
||||
assert client.post(url, json=payload, headers=bob).status_code == 404
|
||||
result = client.post(url, json=payload, headers=alice)
|
||||
assert result.status_code == 200, result.text
|
||||
assert "280 ₽" in result.json()["answer"]
|
||||
rows = db.get_history("alice@x", "tab")
|
||||
assert len(rows) == 2 and rows[1]["id"] == answer_id
|
||||
assert rows[1]["content"] == result.json()["answer"]
|
||||
assert client.post(url, json=payload, headers=alice).status_code == 409
|
||||
assert len(seen) == 1 and seen[0][3] == original
|
||||
|
||||
|
||||
def test_label_must_exist_in_saved_answer(case):
|
||||
_, client, alice, _, answer_id, _, seen = case
|
||||
url = f"/api/messages/{answer_id}/basis"
|
||||
assert client.get(url, params={"label": "Другой край (1 зона)"}, headers=alice).status_code == 400
|
||||
assert seen == []
|
||||
|
||||
|
||||
def test_plain_answer_extracts_explicit_norm_and_volume_without_guessing():
|
||||
import answer_basis
|
||||
question = "Посчитай монтаж 100 пожарных извещателей в Хабаровском крае"
|
||||
answer = "Норма ГЭСНм 10-08-002-02. Измеритель 1 шт. На 100 извещателей объём работ = 100 шт."
|
||||
assert answer_basis.extract_position(question, answer) == ("10-08-002-02", 100.0)
|
||||
assert answer_basis.extract_position("Какая цена?", "ГЭСНм 10-08-002-02") is None
|
||||
|
||||
|
||||
def test_incomplete_engine_result_is_never_presented_as_full_total():
|
||||
import answer_basis
|
||||
result = {"status": "ok", "ok": False, "grand_total": 156066.82,
|
||||
"missing": [{"code": "ГЭСНм 10-08-002-02", "reason": "НР/СП не определены"}],
|
||||
"unpriced": [], "positions": [{"code": "ГЭСНм 10-08-002-02", "qty": 100,
|
||||
"unit": "шт", "total": 156066.82}]}
|
||||
text = answer_basis.format_plain_result(result, "10-08-002-02", 100,
|
||||
"Хабаровский край (2 зона)", "4 квартал 2026")
|
||||
assert "156 066,82 ₽" in text
|
||||
assert "НР/СП не определены" in text
|
||||
assert "итого по локальной смете" not in text.lower()
|
||||
Reference in New Issue
Block a user