Files
DeepTutor/tests/tools/test_ask_user.py
Bingxi Zhao (Frank) 7bdb10b700 feat(chat): Auto mode router + new chat tools (ask_user, web_fetch, etc.)
Auto capability (agents/auto/) gains a three-stage agent loop:
ANALYZING (one LLM call, streamed as thinking) -> DELEGATING (router
emits delegate_to_<cap> tool calls or atomic tool calls, with bounded
retries and a same-cap quota) -> SYNTHESIZING (final inline answer).
Sub-capability events flow through forward_events with call_id tagging
so conversation history keeps only Auto's own synthesis.

New chat-mountable tools:
- ask_user: pauses the turn for 1-3 structured questions, frontend
  renders a card the user navigates and submits in one batch
- web_fetch: HTTP + readable-content extraction with strict
  scheme/private-IP/size guards (pre-flight + post-redirect)
- write_note: replaces save_to_notebook; append (new record) and
  edit (existing record by id) modes
- list_notebook: read-only index / drill-down listing; only mounted
  when the user has notebooks
- github_query: read-only ``gh`` CLI wrapper (pr/issue/run/repo plus
  GET-only api); no mutation verbs reachable

Updates chat agentic_pipeline.py + agentic_chat.yaml (en/zh) to the
new tool surface and source-inventory contract, refits
tools/builtin/__init__.py as the single place to register
chat-mounted tools and their argument-augmentation wrappers, and
tightens core stream + tool_protocol around tool-result metadata.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 18:05:15 +08:00

237 lines
6.8 KiB
Python

"""Unit tests for ``ask_user`` payload building.
Covers both the v2 multi-question schema and the legacy single-question
shorthand which auto-wraps into a one-element list.
"""
from __future__ import annotations
from deeptutor.tools.ask_user import (
MAX_OPTION_CHARS,
MAX_OPTIONS,
MAX_QUESTION_CHARS,
MAX_QUESTIONS,
build_ask_user_payload,
)
# ----------------------------- legacy shape -----------------------------
def test_legacy_rejects_empty_question() -> None:
payload, err = build_ask_user_payload(question=" ")
assert payload is None
assert err and "prompt" in err
def test_legacy_question_only_wraps_to_single() -> None:
payload, err = build_ask_user_payload(question="What's your level?")
assert err is None
assert payload is not None
assert len(payload.questions) == 1
only = payload.questions[0]
assert only.id == "q1"
assert only.prompt == "What's your level?"
assert only.options == ()
assert only.allow_free_text is True
def test_legacy_options_strip_and_dedupe() -> None:
payload, err = build_ask_user_payload(
question="Pick",
options=[" A ", "B", "A", "", "C"],
)
assert err is None
assert payload is not None
assert payload.questions[0].options == ("A", "B", "C")
def test_legacy_caps_option_count() -> None:
payload, _ = build_ask_user_payload(
question="Pick",
options=[f"opt-{i}" for i in range(MAX_OPTIONS + 5)],
)
assert payload is not None
assert len(payload.questions[0].options) == MAX_OPTIONS
def test_legacy_clips_oversized_question() -> None:
payload, _ = build_ask_user_payload(question="q" * (MAX_QUESTION_CHARS + 50))
assert payload is not None
prompt = payload.questions[0].prompt
assert prompt.endswith("…")
assert len(prompt) <= MAX_QUESTION_CHARS + 1
def test_legacy_clips_oversized_option() -> None:
payload, _ = build_ask_user_payload(
question="q",
options=["x" * (MAX_OPTION_CHARS + 100)],
)
assert payload is not None
assert payload.questions[0].options[0].endswith("…")
def test_legacy_rejects_non_list_options() -> None:
payload, err = build_ask_user_payload(question="q", options="not-a-list")
assert payload is None
assert err and "array" in err
# ------------------------------- v2 shape -------------------------------
def test_v2_multiple_questions_assigned_default_ids() -> None:
payload, err = build_ask_user_payload(
questions=[
{"prompt": "scope?"},
{"prompt": "depth?"},
{"prompt": "format?"},
]
)
assert err is None
assert payload is not None
assert payload.question_ids == ("q1", "q2", "q3")
def test_v2_respects_explicit_ids() -> None:
payload, _ = build_ask_user_payload(
questions=[
{"id": "scope", "prompt": "A"},
{"id": "depth", "prompt": "B"},
]
)
assert payload is not None
assert payload.question_ids == ("scope", "depth")
def test_v2_disambiguates_duplicate_ids() -> None:
payload, _ = build_ask_user_payload(
questions=[
{"id": "x", "prompt": "A"},
{"id": "x", "prompt": "B"},
]
)
assert payload is not None
assert payload.question_ids == ("x", "x_2")
def test_v2_caps_question_count() -> None:
payload, _ = build_ask_user_payload(
questions=[{"prompt": f"q{i}"} for i in range(MAX_QUESTIONS + 4)]
)
assert payload is not None
assert len(payload.questions) == MAX_QUESTIONS
def test_v2_intro_included_and_clipped() -> None:
payload, _ = build_ask_user_payload(
questions=[{"prompt": "hi"}],
intro="To tailor the research:",
)
assert payload is not None
assert payload.intro == "To tailor the research:"
def test_v2_empty_intro_becomes_none() -> None:
payload, _ = build_ask_user_payload(
questions=[{"prompt": "hi"}],
intro=" ",
)
assert payload is not None
assert payload.intro is None
def test_v2_rejects_non_object_question() -> None:
payload, err = build_ask_user_payload(questions=["bare string"])
assert payload is None
assert err and "object" in err
def test_v2_rejects_empty_questions_list() -> None:
payload, err = build_ask_user_payload(questions=[])
assert payload is None
assert err and "at least one" in err
def test_v2_rejects_non_array_questions() -> None:
payload, err = build_ask_user_payload(questions={"prompt": "x"})
assert payload is None
assert err and "array" in err
def test_v2_accepts_question_alias_for_prompt() -> None:
"""LLMs sometimes use ``question`` instead of ``prompt`` inside a v2 item."""
payload, err = build_ask_user_payload(
questions=[{"question": "what?", "options": ["a", "b"]}]
)
assert err is None
assert payload is not None
assert payload.questions[0].prompt == "what?"
assert payload.questions[0].options == ("a", "b")
def test_v2_allow_free_text_default_true() -> None:
payload, _ = build_ask_user_payload(questions=[{"prompt": "x"}])
assert payload is not None
assert payload.questions[0].allow_free_text is True
def test_v2_allow_free_text_can_be_disabled() -> None:
payload, _ = build_ask_user_payload(
questions=[{"prompt": "x", "options": ["a"], "allow_free_text": False}]
)
assert payload is not None
assert payload.questions[0].allow_free_text is False
def test_v2_placeholder_stored() -> None:
payload, _ = build_ask_user_payload(
questions=[{"prompt": "x", "placeholder": "type here"}]
)
assert payload is not None
assert payload.questions[0].placeholder == "type here"
# ------------------------- frontend contract shape ------------------------
def test_to_dict_shape_is_v2_for_frontend() -> None:
payload, _ = build_ask_user_payload(
questions=[
{"id": "scope", "prompt": "Q1", "options": ["a"]},
{"id": "depth", "prompt": "Q2"},
],
intro="hi",
)
assert payload is not None
assert payload.to_dict() == {
"intro": "hi",
"questions": [
{
"id": "scope",
"prompt": "Q1",
"options": ["a"],
"allow_free_text": True,
"placeholder": None,
},
{
"id": "depth",
"prompt": "Q2",
"options": [],
"allow_free_text": True,
"placeholder": None,
},
],
}
def test_to_dict_legacy_path_also_emits_v2() -> None:
payload, _ = build_ask_user_payload(question="hi", options=["a", "b"])
assert payload is not None
d = payload.to_dict()
assert d["intro"] is None
assert len(d["questions"]) == 1
assert d["questions"][0]["prompt"] == "hi"
assert d["questions"][0]["options"] == ["a", "b"]