"""Tests for frontmatter parsing utilities."""

from pathlib import Path

import pytest

import frontmatter


class TestReadFrontmatter:
    def test_basic(self, tmp_path: Path):
        f = tmp_path / "test.md"
        f.write_text("---\nid: foo\nplanned: true\n---\nBody.\n")
        fm = frontmatter.read_frontmatter(f)
        assert fm["id"] == "foo"
        assert fm["planned"] == "true"

    def test_no_frontmatter(self, tmp_path: Path):
        f = tmp_path / "test.md"
        f.write_text("# Heading\n")
        assert frontmatter.read_frontmatter(f) == {}

    def test_missing_file(self, tmp_path: Path):
        assert frontmatter.read_frontmatter(tmp_path / "x.md") == {}

    def test_empty_file(self, tmp_path: Path):
        f = tmp_path / "test.md"
        f.write_text("")
        assert frontmatter.read_frontmatter(f) == {}

    def test_multiple_fields(self, tmp_path: Path):
        f = tmp_path / "test.md"
        f.write_text(
            "---\nid: bar\ntitle: A Title\nplanned: false\nexecuted: true\n---\n"
        )
        fm = frontmatter.read_frontmatter(f)
        assert fm["id"] == "bar"
        assert fm["title"] == "A Title"
        assert fm["planned"] == "false"
        assert fm["executed"] == "true"


class TestParseBool:
    @pytest.mark.parametrize(
        "val,expected",
        [
            ("true", True), ("True", True), ("yes", True), ("1", True),
            ("false", False), ("False", False), ("no", False), ("0", False),
            (None, False),
        ],
    )
    def test_values(self, val, expected):
        assert frontmatter.parse_bool(val) is expected


class TestStripQuotes:
    def test_double_quotes(self):
        assert frontmatter.strip_quotes('"hello"') == "hello"

    def test_single_quotes(self):
        assert frontmatter.strip_quotes("'hello'") == "hello"

    def test_no_quotes(self):
        assert frontmatter.strip_quotes("hello") == "hello"

    def test_mismatched_quotes(self):
        assert frontmatter.strip_quotes("'hello\"") == "'hello\""

    def test_empty_string(self):
        assert frontmatter.strip_quotes("") == ""


class TestParseDeps:
    def test_empty(self):
        assert frontmatter.parse_deps("[]") == []
        assert frontmatter.parse_deps("") == []
        assert frontmatter.parse_deps("null") == []
        assert frontmatter.parse_deps("~") == []

    def test_single(self):
        assert frontmatter.parse_deps("[foo]") == ["foo"]

    def test_multiple(self):
        assert frontmatter.parse_deps("[foo, bar, baz]") == ["foo", "bar", "baz"]

    def test_no_brackets(self):
        assert frontmatter.parse_deps("foo, bar") == ["foo", "bar"]
