Files
DeepTutor/tests/cli/test_chat_state_output.py
Bingxi Zhao (Frank) 712d4137f0 feat: DSML streaming robustness, memory observability, and mastery pending questions
Work in progress carried over from earlier sessions, committed together.

Chat / DSML:
- Tolerate partial and malformed tool-call tags mid-stream, with a character
  ceiling so a stray "<" cannot make the live stream wait forever for its
  closing bracket.
- Narration rounds can now mark their prose as visible, so a DSML round that
  keeps the text surrounding a call has it persisted as part of the answer.

Runtime observability:
- New deeptutor/runtime/memory_probe.py reports resident memory across the
  process tree, exposed at /api/v1/system/memory and surfaced in the settings
  status strip. psutil is a soft dependency, imported lazily with a /proc
  fallback, so a missing wheel costs the strip and not the app.

Mastery:
- New deeptutor/learning/pending.py projects a persisted PendingQuestion into
  the smaller public contract safe to hand the tutor model, and owns the
  multiple-choice translations shared by registration and grading.

LightRAG:
- New pipelines/lightrag/worker.py moves local indexing onto a worker thread's
  own event loop, so RAG-Anything's synchronous graph merging stops stalling
  unrelated API and LLM work.

Also: request-scoped output artifact delivery (api/routers/outputs.py),
partner runtime and path_service adjustments, and CLI chat renderer updates.
2026-08-09 02:31:44 +08:00

104 lines
3.0 KiB
Python

"""Regression tests for literal state values in the interactive chat CLI."""
from __future__ import annotations
from io import StringIO
import pytest
from rich.console import Console
from rich.text import Text
from deeptutor_cli import chat
def _render(monkeypatch: pytest.MonkeyPatch, render, state: chat.ChatState) -> str: # noqa: ANN001
output = StringIO()
monkeypatch.setattr(
chat,
"console",
Console(file=output, force_terminal=False, color_system=None, width=1000),
)
render(state)
return output.getvalue()
@pytest.mark.parametrize(
("tools", "expected"),
[
([], "tools=[]"),
(["rag"], "tools=[rag]"),
(["rag", "web_search"], "tools=[rag, web_search]"),
],
)
def test_print_state_renders_tool_lists_literally(
monkeypatch: pytest.MonkeyPatch,
tools: list[str],
expected: str,
) -> None:
output = _render(monkeypatch, chat._print_state, chat.ChatState(tools=tools))
assert expected in output
def test_print_state_renders_rich_markup_characters_literally(
monkeypatch: pytest.MonkeyPatch,
) -> None:
state = chat.ChatState(
session_id="[bold]session[/bold]",
capability="[cyan]chat[/cyan]",
tools=["[rag]"],
knowledge_bases=["[red]kb[/red]"],
history_references=["[green]history[/green]"],
notebook_references=[
{"notebook_id": "[blue]notebook[/blue]", "record_ids": ["[yellow]record[/yellow]"]}
],
language="[magenta]en[/magenta]",
config={"theme": "[italic]literal[/italic]"},
)
output = _render(monkeypatch, chat._print_state, state)
for literal in (
"[bold]session[/bold]",
"[cyan]chat[/cyan]",
"tools=[[rag]]",
"kb=[[red]kb[/red]]",
"history=[[green]history[/green]]",
"[blue]notebook[/blue]:[yellow]record[/yellow]",
"[magenta]en[/magenta]",
'"[italic]literal[/italic]"',
):
assert literal in output
def test_print_state_keeps_dim_style(monkeypatch: pytest.MonkeyPatch) -> None:
rendered: list[object] = []
class RecordingConsole:
def print(self, value, **_kwargs) -> None: # noqa: ANN001
rendered.append(value)
monkeypatch.setattr(chat, "console", RecordingConsole())
chat._print_state(chat.ChatState(tools=["rag"]))
assert len(rendered) == 1
assert isinstance(rendered[0], Text)
assert rendered[0].style == "dim"
def test_print_refs_renders_dynamic_values_literally(monkeypatch: pytest.MonkeyPatch) -> None:
state = chat.ChatState(
tools=["[bold]rag[/bold]"],
knowledge_bases=["[red]kb[/red]"],
history_references=["[green]history[/green]"],
notebook_references=[{"notebook_id": "[blue]notebook[/blue]", "record_ids": []}],
)
output = _render(monkeypatch, chat._print_refs, state)
assert "tools [[bold]rag[/bold]]" in output
assert "kb [[red]kb[/red]]" in output
assert "history [[green]history[/green]]" in output
assert "notebooks [[blue]notebook[/blue]]" in output