209 lines
13 KiB
Python
209 lines
13 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from app.modules.work_assistant import orchestration
|
|
from app.modules.work_assistant.adaptive import continue_adaptive
|
|
from app.modules.work_assistant.answers import compose_verified_answer
|
|
from app.modules.work_assistant.protocol import normalize_proposal
|
|
from app.modules.work_assistant.schemas import AnswerRequest, ContinueRequest, PlanRequest
|
|
|
|
|
|
RESOURCES = [
|
|
{"id": "meetings", "name": "会议记录", "scope": "module", "operations": ["list", "count"],
|
|
"aliases": ["晨会", "周例会"], "fields": {"name": "会议名称"}, "relations": {},
|
|
"time_bindings": {"recorded": ["date_from", "date_to"]},
|
|
"filters": {"type": {"values": {"daily": "晨夕会", "weekly": "周例会"}},
|
|
"date_from": {"type": "date"}, "date_to": {"type": "date"}}},
|
|
{"id": "tasks", "name": "新任务", "scope": "assistant", "operations": ["list", "count"],
|
|
"fields": {"name": "任务名称"}, "relations": {},
|
|
"filters": {"status": {"values": {"today": "今日到期及计划", "overdue": "逾期未完成"}}}},
|
|
{"id": "menu-expense", "name": "报销请款", "scope": "module", "operations": ["list", "count"],
|
|
"fields": {"name": "报销事项"}, "relations": {},
|
|
"filters": {"status": {"values": {"done": "已通过", "open": "待审核"}}}},
|
|
]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def planner_settings(monkeypatch):
|
|
settings = SimpleNamespace(work_assistant_model_enabled=True, work_assistant_planner_timeout_seconds=20)
|
|
monkeypatch.setattr(orchestration, "get_settings", lambda: settings)
|
|
return settings
|
|
|
|
|
|
def request(question, **kwargs):
|
|
return PlanRequest(message=question, allowed_skills=[], protocol_version=2,
|
|
today="2026-11-25", resources=kwargs.pop("resources", RESOURCES), **kwargs)
|
|
|
|
|
|
def no_model(*args, **kwargs):
|
|
pytest.fail("A verified catalog read called the model")
|
|
|
|
|
|
@pytest.mark.parametrize(("question", "resource", "operation", "scope"), [
|
|
("会议记录有哪些?", "meetings", "list", "accessible"),
|
|
("报销请款里一共有多少条记录?", "menu-expense", "count", "accessible"),
|
|
("帮我列出新任务清单。", "tasks", "list", "mine"),
|
|
])
|
|
def test_reviewed_catalog_reads_work_without_model(question, resource, operation, scope, planner_settings):
|
|
planner_settings.work_assistant_model_enabled = False
|
|
plan = orchestration.plan_turn(request(question), invoke_model=no_model)
|
|
assert plan.routing == "rules" and plan.question_spec["catalog_read"] and not plan.clarification
|
|
assert [(q.resource, q.operation, q.scope) for q in plan.resource_queries] == [(resource, operation, scope)]
|
|
|
|
|
|
def test_followup_count_rereads_exact_latest_query():
|
|
active = "我今天有哪些任务要处理?"
|
|
spec = {"question": active, "primary_queries": [{"resource": "tasks", "scope": "mine",
|
|
"operation": "list", "filters": {"status": "today"}, "fields": ["name"], "offset": 8}]}
|
|
context = {"active_question": active, "last_assistant_act": "answered", "question_spec": spec}
|
|
plan = orchestration.plan_turn(request("那一共多少项?", dialogue_context=context), invoke_model=no_model)
|
|
query, = plan.resource_queries
|
|
assert plan.dialogue_mode == "followup"
|
|
assert (query.operation, query.offset, query.scope, query.filters) == ("count", 0, "mine", {"status": "today"})
|
|
|
|
|
|
def test_period_only_followup_keeps_original_conditions_and_date_basis():
|
|
active = "本月晨会一共有多少场?"
|
|
spec = {"question": active, "primary_queries": [{"resource": "meetings", "scope": "accessible",
|
|
"operation": "count", "filters": {"type": "daily", "date_from": "2026-11-01", "date_to": "2026-11-30"}}]}
|
|
context = {"active_question": active, "last_assistant_act": "answered", "question_spec": spec}
|
|
result = orchestration.plan_turn(request("那去年的呢?", dialogue_context=context), invoke_model=no_model)
|
|
query, = result.resource_queries
|
|
assert result.dialogue_mode == "followup" and result.resolved_question == "那去年的呢?"
|
|
assert (query.operation, query.scope, query.filters) == (
|
|
"count", "accessible", {"type": "daily", "date_from": "2025-01-01", "date_to": "2025-12-31"})
|
|
|
|
|
|
def test_catalog_uses_declared_date_basis():
|
|
plan = orchestration.plan_turn(request("上周的会议记录有多少场?"), invoke_model=no_model)
|
|
query, = plan.resource_queries
|
|
assert query.operation == "count" and query.filters == {"date_from": "2026-11-16", "date_to": "2026-11-22"}
|
|
|
|
|
|
def test_read_meeting_agenda_uses_typed_items_on_exact_day():
|
|
meeting = {**RESOURCES[0], "filters": {**RESOURCES[0]["filters"], "date": {"type": "date"}},
|
|
"relations": {"items": {"target": "meeting-items"}}}
|
|
result = orchestration.plan_turn(request("2026年11月25日晨会具体安排了哪些工作?", resources=[meeting]), invoke_model=no_model)
|
|
query, = result.resource_queries
|
|
assert (query.resource, query.include, query.filters) == ("meetings", ["items"], {"date": "2026-11-25", "type": "daily"})
|
|
|
|
|
|
def test_submission_count_uses_authenticated_actor_and_actual_submission_date():
|
|
log = {"id": "work-logs", "name": "日志提交记录", "operations": ["count"],
|
|
"filters": {"type": {"values": {"daily": "日报", "weekly": "周报"}},
|
|
"submitted_from": {"type": "date"}, "submitted_to": {"type": "date"}},
|
|
"time_bindings": {"submitted": ["submitted_from", "submitted_to"]}}
|
|
result = orchestration.plan_turn(request("我在2026年11月25日实际提交了几份日报?只按提交时间查。", resources=[log]), invoke_model=no_model)
|
|
query, = result.resource_queries
|
|
assert (query.operation, query.scope, query.filters) == (
|
|
"count", "mine", {"type": "daily", "submitted_from": "2026-11-25", "submitted_to": "2026-11-25"})
|
|
|
|
|
|
def test_catalog_keeps_dates_separate_for_two_explicit_reads():
|
|
plan = orchestration.plan_turn(request("上周的会议记录有多少场?再查一下报销请款列表。"), invoke_model=no_model)
|
|
assert [(q.resource, q.operation, q.filters) for q in plan.resource_queries] == [
|
|
("meetings", "count", {"date_from": "2026-11-16", "date_to": "2026-11-22"}),
|
|
("menu-expense", "list", {}),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("question", ["周例会有哪些?", "我们部门的报销请款有哪些?",
|
|
"今天报销请款有多少条?", "会议记录有哪些,顺便查一下报销?"])
|
|
def test_catalog_does_not_drop_qualifiers(question, planner_settings):
|
|
planner_settings.work_assistant_model_enabled = False
|
|
assert not orchestration.plan_turn(request(question), invoke_model=no_model).resource_queries
|
|
|
|
|
|
def test_absent_or_ambiguous_resources_do_not_become_direct_queries(planner_settings):
|
|
planner_settings.work_assistant_model_enabled = False
|
|
same_name = [{**RESOURCES[0], "id": "menu-meetings"}, RESOURCES[0]]
|
|
for visible in (same_name, RESOURCES[1:]):
|
|
plan = orchestration.plan_turn(request("会议记录有哪些?", resources=visible), invoke_model=no_model)
|
|
assert not plan.resource_queries
|
|
|
|
|
|
def test_unknown_condition_is_repaired_without_dropping_other_queries():
|
|
attempts = []
|
|
|
|
def model(instruction, content, schema, **kwargs):
|
|
attempts.append(content)
|
|
if len(attempts) == 2:
|
|
assert content["errors"][0]["type"] == "unknown_filter_value"
|
|
return {"intent": "business", "plan": {"resource_queries": [
|
|
{"resource": "tasks", "operation": "count", "filters": {"status": "逾期未完成"}},
|
|
{"resource": "menu-expense", "operation": "count", "filters": {"status": "待审核" if len(attempts) == 2 else "pending"}},
|
|
]}}
|
|
|
|
plan = orchestration.plan_turn(request("我的逾期任务和待审核报销各有多少?"), invoke_model=model)
|
|
assert len(attempts) == 2 and not plan.clarification
|
|
assert [(q.resource, q.filters) for q in plan.resource_queries] == [
|
|
("tasks", {"status": "overdue"}), ("menu-expense", {"status": "open"})]
|
|
|
|
|
|
def test_ambiguous_enum_labels_and_business_names_are_preserved():
|
|
resource = {"id": "records", "filters": {"name": {}, "status": {"values": {"A": "处理", "B": "处理"}}}}
|
|
query = {"resource": "records", "filters": {"name": "处理", "status": "处理"}}
|
|
assert normalize_proposal({"plan": {"resource_queries": [query]}}, [resource])["plan"]["resource_queries"] == [query]
|
|
|
|
|
|
@pytest.mark.parametrize(("date_value", "evidence"), [
|
|
("2026年11月25日", "2026年11月25日"), ("2026-11-25", "2026-11-25"), ("2026/11/25", "2026/11/25"),
|
|
("2026年11月25日", "2026-11-25"), ("2026年11月25日", "2026年11月25日我实际交了"), ("2026年11月25日", ""),
|
|
])
|
|
def test_existing_read_conditions_supply_missing_explicit_focus_dates(date_value, evidence):
|
|
log = {"id": "work-logs", "name": "日志提交记录", "scope": "assistant", "operations": ["count"],
|
|
"fields": {}, "filters": {key: {"type": "date"} for key in ("date_from", "date_to", "submitted_from", "submitted_to")},
|
|
"time_bindings": {"reported": ["date_from", "date_to"], "submitted": ["submitted_from", "submitted_to"]}}
|
|
calls = []
|
|
|
|
def provider(*args, **kwargs):
|
|
calls.append(1)
|
|
return {"intent": "business", "focus": {"subject_action": "self", "time_action": "set", "time_evidence": evidence},
|
|
"plan": {"resource_queries": [{"resource": "work-logs", "operation": "count",
|
|
"filters": {"submitted_from": "2026-11-25", "submitted_to": "2026-11-25"}}]}}
|
|
|
|
planned = orchestration.plan_turn(request(f"{date_value}我实际交了几份?", resources=[log]), invoke_model=provider)
|
|
query, = planned.resource_queries
|
|
assert len(calls) == 1 and query.filters == {"submitted_from": "2026-11-25", "submitted_to": "2026-11-25"}
|
|
assert planned.question_spec["period"]["basis"] == "submitted"
|
|
|
|
|
|
def test_explicit_time_recovery_requires_matching_dates_and_literal_scope():
|
|
from app.modules.work_assistant.schemas import ResourceQuery
|
|
|
|
literal = "2026年11月25日"
|
|
patch = orchestration.FocusPatch(time_action="set", time_evidence=literal)
|
|
query = ResourceQuery(resource="meetings", filters={"date_from": "2026-11-24", "date_to": "2026-11-24"})
|
|
orchestration._restore_explicit_period(patch, [query], {"meetings": RESOURCES[0]}, literal)
|
|
assert not patch.date_from
|
|
query.filters = {"date_from": "2026-11-25", "date_to": "2026-11-25"}
|
|
orchestration._restore_explicit_period(patch, [query], {"meetings": RESOURCES[0]}, literal + "以前的会议")
|
|
assert not patch.date_from
|
|
|
|
|
|
def test_known_list_and_partial_multiquery_show_records_without_model(monkeypatch):
|
|
monkeypatch.setattr("app.modules.work_assistant.investigation.complete_json", no_model)
|
|
good = {"query": "resource:meetings", "title": "会议记录", "resource": "meetings", "status": "ok",
|
|
"total": 1, "fields": {"name": "会议名称"}, "rows": [{"id": "22", "name": "测试会议"}]}
|
|
decision = continue_adaptive(ContinueRequest(**request("会议记录有哪些?").model_dump(),
|
|
evidence=[good], question_spec={"catalog_read": True}))
|
|
assert not decision.answer and not decision.resource_queries and not decision.note
|
|
common = dict(message="会议记录有哪些?", skill_ids=[], scope_label="账号可见范围", data_time="2026-11-25",
|
|
available_resources=RESOURCES, protocol_version=2)
|
|
reply = compose_verified_answer(AnswerRequest(**common, question_spec={"catalog_read": True}, evidence=[good]),
|
|
invoke_model=no_model)
|
|
assert reply["answer_status"] == "complete" and reply["source_ids"] == ["meetings:22"]
|
|
failed = {"query": "resource:menu-expense", "title": "报销请款", "resource": "menu-expense", "status": "error",
|
|
"note": "请确认涉及哪位申请人。"}
|
|
partial = compose_verified_answer(AnswerRequest(**common, evidence=[good, failed]), invoke_model=no_model)
|
|
assert partial["answer_status"] == "partial" and partial["source_ids"] == ["meetings:22"]
|
|
assert "测试会议" in partial["answer"] and "哪位申请人" in partial["answer"] and "共 0 项" not in partial["answer"]
|
|
|
|
|
|
def test_empty_success_does_not_invoke_synthesis(monkeypatch):
|
|
monkeypatch.setattr("app.modules.work_assistant.investigation.complete_json", no_model)
|
|
evidence = {"query": "resource:meetings", "resource": "meetings", "title": "会议记录", "status": "ok", "total": 0, "rows": []}
|
|
decision = continue_adaptive(ContinueRequest(**request("星河楼会谈有什么安排?").model_dump(), evidence=[evidence]))
|
|
assert not decision.answer and not decision.resource_queries and not decision.note
|