"""Book interactive HTML must be complete before it becomes reader content."""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from deeptutor.agents.visualize.models import VisualizationAnalysis
from deeptutor.book.blocks.base import BlockContext
from deeptutor.book.blocks.interactive import InteractiveGenerator
from deeptutor.book.models import Block, BlockStatus, BlockType, Chapter, Page
_HTML = """
Interactive
"""
def _context() -> BlockContext:
chapter = Chapter(id="ch-interactive", title="Mechanics", summary="Stress and strain")
block = Block(type=BlockType.INTERACTIVE)
return BlockContext(
book_id="book-interactive",
chapter=chapter,
page=Page(
id="page-interactive",
book_id="book-interactive",
chapter_id=chapter.id,
),
block=block,
rag_enabled=False,
)
def _pipeline(responses: list[str]) -> MagicMock:
pipeline = MagicMock()
pipeline.run_analysis = AsyncMock(
return_value=VisualizationAnalysis(
render_type="html",
description="An interactive mechanics diagram.",
chart_type="interactive",
)
)
pipeline.run_code_generation = AsyncMock(side_effect=responses)
pipeline.code_agent.binding = "deepseek"
pipeline.code_agent.get_model.return_value = "deepseek-flash"
pipeline.code_agent.get_output_budget.return_value = 16000
return pipeline
async def _run(pipeline: MagicMock, monkeypatch: pytest.MonkeyPatch) -> Block:
from deeptutor.agents.visualize import pipeline as pipeline_module
from deeptutor.services.llm import config as config_module
monkeypatch.setattr(pipeline_module, "VisualizePipeline", MagicMock(return_value=pipeline))
monkeypatch.setattr(
config_module,
"get_llm_config",
lambda: SimpleNamespace(api_key="test", base_url="", api_version=""),
)
return await InteractiveGenerator().generate(_context())
async def _generate(response: str, monkeypatch: pytest.MonkeyPatch) -> Block:
# The same reply on every attempt, so a retry cannot rescue a bad page.
return await _run(_pipeline([response, response]), monkeypatch)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"response",
[
_HTML,
f"Here is the interactive page.\n\n```html\n{_HTML}\n```\n",
f"Here is the interactive page.\n\n{_HTML}\n\nHope this helps.",
f"```\n{_HTML}\n```",
f"```html\n{_HTML}",
f"{_HTML}\n\nNote: the page ends at its closing