from __future__ import annotations

from pathlib import Path

from ocr_pipeline.output.markdown import MarkdownWriter


def test_writer_uses_metadata_and_unique_source_hash(tmp_path: Path, monkeypatch) -> None:
    from ocr_pipeline.output import markdown

    monkeypatch.setattr(markdown.settings.output, "base_directory", str(tmp_path))
    monkeypatch.setattr(markdown.settings.output, "organize_by", "date_run")
    writer = MarkdownWriter()
    writer.start_run("2026-07-19T12:00:00+00:00", 2)
    metadata = {
        "source_path": "/screenshots/SCR-001.png",
        "source_hash": "0123456789abcdef",
        "timestamp": "2026-07-19T12:00:00+00:00",
        "chunk_index": 0,
        "total_chunks": 1,
    }

    output = writer.write_chunk(content="recognized text", metadata=metadata)

    assert output.name == "SCR-001_0123456789ab.md"
    text = output.read_text(encoding="utf-8")
    assert "# Screenshot: SCR-001.png" in text
    assert "recognized text" in text
    assert "source_hash: 0123456789abcdef" in text
