Files
hms-mediaengine/tests/unit/test_domain_ids.py
T

54 lines
1.7 KiB
Python
Raw Normal View History

"""Unit-Tests stabile IDs (PLAN.md §3.6, §6.3)."""
from __future__ import annotations
import uuid
from hms_domain import new_node_id, new_uuid, persistent_node_id
def test_new_uuids_are_unique_and_parseable() -> None:
a, b = new_uuid(), new_uuid()
assert a != b
uuid.UUID(a)
uuid.UUID(b)
def test_node_id_is_uuid_and_random() -> None:
a, b = new_node_id(), new_node_id()
uuid.UUID(a)
assert a != b
def test_persistent_node_id_stable_across_calls(tmp_path) -> None:
identity = tmp_path / "identity" / "node_id"
first = persistent_node_id(identity)
second = persistent_node_id(identity)
assert first == second # IP-/Hostnamenunabhängig: Datei ist Quelle der Wahrheit
assert identity.read_text(encoding="utf-8").strip() == first
def test_persistent_node_id_validated_on_load(tmp_path) -> None:
identity = tmp_path / "identity" / "node_id"
identity.parent.mkdir(parents=True)
identity.write_text("not-a-uuid", encoding="utf-8")
import pytest
with pytest.raises(ValueError):
persistent_node_id(identity)
def test_two_nodes_get_distinct_ids(tmp_path) -> None:
a = persistent_node_id(tmp_path / "a" / "node_id")
b = persistent_node_id(tmp_path / "b" / "node_id")
assert a != b # doppelte node_ids wären ein Fehler (§6.3)
def test_concurrent_creation_yields_single_id(tmp_path) -> None:
"""O_EXCL-Rennbedingung: beide Aufrufer erhalten dieselbe ID."""
identity = tmp_path / "identity" / "node_id"
id_a = persistent_node_id(identity)
# zweite Erzeugung mit bereits existierender Datei → lädt vorhandene ID
id_b = persistent_node_id(identity)
assert id_a == id_b