"""Tests for scripts/import_fabledrulebook.py — parser fixtures only.

The CLI / DB-writing portion is exercised manually on remote dev — it's a
one-shot script.
"""
from pathlib import Path


FIXTURES = Path(__file__).parent / "fixtures" / "fabledrulebook_sample"


def test_parse_topic_file_extracts_topic_title_and_rules():
    from scripts.import_fabledrulebook import parse_topic_file
    topic_title, description, rules = parse_topic_file(FIXTURES / "git-workflow.md")

    assert topic_title == "git-workflow"
    assert "Lead paragraph" in description
    assert len(rules) == 2
    assert rules[0]["title"] == "dev is home"
    assert "Work directly on dev" in rules[0]["statement"]
    assert "minimises ceremony" in rules[0]["why"]
    assert "Default for all repo work" in rules[0]["how_to_apply"]


def test_parse_rule_body_without_why_treats_whole_body_as_statement():
    from scripts.import_fabledrulebook import parse_rule_body
    out = parse_rule_body("test", "Just a body with no markers.")
    assert out["statement"] == "Just a body with no markers."
    assert out["why"] == ""
    assert out["how_to_apply"] == ""


def test_parse_rule_body_handles_only_why():
    from scripts.import_fabledrulebook import parse_rule_body
    out = parse_rule_body("test", "Statement.\n\n**Why:** Because.")
    assert out["statement"] == "Statement."
    assert out["why"] == "Because."
    assert out["how_to_apply"] == ""


def test_parse_rule_body_handles_only_how_to_apply():
    from scripts.import_fabledrulebook import parse_rule_body
    out = parse_rule_body("test", "Statement.\n\n**How to apply:** Do it now.")
    assert out["statement"] == "Statement."
    assert out["why"] == ""
    assert out["how_to_apply"] == "Do it now."
