"""Test helpers for building synthetic libvirt domain XML.

Used by every per-category check test to construct minimal domain
trees with specific elements present or absent. Imported as a
top-level module (pytest ``prepend`` import mode puts each test file's
directory on ``sys.path``), so a plain ``import _isolation_xml`` /
``from _isolation_xml import domain_with`` resolves.
"""

from __future__ import annotations

import xml.etree.ElementTree as ET


def domain_with(*body: str, name: str = "test-vm") -> str:
    """Wrap ``body`` (raw inner XML strings) under ``<domain>/<devices>``."""
    devices_inner = "\n    ".join(body)
    return (
        f"<domain type='kvm'>\n"
        f"  <name>{name}</name>\n"
        f"  <memory unit='MiB'>2048</memory>\n"
        f"  <vcpu>2</vcpu>\n"
        f"  <devices>\n"
        f"    {devices_inner}\n"
        f"  </devices>\n"
        f"</domain>\n"
    )


def parsed(xml_text: str) -> ET.ElementTree:
    """Round-trip XML text through ``ET.fromstring`` + wrap as a tree."""
    return ET.ElementTree(ET.fromstring(xml_text))
