Add RAG basic implementation

This commit is contained in:
2026-11-09 08:43:30 +03:00
parent 447464c16c
commit 7fa1d774a0
76 changed files with 3030 additions and 124 deletions
+67
View File
@@ -790,3 +790,70 @@ def test_market_check_card_says_what_it_compares(ui):
html = wait_done(ui, create_via_ui(ui))
assert "ценой с резервом без НДС" in html and "(без НДС)" in html
# ================================================================== audit trail, preliminary, references
def test_the_page_and_the_history_show_who_did_what(monkeypatch, dsn):
with make_ui(monkeypatch, dsn) as alex, make_ui(monkeypatch, dsn) as bob:
login(alex, "alex")
login(bob, "bob")
eid = create_via_ui(alex)
wait_done(alex, eid)
t = page_token(bob, f"/ui/estimates/{eid}")
assert bob.post(f"/ui/estimates/{eid}/engagement", data={"csrf_token": t, "engagement": "time_and_materials"}).status_code == 303
page = bob.get(f"/ui/estimates/{eid}").text
assert re.search(r"создана [\d.: ]+, alex", page) and "версия 2: смена модели оплаты, bob" in page
assert "версия 1: расчёт агента, alex" in bob.get(f"/ui/estimates/{eid}?version=1").text
history = bob.get("/ui/estimates").text
assert "<th>Автор</th>" in history and "<td>alex</td>" in history # the creator of the estimate, not the editor
def test_each_ui_user_has_their_own_internal_identity(monkeypatch, dsn):
with make_ui(monkeypatch, dsn) as c:
tokens = c.app.state.ui_tokens
assert set(tokens) == {"alex", "bob"} and len(set(tokens.values())) == 2
principals = {p.user: p.client for p in c.app.state.internal_keys.values()}
assert principals == {"alex": "ui", "bob": "ui"} # one shared workspace, two people
with make_ui(monkeypatch, dsn, EST_WEB__WORKSPACE="per_user") as c:
assert {p.user: p.client for p in c.app.state.internal_keys.values()} == {"alex": "ui:alex", "bob": "ui:bob"}
def test_an_estimate_on_assumptions_is_marked_preliminary_in_the_page_and_the_history(monkeypatch, dsn):
gw = AutoGateway(clarify=[clarify_out([q("crm", "Какая CRM?", blocking=True)])])
with make_ui(monkeypatch, dsn, gw) as c:
login(c)
eid = create_via_ui(c, "бот", proceed="1")
html = wait_done(c, eid)
assert "Предварительная оценка." in html and "Не уточнено: Какая CRM?" in html
assert "предварительная" in c.get("/ui/estimates").text
with make_ui(monkeypatch, dsn) as c:
login(c)
done = wait_done(c, create_via_ui(c)) # a complete estimate in the same database
assert "Предварительная оценка." not in done
assert c.get("/ui/estimates").text.count("предварительная") == 1 # only the first estimate carries the badge
def test_references_are_shown_under_the_task_and_are_escaped(monkeypatch, backend_dsn):
from tests.rag_helpers import RAG_ENV, load_corpus
evil = "<script>alert('ref')</script>"
load_corpus(backend_dsn, [
{"project": "P1", "title": "Интеграция с amoCRM", "role": "backend", "hours": "21", "hours_kind": "actual"},
{"project": f"P2 {evil}", "title": f"Интеграция amoCRM {evil}", "role": "backend", "hours": "9", "hours_kind": "estimate"},
]) # fmt: skip
with make_ui(monkeypatch, backend_dsn, **RAG_ENV) as c:
login(c)
html = wait_done(c, create_via_ui(c))
assert "Аналоги из истории" in html
assert "Интеграция с amoCRM: 21\u00a0ч, факт, P1, сходство" in html
assert ", оценка, " in html # an estimate is labelled differently from a measured value
page = c.get(f"/ui/estimates/{html.split('/ui/estimates/')[1].split(chr(34))[0].split('/')[0]}").text if False else html
assert evil not in page and "&lt;script&gt;alert(&#39;ref&#39;)&lt;/script&gt;" in page
assert "<script>alert" not in page
def test_no_references_block_when_retrieval_is_off(ui):
html = wait_done(ui, create_via_ui(ui))
assert "Аналоги из истории" not in html