Files
DeepTutor/tests/utils/test_json_parser.py
khunmax2 da6038b379 fix(mcp): reconnect on credential change; stop reporting transport failures as tool timeouts (#813)
* update

* fix(mcp): reconnect on credential change; stop masking transport errors as timeouts

Two independent bugs, both surfaced connecting a remote MCP server that
authenticates with a header key.

connection_signature() fingerprints the stored config, which holds
${secret:...} references rather than values, so rotating a credential left
the reload diff byte-identical and the session kept using the replaced key
until the process restarted. _signature() now mixes in a digest of the
materialized config. The digest, never the value, reaches the signature;
configs with no references keep their plain signature, so this does not
invalidate and drop every live session on upgrade.

A transport failure on the POST carrying a tool call is raised inside the
SDK's own anyio task group and never resolves the caller's request, so a
call that failed in under a second was reported as a tool timeout -- the
one explanation that rules out the actual cause. call_tool() now watches
the connection task alongside the call and reports the error that task
recorded. _run_server() routes its post-live failure through the existing
describe_connect_failure(), so that string names the HTTP status rather
than "unhandled errors in a TaskGroup".

Tests: tests/services/mcp/test_call_failures.py

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs: add Open Source Partners section with PageIndex

* docs: replace partner blurb with PageIndex promo code

* docs: link PageIndex subscription to developer portal

* docs: point PageIndex logo at its GitHub repo

* fix: harden Responses stream parsing (#825)

* fix: remove obsolete Typer extra (#817)

* fix: normalize RAG citation payloads (#808)

* fix: decouple mastery paths from chat sessions (#810)

* fix(llm): mark OpenAI Codex as vision capable (#821)

Declare the Codex Responses provider as image-capable so multimodal calls do not silently degrade to text and RAG image indexing can use it. Cover capability resolution, fallback behavior, and input_image conversion.

* fix: return minimal reasoning_effort for Gemini 3 and 2.5 Pro (#809)

Gemini 3 models and Gemini 2.5 Pro reject reasoning_effort="none"
with a 400 INVALID_ARGUMENT error. Per Google's OpenAI-compat docs,
"none" is only valid for the 2.5 Flash family.

Return "minimal" (the lowest valid level) for Gemini 3 and 2.5 Pro,
preserving the cost-intent while avoiding the API error.

Fixes #734

Co-authored-by: 0xtidalbore <ahmed@itcloud.ca>

* fix(mcp): don't orphan a cancelled call, or leak a URL credential

Follow-ups to this branch's own two fixes, both on paths it opened:

- ``asyncio.wait`` — unlike ``gather`` — leaves the futures it waits on
  running when the *waiter* is cancelled, and the call was moved into a
  free-standing task, so nothing else would ever stop it. Stopping a turn
  left the tool running against the server with its result discarded.
  Cancel and absorb it whenever the wait is interrupted, sharing one
  ``_abandon`` helper with the connection-died path.
- A dead transport is now reported to the *model*, not just in settings,
  and httpx names the full request URL in its message. ``CredentialTarget``
  includes "url_param" and several curated servers use it, so that string
  can carry an API key. Redact query and userinfo before it travels.

Also: route the one transient retry through the same watcher (a retry is
if anything more likely to meet a dead transport — the exact failure this
branch exists to stop reporting as a timeout), and skip materializing a
config that holds no ``${secret:...}`` references at all. The latter is
worth it: ``_signature`` runs per live connection on every reload, which
``ensure_scope`` performs once per turn, and resolving does per-reference
mkdir + chmod + read synchronously on that path.

---------

Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: NoxsMedia <155662416+NoxsMedia@users.noreply.github.com>
Co-authored-by: 0xTidalBore <142905837+0xTidalBore@users.noreply.github.com>
Co-authored-by: 0xtidalbore <ahmed@itcloud.ca>
2026-08-12 17:06:37 +08:00

225 lines
8.8 KiB
Python

"""Tests for robust JSON parsing utilities."""
from __future__ import annotations
import json
import logging
from unittest.mock import patch
import pytest
from deeptutor.utils.json_parser import parse_json_response, safe_json_loads
# ---------------------------------------------------------------------------
# parse_json_response — direct parsing
# ---------------------------------------------------------------------------
class TestParseJsonResponseDirect:
def test_valid_json_object(self) -> None:
assert parse_json_response('{"key": "value"}') == {"key": "value"}
def test_valid_json_array(self) -> None:
assert parse_json_response("[1, 2, 3]") == [1, 2, 3]
def test_valid_json_string(self) -> None:
assert parse_json_response('"hello"') == "hello"
def test_empty_string_returns_fallback(self) -> None:
assert parse_json_response("") == {}
def test_whitespace_only_returns_fallback(self) -> None:
assert parse_json_response(" \n ") == {}
def test_none_returns_fallback(self) -> None:
assert parse_json_response(None) == {} # type: ignore[arg-type]
def test_explicit_none_fallback(self) -> None:
assert parse_json_response("", fallback=None) is None
def test_custom_fallback(self) -> None:
assert parse_json_response("not json", fallback={"default": True}) == {"default": True}
# ---------------------------------------------------------------------------
# parse_json_response — markdown extraction
# ---------------------------------------------------------------------------
class TestParseJsonResponseMarkdown:
def test_json_code_block(self) -> None:
response = '```json\n{"key": "value"}\n```'
assert parse_json_response(response) == {"key": "value"}
def test_plain_code_block(self) -> None:
response = '```\n{"answer": 42}\n```'
assert parse_json_response(response) == {"answer": 42}
def test_code_block_with_surrounding_text(self) -> None:
response = 'Here is the result:\n```json\n{"x": 1}\n```\nDone.'
assert parse_json_response(response) == {"x": 1}
def test_nested_backticks_extracts_first(self) -> None:
response = '```json\n{"a": 1}\n```\nsome text\n```json\n{"b": 2}\n```'
result = parse_json_response(response)
assert result == {"a": 1}
def test_fenced_code_inside_json_string_is_not_extracted(self) -> None:
payload = {
"content": "Use this example:\n```python\nprint('hello')\n```",
"language": "en",
}
assert parse_json_response(json.dumps(payload)) == payload
# ---------------------------------------------------------------------------
# parse_json_response — json-repair
# ---------------------------------------------------------------------------
class TestParseJsonResponseRepair:
def test_trailing_comma_repaired(self) -> None:
"""json-repair should handle trailing commas if installed."""
response = '{"key": "value",}'
result = parse_json_response(response)
if result == {}:
pytest.skip("json-repair not installed")
assert result == {"key": "value"}
def test_missing_quotes_repaired(self) -> None:
response = "{key: value}"
result = parse_json_response(response)
if result == {}:
pytest.skip("json-repair not installed")
assert isinstance(result, dict)
def test_repair_unavailable_returns_fallback(self) -> None:
with patch("deeptutor.utils.json_parser.repair_json", None):
result = parse_json_response("{bad json", fallback={"err": True})
assert result == {"err": True}
def test_invalid_plain_text_is_not_logged_as_error(self, caplog) -> None:
caplog.set_level(logging.ERROR)
result = parse_json_response("not json", fallback={"default": True})
assert result == {"default": True}
assert not [r for r in caplog.records if r.levelno >= logging.ERROR]
# ---------------------------------------------------------------------------
# safe_json_loads
# ---------------------------------------------------------------------------
class TestSafeJsonLoads:
def test_valid_json(self) -> None:
assert safe_json_loads('{"a": 1}') == {"a": 1}
def test_invalid_json_returns_default_fallback(self) -> None:
assert safe_json_loads("not json") == {}
def test_invalid_json_returns_custom_fallback(self) -> None:
assert safe_json_loads("not json", fallback=[]) == []
def test_explicit_none_fallback(self) -> None:
assert safe_json_loads("bad", fallback=None) is None
class TestParseJsonResponseTrailingProse:
def test_trailing_brace_prose_keeps_object(self) -> None:
raw = '{"chapters":[{"title":"Intro"}]} note: see {schema}'
result = parse_json_response(raw)
assert result == {"chapters": [{"title": "Intro"}]}
def test_trailing_brace_prose_keeps_array(self) -> None:
raw = '[{"id":1},{"id":2}] trailing {x}'
result = parse_json_response(raw, fallback=None)
assert result == [{"id": 1}, {"id": 2}]
class TestParseJsonResponseLeadingProse:
"""Untagged reasoning preludes with JSON fragments inside (issue #692)."""
def test_leading_prose_with_brace_example_keeps_payload(self) -> None:
raw = (
'The output format is {"chapters": []} so I will fill it in.\n'
'{"chapters":[{"title":"C1"},{"title":"C2"},{"title":"C3"}]}'
)
result = parse_json_response(raw, fallback=None)
assert result == {"chapters": [{"title": "C1"}, {"title": "C2"}, {"title": "C3"}]}
def test_two_concatenated_objects_longest_wins(self) -> None:
raw = '{"plan": "six chapters"}\n{"chapters":[{"title":"C1"},{"title":"C2"}]}'
result = parse_json_response(raw, fallback=None)
assert result == {"chapters": [{"title": "C1"}, {"title": "C2"}]}
def test_prose_on_both_sides_keeps_payload(self) -> None:
raw = (
'Schema: {"chapters": []}.\n'
'{"chapters":[{"title":"C1"}]}\n'
"Note: each entry follows {title} form."
)
result = parse_json_response(raw, fallback=None)
assert result == {"chapters": [{"title": "C1"}]}
def test_single_small_object_still_returned(self) -> None:
raw = 'Prefix prose {"only": 1} suffix prose'
assert parse_json_response(raw, fallback=None) == {"only": 1}
# ---------------------------------------------------------------------------
# parse_json_response — <think> reasoning tags (issue #673)
# ---------------------------------------------------------------------------
class TestParseJsonResponseThinkTags:
def test_think_block_before_json(self) -> None:
response = (
"<think>\n1. Analyze user intent...\n2. Determine target audience...\n</think>\n"
'{"title":"T","description":"D","scope":"S","rationale":"R"}'
)
assert parse_json_response(response) == {
"title": "T",
"description": "D",
"scope": "S",
"rationale": "R",
}
def test_think_block_containing_braces_does_not_leak(self) -> None:
"""Braces inside the reasoning must not be returned instead of the real object."""
response = (
'<think>maybe output {"draft": true} or [a, b]</think>\n'
'{"title":"Real","description":"D"}'
)
assert parse_json_response(response) == {"title": "Real", "description": "D"}
def test_think_block_with_markdown_fence(self) -> None:
response = '<think>reasoning here</think>\n```json\n{"answer": 42}\n```'
assert parse_json_response(response) == {"answer": 42}
def test_think_tag_case_insensitive(self) -> None:
response = '<THINK>step by step</THINK>{"ok": true}'
assert parse_json_response(response) == {"ok": True}
def test_only_think_block_returns_fallback(self) -> None:
response = "<think>just reasoning, no payload</think>"
assert parse_json_response(response) == {}
assert parse_json_response(response, fallback=None) is None
def test_multiple_think_blocks_stripped(self) -> None:
response = '<think>one</think>{"a":1}<think>two {b}</think>'
assert parse_json_response(response) == {"a": 1}
def test_literal_think_in_valid_json_preserved(self) -> None:
"""A valid JSON string value that literally contains <think> must not be altered."""
response = '{"x":"literal <think>keep</think> value"}'
assert parse_json_response(response, fallback=None) == {
"x": "literal <think>keep</think> value"
}
def test_unclosed_think_prelude_without_braces(self) -> None:
response = '<think>reasoning here, no JSON yet\n{"real":1}'
assert parse_json_response(response, fallback=None) == {"real": 1}