"""Replay test: running pipeline twice produces no duplicate rows."""

import pytest


@pytest.mark.replay
def test_parser_idempotency():
    """Running parser twice on same text → identical output dict."""
    from libs.parser.rule_parser import RuleBasedParser

    text = """
    Item 2.02 Results of Operations
    Revenue guidance raised. Demand remains strong. Margin expansion.
    Customer additions accelerating. Adjusted EPS excludes non-GAAP items.
    """
    p = RuleBasedParser()
    metadata = {"filing_date": "2026-01-29"}
    out1 = p.parse("DOC::idem::test", "8-K", text, metadata).model_dump()
    out2 = p.parse("DOC::idem::test", "8-K", text, metadata).model_dump()
    assert out1 == out2


@pytest.mark.replay
def test_checksum_idempotency(tmp_path, monkeypatch):
    """Writing same exhibit twice: file content unchanged, checksum stable."""
    from libs.common import config
    config.get_settings.cache_clear()
    monkeypatch.setenv("DATA_ROOT", str(tmp_path))

    from libs.common.file_store import get_checksum, write_exhibit

    content = "Idempotency test content"
    c1 = write_exhibit("IDEM001", "EX-99.1", content)
    # Second write to same path should overwrite (atomic)
    c2 = write_exhibit("IDEM001", "EX-99.1", content)
    assert c1 == c2

    stored = get_checksum("IDEM001", "EX-99.1")
    assert stored == c1
    config.get_settings.cache_clear()
