from agentmux.utils.frontmatter import dump, parse


def test_parse_with_frontmatter() -> None:
    content = "---\nname: foo\n---\nbody text"
    meta, body = parse(content)
    assert meta == {"name": "foo"}
    assert body == "body text"


def test_parse_multiple_fields() -> None:
    content = "---\nname: foo\ndescription: bar\nalwaysApply: true\n---\nbody"
    meta, body = parse(content)
    assert meta["name"] == "foo"
    assert meta["description"] == "bar"
    assert meta["alwaysApply"] is True
    assert body == "body"


def test_parse_without_frontmatter() -> None:
    content = "just body text"
    meta, body = parse(content)
    assert meta == {}
    assert body == "just body text"


def test_parse_empty_string() -> None:
    meta, body = parse("")
    assert meta == {}
    assert body == ""


def test_parse_frontmatter_only() -> None:
    content = "---\nname: foo\n---\n"
    meta, body = parse(content)
    assert meta == {"name": "foo"}
    assert body == ""


def test_parse_body_only() -> None:
    content = "no frontmatter\njust content"
    meta, body = parse(content)
    assert meta == {}
    assert body == content


def test_parse_multiline_body() -> None:
    content = "---\nname: test\n---\nline one\nline two\nline three"
    meta, body = parse(content)
    assert meta == {"name": "test"}
    assert body == "line one\nline two\nline three"


def test_parse_empty_frontmatter() -> None:
    content = "---\n---\nbody"
    meta, body = parse(content)
    assert meta == {}
    assert body == "body"


def test_dump_with_meta() -> None:
    result = dump({"name": "foo"}, "body text")
    assert result.startswith("---\n")
    assert "name: foo" in result
    assert result.endswith("body text")


def test_dump_without_meta() -> None:
    result = dump({}, "body text")
    assert result == "body text"


def test_roundtrip() -> None:
    original_meta = {"name": "test", "description": "a test", "alwaysApply": False}
    original_body = "Some content here"
    rendered = dump(original_meta, original_body)
    meta, body = parse(rendered)
    assert meta["name"] == original_meta["name"]
    assert meta["description"] == original_meta["description"]
    assert body == original_body


# ── CRLF support ──────────────────────────────────────────────────────────────


def test_parse_crlf_line_endings() -> None:
    content = "---\r\nname: foo\r\n---\r\nbody text"
    meta, body = parse(content)
    assert meta == {"name": "foo"}
    assert body == "body text"


def test_parse_crlf_multiline_body() -> None:
    content = "---\r\nname: test\r\n---\r\nline one\r\nline two"
    meta, body = parse(content)
    assert meta == {"name": "test"}
    assert "line one" in body
    assert "line two" in body


def test_parse_crlf_empty_frontmatter() -> None:
    content = "---\r\n---\r\nbody"
    meta, body = parse(content)
    assert meta == {}
    assert body == "body"


# ── Non-frontmatter divider handling ─────────────────────────────────────────


def test_parse_bare_divider_not_treated_as_frontmatter() -> None:
    """A `---` divider whose content parses to a scalar must not be treated as frontmatter."""
    content = "---\nThis is a section divider, not frontmatter\n---\nBody text"
    meta, body = parse(content)
    assert meta == {}
    assert body == content


def test_parse_empty_frontmatter_block_still_returns_empty_dict() -> None:
    """An empty `--- ---` block (YAML None) should still yield {} not None."""
    content = "---\n---\nbody"
    meta, body = parse(content)
    assert meta == {}
    assert body == "body"
