"""Tests para format_obsidian_note."""

from format_obsidian_note import format_obsidian_note
from parse_obsidian_frontmatter import parse_obsidian_frontmatter


def test_con_frontmatter():
    out = format_obsidian_note({"title": "My Note", "tags": ["a", "b"]}, "Hello.")
    assert out == "---\ntitle: My Note\ntags:\n- a\n- b\n---\n\nHello."


def test_frontmatter_vacio_devuelve_body():
    assert format_obsidian_note({}, "just a body") == "just a body"


def test_frontmatter_none_devuelve_body():
    assert format_obsidian_note(None, "just a body") == "just a body"


def test_preserva_orden_de_claves():
    fm = {"zeta": 1, "alpha": 2, "mid": 3}
    out = format_obsidian_note(fm, "body")
    # sort_keys=False -> insertion order preserved.
    assert out.index("zeta") < out.index("alpha") < out.index("mid")


def test_unicode_literal():
    out = format_obsidian_note({"title": "Año Nuevo"}, "Cuerpo con ñ.")
    assert "Año Nuevo" in out
    assert "Cuerpo con ñ." in out


def test_round_trip_con_parse():
    fm = {"title": "Round Trip", "status": "open", "count": 3}
    body = "Body with a [[link]] and #tag."
    note = format_obsidian_note(fm, body)
    parsed = parse_obsidian_frontmatter(note)
    assert parsed["frontmatter"] == fm
    # The format inserts a blank separator line before the body.
    assert parsed["body"].strip() == body
