"""Concise presentation must keep factual bindings, qualifications and gaps.""" import copy import sqlite3 import pytest from test_quality_block import corpus, prepare, complete from test_evidence_selection import update_eva, selection from tools.answer_evaluation.context_policy import answer_context_issues from tools.answer_evaluation.content_selection import facet_lines from tools.answer_evaluation.graph_context import graph_context from tools.answer_evaluation.presentation import comparison_table from tools.answer_evaluation.rag_evidence import rag_evidence_context from tools.answer_evaluation.render_answer import render_graph_answer from tools.answer_evaluation.verified_rag import generate_verified_rag, rag_answer_issues def test_missing_aspects_share_one_specific_notice_without_empty_sections(corpus): update_eva(corpus,'Stav:\n- Implementovaný parser.') q='Aké výsledky, problémy a plánované experimenty má diplomová práca Evy Novej?' p=prepare(corpus,q); c=rag_evidence_context(q,p) result=generate_verified_rag(q,p,complete(selection(c))) assert result['accepted_for_delivery'] and result['answer_coverage']['status']=='partial' text=result['answer'] assert 'Implementovaný parser.' in text assert text.count('V poskytnutých podkladoch chýbajú údaje')==1 assert 'riešených problémoch, plánovaných úlohách a plánovaných experimentoch' in text assert '**Riešené problémy:**' not in text and '**Plánované experimenty' not in text assert 'Táto časť odpovede' not in text and '\n\n\n' not in text def test_short_comparison_aligns_people_and_different_results(corpus): q='Porovnaj výsledky Evy Novej a Borisa Nového.' p=prepare(corpus,q); c=rag_evidence_context(q,p); a=selection(c) before=copy.deepcopy((a,c)) lines=facet_lines(a,c); text='\n'.join(lines) assert text.startswith('| Oblasť | Eva Nová — diplomová práca (2024) | Boris Nový — bakalárska práca (2023) |') row=next(line for line in lines if line.startswith('| Zaznamenané výsledky |')) assert 'BERT' in row.split('|')[2] and 'Python' in row.split('|')[3] assert 'Konzultácia' not in text and 'Pripravte experiment' not in text assert 'lepš' not in text and (a,c)==before r=generate_verified_rag(q,p,complete(a)) assert r['accepted_for_delivery'] and len(r['source_urls'])==2 @pytest.mark.parametrize('literal',[ 'Implementovaný parser. Výsledok platí iba pre malý korpus. '+('Technický opis. '*70), 'Implementovaný parser pre `a | b` iba na testovacích dátach.', 'Implementovaný parser podľa https://example.test/method iba na testovacích dátach.', ]) def test_complex_comparisons_fall_back_without_losing_qualifiers_or_literals(corpus,literal): update_eva(corpus,'Výsledky:\n- '+literal) q='Porovnaj výsledky Evy Novej a Borisa Nového.' p=prepare(corpus,q); c=rag_evidence_context(q,p); a=selection(c) text='\n'.join(facet_lines(a,c)) assert not text.startswith('| Oblasť |') and literal.strip() in text assert 'Boris Nový' in text and 'Python' in text assert all('Ďalšie doložené' not in line for line in text.splitlines()) def test_shared_excerpt_is_not_repeated_under_each_matching_facet(corpus): update_eva(corpus,'Výsledky:\n- Vyhodnotená presnosť modelu v experimente.') q='Aké experimenty a výsledky má diplomová práca Evy Novej?' p=prepare(corpus,q); c=rag_evidence_context(q,p) result=generate_verified_rag(q,p,complete(selection(c))) assert result['accepted_for_delivery'] assert result['answer'].count('Vyhodnotená presnosť modelu v experimente.')==1 assert 'chýbajú údaje' not in result['answer'] and 'Doložené vyššie' not in result['answer'] assert 'Zaznamenaný výsledok:' in result['answer'] or '**Zaznamenané výsledky:**' in result['answer'] def test_multiline_table_is_bounded_and_does_not_rewrite_source_html(): table=comparison_table(['Oblasť','Eva','Boris'],[('Výsledky',['Prvý výsledok.\nDruhý výsledok.','Parser.'])]) assert table and 'Prvý výsledok.
Druhý výsledok.' in table[-1] assert comparison_table(['Oblasť','Eva'],[('Výsledky',['Prvý výsledok.'])]) is None def test_shared_result_precedes_people_and_citations_without_repeating_tags(): people=['Ján Pták','Ján Malinovský']; urls=['https://example.test/ptak','https://example.test/malinovsky'] claims=[{'person':person,'keyword':tag} for person in people for tag in ['NLP','RAG']] c={'status':'ok','complete':True,'truncated':False,'facts':[ {**claim,'source_url':urls[people.index(claim['person'])]} for claim in claims], 'sources':[{'source_url':u} for u in urls], 'requested_people':people, 'shared_properties':[{'field':'keyword','value':tag,'people':people} for tag in ['NLP','RAG']]} a={'status':'ok','claims':claims,'source_urls':urls} r=render_graph_answer(a,c); text=r['answer'] assert text.startswith('Spoločné kľúčové slová: **NLP**, **RAG**.') assert text.index('RAG')