Skip to content

[Feature] Unit tests for jsonblock JSON extraction and truncation recovery #9

Description

@sjohnston1972

Part of the "Add an automated backend test suite" parent issue. Depends on the pytest-scaffolding sub-issue.

Background

backend/app/jsonblock.py extracts a JSON object from raw LLM output. It has four strategies (fenced ```json block, any fenced block, last balanced {...}, and a brace-counting truncation-recovery pass in `_try_truncation_recovery`). All designer/critic/orchestrator/extractor parsing depends on it, so a regression here silently breaks every agent.

Problem / Goal

The module is pure (no I/O) and completely untested. Add thorough unit tests.

Where to look

  • backend/app/jsonblock.pyextract_json (entry point) and helpers _clean, _try_parse, _try_truncation_recovery, _last_balanced_object.
  • The module docstring lists the exact cases it's meant to handle (nested objects, trailing commas, // and /* */ comments, truncation). Use it as the test checklist.

Suggested approach

Write backend/tests/test_jsonblock.py covering at least:

  1. Clean fenced ```json block → parsed dict.
  2. Unfenced JSON with trailing prose after it → last balanced object wins.
  3. Nested objects like {"citations": [{"page": 42}]} → returns the full object, not the first inner } (the bug the module was written to fix).
  4. Trailing commas before }/] and // / /* */ comments → recovered via _clean.
  5. A brace inside a string value, e.g. {"note": "value with } brace"} → not fooled by the }.
  6. Truncation: input like {"a": 1, "b": [{"c": 1}, {"c": 2}, {"c": 3 → recovers {"a":1,"b":[{"c":1},{"c":2}]} (the docstring's own example).
  7. Empty string / no JSON at all → returns None.

Acceptance criteria

  • test_jsonblock.py covers all seven cases above and passes.
  • Each of the four extraction strategies is exercised by at least one test.

Testing

cd backend && pytest tests/test_jsonblock.py -q.

Out of scope

  • Changing jsonblock.py behavior — this is characterization/coverage only. If a test surfaces a genuine bug, file it separately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions