"""Tests for mgf-cloud's resource models + tagging helpers (Pass 2)."""

from __future__ import annotations

import pytest
from pydantic import ValidationError

from mgf.cloud import models


def test_owned_metadata_and_is_owned() -> None:
    md = models.owned_metadata("federation", {"role": "ci"})
    assert md[models.OWNER_TAG] == "federation"
    assert md["role"] == "ci"
    assert models.is_owned(md)
    assert models.is_owned(md, "federation")
    assert not models.is_owned(md, "someone-else")
    assert not models.is_owned({})


def test_owned_metadata_default_owner() -> None:
    assert models.owned_metadata()[models.OWNER_TAG] == models.DEFAULT_OWNER


def test_server_model_is_frozen() -> None:
    server = models.Server(id="s1", name="web")
    with pytest.raises(ValidationError):
        server.name = "renamed"


def test_serverspec_json_roundtrip() -> None:
    spec = models.ServerSpec(
        name="web",
        flavor="a2-ram2-disk20",
        image="Ubuntu 24.04 LTS",
        assign_floating_ip=True,
        metadata=models.owned_metadata(),
    )
    again = models.ServerSpec.model_validate(spec.model_dump())
    assert again == spec


def test_flavor_and_volume_fields() -> None:
    flavor = models.Flavor(id="f", name="a2-ram2-disk20", vcpus=1, ram_mb=2048, disk_gb=20)
    assert flavor.ram_mb == 2048
    volume = models.VolumeSpec(name="data", size_gb=50)
    assert volume.size_gb == 50
    assert volume.bootable is False
