import hashlib
import json
from pathlib import Path

from m3m_twin.demo import run_demo


ROOT = Path(__file__).resolve().parents[1]


def _digest(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


def test_engineering_receipt_is_idempotent_and_never_hashes_itself(tmp_path):
    run_demo(ROOT, output_root=tmp_path)
    receipt_path = tmp_path / "outputs" / "demo" / "engineering-demo-receipt.json"
    first_hash = _digest(receipt_path)
    first = json.loads(receipt_path.read_text(encoding="utf-8"))

    run_demo(ROOT, output_root=tmp_path)
    second_hash = _digest(receipt_path)
    second = json.loads(receipt_path.read_text(encoding="utf-8"))

    assert first_hash == second_hash
    assert first == second
    assert all(record["path"] != "outputs/demo/engineering-demo-receipt.json" for record in second["artifacts"])
