"""Tests for nested PARAM field extraction from body_example."""

from __future__ import annotations

from app.vsphere.rest.param_fields import body_fields_from_example, set_by_path


def test_body_fields_from_example_flattens_nested_scalars() -> None:
    fields = body_fields_from_example(
        {
            "name": "lab-vm",
            "placement": {"host": "host-11", "folder": "group-v23"},
            "cpu": {"count": 2, "cores_per_socket": 1},
            "disks": [{"new_vmdk": {"name": "disk-0", "capacity": 1024}}],
        }
    )
    by_name = {field["name"]: field for field in fields}
    assert by_name["name"]["example"] == "lab-vm"
    assert by_name["name"]["type"] == "string"
    assert by_name["placement.host"]["example"] == "host-11"
    assert by_name["cpu.count"]["type"] == "integer"
    assert by_name["cpu.count"]["example"] == 2
    assert by_name["disks.0.new_vmdk.name"]["example"] == "disk-0"
    assert by_name["disks.0.new_vmdk.capacity"]["type"] == "integer"
    assert "placement" not in by_name
    assert "disks" not in by_name


def test_set_by_path_builds_nested_dicts() -> None:
    root: dict = {}
    set_by_path(root, "placement.host", "host-11")
    set_by_path(root, "cpu.count", 2)
    assert root == {"placement": {"host": "host-11"}, "cpu": {"count": 2}}
