Repository: ErikBjare/gptme
File: tests/test_codeblock.py (Python)

Code before:
    And prematurely think "this closes the save block!" when it's actually
    opening a nested example block.

    In streaming mode, a blank line after the closing fence confirms completion.
    This test verifies the parser correctly handles this pattern in streaming.
    """
    fence = "```"
    markdown = f"""{fence}save file.txt
This is a long file with multiple sections.

Here's some initial content that works fine.

**Structure:**
{fence}
More content that should be included but gets cut off.

## Another Section
Even more content here.
{fence}

"""  # Blank line after closing fence confirms completion in streaming mode

    blocks = list(_extract_codeblocks(markdown, streaming=True))
    assert len(blocks) == 1, "Should extract complete block in streaming mode"
    content = blocks[0].content
    assert "This is a long file" in content
    assert "Structure:" in content
    assert "More content that should be included" in content
    assert "Another Section" in content
    assert "Even more content here" in content


def test_append_with_markdown_header_and_bare_backticks():
    """
    Another common failure from autonomous runs: append with markdown headers
    (## Subtitle) followed by bare backticks, causing early tool termination.

    The content after the bare backticks is lost because parser treats it as
    the closing fence.
    """
    fence = "```"
    markdown = f"""{fence}append journal.md
# Journal Entry

Some initial content here.

## Subtitle
{fence}
This content after the bare backticks should be included but isn't.

## Another Section

Review comment (bug):
This is actually incorrect, this should have opened two codeblocks, the main save one and the no-langtag one after Structure (counted as an open tag since it has content after it). Then the one after the "Another section" closes the "Structure" one, so we have no full codeblock ready to extract.

Code after:
    Streaming mode variant of test_save_with_structure_header_and_bare_backticks.

    During streaming, the parser receives content incrementally and might see:
    - "**Structure:**"
    - "```"
    And prematurely think "this closes the save block!" when it's actually
    opening a nested example block.

    In streaming mode, a blank line after the closing fence confirms completion.
    This test verifies the parser correctly handles this pattern in streaming.
    """
    fence = "```"
    markdown = f"""{fence}save file.txt
This is a long file with multiple sections.

Here's some initial content that works fine.

**Structure:**
{fence}
More content that should be included but gets cut off.

## Another Section
Even more content here.
{fence}

"""  # Blank line after closing fence confirms completion in streaming mode

    blocks = list(_extract_codeblocks(markdown, streaming=True))
    # No complete blocks should be extracted because the opening save fence is never closed
    # (the fence after "Structure:" opens a new block, and the final fence closes that block)
    assert len(blocks) == 0, "Should not extract incomplete block in streaming mode"


def test_append_with_markdown_header_and_bare_backticks():
    """
    Another common failure from autonomous runs: append with markdown headers
    (## Subtitle) followed by bare backticks, causing early tool termination.

    The content after the bare backticks is lost because parser treats it as
    the closing fence.
    """
    fence = "```"
    markdown = f"""{fence}append journal.md
# Journal Entry

Some initial content here.

## Subtitle
{fence}
This content after the bare backticks should be included but isn't.