"""Tests for :mod:`mgf.vm.core.configurator.metadata` (pure planner)."""

from __future__ import annotations

import xml.etree.ElementTree as ET

from mgf.vm.core.configurator import metadata


def _dom(title: str = "", description: str = "") -> ET.Element:
    parts = ["<domain><name>web</name><uuid>u</uuid>"]
    if title:
        parts.append(f"<title>{title}</title>")
    if description:
        parts.append(f"<description>{description}</description>")
    parts.append("</domain>")
    return ET.fromstring("".join(parts))


class TestPlan:
    def test_set_new_title(self) -> None:
        change = metadata.plan(_dom(), title="web frontend")
        assert "title" in change.summary
        assert change.requires_shutdown is False
        assert "web frontend" in change.new_xml

    def test_clear_title(self) -> None:
        change = metadata.plan(_dom(title="old"), title="")
        assert "cleared" in change.summary
        assert "<title>" not in change.new_xml

    def test_clear_absent_title_is_noop(self) -> None:
        change = metadata.plan(_dom(), title="")
        assert change.summary == "no change"

    def test_no_change(self) -> None:
        change = metadata.plan(_dom(title="same"), title="same")
        assert change.summary == "no change"

    def test_description_update(self) -> None:
        change = metadata.plan(_dom(description="old"), description="brand new")
        assert "description updated" in change.summary
        assert "brand new" in change.new_xml

    def test_clear_description(self) -> None:
        change = metadata.plan(_dom(description="old"), description="")
        assert "description (cleared)" in change.summary
        assert "<description>" not in change.new_xml

    def test_set_both_fields(self) -> None:
        change = metadata.plan(_dom(), title="t", description="d")
        assert "<title>t</title>" in change.new_xml
        assert "<description>d</description>" in change.new_xml
