213 lines
11 KiB
Python
213 lines
11 KiB
Python
import copy
|
|
import pytest
|
|
|
|
from app.modules.work_assistant.message_push import PushPreviewRequest, generate_preview
|
|
|
|
|
|
def request(kind='log', content='张甲已提交会议纪要,待负责人核对。'):
|
|
return PushPreviewRequest(kind=kind, subject='综合部', period='2026-11-16 至 2026-11-22',
|
|
contents={'log':['内容检查'],'project':['交付与阻碍'],'risk':['节点风险'],'performance':['履职检查']}[kind],
|
|
presentation='executive', evidence=[dict(id='s1',label='张甲工作记录',content=content,kind='review',relation='published_result')],limits=[])
|
|
|
|
|
|
def candidate():
|
|
basis=[dict(excerpt_id='e1')]
|
|
return dict(summary=dict(text='张甲已提交会议纪要,待负责人核对。',basis=basis),messages=[dict(
|
|
title='纪要已提交,待核对',work='张甲',judgment='已提交会议纪要,核对结果尚未确认。',
|
|
suggestion='请负责人核对会议纪要。',priority='verify',basis=basis)],empty_reason='')
|
|
|
|
|
|
@pytest.mark.parametrize('kind',['log','project','risk','performance'])
|
|
def test_each_kind_runs_generation_and_audit_with_bound_citations(kind):
|
|
calls=[]
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
calls.append((prompt,material))
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='与引用一致。') for p in material['points']]}
|
|
return candidate()
|
|
result=generate_preview(request(kind),invoke=invoke)
|
|
assert result['version']=='message-brief-v2'
|
|
assert len(calls)==2
|
|
assert {'log':'工作日志简报','project':'项目进展简报','risk':'项目风险简报','performance':'绩效检查简报'}[kind] in calls[0][0]
|
|
assert result['summary']['basis'][0]==dict(id='s1',quote='张甲已提交会议纪要,待负责人核对。')
|
|
assert result['messages'][0]['work']=='张甲'
|
|
|
|
|
|
def run(value, decisions=None, content='张甲已提交会议纪要,待负责人核对。'):
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=decisions(p) if decisions else True,reason='隔离测试结论。') for p in material['points']]}
|
|
return copy.deepcopy(value)
|
|
return generate_preview(request(content=content),invoke=invoke)
|
|
|
|
|
|
def test_rejected_lead_cannot_survive_while_details_pass():
|
|
value=candidate();value['summary']['text']='全公司所有工作均按期交付。'
|
|
result=run(value)
|
|
assert result['summary']['text']=='本期重点:纪要已提交,待核对'
|
|
assert '全公司' not in str(result)
|
|
|
|
|
|
def test_rejected_lead_uses_verified_titles_without_regenerating_good_items():
|
|
value=candidate();value['summary']['text']='全公司所有工作均按期交付。'
|
|
second=copy.deepcopy(value['messages'][0]);second.update(work='李乙',title='另一项记录待核对');value['messages'].append(second)
|
|
calls=[]
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
calls.append(material)
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='核对。') for p in material['points']]}
|
|
return value
|
|
result=generate_preview(request(),invoke=invoke)
|
|
assert len(calls)==2
|
|
assert result['summary']['text']=='本期重点:纪要已提交,待核对;另一项记录待核对'
|
|
assert len(result['summary']['basis'])==1
|
|
|
|
|
|
def test_registration_cannot_become_acceptance_even_if_auditor_accepts():
|
|
value=candidate();value['messages'][0]['judgment']='任务已完成验收并完成交付。'
|
|
result=run(value,content='任务登记完成,实际验收尚待核对。')
|
|
assert result['messages']==[] and result['summary'] is None
|
|
assert result['outcome']=='unverified'
|
|
assert result['empty_reason']
|
|
|
|
|
|
@pytest.mark.parametrize('bad',['task-13247 的完成时间为null。','任务登记完成时间为null,进度待核对。','任务状态为 undefined。','已提交会议纪要。\n另一条:请处理。'])
|
|
def test_internal_fields_and_inline_lists_are_repaired_or_withheld(bad):
|
|
value=candidate();value['messages'][0]['judgment']=bad
|
|
assert run(value)['messages']==[]
|
|
|
|
|
|
def test_same_person_cannot_be_split_between_progress_and_actions():
|
|
value=candidate();value['messages'].append(copy.deepcopy(value['messages'][0]))
|
|
value['messages'][1]['title']='负责人核对会议纪要'
|
|
value['messages'][1]['judgment']='会议待办已登记。'
|
|
value['messages'][1]['basis']=[dict(excerpt_id='e2')]
|
|
items=run(value,content='张甲已提交会议纪要,待负责人核对。会议待办已登记。')['messages']
|
|
assert len(items)==1
|
|
assert '会议待办已登记' in items[0]['judgment']
|
|
assert '纪要' in items[0]['judgment']
|
|
assert len(items[0]['basis'])==2
|
|
|
|
|
|
def test_normal_progress_needs_no_invented_action():
|
|
value=candidate();value['messages'][0]['suggestion']='';value['messages'][0]['priority']='progress'
|
|
assert run(value)['messages'][0]['suggestion']==''
|
|
|
|
|
|
def test_empty_action_word_is_not_a_useless_next_step():
|
|
value=candidate();value['messages'][0]['suggestion']='无。'
|
|
assert run(value)['messages'][0]['suggestion']==''
|
|
|
|
|
|
def test_title_cannot_rename_the_project_to_its_owner():
|
|
req=request('risk');req.evidence[0].entity='甲项目'
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='测试核对。') for p in material['points']]}
|
|
return candidate()
|
|
assert generate_preview(req,invoke=invoke)['messages']==[]
|
|
|
|
|
|
def test_submission_is_not_supervisor_approval():
|
|
value=candidate();value['messages'][0]['judgment']='已提交会议纪要并获主管确认。'
|
|
assert run(value,content='已提交会议纪要,主管已经接收。')['messages']==[]
|
|
|
|
|
|
@pytest.mark.parametrize('claim',['需求确认未完成','需求调研未按时完成'])
|
|
def test_missing_completion_registration_cannot_be_rephrased_as_actual_delay(claim):
|
|
value=candidate();value['messages'][0]['title']=claim
|
|
result=run(value,content='原计划11月20日确认需求,截至11月22日未登记完成,实际进展待核对。')
|
|
assert result['outcome']=='unverified'
|
|
assert result['messages']==[]
|
|
|
|
|
|
@pytest.mark.parametrize('title',['需求确认未登记完成', '需求确认登记未完成', '需求确认登记尚未完成'])
|
|
def test_registration_qualification_is_kept_in_a_verified_title(title):
|
|
value=candidate();value['messages'][0]['title']=title
|
|
result=run(value,content='原计划11月20日确认需求,截至11月22日未登记完成,实际进展待核对。')
|
|
assert result['messages'][0]['title']==title
|
|
|
|
|
|
def test_missing_completion_field_does_not_create_a_rectification_requirement():
|
|
value=candidate();value['messages'][0]['suggestion']='请补充任务登记时间。'
|
|
assert run(value,content='任务登记完成时间为空,实际结果待核对。')['messages']==[]
|
|
|
|
|
|
def test_partial_audit_rebuilds_lead_from_accepted_fact():
|
|
value=candidate();second=copy.deepcopy(value['messages'][0]);second['work']='李乙';value['messages'].append(second)
|
|
result=run(value,decisions=lambda p:p['index']!=2)
|
|
assert len(result['messages'])==1
|
|
assert result['summary']['text']=='本期重点:纪要已提交,待核对'
|
|
|
|
|
|
def test_missing_sources_never_calls_model_or_invents_conclusion():
|
|
req=request();req.evidence=[]
|
|
result=generate_preview(req,invoke=lambda *a,**kw:pytest.fail('No source needs no model'))
|
|
assert result['messages']==[] and result['summary'] is None
|
|
assert result['outcome']=='empty'
|
|
|
|
|
|
def test_missing_timestamp_between_two_facts_preserves_literal_quotes():
|
|
raw='初审已经完成,任务登记完成时间为null,复核安排待确认。'
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
assert [x['text'] for x in material['excerpts']]==['初审已经完成,','复核安排待确认。']
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='引用为原文。') for p in material['points']]}
|
|
value=candidate()
|
|
value['summary'].update(text='初审完成,复核安排待确认。',basis=[dict(excerpt_id='e1'),dict(excerpt_id='e2')])
|
|
value['messages'][0].update(title='初审完成',judgment='初审已完成,复核安排待确认。',suggestion='',basis=value['summary']['basis'])
|
|
return value
|
|
result=generate_preview(request(content=raw),invoke=invoke)
|
|
assert result['outcome']=='ready'
|
|
assert all(b['quote'] in raw for b in result['messages'][0]['basis'])
|
|
|
|
|
|
def test_only_missing_timestamp_needs_no_model():
|
|
result=generate_preview(request(content='任务登记完成时间为null。'),invoke=lambda *a,**kw:pytest.fail('No facts need no model'))
|
|
assert result['outcome']=='empty'
|
|
|
|
|
|
def test_empty_model_answer_does_not_publish_unverified_reason():
|
|
result=generate_preview(request(),invoke=lambda *a,**kw:dict(summary=None,messages=[],empty_reason='所有任务均通过验收。'))
|
|
assert result['outcome']=='empty'
|
|
assert '验收' not in result['empty_reason']
|
|
|
|
|
|
def test_repair_returning_empty_does_not_drop_verified_items():
|
|
value=candidate()
|
|
second=copy.deepcopy(value['messages'][0]);second.update(work='李乙',judgment='内容未归档 task-13247')
|
|
value['messages'].append(second)
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='对照。') for p in material['points']]}
|
|
return dict(summary=None,messages=[],empty_reason='') if 'audit' in material else value
|
|
result=generate_preview(request(),invoke=invoke)
|
|
assert result['outcome']=='ready'
|
|
assert [item['work'] for item in result['messages']]==['张甲']
|
|
|
|
|
|
def test_repair_keeps_verified_item_when_model_only_returns_corrected_item():
|
|
original=candidate()
|
|
second=copy.deepcopy(original['messages'][0]);second.update(work='李乙',judgment='内部编号 task-13247')
|
|
original['messages'].append(second)
|
|
revised=candidate();revised['messages'][0].update(work='李乙',title='复核安排待确认')
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
if 'decisions' in schema['properties']:
|
|
return {'decisions':[dict(index=p['index'],supported=True,reason='对照。') for p in material['points']]}
|
|
return revised if 'audit' in material else original
|
|
result=generate_preview(request(),invoke=invoke)
|
|
assert [item['work'] for item in result['messages']]==['张甲','李乙']
|
|
assert result['summary']['text']=='本期重点:纪要已提交,待核对;复核安排待确认'
|
|
|
|
|
|
def test_citations_from_another_material_are_never_published():
|
|
value=candidate();value['messages'][0]['basis']=[dict(excerpt_id='e999')]
|
|
assert run(value)['messages']==[]
|
|
|
|
|
|
def test_incomplete_audit_is_not_treated_as_approval():
|
|
def invoke(prompt,material,schema,**kwargs):
|
|
return dict(decisions=[]) if 'decisions' in schema['properties'] else candidate()
|
|
with pytest.raises(ValueError,match='incomplete_brief_audit'):
|
|
generate_preview(request(),invoke=invoke)
|